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.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      private final static FileName tmpFileName = new AbstractFileName("tmp", "/")
    48      {
    49          protected FileName createName(String absPath)
    50          {
    51              return null;
    52          }
    53
    54          protected void appendRootUri(StringBuffer buffer)
    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          // Parse the name
    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         // Create the temp file system if it does not exist
    104         // FileSystem filesystem = findFileSystem( this, (Properties) null);
    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             // final FileName rootName =
    115             //    new LocalFileName(scheme, scheme + ":", FileName.ROOT_PATH);
    116             filesystem = new LocalFileSystem(rootName, rootFile.getAbsolutePath(), properties);
    117             addFileSystem(this, filesystem);
    118         }
    119
    120         // Find the file
    121         return filesystem.resolveFile(path);
    122     }
    123
    124     public Collection getCapabilities()
    125     {
    126         return DefaultLocalFileProvider.capabilities;
    127     }
    128 }