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;
18  
19  import org.apache.commons.vfs2.FileName;
20  import org.apache.commons.vfs2.FileObject;
21  import org.apache.commons.vfs2.FileSystem;
22  import org.apache.commons.vfs2.FileSystemException;
23  import org.apache.commons.vfs2.FileSystemOptions;
24  
25  /**
26   * A {@link FileProvider} that handles physical files, such as the files in a local fs, or on an FTP server. An
27   * originating file system cannot be layered on top of another file system.
28   */
29  public abstract class AbstractOriginatingFileProvider extends AbstractFileProvider {
30  
31      public AbstractOriginatingFileProvider() {
32      }
33  
34      /**
35       * Locates a file object, by absolute URI.
36       *
37       * @param baseFileObject The base file object.
38       * @param uri The URI of the file to locate
39       * @param fileSystemOptions The FileSystem options.
40       * @return The located FileObject
41       * @throws FileSystemException if an error occurs.
42       */
43      @Override
44      public FileObjectect.html#FileObject">FileObject findFile(final FileObject baseFileObject, final String uri, final FileSystemOptions fileSystemOptions)
45              throws FileSystemException {
46          // Parse the URI
47          final FileName name;
48          try {
49              name = parseUri(baseFileObject != null ? baseFileObject.getName() : null, uri);
50          } catch (final FileSystemException exc) {
51              throw new FileSystemException("vfs.provider/invalid-absolute-uri.error", uri, exc);
52          }
53  
54          // Locate the file
55          return findFile(name, fileSystemOptions);
56      }
57  
58      /**
59       * Locates a file from its parsed URI.
60       *
61       * @param fileName The file name.
62       * @param fileSystemOptions FileSystem options.
63       * @return A FileObject associated with the file.
64       * @throws FileSystemException if an error occurs.
65       */
66      protected FileObject findFile(final FileName fileName, final FileSystemOptions fileSystemOptions)
67              throws FileSystemException {
68          // Check in the cache for the file system
69          final FileName rootName = getContext().getFileSystemManager().resolveName(fileName, FileName.ROOT_PATH);
70  
71          final FileSystem fs = getFileSystem(rootName, fileSystemOptions);
72  
73          // Locate the file
74          // return fs.resolveFile(name.getPath());
75          return fs.resolveFile(fileName);
76      }
77  
78      /**
79       * Returns the FileSystem associated with the specified root.
80       *
81       * @param rootFileName The root path.
82       * @param fileSystemOptions The FileSystem options.
83       * @return The FileSystem.
84       * @throws FileSystemException if an error occurs.
85       * @since 2.0
86       */
87      protected synchronized FileSystem getFileSystem(final FileName rootFileName, final FileSystemOptions fileSystemOptions)
88              throws FileSystemException {
89          FileSystem fs = findFileSystem(rootFileName, fileSystemOptions);
90          if (fs == null) {
91              // Need to create the file system, and cache it
92              fs = doCreateFileSystem(rootFileName, fileSystemOptions);
93              addFileSystem(rootFileName, fs);
94          }
95          return fs;
96      }
97  
98      /**
99       * Creates a {@link FileSystem}. If the returned FileSystem implements {@link VfsComponent}, it will be initialized.
100      *
101      * @param rootFileName The name of the root file of the file system to create.
102      * @param fileSystemOptions The FileSystem options.
103      * @return The FileSystem.
104      * @throws FileSystemException if an error occurs.
105      */
106     protected abstract FileSystem doCreateFileSystem(final FileName rootFileName, final FileSystemOptions fileSystemOptions)
107             throws FileSystemException;
108 }