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 handles physical files, such as the files in a
27   * local fs, or on an FTP server.  An originating file system cannot be
28   * layered on top of another file system.
29   *
30   * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
31   * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
32   */
33  public abstract class AbstractOriginatingFileProvider
34      extends AbstractFileProvider
35  {
36      public AbstractOriginatingFileProvider()
37      {
38          super();
39      }
40  
41      /***
42       * Locates a file object, by absolute URI.
43       *
44       * @param uri
45       */
46      public FileObject findFile(final FileObject baseFile,
47                                 final String uri,
48                                 final FileSystemOptions fileSystemOptions) throws FileSystemException
49      {
50          // Parse the URI
51          final FileName name;
52          try
53          {
54              name = parseUri(baseFile!=null?baseFile.getName():null, uri);
55          }
56          catch (FileSystemException exc)
57          {
58              throw new FileSystemException("vfs.provider/invalid-absolute-uri.error", uri, exc);
59          }
60  
61          // Locate the file
62          return findFile(name, fileSystemOptions);
63      }
64  
65      /***
66       * Locates a file from its parsed URI.
67       */
68      protected FileObject findFile(final FileName name, final FileSystemOptions fileSystemOptions)
69          throws FileSystemException
70      {
71  		// Check in the cache for the file system
72  		final FileName rootName = getContext().getFileSystemManager().resolveName(name, FileName.ROOT_PATH);
73  		
74  		FileSystem fs;
75  		synchronized (this)
76  		{
77  			fs = findFileSystem(rootName, fileSystemOptions);
78  			if (fs == null)
79  			{
80  				// Need to create the file system, and cache it
81  				fs = doCreateFileSystem(rootName, fileSystemOptions);
82  				addFileSystem(rootName, fs);
83  			}
84  		}
85  
86  		// Locate the file
87          // return fs.resolveFile(name.getPath());
88          return fs.resolveFile(name);
89      }
90  
91      /***
92       * Creates a {@link FileSystem}.  If the returned FileSystem implements
93       * {@link VfsComponent}, it will be initialised.
94       *
95       * @param rootName The name of the root file of the file system to create.
96       */
97      protected abstract FileSystem doCreateFileSystem(final FileName rootName, final FileSystemOptions fileSystemOptions)
98          throws FileSystemException;
99  }