KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > temp > TemporaryFileProvider


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.provider.temp;
17
18 import org.apache.commons.vfs.FileName;
19 import org.apache.commons.vfs.FileObject;
20 import org.apache.commons.vfs.FileSystem;
21 import org.apache.commons.vfs.FileSystemException;
22 import org.apache.commons.vfs.FileSystemOptions;
23 import org.apache.commons.vfs.FileType;
24 import org.apache.commons.vfs.provider.AbstractFileProvider;
25 import org.apache.commons.vfs.provider.FileProvider;
26 import org.apache.commons.vfs.provider.UriParser;
27 import org.apache.commons.vfs.provider.local.DefaultLocalFileProvider;
28 import org.apache.commons.vfs.provider.local.LocalFileSystem;
29
30 import java.io.File JavaDoc;
31 import java.util.Collection JavaDoc;
32
33 /**
34  * A provider for temporary files.
35  *
36  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
37  */

38 public class TemporaryFileProvider
39     extends AbstractFileProvider
40     implements FileProvider, Comparable JavaDoc
41 {
42     private File JavaDoc rootFile;
43
44     /*
45     private final static FileName tmpFileName = new AbstractFileName("tmp", "/")
46     {
47         protected FileName createName(String absPath)
48         {
49             return null;
50         }
51
52         protected void appendRootUri(StringBuffer buffer)
53         {
54         }
55     };
56 */

57
58     public TemporaryFileProvider(final File JavaDoc rootFile)
59     {
60         this();
61
62         this.rootFile = rootFile;
63     }
64
65     public TemporaryFileProvider()
66     {
67         super();
68     }
69
70     public int compareTo(Object JavaDoc o)
71     {
72         int h1 = hashCode();
73         int h2 = o.hashCode();
74         if (h1 < h2)
75         {
76             return -1;
77         }
78         if (h1 > h2)
79         {
80             return 1;
81         }
82
83         return 0;
84     }
85
86     /**
87      * Locates a file object, by absolute URI.
88      */

89     public synchronized FileObject findFile(final FileObject baseFile, final String JavaDoc uri, final FileSystemOptions properties)
90         throws FileSystemException
91     {
92         // Parse the name
93
final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(uri);
94         final String JavaDoc scheme = UriParser.extractScheme(uri, buffer);
95
96         UriParser.fixSeparators(buffer);
97         
98         FileType fileType = UriParser.normalisePath(buffer);
99         final String JavaDoc path = buffer.toString();
100
101         // Create the temp file system if it does not exist
102
// FileSystem filesystem = findFileSystem( this, (Properties) null);
103
FileSystem filesystem = findFileSystem(this, properties);
104         if (filesystem == null)
105         {
106             if (rootFile == null)
107             {
108                 rootFile = getContext().getTemporaryFileStore().allocateFile("tempfs");
109             }
110             final FileName rootName =
111                 getContext().parseURI(scheme + ":" + FileName.ROOT_PATH);
112             // final FileName rootName =
113
// new LocalFileName(scheme, scheme + ":", FileName.ROOT_PATH);
114
filesystem = new LocalFileSystem(rootName, rootFile.getAbsolutePath(), properties);
115             addFileSystem(this, filesystem);
116         }
117
118         // Find the file
119
return filesystem.resolveFile(path);
120     }
121
122     public Collection JavaDoc getCapabilities()
123     {
124         return DefaultLocalFileProvider.capabilities;
125     }
126 }
127
Popular Tags