KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > impl > DefaultFileReplicator


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Random JavaDoc;
34
35 /**
36  * A simple file replicator and temporary file store.
37  *
38  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
39  */

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 JavaDoc copies = new ArrayList JavaDoc();
47     private File JavaDoc tempDir;
48     private long filecount;
49
50     private char[] TMP_RESERVED_CHARS = new char[]
51     {
52         '?', '/', '\\', ' ', '&', '"', '\'', '*', '#', ';', ':', '<', '>', '|'
53     };
54
55     /**
56      * constructor to set the location of the temporary directory
57      *
58      * @param tempDir
59      */

60     public DefaultFileReplicator(final File JavaDoc tempDir)
61     {
62         this.tempDir = tempDir;
63     }
64
65     public DefaultFileReplicator()
66     {
67     }
68
69     /**
70      * Initialises this component.
71      */

72     public void init() throws FileSystemException
73     {
74         if (tempDir == null)
75         {
76             tempDir = new File JavaDoc("vfs_cache").getAbsoluteFile();
77         }
78
79         filecount = new Random JavaDoc().nextInt() & 0xffff;
80     }
81
82     /**
83      * Closes the replicator, deleting all temporary files.
84      */

85     public void close()
86     {
87         // Delete the temporary files
88
while (copies.size() > 0)
89         {
90             final File JavaDoc file = (File JavaDoc) 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 JavaDoc message = Messages.getString("vfs.impl/delete-temp.warn", file.getName());
99                 // getLogger().warn(message, e);
100
VfsLog.warn(getLogger(), log, message, e);
101             }
102         }
103
104         // Clean up the temp directory, if it is empty
105
if (tempDir != null && tempDir.exists() && tempDir.list().length == 0)
106         {
107             tempDir.delete();
108             tempDir = null;
109         }
110     }
111
112     /**
113      * Allocates a new temporary file.
114      */

115     public File JavaDoc allocateFile(final String JavaDoc baseName) throws FileSystemException
116     {
117         // Create a unique-ish file name
118
final String JavaDoc basename = createFilename(baseName);
119         synchronized(this)
120         {
121             filecount++;
122         }
123         final File JavaDoc file = createFile(tempDir, basename);
124
125         // Keep track to delete later
126
copies.add(file);
127
128         return file;
129     }
130
131     protected long getFilecount()
132     {
133         return filecount;
134     }
135
136     /**
137      * create the temporary file name
138      */

139     protected String JavaDoc createFilename(final String JavaDoc baseName)
140     {
141         // BUG29007
142
// return baseName + "_" + getFilecount() + ".tmp";
143

144         // imario@apache.org: BUG34976 get rid of maybe reserved and dangerous characters
145
// e.g. to allow replication of http://hostname.org/fileservlet?file=abc.txt
146
// String safeBasename = UriParser.encode(baseName, TMP_RESERVED_CHARS).replace('%', '_');
147
String JavaDoc safeBasename = UriParser.encode(baseName).replace('%', '_');
148         return "tmp_" + getFilecount() + "_" + safeBasename;
149     }
150
151     /**
152      * create the temporary file
153      */

154     protected File JavaDoc createFile(final File JavaDoc parent, final String JavaDoc name) throws FileSystemException
155     {
156         return new File JavaDoc(parent, UriParser.decode(name));
157     }
158
159     /**
160      * Creates a local copy of the file, and all its descendents.
161      */

162     public File JavaDoc replicateFile(final FileObject srcFile,
163                               final FileSelector selector)
164         throws FileSystemException
165     {
166         final String JavaDoc basename = srcFile.getName().getBaseName();
167         final File JavaDoc file = allocateFile(basename);
168
169         // Copy from the source file
170
final FileObject destFile = getContext().toFileObject(file);
171         destFile.copyFrom(srcFile, selector);
172
173         return file;
174     }
175 }
176
Popular Tags