View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.vfs2.provider.temp;
18  
19  import java.io.File;
20  import java.util.Collection;
21  
22  import org.apache.commons.vfs2.Capability;
23  import org.apache.commons.vfs2.FileName;
24  import org.apache.commons.vfs2.FileObject;
25  import org.apache.commons.vfs2.FileSystem;
26  import org.apache.commons.vfs2.FileSystemException;
27  import org.apache.commons.vfs2.FileSystemOptions;
28  import org.apache.commons.vfs2.provider.AbstractFileProvider;
29  import org.apache.commons.vfs2.provider.UriParser;
30  import org.apache.commons.vfs2.provider.local.DefaultLocalFileProvider;
31  import org.apache.commons.vfs2.provider.local.LocalFileSystem;
32  
33  /**
34   * A provider for temporary files.
35   */
36  public class TemporaryFileProvider extends AbstractFileProvider implements Comparable<Object> {
37  
38      private File rootFile;
39  
40      /*
41       * private final static FileName tmpFileName = new AbstractFileName("tmp", "/") { protected FileName
42       * createName(String absPath) { return null; }
43       *
44       * protected void appendRootUri(StringBuffer buffer) { } };
45       */
46  
47      public TemporaryFileProvider(final File rootFile) {
48          this();
49  
50          this.rootFile = rootFile;
51      }
52  
53      public TemporaryFileProvider() {
54      }
55  
56      @Override
57      public int compareTo(final Object o) {
58          final int h1 = hashCode();
59          final int h2 = o.hashCode();
60          return Integer.compare(h1, h2);
61  
62      }
63  
64      /**
65       * Locates a file object, by absolute URI.
66       *
67       * @param baseFile The base FileObject.
68       * @param uri The URI of the file to be located.
69       * @param fileSystemOptions FileSystemOptions to use to locate or create the file.
70       * @return The FileObject.
71       * @throws FileSystemException if an error occurs.
72       */
73      @Override
74      public synchronized FileObjectObject.html#FileObject">FileObject findFile(final FileObject baseFile, final String uri,
75              final FileSystemOptions fileSystemOptions) throws FileSystemException {
76          // Parse the name
77          final StringBuilder buffer = new StringBuilder(uri);
78          final String scheme = UriParser.extractScheme(getContext().getFileSystemManager().getSchemes(), uri, buffer);
79          UriParser.fixSeparators(buffer);
80          UriParser.normalisePath(buffer);
81          final String path = buffer.toString();
82  
83          // Create the temp file system if it does not exist
84          // FileSystem filesystem = findFileSystem( this, (Properties) null);
85          FileSystem filesystem = findFileSystem(this, fileSystemOptions);
86          if (filesystem == null) {
87              if (rootFile == null) {
88                  rootFile = getContext().getTemporaryFileStore().allocateFile("tempfs");
89              }
90              final FileName rootName = getContext().parseURI(scheme + ":" + FileName.ROOT_PATH);
91              // final FileName rootName =
92              // new LocalFileName(scheme, scheme + ":", FileName.ROOT_PATH);
93              filesystem = new LocalFileSystem(rootName, rootFile.getAbsolutePath(), fileSystemOptions);
94              addFileSystem(this, filesystem);
95          }
96  
97          // Find the file
98          return filesystem.resolveFile(path);
99      }
100 
101     @Override
102     public Collection<Capability> getCapabilities() {
103         return DefaultLocalFileProvider.capabilities;
104     }
105 }