You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2013/11/28 04:52:32 UTC
svn commit: r1546276 -
/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/impl/DefaultFileReplicator.java
Author: ggregory
Date: Thu Nov 28 03:52:31 2013
New Revision: 1546276
URL: http://svn.apache.org/r1546276
Log:
Sort members in AB order.
Modified:
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/impl/DefaultFileReplicator.java
Modified: commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/impl/DefaultFileReplicator.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/impl/DefaultFileReplicator.java?rev=1546276&r1=1546275&r2=1546276&view=diff
==============================================================================
--- commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/impl/DefaultFileReplicator.java (original)
+++ commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/impl/DefaultFileReplicator.java Thu Nov 28 03:52:31 2013
@@ -39,19 +39,23 @@ public class DefaultFileReplicator
extends AbstractVfsComponent
implements FileReplicator, TemporaryFileStore
{
+ private static final Log log = LogFactory.getLog(DefaultFileReplicator.class);
+ private static final int MASK = 0xffff;
+
private static final char[] TMP_RESERVED_CHARS = new char[]
{
'?', '/', '\\', ' ', '&', '"', '\'', '*', '#', ';', ':', '<', '>', '|'
};
- private static final Log log = LogFactory.getLog(DefaultFileReplicator.class);
-
- private static final int MASK = 0xffff;
private final ArrayList<Object> copies = new ArrayList<Object>();
- private File tempDir;
private long filecount;
+ private File tempDir;
private boolean tempDirMessageLogged;
+ public DefaultFileReplicator()
+ {
+ }
+
/**
* constructor to set the location of the temporary directory.
*
@@ -62,33 +66,31 @@ public class DefaultFileReplicator
this.tempDir = tempDir;
}
- public DefaultFileReplicator()
+ protected void addFile(final Object file)
{
+ synchronized (copies)
+ {
+ copies.add(file);
+ }
}
/**
- * Initializes this component.
+ * Allocates a new temporary file.
+ * @param baseName the base file name.
+ * @return The created File.
* @throws FileSystemException if an error occurs.
*/
@Override
- public void init() throws FileSystemException
+ public File allocateFile(final String baseName) throws FileSystemException
{
- if (tempDir == null)
+ // Create a unique-ish file name
+ final String basename = createFilename(baseName);
+ synchronized (this)
{
- final String baseTmpDir = System.getProperty("java.io.tmpdir");
-
- tempDir = new File(baseTmpDir, "vfs_cache").getAbsoluteFile();
+ filecount++;
}
- filecount = new Random().nextInt() & MASK;
-
- if (!tempDirMessageLogged)
- {
- final String message = Messages.getString("vfs.impl/temp-dir.info", tempDir);
- VfsLog.info(getLogger(), log, message);
-
- tempDirMessageLogged = true;
- }
+ return createAndAddFile(tempDir, basename);
}
/**
@@ -115,6 +117,44 @@ public class DefaultFileReplicator
}
}
+ protected File createAndAddFile(final File parent, final String basename) throws FileSystemException
+ {
+ final File file = createFile(tempDir, basename);
+
+ // Keep track to delete later
+ addFile(file);
+
+ return file;
+ }
+
+ /**
+ * create the temporary file
+ * @param parent The file to use as the parent of the file being created.
+ * @param name The name of the file to create.
+ * @return The File that was created.
+ * @throws FileSystemException if an error occurs creating the file.
+ */
+ protected File createFile(final File parent, final String name) throws FileSystemException
+ {
+ return new File(parent, UriParser.decode(name));
+ }
+
+ /**
+ * create the temporary file name
+ * @param baseName The base to prepend to the file name being created.
+ * @return the name of the File.
+ */
+ protected String createFilename(final String baseName)
+ {
+ // BUG29007
+ // return baseName + "_" + getFilecount() + ".tmp";
+
+ // imario@apache.org: BUG34976 get rid of maybe reserved and dangerous characters
+ // e.g. to allow replication of http://hostname.org/fileservlet?file=abc.txt
+ final String safeBasename = UriParser.encode(baseName, TMP_RESERVED_CHARS).replace('%', '_');
+ return "tmp_" + getFilecount() + "_" + safeBasename;
+ }
+
/**
* physically deletes the file from the filesystem
* @param file The File to delete.
@@ -133,99 +173,59 @@ public class DefaultFileReplicator
}
}
- /**
- * removes a file from the copies list. Will be used for cleanup. <br/>
- * Notice: The system awaits that the returning object can be cast to a java.io.File
- * @return the File that was removed.
- */
- protected Object removeFile()
- {
- synchronized (copies)
- {
- return copies.remove(0);
- }
- }
-
- /**
- * removes a instance from the list of copies
- * @param file The File to remove.
- */
- protected void removeFile(final Object file)
+ protected long getFilecount()
{
- synchronized (copies)
- {
- copies.remove(file);
- }
+ return filecount;
}
/**
- * Allocates a new temporary file.
- * @param baseName the base file name.
- * @return The created File.
+ * Initializes this component.
* @throws FileSystemException if an error occurs.
*/
@Override
- public File allocateFile(final String baseName) throws FileSystemException
+ public void init() throws FileSystemException
{
- // Create a unique-ish file name
- final String basename = createFilename(baseName);
- synchronized (this)
+ if (tempDir == null)
{
- filecount++;
- }
-
- return createAndAddFile(tempDir, basename);
- }
-
- protected File createAndAddFile(final File parent, final String basename) throws FileSystemException
- {
- final File file = createFile(tempDir, basename);
+ final String baseTmpDir = System.getProperty("java.io.tmpdir");
- // Keep track to delete later
- addFile(file);
+ tempDir = new File(baseTmpDir, "vfs_cache").getAbsoluteFile();
+ }
- return file;
- }
+ filecount = new Random().nextInt() & MASK;
- protected void addFile(final Object file)
- {
- synchronized (copies)
+ if (!tempDirMessageLogged)
{
- copies.add(file);
- }
- }
+ final String message = Messages.getString("vfs.impl/temp-dir.info", tempDir);
+ VfsLog.info(getLogger(), log, message);
- protected long getFilecount()
- {
- return filecount;
+ tempDirMessageLogged = true;
+ }
}
/**
- * create the temporary file name
- * @param baseName The base to prepend to the file name being created.
- * @return the name of the File.
+ * removes a file from the copies list. Will be used for cleanup. <br/>
+ * Notice: The system awaits that the returning object can be cast to a java.io.File
+ * @return the File that was removed.
*/
- protected String createFilename(final String baseName)
+ protected Object removeFile()
{
- // BUG29007
- // return baseName + "_" + getFilecount() + ".tmp";
-
- // imario@apache.org: BUG34976 get rid of maybe reserved and dangerous characters
- // e.g. to allow replication of http://hostname.org/fileservlet?file=abc.txt
- final String safeBasename = UriParser.encode(baseName, TMP_RESERVED_CHARS).replace('%', '_');
- return "tmp_" + getFilecount() + "_" + safeBasename;
+ synchronized (copies)
+ {
+ return copies.remove(0);
+ }
}
/**
- * create the temporary file
- * @param parent The file to use as the parent of the file being created.
- * @param name The name of the file to create.
- * @return The File that was created.
- * @throws FileSystemException if an error occurs creating the file.
+ * removes a instance from the list of copies
+ * @param file The File to remove.
*/
- protected File createFile(final File parent, final String name) throws FileSystemException
+ protected void removeFile(final Object file)
{
- return new File(parent, UriParser.decode(name));
+ synchronized (copies)
+ {
+ copies.remove(file);
+ }
}
/**