You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@synapse.apache.org by ra...@apache.org on 2010/09/23 08:04:34 UTC
svn commit: r1000332 [15/27] - in
/synapse/branches/commons-vfs-2-synapse-2.0: ./ core/ core/src/
core/src/main/ core/src/main/java/ core/src/main/java/org/
core/src/main/java/org/apache/ core/src/main/java/org/apache/commons/
core/src/main/java/org/ap...
Added: synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/RamFileData.java
URL: http://svn.apache.org/viewvc/synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/RamFileData.java?rev=1000332&view=auto
==============================================================================
--- synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/RamFileData.java (added)
+++ synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/RamFileData.java Thu Sep 23 06:04:21 2010
@@ -0,0 +1,277 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.vfs.provider.ram;
+
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+
+import org.apache.commons.vfs.FileName;
+import org.apache.commons.vfs.FileSystemException;
+import org.apache.commons.vfs.FileType;
+
+/**
+ * RAM File Object Data.
+ * @author <a href="http://commons.apache.org/vfs/team-list.html">Commons VFS team</a>
+ */
+class RamFileData implements Serializable
+{
+ /**
+ * File Name.
+ */
+ private FileName name;
+
+ /**
+ * File Type.
+ */
+ private FileType type;
+
+ /**
+ * Bytes.
+ */
+ private byte[] buffer;
+
+ /**
+ * Last modified time
+ */
+ private long lastModified;
+
+ /**
+ * Children
+ */
+ private Collection children;
+
+ /**
+ * Constructor.
+ * @param name The file name.
+ */
+ public RamFileData(FileName name)
+ {
+ super();
+ this.clear();
+ if (name == null)
+ {
+ throw new IllegalArgumentException("name can not be null");
+ }
+ this.name = name;
+ }
+
+ /**
+ * @return Returns the buffer.
+ */
+ byte[] getBuffer()
+ {
+ return buffer;
+ }
+
+ /**
+ * @param buffer The buffer.
+ */
+ void setBuffer(byte[] buffer)
+ {
+ updateLastModified();
+ this.buffer = buffer;
+ }
+
+ /**
+ * @return Returns the lastModified.
+ */
+ long getLastModified()
+ {
+ return lastModified;
+ }
+
+ /**
+ * @param lastModified
+ * The lastModified to set.
+ */
+ void setLastModified(long lastModified)
+ {
+ this.lastModified = lastModified;
+ }
+
+ /**
+ * @return Returns the type.
+ */
+ FileType getType()
+ {
+ return type;
+ }
+
+ /**
+ * @param type
+ * The type to set.
+ */
+ void setType(FileType type)
+ {
+ this.type = type;
+ }
+
+ /**
+ *
+ */
+ void clear()
+ {
+ this.buffer = new byte[0];
+ updateLastModified();
+ this.type = FileType.IMAGINARY;
+ this.children = Collections.synchronizedCollection(new ArrayList());
+ this.name = null;
+ }
+
+ void updateLastModified()
+ {
+ this.lastModified = System.currentTimeMillis();
+ }
+
+ /**
+ * @return Returns the name.
+ */
+ FileName getName()
+ {
+ return name;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#toString()
+ */
+ public String toString()
+ {
+ return this.name.toString();
+ }
+
+ /**
+ * Add a child.
+ *
+ * @param data The file data.
+ * @throws FileSystemException if an error occurs.
+ */
+ void addChild(RamFileData data) throws FileSystemException
+ {
+ if (!this.getType().hasChildren())
+ {
+ throw new FileSystemException(
+ "A child can only be added in a folder");
+ }
+
+ if (data == null)
+ {
+ throw new FileSystemException("No child can be null");
+ }
+
+ if (this.children.contains(data))
+ {
+ throw new FileSystemException("Child already exists. " + data);
+ }
+
+ this.children.add(data);
+ updateLastModified();
+ }
+
+ /**
+ * Remove a child.
+ *
+ * @param data The file data.
+ * @throws FileSystemException if an error occurs.
+ */
+ void removeChild(RamFileData data) throws FileSystemException
+ {
+ if (!this.getType().hasChildren())
+ {
+ throw new FileSystemException(
+ "A child can only be removed from a folder");
+ }
+ if (!this.children.contains(data))
+ {
+ throw new FileSystemException("Child not found. " + data);
+ }
+ this.children.remove(data);
+ updateLastModified();
+ }
+
+ /**
+ * @return Returns the children.
+ */
+ Collection getChildren()
+ {
+ if (name == null)
+ {
+ throw new IllegalStateException("Data is clear");
+ }
+ return children;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ public boolean equals(Object o)
+ {
+ if (this == o)
+ {
+ return true;
+ }
+ if (!(o instanceof RamFileData))
+ {
+ return false;
+ }
+ RamFileData data = (RamFileData) o;
+ return this.getName().equals(data.getName());
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.lang.Object#hashCode()
+ */
+ public int hashCode()
+ {
+ return this.getName().hashCode();
+ }
+
+ boolean hasChildren(RamFileData data)
+ {
+ return this.children.contains(data);
+ }
+
+ /**
+ * @return Returns the size of the buffer
+ */
+ int size()
+ {
+ return buffer.length;
+ }
+
+ /**
+ * Resize the buffer
+ *
+ * @param newSize The new buffer size.
+ */
+ void resize(int newSize)
+ {
+ int size = this.size();
+ byte[] newBuf = new byte[newSize];
+ System.arraycopy(this.buffer, 0, newBuf, 0, size);
+ this.buffer = newBuf;
+ updateLastModified();
+ }
+
+}
Added: synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/RamFileObject.java
URL: http://svn.apache.org/viewvc/synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/RamFileObject.java?rev=1000332&view=auto
==============================================================================
--- synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/RamFileObject.java (added)
+++ synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/RamFileObject.java Thu Sep 23 06:04:21 2010
@@ -0,0 +1,270 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.vfs.provider.ram;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import org.apache.commons.vfs.FileName;
+import org.apache.commons.vfs.FileObject;
+import org.apache.commons.vfs.FileSystemException;
+import org.apache.commons.vfs.FileType;
+import org.apache.commons.vfs.RandomAccessContent;
+import org.apache.commons.vfs.provider.AbstractFileObject;
+import org.apache.commons.vfs.util.RandomAccessMode;
+
+/**
+ * A RAM File contains a single RAM FileData instance, it provides methods to
+ * access the data by implementing FileObject interface.
+ * @author <a href="http://commons.apache.org/vfs/team-list.html">Commons VFS team</a>
+ */
+public class RamFileObject extends AbstractFileObject implements FileObject
+{
+ /**
+ * File System.
+ */
+ private RamFileSystem fs;
+
+ /**
+ * RAM File Object Data.
+ */
+ private RamFileData data;
+
+ /**
+ * @param name The name of the file.
+ * @param fs The FileSystem.
+ */
+ protected RamFileObject(FileName name, RamFileSystem fs)
+ {
+ super(name, fs);
+ this.fs = fs;
+ this.fs.attach(this);
+ }
+
+ private void save() throws FileSystemException
+ {
+ this.fs.save(this);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.commons.vfs.provider.AbstractFileObject#doGetType()
+ */
+ protected FileType doGetType() throws Exception
+ {
+ return data.getType();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.commons.vfs.provider.AbstractFileObject#doListChildren()
+ */
+ protected String[] doListChildren() throws Exception
+ {
+ return this.fs.listChildren(this.getName());
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.commons.vfs.provider.AbstractFileObject#doGetContentSize()
+ */
+ protected long doGetContentSize() throws Exception
+ {
+ return this.data.getBuffer().length;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.commons.vfs.provider.AbstractFileObject#doGetInputStream()
+ */
+ protected InputStream doGetInputStream() throws Exception
+ {
+ // VFS-210: ram allows to gather an input stream even from a directory. So we need to check the type anyway.
+ if (!getType().hasContent())
+ {
+ throw new FileSystemException("vfs.provider/read-not-file.error", getName());
+ }
+
+ return new ByteArrayInputStream(this.data.getBuffer());
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.commons.vfs.provider.AbstractFileObject#doGetOutputStream(boolean)
+ */
+ protected OutputStream doGetOutputStream(boolean bAppend) throws Exception
+ {
+ if (!bAppend)
+ {
+ this.data.setBuffer(new byte[0]);
+ }
+ return new RamFileOutputStream(this);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.commons.vfs.provider.AbstractFileObject#doDelete()
+ */
+ protected void doDelete() throws Exception
+ {
+ fs.delete(this);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.commons.vfs.provider.AbstractFileObject#doGetLastModifiedTime()
+ */
+ protected long doGetLastModifiedTime() throws Exception
+ {
+ return data.getLastModified();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.commons.vfs.provider.AbstractFileObject#doSetLastModifiedTime(long)
+ */
+ protected boolean doSetLastModTime(long modtime) throws Exception
+ {
+ data.setLastModified(modtime);
+ return true;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.commons.vfs.provider.AbstractFileObject#doCreateFolder()
+ */
+ protected void doCreateFolder() throws Exception
+ {
+ this.injectType(FileType.FOLDER);
+ this.save();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.commons.vfs.provider.AbstractFileObject#doRename(org.apache.commons.vfs.FileObject)
+ */
+ protected void doRename(FileObject newfile) throws Exception
+ {
+ fs.rename(this, (RamFileObject) newfile);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.commons.vfs.provider.AbstractFileObject#doGetRandomAccessContent(
+ * org.apache.commons.vfs.util.RandomAccessMode)
+ */
+ protected RandomAccessContent doGetRandomAccessContent(RandomAccessMode mode)
+ throws Exception
+ {
+ return new RamFileRandomAccessContent(this, mode);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.commons.vfs.provider.AbstractFileObject#doAttach()
+ */
+ protected void doAttach() throws Exception
+ {
+ this.fs.attach(this);
+ }
+
+ /**
+ * @return Returns the data.
+ */
+ RamFileData getData()
+ {
+ return data;
+ }
+
+ /**
+ * @param data
+ * The data to set.
+ */
+ void setData(RamFileData data)
+ {
+ this.data = data;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.commons.vfs.provider.AbstractFileObject#injectType(org.apache.commons.vfs.FileType)
+ */
+ protected void injectType(FileType fileType)
+ {
+ this.data.setType(fileType);
+ super.injectType(fileType);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.commons.vfs.provider.AbstractFileObject#endOutput()
+ */
+ protected void endOutput() throws Exception
+ {
+ super.endOutput();
+ this.save();
+ }
+
+ /**
+ * @return Returns the size of the RAMFileData
+ */
+ int size()
+ {
+ if (data == null)
+ {
+ return 0;
+ }
+ return data.size();
+ }
+
+ /**
+ * @param newSize
+ * @throws IOException
+ * if the new size exceeds the limit
+ */
+ synchronized void resize(int newSize) throws IOException
+ {
+ if (fs.getFileSystemOptions() != null)
+ {
+ int maxSize = RamFileSystemConfigBuilder.getInstance().getMaxSize(
+ fs.getFileSystemOptions());
+ if (fs.size() + newSize - this.size() > maxSize)
+ {
+ throw new IOException("FileSystem capacity (" + maxSize
+ + ") exceeded.");
+ }
+ }
+ this.data.resize(newSize);
+ }
+
+}
Added: synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/RamFileOutputStream.java
URL: http://svn.apache.org/viewvc/synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/RamFileOutputStream.java?rev=1000332&view=auto
==============================================================================
--- synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/RamFileOutputStream.java (added)
+++ synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/RamFileOutputStream.java Thu Sep 23 06:04:21 2010
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.vfs.provider.ram;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+import org.apache.commons.vfs.FileSystemException;
+
+/**
+ * OutputStream to a RamFile.
+ * @author <a href="http://commons.apache.org/vfs/team-list.html">Commons VFS team</a>
+ */
+public class RamFileOutputStream extends OutputStream
+{
+
+ /**
+ * File.
+ */
+ protected RamFileObject file;
+
+ /**
+ * buffer.
+ */
+ protected byte[] buffer1 = new byte[1];
+
+ /** File is open or closed */
+ protected boolean closed = false;
+
+ private IOException exc;
+
+ /**
+ * @param file The base file.
+ */
+ public RamFileOutputStream(RamFileObject file)
+ {
+ super();
+ this.file = file;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataOutput#write(byte[], int, int)
+ */
+ public void write(byte[] b, int off, int len) throws IOException
+ {
+ int size = this.file.getData().size();
+ int newSize = this.file.getData().size() + len;
+ // Store the Exception in order to notify the client again on close()
+ try
+ {
+ this.file.resize(newSize);
+ }
+ catch (IOException e)
+ {
+ this.exc = e;
+ throw e;
+ }
+ System.arraycopy(b, off, this.file.getData().getBuffer(), size, len);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataOutput#write(int)
+ */
+ public void write(int b) throws IOException
+ {
+ buffer1[0] = (byte) b;
+ this.write(buffer1);
+ }
+
+ public void flush() throws IOException
+ {
+ }
+
+ public void close() throws IOException
+ {
+ if (closed)
+ {
+ return;
+ }
+ // Notify on close that there was an IOException while writing
+ if (exc != null)
+ {
+ throw exc;
+ }
+ try
+ {
+ this.closed = true;
+ // Close the
+ this.file.endOutput();
+ }
+ catch (Exception e)
+ {
+ throw new FileSystemException(e);
+ }
+ }
+
+}
Added: synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/RamFileProvider.java
URL: http://svn.apache.org/viewvc/synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/RamFileProvider.java?rev=1000332&view=auto
==============================================================================
--- synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/RamFileProvider.java (added)
+++ synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/RamFileProvider.java Thu Sep 23 06:04:21 2010
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.vfs.provider.ram;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+
+import org.apache.commons.vfs.Capability;
+import org.apache.commons.vfs.FileName;
+import org.apache.commons.vfs.FileSystem;
+import org.apache.commons.vfs.FileSystemException;
+import org.apache.commons.vfs.FileSystemOptions;
+import org.apache.commons.vfs.provider.AbstractOriginatingFileProvider;
+import org.apache.commons.vfs.provider.FileProvider;
+
+/**
+ * RAM File Provider.
+ * @author <a href="http://commons.apache.org/vfs/team-list.html">Commons VFS team</a>
+ *
+ */
+public class RamFileProvider extends AbstractOriginatingFileProvider implements
+ FileProvider
+{
+ /** The provider's capabilities. */
+ public static final Collection capabilities = Collections
+ .unmodifiableCollection(Arrays.asList(new Capability[]
+ {Capability.CREATE, Capability.DELETE, Capability.RENAME,
+ Capability.GET_TYPE, Capability.GET_LAST_MODIFIED,
+ Capability.SET_LAST_MODIFIED_FILE,
+ Capability.SET_LAST_MODIFIED_FOLDER,
+ Capability.LIST_CHILDREN, Capability.READ_CONTENT,
+ Capability.URI, Capability.WRITE_CONTENT,
+ Capability.APPEND_CONTENT, Capability.RANDOM_ACCESS_READ,
+ Capability.RANDOM_ACCESS_WRITE
+ }));
+
+ /**
+ * Constructor.
+ */
+ public RamFileProvider()
+ {
+ super();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.commons.vfs.provider.AbstractOriginatingFileProvider#doCreateFileSystem(
+ * org.apache.commons.vfs.FileName, org.apache.commons.vfs.FileSystemOptions)
+ */
+ protected FileSystem doCreateFileSystem(FileName name,
+ FileSystemOptions fileSystemOptions) throws FileSystemException
+ {
+ return new RamFileSystem(name, fileSystemOptions);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.commons.vfs.provider.FileProvider#getCapabilities()
+ */
+ public Collection getCapabilities()
+ {
+ return capabilities;
+ }
+}
Added: synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/RamFileRandomAccessContent.java
URL: http://svn.apache.org/viewvc/synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/RamFileRandomAccessContent.java?rev=1000332&view=auto
==============================================================================
--- synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/RamFileRandomAccessContent.java (added)
+++ synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/RamFileRandomAccessContent.java Thu Sep 23 06:04:21 2010
@@ -0,0 +1,614 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.vfs.provider.ram;
+
+import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.EOFException;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.commons.vfs.RandomAccessContent;
+import org.apache.commons.vfs.util.RandomAccessMode;
+
+/**
+ * RAM File Random Access Content.
+ * @author <a href="http://commons.apache.org/vfs/team-list.html">Commons VFS team</a>
+ */
+public class RamFileRandomAccessContent implements RandomAccessContent
+{
+ /**
+ * File Pointer
+ */
+ protected int filePointer = 0;
+
+ /**
+ * Buffer
+ */
+ private byte[] buf;
+
+ /**
+ * buffer
+ */
+ private byte[] buffer8 = new byte[8];
+
+ /**
+ * buffer
+ */
+ private byte[] buffer4 = new byte[4];
+
+ /**
+ * buffer
+ */
+ private byte[] buffer2 = new byte[2];
+
+ /**
+ * buffer
+ */
+ private byte[] buffer1 = new byte[1];
+
+ /**
+ * Mode
+ */
+ private RandomAccessMode mode;
+
+ /**
+ * File
+ */
+ private RamFileObject file;
+
+ private InputStream rafis;
+
+ /**
+ * @param file The file to access.
+ * @param mode The access mode.
+ */
+ public RamFileRandomAccessContent(RamFileObject file, RandomAccessMode mode)
+ {
+ super();
+ this.buf = file.getData().getBuffer();
+ this.file = file;
+ this.mode = mode;
+
+ rafis = new InputStream()
+ {
+ public int read() throws IOException
+ {
+ try
+ {
+ return readByte();
+ }
+ catch (EOFException e)
+ {
+ return -1;
+ }
+ }
+
+ public long skip(long n) throws IOException
+ {
+ seek(getFilePointer() + n);
+ return n;
+ }
+
+ public void close() throws IOException
+ {
+ }
+
+ public int read(byte[] b) throws IOException
+ {
+ return read(b, 0, b.length);
+ }
+
+ public int read(byte[] b, int off, int len) throws IOException
+ {
+ int retLen = Math.min(len, getLeftBytes());
+ RamFileRandomAccessContent.this.readFully(b, off, retLen);
+ return retLen;
+ }
+
+ public int available() throws IOException
+ {
+ return getLeftBytes();
+ }
+ };
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.commons.vfs.RandomAccessContent#getFilePointer()
+ */
+ public long getFilePointer() throws IOException
+ {
+ return this.filePointer;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.commons.vfs.RandomAccessContent#seek(long)
+ */
+ public void seek(long pos) throws IOException
+ {
+ this.filePointer = (int) pos;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.commons.vfs.RandomAccessContent#length()
+ */
+ public long length() throws IOException
+ {
+ return buf.length;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.commons.vfs.RandomAccessContent#close()
+ */
+ public void close() throws IOException
+ {
+
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataInput#readByte()
+ */
+ public byte readByte() throws IOException
+ {
+ return (byte) this.readUnsignedByte();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataInput#readChar()
+ */
+ public char readChar() throws IOException
+ {
+ int ch1 = this.readUnsignedByte();
+ int ch2 = this.readUnsignedByte();
+ return (char) ((ch1 << 8) + (ch2 << 0));
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataInput#readDouble()
+ */
+ public double readDouble() throws IOException
+ {
+ return Double.longBitsToDouble(this.readLong());
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataInput#readFloat()
+ */
+ public float readFloat() throws IOException
+ {
+ return Float.intBitsToFloat(this.readInt());
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataInput#readInt()
+ */
+ public int readInt() throws IOException
+ {
+ return (readUnsignedByte() << 24) | (readUnsignedByte() << 16)
+ | (readUnsignedByte() << 8) | readUnsignedByte();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataInput#readUnsignedByte()
+ */
+ public int readUnsignedByte() throws IOException
+ {
+ if (filePointer < buf.length)
+ {
+ return buf[filePointer++] & 0xFF;
+ }
+ else
+ {
+ throw new EOFException();
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataInput#readUnsignedShort()
+ */
+ public int readUnsignedShort() throws IOException
+ {
+ this.readFully(buffer2);
+ return toUnsignedShort(buffer2);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataInput#readLong()
+ */
+ public long readLong() throws IOException
+ {
+ this.readFully(buffer8);
+ return toLong(buffer8);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataInput#readShort()
+ */
+ public short readShort() throws IOException
+ {
+ this.readFully(buffer2);
+ return toShort(buffer2);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataInput#readBoolean()
+ */
+ public boolean readBoolean() throws IOException
+ {
+ return (this.readUnsignedByte() != 0);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataInput#skipBytes(int)
+ */
+ public int skipBytes(int n) throws IOException
+ {
+ if (n < 0)
+ {
+ throw new IndexOutOfBoundsException(
+ "The skip number can't be negative");
+ }
+
+ long newPos = filePointer + n;
+
+ if (newPos > buf.length)
+ {
+ throw new IndexOutOfBoundsException("Tyring to skip too much bytes");
+ }
+
+ seek(newPos);
+
+ return n;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataInput#readFully(byte[])
+ */
+ public void readFully(byte[] b) throws IOException
+ {
+ this.readFully(b, 0, b.length);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataInput#readFully(byte[], int, int)
+ */
+ public void readFully(byte[] b, int off, int len) throws IOException
+ {
+ if (len < 0)
+ {
+ throw new IndexOutOfBoundsException("Length is lower than 0");
+ }
+
+ if (len > this.getLeftBytes())
+ {
+ throw new IndexOutOfBoundsException("Read length (" + len
+ + ") is higher than buffer left bytes ("
+ + this.getLeftBytes() + ") ");
+ }
+
+ System.arraycopy(buf, filePointer, b, off, len);
+
+ filePointer += len;
+ }
+
+ private int getLeftBytes()
+ {
+ return buf.length - filePointer;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataInput#readUTF()
+ */
+ public String readUTF() throws IOException
+ {
+ return DataInputStream.readUTF(this);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataOutput#write(byte[], int, int)
+ */
+ public void write(byte[] b, int off, int len) throws IOException
+ {
+ if (this.getLeftBytes() < len)
+ {
+ int newSize = this.buf.length + len - this.getLeftBytes();
+ this.file.resize(newSize);
+ this.buf = this.file.getData().getBuffer();
+ }
+ System.arraycopy(b, off, this.buf, filePointer, len);
+ this.filePointer += len;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataOutput#write(byte[])
+ */
+ public void write(byte[] b) throws IOException
+ {
+ this.write(b, 0, b.length);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataOutput#writeByte(int)
+ */
+ public void writeByte(int i) throws IOException
+ {
+ this.write(i);
+ }
+
+ /**
+ * Build a long from first 8 bytes of the array.
+ *
+ * @author Apache-Commons-Id Team
+ * @param b
+ * The byte[] to convert.
+ * @return A long.
+ */
+ public static long toLong(byte[] b)
+ {
+ return ((((long) b[7]) & 0xFF) + ((((long) b[6]) & 0xFF) << 8)
+ + ((((long) b[5]) & 0xFF) << 16)
+ + ((((long) b[4]) & 0xFF) << 24)
+ + ((((long) b[3]) & 0xFF) << 32)
+ + ((((long) b[2]) & 0xFF) << 40)
+ + ((((long) b[1]) & 0xFF) << 48) + ((((long) b[0]) & 0xFF) << 56));
+ }
+
+ /**
+ * Build a 8-byte array from a long. No check is performed on the array
+ * length.
+ *
+ * @author Commons-Id Team
+ *
+ * @param n
+ * The number to convert.
+ * @param b
+ * The array to fill.
+ * @return A byte[].
+ */
+ public static byte[] toBytes(long n, byte[] b)
+ {
+ b[7] = (byte) (n);
+ n >>>= 8;
+ b[6] = (byte) (n);
+ n >>>= 8;
+ b[5] = (byte) (n);
+ n >>>= 8;
+ b[4] = (byte) (n);
+ n >>>= 8;
+ b[3] = (byte) (n);
+ n >>>= 8;
+ b[2] = (byte) (n);
+ n >>>= 8;
+ b[1] = (byte) (n);
+ n >>>= 8;
+ b[0] = (byte) (n);
+ return b;
+ }
+
+ /**
+ * Build a short from first 2 bytes of the array.
+ *
+ * @author Apache-Commons-Id Team
+ * @param b
+ * The byte[] to convert.
+ * @return A short.
+ */
+ public static short toShort(byte[] b)
+ {
+ return (short) toUnsignedShort(b);
+ }
+
+ /**
+ * Build a short from first 2 bytes of the array.
+ *
+ * @author Apache-Commons-Id Team
+ * @param b
+ * The byte[] to convert.
+ * @return A short.
+ */
+ public static int toUnsignedShort(byte[] b)
+ {
+ return ((b[1] & 0xFF) + ((b[0] & 0xFF) << 8));
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataOutput#write(int)
+ */
+ public void write(int b) throws IOException
+ {
+ buffer1[0] = (byte) b;
+ this.write(buffer1);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataOutput#writeBoolean(boolean)
+ */
+ public void writeBoolean(boolean v) throws IOException
+ {
+ this.write(v ? 1 : 0);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataOutput#writeBytes(java.lang.String)
+ */
+ public void writeBytes(String s) throws IOException
+ {
+ write(s.getBytes());
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataOutput#writeChar(int)
+ */
+ public void writeChar(int v) throws IOException
+ {
+ buffer2[0] = (byte) ((v >>> 8) & 0xFF);
+ buffer2[1] = (byte) ((v >>> 0) & 0xFF);
+ write(buffer2);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataOutput#writeChars(java.lang.String)
+ */
+ public void writeChars(String s) throws IOException
+ {
+ int len = s.length();
+ for (int i = 0; i < len; i++)
+ {
+ writeChar(s.charAt(i));
+ }
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataOutput#writeDouble(double)
+ */
+ public void writeDouble(double v) throws IOException
+ {
+ writeLong(Double.doubleToLongBits(v));
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataOutput#writeFloat(float)
+ */
+ public void writeFloat(float v) throws IOException
+ {
+ writeInt(Float.floatToIntBits(v));
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataOutput#writeInt(int)
+ */
+ public void writeInt(int v) throws IOException
+ {
+ buffer4[0] = (byte) ((v >>> 24) & 0xFF);
+ buffer4[1] = (byte) ((v >>> 16) & 0xFF);
+ buffer4[2] = (byte) ((v >>> 8) & 0xFF);
+ buffer4[3] = (byte) (v & 0xFF);
+ write(buffer4);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataOutput#writeLong(long)
+ */
+ public void writeLong(long v) throws IOException
+ {
+ write(toBytes(v, buffer8));
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataOutput#writeShort(int)
+ */
+ public void writeShort(int v) throws IOException
+ {
+ buffer2[0] = (byte) ((v >>> 8) & 0xFF);
+ buffer2[1] = (byte) (v & 0xFF);
+ write(buffer2);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataOutput#writeUTF(java.lang.String)
+ */
+ public void writeUTF(String str) throws IOException
+ {
+ ByteArrayOutputStream out = new ByteArrayOutputStream(str.length());
+ DataOutputStream dataOut = new DataOutputStream(out);
+ dataOut.writeUTF(str);
+ dataOut.flush();
+ dataOut.close();
+ byte[] b = out.toByteArray();
+ write(b);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see java.io.DataInput#readLine()
+ */
+ public String readLine() throws IOException
+ {
+ throw new UnsupportedOperationException("deprecated");
+ }
+
+ public InputStream getInputStream() throws IOException
+ {
+ return rafis;
+ }
+}
Added: synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/RamFileSystem.java
URL: http://svn.apache.org/viewvc/synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/RamFileSystem.java?rev=1000332&view=auto
==============================================================================
--- synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/RamFileSystem.java (added)
+++ synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/RamFileSystem.java Thu Sep 23 06:04:21 2010
@@ -0,0 +1,313 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.vfs.provider.ram;
+
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+import org.apache.commons.vfs.FileName;
+import org.apache.commons.vfs.FileObject;
+import org.apache.commons.vfs.FileSystemException;
+import org.apache.commons.vfs.FileSystemOptions;
+import org.apache.commons.vfs.FileType;
+import org.apache.commons.vfs.provider.AbstractFileSystem;
+
+/**
+ * A RAM File System.
+ * @author <a href="http://commons.apache.org/vfs/team-list.html">Commons VFS team</a>
+ */
+public class RamFileSystem extends AbstractFileSystem implements Serializable
+{
+ /**
+ * Cache of RAM File Data
+ */
+ private Map cache;
+
+ /**
+ * @param rootName The root file name.
+ * @param fileSystemOptions The FileSystem options.
+ */
+ protected RamFileSystem(FileName rootName,
+ FileSystemOptions fileSystemOptions)
+ {
+ super(rootName, null, fileSystemOptions);
+ this.cache = Collections.synchronizedMap(new HashMap());
+ // create root
+ RamFileData rootData = new RamFileData(rootName);
+ rootData.setType(FileType.FOLDER);
+ rootData.setLastModified(System.currentTimeMillis());
+ this.cache.put(rootName, rootData);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.commons.vfs.provider.AbstractFileSystem#createFile(org.apache.commons.vfs.FileName)
+ */
+ protected FileObject createFile(FileName name) throws Exception
+ {
+ RamFileObject file = new RamFileObject(name, this);
+ return file;
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.apache.commons.vfs.provider.AbstractFileSystem#addCapabilities(java.util.Collection)
+ */
+ protected void addCapabilities(Collection caps)
+ {
+ caps.addAll(RamFileProvider.capabilities);
+ }
+
+ /**
+ * @param name The name of the file.
+ * @return children The names of the children.
+ */
+ String[] listChildren(FileName name)
+ {
+ RamFileData data = (RamFileData) this.cache.get(name);
+ if (data == null || !data.getType().hasChildren())
+ {
+ return null;
+ }
+ Collection children = data.getChildren();
+
+ String[] names = new String[children.size()];
+
+ int pos = 0;
+ Iterator iter = children.iterator();
+ while (iter.hasNext())
+ {
+ RamFileData childData = (RamFileData) iter.next();
+ names[pos] = childData.getName().getBaseName();
+ pos++;
+ }
+
+ return names;
+ }
+
+ /**
+ * Delete a file
+ *
+ * @param file
+ * @throws FileSystemException
+ */
+ void delete(RamFileObject file) throws FileSystemException
+ {
+ // root is read only check
+ if (file.getParent() == null)
+ {
+ throw new FileSystemException("unable to delete root");
+ }
+
+ // Remove reference from cache
+ this.cache.remove(file.getName());
+ // Notify the parent
+ RamFileObject parent = (RamFileObject) this.resolveFile(file
+ .getParent().getName());
+ parent.getData().removeChild(file.getData());
+ parent.close();
+ // Close the file
+ file.getData().clear();
+ file.close();
+ }
+
+ /**
+ * Saves a file
+ *
+ * @param file
+ * @throws FileSystemException
+ */
+ void save(final RamFileObject file) throws FileSystemException
+ {
+
+ // Validate name
+ if (file.getData().getName() == null)
+ {
+ throw new FileSystemException(new IllegalStateException(
+ "The data has no name. " + file));
+ }
+
+ // Add to the parent
+ if (file.getName().getDepth() > 0)
+ {
+ RamFileData parentData = (RamFileData) this.cache.get(file
+ .getParent().getName());
+ // Only if not already added
+ if (!parentData.hasChildren(file.getData()))
+ {
+ RamFileObject parent = (RamFileObject) file.getParent();
+ parent.getData().addChild(file.getData());
+ parent.close();
+ }
+ }
+ // Store in cache
+ cache.put(file.getName(), file.getData());
+ file.getData().updateLastModified();
+ file.close();
+ }
+
+ /**
+ * @param from The original file.
+ * @param to The new file.
+ * @throws FileSystemException if an error occurs.
+ */
+ void rename(RamFileObject from, RamFileObject to)
+ throws FileSystemException
+ {
+ if (!this.cache.containsKey(from.getName()))
+ {
+ throw new FileSystemException("File does not exist: "
+ + from.getName());
+ }
+ // Copy data
+
+ to.getData().setBuffer(from.getData().getBuffer());
+ to.getData().setLastModified(from.getData().getLastModified());
+ to.getData().setType(from.getData().getType());
+
+ this.save(to);
+ this.delete(from);
+ }
+
+ public void attach(RamFileObject fo)
+ {
+ if (fo.getName() == null)
+ {
+ throw new IllegalArgumentException("Null argument");
+ }
+ RamFileData data = (RamFileData) this.cache.get(fo.getName());
+ if (data == null)
+ {
+ data = new RamFileData(fo.getName());
+ }
+ fo.setData(data);
+ }
+
+ /**
+ * Import a Tree.
+ *
+ * @param file The File
+ * @throws FileSystemException if an error occurs.
+ */
+ public void importTree(File file) throws FileSystemException
+ {
+ FileObject fileFo = getFileSystemManager().toFileObject(file);
+ this.toRamFileObject(fileFo, fileFo);
+ }
+
+ /**
+ * Import the given file with the name relative to the given root
+ *
+ * @param fo
+ * @param root
+ * @throws FileSystemException
+ */
+ void toRamFileObject(FileObject fo, FileObject root)
+ throws FileSystemException
+ {
+ RamFileObject memFo = (RamFileObject) this.resolveFile(fo.getName()
+ .getPath().substring(root.getName().getPath().length()));
+ if (fo.getType().hasChildren())
+ {
+ // Create Folder
+ memFo.createFolder();
+ // Import recursively
+ FileObject[] fos = fo.getChildren();
+ for (int i = 0; i < fos.length; i++)
+ {
+ FileObject child = fos[i];
+ this.toRamFileObject(child, root);
+ }
+ }
+ else if (fo.getType().equals(FileType.FILE))
+ {
+ // Read bytes
+ try
+ {
+ InputStream is = fo.getContent().getInputStream();
+ try
+ {
+ OutputStream os = new BufferedOutputStream(memFo
+ .getOutputStream(), 512);
+ int i;
+ while ((i = is.read()) != -1)
+ {
+ os.write(i);
+ }
+ os.flush();
+ os.close();
+ }
+ finally
+ {
+ try
+ {
+ is.close();
+ }
+ catch (IOException e)
+ {
+ // ignore on close exception
+ }
+ }
+ }
+ catch (IOException e)
+ {
+ throw new FileSystemException(e.getClass().getName() + " "
+ + e.getMessage());
+ }
+ }
+ else
+ {
+ throw new FileSystemException("File is not a folder nor a file "
+ + memFo);
+ }
+ }
+
+ /**
+ * @return Returns the size of the FileSystem
+ */
+ int size()
+ {
+ int size = 0;
+ Iterator iter = cache.values().iterator();
+ while (iter.hasNext())
+ {
+ RamFileData data = (RamFileData) iter.next();
+ size += data.size();
+ }
+ return size;
+ }
+
+ /**
+ * Close the RAMFileSystem.
+ */
+ public void close()
+ {
+ this.cache = null;
+ super.close();
+ }
+}
Added: synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/RamFileSystemConfigBuilder.java
URL: http://svn.apache.org/viewvc/synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/RamFileSystemConfigBuilder.java?rev=1000332&view=auto
==============================================================================
--- synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/RamFileSystemConfigBuilder.java (added)
+++ synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/RamFileSystemConfigBuilder.java Thu Sep 23 06:04:21 2010
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.vfs.provider.ram;
+
+import org.apache.commons.vfs.FileSystemConfigBuilder;
+import org.apache.commons.vfs.FileSystemOptions;
+
+/**
+ * Config Builder for the RAM filesystem.
+ * @author <a href="http://commons.apache.org/vfs/team-list.html">Commons VFS team</a>
+ */
+public final class RamFileSystemConfigBuilder extends FileSystemConfigBuilder
+{
+
+ /** max size key. */
+ private static final String MAX_SIZE_KEY = "maxsize";
+
+ /** config builder singleton. */
+ private static RamFileSystemConfigBuilder singleton = new RamFileSystemConfigBuilder();
+
+ /**
+ * Constructor
+ */
+ private RamFileSystemConfigBuilder()
+ {
+ super("ram.");
+ }
+
+ /**
+ * @return the config builder singleton
+ */
+ public static RamFileSystemConfigBuilder getInstance()
+ {
+ return singleton;
+ }
+
+ /**
+ * @inheritDoc
+ */
+ protected Class getConfigClass()
+ {
+ return RamFileSystem.class;
+ }
+
+ /**
+ * @param opts The FileSystem options.
+ * @return The maximum size of the file.
+ * @see #setMaxSize
+ */
+ public int getMaxSize(FileSystemOptions opts)
+ {
+ return getInteger(opts, MAX_SIZE_KEY, Integer.MAX_VALUE);
+ }
+
+ /**
+ * Sets the maximum size of the file system.
+ *
+ * @param opts The FileSystem options.
+ * @param sizeInBytes The maximum file size.
+ */
+ public void setMaxSize(FileSystemOptions opts, int sizeInBytes)
+ {
+ setParam(opts, MAX_SIZE_KEY, new Integer(sizeInBytes));
+ }
+
+}
Added: synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/package.html
URL: http://svn.apache.org/viewvc/synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/package.html?rev=1000332&view=auto
==============================================================================
--- synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/package.html (added)
+++ synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/ram/package.html Thu Sep 23 06:04:21 2010
@@ -0,0 +1,19 @@
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<body>
+<p>The RAM File Provider.</p>
+</body>
\ No newline at end of file
Added: synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/res/ResourceFileProvider.java
URL: http://svn.apache.org/viewvc/synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/res/ResourceFileProvider.java?rev=1000332&view=auto
==============================================================================
--- synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/res/ResourceFileProvider.java (added)
+++ synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/res/ResourceFileProvider.java Thu Sep 23 06:04:21 2010
@@ -0,0 +1,99 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.vfs.provider.res;
+
+import org.apache.commons.vfs.Capability;
+import org.apache.commons.vfs.FileObject;
+import org.apache.commons.vfs.FileSystem;
+import org.apache.commons.vfs.FileSystemConfigBuilder;
+import org.apache.commons.vfs.FileSystemException;
+import org.apache.commons.vfs.FileSystemOptions;
+import org.apache.commons.vfs.provider.AbstractFileProvider;
+import org.apache.commons.vfs.provider.UriParser;
+
+import java.net.URL;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+
+/**
+ * The Resource provider.
+ *
+ * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
+ * @version $Revision: 895278 $ $Date: 2010-01-03 01:32:29 +0530 (Sun, 03 Jan 2010) $
+ */
+public class ResourceFileProvider extends AbstractFileProvider
+{
+ /** The provider's capabilities */
+ protected static final Collection capabilities = Collections.unmodifiableCollection(Arrays.asList(new Capability[]
+ {
+ Capability.DISPATCHER
+ }));
+
+ public ResourceFileProvider()
+ {
+ super();
+ }
+
+ /**
+ * Locates a file object, by absolute URI.
+ * @param baseFile The base file.
+ * @param uri The URI of the file to locate.
+ * @param fileSystemOptions The FileSystem options.
+ * @return the FileObject.
+ * @throws FileSystemException if an error occurs.
+ */
+ public FileObject findFile(final FileObject baseFile,
+ final String uri,
+ final FileSystemOptions fileSystemOptions)
+ throws FileSystemException
+ {
+ StringBuffer buf = new StringBuffer(80);
+ UriParser.extractScheme(uri, buf);
+ String resourceName = buf.toString();
+
+ ClassLoader cl = ResourceFileSystemConfigBuilder.getInstance().getClassLoader(fileSystemOptions);
+ if (cl == null)
+ {
+ cl = getClass().getClassLoader();
+ }
+ final URL url = cl.getResource(resourceName);
+
+ if (url == null)
+ {
+ throw new FileSystemException("vfs.provider.url/badly-formed-uri.error", uri);
+ }
+
+ FileObject fo = getContext().getFileSystemManager().resolveFile(url.toExternalForm());
+ return fo;
+ }
+
+ public FileSystemConfigBuilder getConfigBuilder()
+ {
+ return org.apache.commons.vfs.provider.res.ResourceFileSystemConfigBuilder.getInstance();
+ }
+
+ public void closeFileSystem(FileSystem filesystem)
+ {
+ // no filesystem created here - so nothing to do
+ }
+
+ public Collection getCapabilities()
+ {
+ return capabilities;
+ }
+}
Added: synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/res/ResourceFileSystemConfigBuilder.java
URL: http://svn.apache.org/viewvc/synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/res/ResourceFileSystemConfigBuilder.java?rev=1000332&view=auto
==============================================================================
--- synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/res/ResourceFileSystemConfigBuilder.java (added)
+++ synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/res/ResourceFileSystemConfigBuilder.java Thu Sep 23 06:04:21 2010
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.vfs.provider.res;
+
+import org.apache.commons.vfs.FileSystemConfigBuilder;
+import org.apache.commons.vfs.FileSystemOptions;
+import org.apache.commons.vfs.provider.url.UrlFileSystem;
+
+/**
+ * The config BUILDER for various ftp configuration options.
+ *
+ * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
+ * @version $Revision: 895278 $ $Date: 2010-01-03 01:32:29 +0530 (Sun, 03 Jan 2010) $
+ */
+public final class ResourceFileSystemConfigBuilder extends FileSystemConfigBuilder
+{
+ private static final ResourceFileSystemConfigBuilder BUILDER = new ResourceFileSystemConfigBuilder();
+
+ private ResourceFileSystemConfigBuilder()
+ {
+ super("resource.");
+ }
+
+ public static ResourceFileSystemConfigBuilder getInstance()
+ {
+ return BUILDER;
+ }
+
+
+ public void setClassLoader(FileSystemOptions opts, ClassLoader classLoader)
+ {
+ setParam(opts, ClassLoader.class.getName(), classLoader);
+ }
+
+ public ClassLoader getClassLoader(FileSystemOptions opts)
+ {
+ return (ClassLoader) getParam(opts, ClassLoader.class.getName());
+ }
+
+ protected Class getConfigClass()
+ {
+ return UrlFileSystem.class;
+ }
+}
Added: synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/res/package.html
URL: http://svn.apache.org/viewvc/synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/res/package.html?rev=1000332&view=auto
==============================================================================
--- synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/res/package.html (added)
+++ synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/res/package.html Thu Sep 23 06:04:21 2010
@@ -0,0 +1,19 @@
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+-->
+<body>
+<p>The Resource File Provider</p>
+</body>
\ No newline at end of file
Added: synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/sftp/SftpClientFactory.java
URL: http://svn.apache.org/viewvc/synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/sftp/SftpClientFactory.java?rev=1000332&view=auto
==============================================================================
--- synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/sftp/SftpClientFactory.java (added)
+++ synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/sftp/SftpClientFactory.java Thu Sep 23 06:04:21 2010
@@ -0,0 +1,290 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.vfs.provider.sftp;
+
+import com.jcraft.jsch.JSch;
+import com.jcraft.jsch.JSchException;
+import com.jcraft.jsch.Session;
+import com.jcraft.jsch.UserInfo;
+import com.jcraft.jsch.Proxy;
+import com.jcraft.jsch.ProxyHTTP;
+import com.jcraft.jsch.ProxySOCKS5;
+import org.apache.commons.vfs.FileSystemException;
+import org.apache.commons.vfs.FileSystemOptions;
+import org.apache.commons.vfs.util.Os;
+
+import java.io.File;
+import java.util.Properties;
+
+/**
+ * Create a JSch Session instance.
+ *
+ * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
+ * @version $Revision: 895278 $ $Date: 2010-01-03 01:32:29 +0530 (Sun, 03 Jan 2010) $
+ */
+public final class SftpClientFactory
+{
+ private static final String SSH_DIR_NAME = ".ssh";
+
+ private SftpClientFactory()
+ {
+ }
+
+ /**
+ * Creates a new connection to the server.
+ * @param hostname The name of the host to connect to.
+ * @param port The port to use.
+ * @param username The user's id.
+ * @param password The user's password.
+ * @param fileSystemOptions The FileSystem options.
+ * @return A Session.
+ * @throws FileSystemException if an error occurs.
+ */
+ public static Session createConnection(String hostname, int port, char[] username, char[] password,
+ FileSystemOptions fileSystemOptions) throws FileSystemException
+ {
+ JSch jsch = new JSch();
+
+ File sshDir = null;
+
+ // new style - user passed
+ File knownHostsFile = SftpFileSystemConfigBuilder.getInstance().getKnownHosts(fileSystemOptions);
+ File[] identities = SftpFileSystemConfigBuilder.getInstance().getIdentities(fileSystemOptions);
+
+ if (knownHostsFile != null)
+ {
+ try
+ {
+ jsch.setKnownHosts(knownHostsFile.getAbsolutePath());
+ }
+ catch (JSchException e)
+ {
+ throw new FileSystemException("vfs.provider.sftp/known-hosts.error",
+ knownHostsFile.getAbsolutePath(), e);
+ }
+ }
+ else
+ {
+ if (sshDir == null)
+ {
+ sshDir = findSshDir();
+ }
+ // Load the known hosts file
+ knownHostsFile = new File(sshDir, "known_hosts");
+ if (knownHostsFile.isFile() && knownHostsFile.canRead())
+ {
+ try
+ {
+ jsch.setKnownHosts(knownHostsFile.getAbsolutePath());
+ }
+ catch (JSchException e)
+ {
+ throw new FileSystemException("vfs.provider.sftp/known-hosts.error",
+ knownHostsFile.getAbsolutePath(), e);
+ }
+ }
+ }
+
+ if (identities != null)
+ {
+ for (int iterIdentities = 0; iterIdentities < identities.length; iterIdentities++)
+ {
+ final File privateKeyFile = identities[iterIdentities];
+ try
+ {
+ jsch.addIdentity(privateKeyFile.getAbsolutePath());
+ }
+ catch (final JSchException e)
+ {
+ throw new FileSystemException("vfs.provider.sftp/load-private-key.error", privateKeyFile, e);
+ }
+ }
+ }
+ else
+ {
+ if (sshDir == null)
+ {
+ sshDir = findSshDir();
+ }
+
+ // Load the private key (rsa-key only)
+ final File privateKeyFile = new File(sshDir, "id_rsa");
+ if (privateKeyFile.isFile() && privateKeyFile.canRead())
+ {
+ try
+ {
+ jsch.addIdentity(privateKeyFile.getAbsolutePath());
+ }
+ catch (final JSchException e)
+ {
+ throw new FileSystemException("vfs.provider.sftp/load-private-key.error", privateKeyFile, e);
+ }
+ }
+ }
+
+ Session session;
+ try
+ {
+ session = jsch.getSession(new String(username),
+ hostname,
+ port);
+ if (password != null)
+ {
+ session.setPassword(new String(password));
+ }
+
+ Integer timeout = SftpFileSystemConfigBuilder.getInstance().getTimeout(fileSystemOptions);
+ if (timeout != null)
+ {
+ session.setTimeout(timeout.intValue());
+ }
+
+ UserInfo userInfo = SftpFileSystemConfigBuilder.getInstance().getUserInfo(fileSystemOptions);
+ if (userInfo != null)
+ {
+ session.setUserInfo(userInfo);
+ }
+
+ Properties config = new Properties();
+
+ //set StrictHostKeyChecking property
+ String strictHostKeyChecking =
+ SftpFileSystemConfigBuilder.getInstance().getStrictHostKeyChecking(fileSystemOptions);
+ if (strictHostKeyChecking != null)
+ {
+ config.setProperty("StrictHostKeyChecking", strictHostKeyChecking);
+ }
+ //set PreferredAuthentications property
+ String preferredAuthentications = SftpFileSystemConfigBuilder.getInstance().
+ getPreferredAuthentications(fileSystemOptions);
+ if (preferredAuthentications != null)
+ {
+ config.setProperty("PreferredAuthentications", preferredAuthentications);
+ }
+
+ //set compression property
+ String compression = SftpFileSystemConfigBuilder.getInstance().getCompression(fileSystemOptions);
+ if (compression != null)
+ {
+ config.setProperty("compression.s2c", compression);
+ config.setProperty("compression.c2s", compression);
+ }
+
+ String proxyHost = SftpFileSystemConfigBuilder.getInstance().getProxyHost(fileSystemOptions);
+ if (proxyHost != null)
+ {
+ int proxyPort = SftpFileSystemConfigBuilder.getInstance().getProxyPort(fileSystemOptions);
+ SftpFileSystemConfigBuilder.ProxyType proxyType =
+ SftpFileSystemConfigBuilder.getInstance().getProxyType(fileSystemOptions);
+ Proxy proxy = null;
+ if (SftpFileSystemConfigBuilder.PROXY_HTTP.equals(proxyType))
+ {
+ if (proxyPort != 0)
+ {
+ proxy = new ProxyHTTP(proxyHost, proxyPort);
+ }
+ else
+ {
+ proxy = new ProxyHTTP(proxyHost);
+ }
+ }
+ else if (SftpFileSystemConfigBuilder.PROXY_SOCKS5.equals(proxyType))
+ {
+ if (proxyPort != 0)
+ {
+ proxy = new ProxySOCKS5(proxyHost, proxyPort);
+ }
+ else
+ {
+ proxy = new ProxySOCKS5(proxyHost);
+ }
+ }
+
+ if (proxy != null)
+ {
+ session.setProxy(proxy);
+ }
+ }
+
+ //set properties for the session
+ if (config.size() > 0)
+ {
+ session.setConfig(config);
+ }
+ session.setDaemonThread(true);
+ session.connect();
+ }
+ catch (final Exception exc)
+ {
+ throw new FileSystemException("vfs.provider.sftp/connect.error", new Object[]{hostname}, exc);
+ }
+
+
+ return session;
+ }
+
+ /**
+ * Finds the .ssh directory.
+ * <p>The lookup order is:</p>
+ * <ol>
+ * <li>The system property <code>vfs.sftp.sshdir</code> (the override
+ * mechanism)</li>
+ * <li><code>{user.home}/.ssh</code></li>
+ * <li>On Windows only: C:\cygwin\home\{user.name}\.ssh</li>
+ * <li>The current directory, as a last resort.</li>
+ * <ol>
+ * <p/>
+ * Windows Notes:
+ * The default installation directory for Cygwin is <code>C:\cygwin</code>.
+ * On my set up (Gary here), I have Cygwin in C:\bin\cygwin, not the default.
+ * Also, my .ssh directory was created in the {user.home} directory.
+ * </p>
+ *
+ * @return The .ssh directory
+ */
+ private static File findSshDir()
+ {
+ String sshDirPath;
+ sshDirPath = System.getProperty("vfs.sftp.sshdir");
+ if (sshDirPath != null)
+ {
+ File sshDir = new File(sshDirPath);
+ if (sshDir.exists())
+ {
+ return sshDir;
+ }
+ }
+
+ File sshDir = new File(System.getProperty("user.home"), SSH_DIR_NAME);
+ if (sshDir.exists())
+ {
+ return sshDir;
+ }
+
+ if (Os.isFamily(Os.OS_FAMILY_WINDOWS))
+ {
+ // TODO - this may not be true
+ final String userName = System.getProperty("user.name");
+ sshDir = new File("C:\\cygwin\\home\\" + userName + "\\" + SSH_DIR_NAME);
+ if (sshDir.exists())
+ {
+ return sshDir;
+ }
+ }
+ return new File("");
+ }
+}
Added: synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/sftp/SftpFileNameParser.java
URL: http://svn.apache.org/viewvc/synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/sftp/SftpFileNameParser.java?rev=1000332&view=auto
==============================================================================
--- synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/sftp/SftpFileNameParser.java (added)
+++ synapse/branches/commons-vfs-2-synapse-2.0/core/src/main/java/org/apache/commons/vfs/provider/sftp/SftpFileNameParser.java Thu Sep 23 06:04:21 2010
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.vfs.provider.sftp;
+
+import org.apache.commons.vfs.provider.FileNameParser;
+import org.apache.commons.vfs.provider.URLFileNameParser;
+
+
+/**
+ * Implementation for sftp. set default port to 22.
+ * @author <a href="http://commons.apache.org/vfs/team-list.html">Commons VFS team</a>
+ */
+public class SftpFileNameParser extends URLFileNameParser
+{
+ private static final SftpFileNameParser INSTANCE = new SftpFileNameParser();
+
+ public SftpFileNameParser()
+ {
+ super(22);
+ }
+
+ public static FileNameParser getInstance()
+ {
+ return INSTANCE;
+ }
+}