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 is layered on top of another, such as the
27: * contents of a zip or tar file.
28: *
29: * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
30: * @version $Revision: 480428 $ $Date: 2006-11-28 22:15:24 -0800 (Tue, 28 Nov 2006) $
31: */
32: public abstract class AbstractLayeredFileProvider extends
33: AbstractFileProvider implements FileProvider {
34: public AbstractLayeredFileProvider() {
35: super ();
36: setFileNameParser(LayeredFileNameParser.getInstance());
37: }
38:
39: /**
40: * Locates a file object, by absolute URI.
41: */
42: public FileObject findFile(final FileObject baseFile,
43: final String uri, final FileSystemOptions properties)
44: throws FileSystemException {
45: // Split the URI up into its parts
46: final LayeredFileName name = (LayeredFileName) parseUri(
47: baseFile != null ? baseFile.getName() : null, uri);
48:
49: // Make the URI canonical
50:
51: // Resolve the outer file name
52: final FileName fileName = name.getOuterName();
53: final FileObject file = getContext().resolveFile(baseFile,
54: fileName.getURI(), properties);
55:
56: // Create the file system
57: final FileObject rootFile = createFileSystem(name.getScheme(),
58: file, properties);
59:
60: // Resolve the file
61: return rootFile.resolveFile(name.getPath());
62: }
63:
64: /**
65: * Creates a layered file system.
66: */
67: public synchronized FileObject createFileSystem(
68: final String scheme, final FileObject file,
69: final FileSystemOptions fileSystemOptions)
70: throws FileSystemException {
71: // Check if cached
72: final FileName rootName = file.getName();
73: FileSystem fs = findFileSystem(rootName, null);
74: if (fs == null) {
75: // Create the file system
76: fs = doCreateFileSystem(scheme, file, fileSystemOptions);
77: addFileSystem(rootName, fs);
78: }
79: return fs.getRoot();
80: }
81:
82: /**
83: * Creates a layered file system. This method is called if the file system
84: * is not cached. The file system may implement {@link VfsComponent}.
85: *
86: * @param scheme The URI scheme.
87: * @param file The file to create the file system on top of.
88: * @return The file system.
89: */
90: protected abstract FileSystem doCreateFileSystem(
91: final String scheme, final FileObject file,
92: final FileSystemOptions fileSystemOptions)
93: throws FileSystemException;
94:
95: }
|