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          super();
33      }
34  
35      /**
36       * Locates a file object, by absolute URI.
37       *
38       * @param baseFile The base file object.
39       * @param uri The URI of the file to locate
40       * @param fileSystemOptions The FileSystem options.
41       * @return The located FileObject
42       * @throws FileSystemException if an error occurs.
43       */
44      @Override
45      public FileObjectect.html#FileObject">FileObject findFile(final FileObject baseFile, final String uri, final FileSystemOptions fileSystemOptions)
46              throws FileSystemException {
47          // Parse the URI
48          final FileName name;
49          try {
50              name = parseUri(baseFile != null ? baseFile.getName() : null, uri);
51          } catch (final FileSystemException exc) {
52              throw new FileSystemException("vfs.provider/invalid-absolute-uri.error", uri, exc);
53          }
54  
55          // Locate the file
56          return findFile(name, fileSystemOptions);
57      }
58  
59      /**
60       * Locates a file from its parsed URI.
61       *
62       * @param name The file name.
63       * @param fileSystemOptions FileSystem options.
64       * @return A FileObject associated with the file.
65       * @throws FileSystemException if an error occurs.
66       */
67      protected FileObject findFile(final FileName name, final FileSystemOptions fileSystemOptions)
68              throws FileSystemException {
69          // Check in the cache for the file system
70          final FileName rootName = getContext().getFileSystemManager().resolveName(name, FileName.ROOT_PATH);
71  
72          final FileSystem fs = getFileSystem(rootName, fileSystemOptions);
73  
74          // Locate the file
75          // return fs.resolveFile(name.getPath());
76          return fs.resolveFile(name);
77      }
78  
79      /**
80       * Returns the FileSystem associated with the specified root.
81       *
82       * @param rootName The root path.
83       * @param fileSystemOptions The FileSystem options.
84       * @return The FileSystem.
85       * @throws FileSystemException if an error occurs.
86       * @since 2.0
87       */
88      protected synchronized FileSystem getFileSystem(final FileName rootName, final FileSystemOptions fileSystemOptions)
89              throws FileSystemException {
90          FileSystem fs = findFileSystem(rootName, fileSystemOptions);
91          if (fs == null) {
92              // Need to create the file system, and cache it
93              fs = doCreateFileSystem(rootName, fileSystemOptions);
94              addFileSystem(rootName, fs);
95          }
96          return fs;
97      }
98  
99      /**
100      * Creates a {@link FileSystem}. If the returned FileSystem implements {@link VfsComponent}, it will be initialised.
101      *
102      * @param rootName The name of the root file of the file system to create.
103      * @param fileSystemOptions The FileSystem options.
104      * @return The FileSystem.
105      * @throws FileSystemException if an error occurs.
106      */
107     protected abstract FileSystem doCreateFileSystem(final FileName rootName, final FileSystemOptions fileSystemOptions)
108             throws FileSystemException;
109 }