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.FileSystemException;
23  import org.apache.commons.vfs.FileSystemOptions;
24  
25  /***
26   * A {@link FileProvider} that is layered on top of another, such as the
27   * contents of a zip or tar file.
28   *
29   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
30   * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
31   */
32  public abstract class AbstractLayeredFileProvider
33      extends AbstractFileProvider
34      implements FileProvider
35  {
36      public AbstractLayeredFileProvider()
37      {
38          super();
39          setFileNameParser(LayeredFileNameParser.getInstance());
40      }
41  
42      /***
43       * Locates a file object, by absolute URI.
44       */
45      public FileObject findFile(final FileObject baseFile,
46                                 final String uri,
47                                 final FileSystemOptions properties) throws FileSystemException
48      {
49          // Split the URI up into its parts
50          final LayeredFileName name = (LayeredFileName) parseUri(baseFile!=null?baseFile.getName():null, uri);
51  
52          // Make the URI canonical
53  
54          // Resolve the outer file name
55          final FileName fileName = name.getOuterName();
56          final FileObject file = getContext().resolveFile(baseFile, fileName.getURI(), properties);
57  
58          // Create the file system
59          final FileObject rootFile = createFileSystem(name.getScheme(), file, properties);
60  
61          // Resolve the file
62          return rootFile.resolveFile(name.getPath());
63      }
64  
65      /***
66       * Creates a layered file system.
67       */
68      public synchronized FileObject createFileSystem(final String scheme,
69                                                      final FileObject file,
70                                                      final FileSystemOptions fileSystemOptions)
71          throws FileSystemException
72      {
73          // Check if cached
74          final FileName rootName = file.getName();
75          FileSystem fs = findFileSystem(rootName, null);
76          if (fs == null)
77          {
78              // Create the file system
79              fs = doCreateFileSystem(scheme, file, fileSystemOptions);
80              addFileSystem(rootName, fs);
81          }
82          return fs.getRoot();
83      }
84  
85      /***
86       * Creates a layered file system.  This method is called if the file system
87       * is not cached.  The file system may implement {@link VfsComponent}.
88       *
89       * @param scheme The URI scheme.
90       * @param file   The file to create the file system on top of.
91       * @return The file system.
92       */
93      protected abstract FileSystem doCreateFileSystem(final String scheme,
94                                                       final FileObject file,
95                                                       final FileSystemOptions fileSystemOptions)
96          throws FileSystemException;
97  
98  }