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.res;
18:
19: import org.apache.commons.vfs.Capability;
20: import org.apache.commons.vfs.FileObject;
21: import org.apache.commons.vfs.FileSystem;
22: import org.apache.commons.vfs.FileSystemConfigBuilder;
23: import org.apache.commons.vfs.FileSystemException;
24: import org.apache.commons.vfs.FileSystemOptions;
25: import org.apache.commons.vfs.provider.AbstractFileProvider;
26: import org.apache.commons.vfs.provider.UriParser;
27:
28: import java.net.URL;
29: import java.util.Arrays;
30: import java.util.Collection;
31: import java.util.Collections;
32:
33: /**
34: * Description
35: *
36: * @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
37: * @version $Revision: 480428 $ $Date: 2006-11-28 22:15:24 -0800 (Tue, 28 Nov 2006) $
38: */
39: public class ResourceFileProvider extends AbstractFileProvider {
40: protected final static Collection capabilities = Collections
41: .unmodifiableCollection(Arrays
42: .asList(new Capability[] { Capability.DISPATCHER }));
43:
44: public ResourceFileProvider() {
45: super ();
46: }
47:
48: /**
49: * Locates a file object, by absolute URI.
50: */
51: public FileObject findFile(final FileObject baseFile,
52: final String uri, final FileSystemOptions fileSystemOptions)
53: throws FileSystemException {
54: StringBuffer buf = new StringBuffer(80);
55: UriParser.extractScheme(uri, buf);
56: String resourceName = buf.toString();
57:
58: ClassLoader cl = ResourceFileSystemConfigBuilder.getInstance()
59: .getClassLoader(fileSystemOptions);
60: if (cl == null) {
61: cl = getClass().getClassLoader();
62: }
63: final URL url = cl.getResource(resourceName);
64:
65: if (url == null) {
66: throw new FileSystemException(
67: "vfs.provider.url/badly-formed-uri.error", uri);
68: }
69:
70: FileObject fo = getContext().getFileSystemManager()
71: .resolveFile(url.toExternalForm());
72: return fo;
73: }
74:
75: public FileSystemConfigBuilder getConfigBuilder() {
76: return org.apache.commons.vfs.provider.res.ResourceFileSystemConfigBuilder
77: .getInstance();
78: }
79:
80: public void closeFileSystem(FileSystem filesystem) {
81: // no filesystem created here - so nothing to do
82: }
83:
84: public Collection getCapabilities() {
85: return capabilities;
86: }
87: }
|