1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs.provider.temp;
18
19 import org.apache.commons.vfs.FileName;
20 import org.apache.commons.vfs.FileObject;
21 import org.apache.commons.vfs.FileSystem;
22 import org.apache.commons.vfs.FileSystemException;
23 import org.apache.commons.vfs.FileSystemOptions;
24 import org.apache.commons.vfs.FileType;
25 import org.apache.commons.vfs.provider.AbstractFileProvider;
26 import org.apache.commons.vfs.provider.FileProvider;
27 import org.apache.commons.vfs.provider.UriParser;
28 import org.apache.commons.vfs.provider.local.DefaultLocalFileProvider;
29 import org.apache.commons.vfs.provider.local.LocalFileSystem;
30
31 import java.io.File;
32 import java.util.Collection;
33
34 /***
35 * A provider for temporary files.
36 *
37 * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
38 * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
39 */
40 public class TemporaryFileProvider
41 extends AbstractFileProvider
42 implements FileProvider, Comparable
43 {
44 private File rootFile;
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 public TemporaryFileProvider(final File rootFile)
61 {
62 this();
63
64 this.rootFile = rootFile;
65 }
66
67 public TemporaryFileProvider()
68 {
69 super();
70 }
71
72 public int compareTo(Object o)
73 {
74 int h1 = hashCode();
75 int h2 = o.hashCode();
76 if (h1 < h2)
77 {
78 return -1;
79 }
80 if (h1 > h2)
81 {
82 return 1;
83 }
84
85 return 0;
86 }
87
88 /***
89 * Locates a file object, by absolute URI.
90 */
91 public synchronized FileObject findFile(final FileObject baseFile, final String uri, final FileSystemOptions properties)
92 throws FileSystemException
93 {
94
95 final StringBuffer buffer = new StringBuffer(uri);
96 final String scheme = UriParser.extractScheme(uri, buffer);
97
98 UriParser.fixSeparators(buffer);
99
100 FileType fileType = UriParser.normalisePath(buffer);
101 final String path = buffer.toString();
102
103
104
105 FileSystem filesystem = findFileSystem(this, properties);
106 if (filesystem == null)
107 {
108 if (rootFile == null)
109 {
110 rootFile = getContext().getTemporaryFileStore().allocateFile("tempfs");
111 }
112 final FileName rootName =
113 getContext().parseURI(scheme + ":" + FileName.ROOT_PATH);
114
115
116 filesystem = new LocalFileSystem(rootName, rootFile.getAbsolutePath(), properties);
117 addFileSystem(this, filesystem);
118 }
119
120
121 return filesystem.resolveFile(path);
122 }
123
124 public Collection getCapabilities()
125 {
126 return DefaultLocalFileProvider.capabilities;
127 }
128 }