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;
    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.FileSystemConfigBuilder;
    23  import org.apache.commons.vfs.FileSystemException;
    24  import org.apache.commons.vfs.FileSystemOptions;
    25  import org.apache.commons.vfs.provider.local.GenericFileNameParser;
    26
    27  import java.util.Map;
    28  import java.util.TreeMap;
    29
    30  /***
    31   * A partial {@link FileProvider} implementation.  Takes care of managing the
    32   * file systems created by the provider.
    33   *
    34   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
    35   * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
    36   */
    37  public abstract class AbstractFileProvider
    38      extends AbstractVfsContainer
    39      implements FileProvider
    40  {
    41      private FileNameParser parser;
    42
    43      public AbstractFileProvider()
    44      {
    45          parser = GenericFileNameParser.getInstance();
    46      }
    47
    48      protected FileNameParser getFileNameParser()
    49      {
    50          return parser;
    51      }
    52
    53      protected void setFileNameParser(FileNameParser parser)
    54      {
    55          this.parser = parser;
    56      }
    57
    58      /***
    59       * The cached file systems.  This is a mapping from root URI to
    60       * FileSystem object.
    61       */
    62      // private final Map fileSystems = new HashMap();
    63      private final Map fileSystems = new TreeMap();
    64
    65      /***
    66       * Closes the file systems created by this provider.
    67       */
    68      public void close()
    69      {
    70          synchronized (this)
    71          {
    72              fileSystems.clear();
    73          }
    74
    75          super.close();
    76      }
    77
    78      /***
    79       * Creates a layered file system.  This method throws a 'not supported' exception.
    80       */
    81      public FileObject createFileSystem(final String scheme, final FileObject file, final FileSystemOptions properties)
    82          throws FileSystemException
    83      {
    84          // Can't create a layered file system
    85          throw new FileSystemException("vfs.provider/not-layered-fs.error", scheme);
    86      }
    87
    88      /***
    89       * Adds a file system to those cached by this provider.  The file system
    90       * may implement {@link VfsComponent}, in which case it is initialised.
    91       */
    92      protected void addFileSystem(final Comparable key, final FileSystem fs)
    93          throws FileSystemException
    94      {
    95          // Add to the cache
    96          addComponent(fs);
    97
    98          FileSystemKey treeKey = new FileSystemKey(key, fs.getFileSystemOptions());
    99          ((AbstractFileSystem) fs).setCacheKey(treeKey);
    100
    101         synchronized (this)
    102         {
    103             fileSystems.put(treeKey, fs);
    104         }
    105     }
    106
    107     /***
    108      * Locates a cached file system
    109      *
    110      * @return The provider, or null if it is not cached.
    111      */
    112     protected FileSystem findFileSystem(final Comparable key, final FileSystemOptions fileSystemProps)
    113     {
    114         FileSystemKey treeKey = new FileSystemKey(key, fileSystemProps);
    115
    116         synchronized (this)
    117         {
    118             return (FileSystem) fileSystems.get(treeKey);
    119         }
    120     }
    121
    122     public FileSystemConfigBuilder getConfigBuilder()
    123     {
    124         return null;
    125     }
    126
    127     public void freeUnusedResources()
    128     {
    129         Object[] item;
    130         synchronized (this)
    131         {
    132             item = fileSystems.values().toArray();
    133         }
    134         for (int i = 0; i < item.length; ++i)
    135         {
    136             AbstractFileSystem fs = (AbstractFileSystem) item[i];
    137             if (fs.isReleaseable())
    138             {
    139                 fs.closeCommunicationLink();
    140             }
    141         }
    142     }
    143
    144     public void closeFileSystem(final FileSystem filesystem)
    145     {
    146         AbstractFileSystem fs = (AbstractFileSystem) filesystem;
    147
    148         synchronized (this)
    149         {
    150             if (fs.getCacheKey() != null)
    151             {
    152                 fileSystems.remove(fs.getCacheKey());
    153             }
    154         }
    155
    156         removeComponent(fs);
    157         fs.close();
    158     }
    159
    160     /***
    161      * Parses an absolute URI.
    162      *
    163      * @param base The base file - if null the <code>uri</code> needs to be absolute
    164      * @param uri The URI to parse.
    165      */
    166     public FileName parseUri(FileName base, String uri) throws FileSystemException
    167     {
    168         if (getFileNameParser() != null)
    169         {
    170             return getFileNameParser().parseUri(getContext(), base, uri);
    171         }
    172
    173         throw new FileSystemException("vfs.provider/filename-parser-missing.error");
    174         // return GenericFileName.parseUri(getFileNameParser(), uri, 0);
    175     }
    176 }