1 16 package org.apache.commons.vfs.impl; 17 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.logging.LogFactory; 20 import org.apache.commons.vfs.FileObject; 21 import org.apache.commons.vfs.FileSelector; 22 import org.apache.commons.vfs.FileSystemException; 23 import org.apache.commons.vfs.Selectors; 24 import org.apache.commons.vfs.VfsLog; 25 import org.apache.commons.vfs.provider.AbstractVfsComponent; 26 import org.apache.commons.vfs.provider.FileReplicator; 27 import org.apache.commons.vfs.provider.TemporaryFileStore; 28 import org.apache.commons.vfs.provider.UriParser; 29 import org.apache.commons.vfs.util.Messages; 30 31 import java.io.File ; 32 import java.util.ArrayList ; 33 import java.util.Random ; 34 35 40 public final class DefaultFileReplicator 41 extends AbstractVfsComponent 42 implements FileReplicator, TemporaryFileStore 43 { 44 private final static Log log = LogFactory.getLog(DefaultFileReplicator.class); 45 46 private final ArrayList copies = new ArrayList (); 47 private File tempDir; 48 private long filecount; 49 50 private char[] TMP_RESERVED_CHARS = new char[] 51 { 52 '?', '/', '\\', ' ', '&', '"', '\'', '*', '#', ';', ':', '<', '>', '|' 53 }; 54 55 60 public DefaultFileReplicator(final File tempDir) 61 { 62 this.tempDir = tempDir; 63 } 64 65 public DefaultFileReplicator() 66 { 67 } 68 69 72 public void init() throws FileSystemException 73 { 74 if (tempDir == null) 75 { 76 tempDir = new File ("vfs_cache").getAbsoluteFile(); 77 } 78 79 filecount = new Random ().nextInt() & 0xffff; 80 } 81 82 85 public void close() 86 { 87 while (copies.size() > 0) 89 { 90 final File file = (File ) copies.remove(0); 91 try 92 { 93 final FileObject fileObject = getContext().toFileObject(file); 94 fileObject.delete(Selectors.SELECT_ALL); 95 } 96 catch (final FileSystemException e) 97 { 98 final String message = Messages.getString("vfs.impl/delete-temp.warn", file.getName()); 99 VfsLog.warn(getLogger(), log, message, e); 101 } 102 } 103 104 if (tempDir != null && tempDir.exists() && tempDir.list().length == 0) 106 { 107 tempDir.delete(); 108 tempDir = null; 109 } 110 } 111 112 115 public File allocateFile(final String baseName) throws FileSystemException 116 { 117 final String basename = createFilename(baseName); 119 synchronized(this) 120 { 121 filecount++; 122 } 123 final File file = createFile(tempDir, basename); 124 125 copies.add(file); 127 128 return file; 129 } 130 131 protected long getFilecount() 132 { 133 return filecount; 134 } 135 136 139 protected String createFilename(final String baseName) 140 { 141 144 String safeBasename = UriParser.encode(baseName).replace('%', '_'); 148 return "tmp_" + getFilecount() + "_" + safeBasename; 149 } 150 151 154 protected File createFile(final File parent, final String name) throws FileSystemException 155 { 156 return new File (parent, UriParser.decode(name)); 157 } 158 159 162 public File replicateFile(final FileObject srcFile, 163 final FileSelector selector) 164 throws FileSystemException 165 { 166 final String basename = srcFile.getName().getBaseName(); 167 final File file = allocateFile(basename); 168 169 final FileObject destFile = getContext().toFileObject(file); 171 destFile.copyFrom(srcFile, selector); 172 173 return file; 174 } 175 } 176 | Popular Tags |