01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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-28 22:15:24 -0800 (Tue, 28 Nov 2006) $
32: */
33: public abstract class AbstractOriginatingFileProvider extends
34: AbstractFileProvider {
35: public AbstractOriginatingFileProvider() {
36: super ();
37: }
38:
39: /**
40: * Locates a file object, by absolute URI.
41: *
42: * @param uri
43: */
44: public FileObject findFile(final FileObject baseFile,
45: 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()
51: : null, uri);
52: } catch (FileSystemException exc) {
53: throw new FileSystemException(
54: "vfs.provider/invalid-absolute-uri.error", uri, exc);
55: }
56:
57: // Locate the file
58: return findFile(name, fileSystemOptions);
59: }
60:
61: /**
62: * Locates a file from its parsed URI.
63: */
64: protected FileObject findFile(final FileName name,
65: final FileSystemOptions fileSystemOptions)
66: throws FileSystemException {
67: // Check in the cache for the file system
68: final FileName rootName = getContext().getFileSystemManager()
69: .resolveName(name, FileName.ROOT_PATH);
70:
71: FileSystem fs;
72: synchronized (this ) {
73: fs = findFileSystem(rootName, fileSystemOptions);
74: if (fs == null) {
75: // Need to create the file system, and cache it
76: fs = doCreateFileSystem(rootName, fileSystemOptions);
77: addFileSystem(rootName, fs);
78: }
79: }
80:
81: // Locate the file
82: // return fs.resolveFile(name.getPath());
83: return fs.resolveFile(name);
84: }
85:
86: /**
87: * Creates a {@link FileSystem}. If the returned FileSystem implements
88: * {@link VfsComponent}, it will be initialised.
89: *
90: * @param rootName The name of the root file of the file system to create.
91: */
92: protected abstract FileSystem doCreateFileSystem(
93: final FileName rootName,
94: final FileSystemOptions fileSystemOptions)
95: throws FileSystemException;
96: }
|