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      /**
48       * Constructs a new instance.
49       */
50      public TemporaryFileProvider() {
51      }
52  
53      /**
54       * Constructs a new instance.
55       *
56       * @param rootFile The root file.
57       */
58      public TemporaryFileProvider(final File rootFile) {
59          this();
60          this.rootFile = rootFile;
61      }
62  
63      @Override
64      public int compareTo(final Object o) {
65          final int h1 = hashCode();
66          final int h2 = o.hashCode();
67          return Integer.compare(h1, h2);
68      }
69  
70      /**
71       * Locates a file object, by absolute URI.
72       *
73       * @param baseFile The base FileObject.
74       * @param uri The URI of the file to be located.
75       * @param fileSystemOptions FileSystemOptions to use to locate or create the file.
76       * @return The FileObject.
77       * @throws FileSystemException if an error occurs.
78       */
79      @Override
80      public synchronized FileObject findFile(final FileObject baseFile, final String uri,
81              final FileSystemOptions fileSystemOptions) throws FileSystemException {
82          // Parse the name
83          final StringBuilder buffer = new StringBuilder(uri);
84          final String scheme = UriParser.extractScheme(getContext().getFileSystemManager().getSchemes(), uri, buffer);
85          UriParser.fixSeparators(buffer);
86          UriParser.normalisePath(buffer);
87          final String path = buffer.toString();
88  
89          // Create the temp file system if it does not exist
90          // FileSystem filesystem = findFileSystem( this, (Properties) null);
91          FileSystem filesystem = findFileSystem(this, fileSystemOptions);
92          if (filesystem == null) {
93              if (rootFile == null) {
94                  rootFile = getContext().getTemporaryFileStore().allocateFile("tempfs");
95              }
96              final FileName rootName = getContext().parseURI(scheme + ":" + FileName.ROOT_PATH);
97              // final FileName rootName =
98              // new LocalFileName(scheme, scheme + ":", FileName.ROOT_PATH);
99              filesystem = new LocalFileSystem(rootName, rootFile.getAbsolutePath(), fileSystemOptions);
100             addFileSystem(this, filesystem);
101         }
102 
103         // Find the file
104         return filesystem.resolveFile(path);
105     }
106 
107     @Override
108     public Collection<Capability> getCapabilities() {
109         return DefaultLocalFileProvider.capabilities;
110     }
111 }