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