KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > res > ResourceFileProvider


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.vfs.provider.res;
17
18 import org.apache.commons.vfs.Capability;
19 import org.apache.commons.vfs.FileObject;
20 import org.apache.commons.vfs.FileSystem;
21 import org.apache.commons.vfs.FileSystemConfigBuilder;
22 import org.apache.commons.vfs.FileSystemException;
23 import org.apache.commons.vfs.FileSystemOptions;
24 import org.apache.commons.vfs.provider.AbstractFileProvider;
25 import org.apache.commons.vfs.provider.UriParser;
26
27 import java.net.URL JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Collections JavaDoc;
31
32 /**
33  * Description
34  *
35  * @author <a HREF="mailto:imario@apache.org">Mario Ivankovits</a>
36  */

37 public class ResourceFileProvider extends AbstractFileProvider
38 {
39     protected final static Collection JavaDoc capabilities = Collections.unmodifiableCollection(Arrays.asList(new Capability[]
40     {
41         Capability.DISPATCHER
42     }));
43
44     public ResourceFileProvider()
45     {
46         super();
47     }
48
49     /**
50      * Locates a file object, by absolute URI.
51      */

52     public FileObject findFile(final FileObject baseFile,
53                                final String JavaDoc uri,
54                                final FileSystemOptions fileSystemOptions)
55         throws FileSystemException
56     {
57         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(80);
58         UriParser.extractScheme(uri, buf);
59         String JavaDoc resourceName = buf.toString();
60
61         ClassLoader JavaDoc cl = ResourceFileSystemConfigBuilder.getInstance().getClassLoader(fileSystemOptions);
62         if (cl == null)
63         {
64             cl = getClass().getClassLoader();
65         }
66         final URL JavaDoc url = cl.getResource(resourceName);
67
68         if (url == null)
69         {
70             throw new FileSystemException("vfs.provider.url/badly-formed-uri.error", uri);
71         }
72
73         FileObject fo = getContext().getFileSystemManager().resolveFile(url.toExternalForm());
74         return fo;
75     }
76
77     public FileSystemConfigBuilder getConfigBuilder()
78     {
79         return org.apache.commons.vfs.provider.res.ResourceFileSystemConfigBuilder.getInstance();
80     }
81
82     public void closeFileSystem(FileSystem filesystem)
83     {
84         // no filesystem created here - so nothing to do
85
}
86
87     public Collection JavaDoc getCapabilities()
88     {
89         return capabilities;
90     }
91 }
92
Popular Tags