View Javadoc

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.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-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
38   */
39  public class ResourceFileProvider extends AbstractFileProvider
40  {
41      protected final static Collection capabilities = Collections.unmodifiableCollection(Arrays.asList(new Capability[]
42      {
43          Capability.DISPATCHER
44      }));
45  
46      public ResourceFileProvider()
47      {
48          super();
49      }
50  
51      /***
52       * Locates a file object, by absolute URI.
53       */
54      public FileObject findFile(final FileObject baseFile,
55                                 final String uri,
56                                 final FileSystemOptions fileSystemOptions)
57          throws FileSystemException
58      {
59          StringBuffer buf = new StringBuffer(80);
60          UriParser.extractScheme(uri, buf);
61          String resourceName = buf.toString();
62  
63          ClassLoader cl = ResourceFileSystemConfigBuilder.getInstance().getClassLoader(fileSystemOptions);
64          if (cl == null)
65          {
66              cl = getClass().getClassLoader();
67          }
68          final URL url = cl.getResource(resourceName);
69  
70          if (url == null)
71          {
72              throw new FileSystemException("vfs.provider.url/badly-formed-uri.error", uri);
73          }
74  
75          FileObject fo = getContext().getFileSystemManager().resolveFile(url.toExternalForm());
76          return fo;
77      }
78  
79      public FileSystemConfigBuilder getConfigBuilder()
80      {
81          return org.apache.commons.vfs.provider.res.ResourceFileSystemConfigBuilder.getInstance();
82      }
83  
84      public void closeFileSystem(FileSystem filesystem)
85      {
86          // no filesystem created here - so nothing to do
87      }
88  
89      public Collection getCapabilities()
90      {
91          return capabilities;
92      }
93  }