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.vfs2.provider.res;
18  
19  import java.net.URL;
20  import java.util.Arrays;
21  import java.util.Collection;
22  import java.util.Collections;
23  
24  import org.apache.commons.vfs2.Capability;
25  import org.apache.commons.vfs2.FileObject;
26  import org.apache.commons.vfs2.FileSystem;
27  import org.apache.commons.vfs2.FileSystemConfigBuilder;
28  import org.apache.commons.vfs2.FileSystemException;
29  import org.apache.commons.vfs2.FileSystemOptions;
30  import org.apache.commons.vfs2.VFS;
31  import org.apache.commons.vfs2.provider.AbstractFileProvider;
32  import org.apache.commons.vfs2.provider.UriParser;
33  
34  /**
35   * The Resource provider.
36   */
37  public class ResourceFileProvider extends AbstractFileProvider {
38  
39      /** The provider's capabilities */
40      protected static final Collection<Capability> capabilities = Collections
41              .unmodifiableCollection(Arrays.asList(new Capability[] { Capability.DISPATCHER }));
42  
43      private static final int BUFFER_SIZE = 80;
44  
45      public ResourceFileProvider() {
46          super();
47      }
48  
49      /**
50       * Locates a file object, by absolute URI.
51       *
52       * @param baseFile The base file.
53       * @param uri The URI of the file to locate.
54       * @param fileSystemOptions The FileSystem options.
55       * @return the FileObject.
56       * @throws FileSystemException if an error occurs.
57       */
58      @Override
59      public FileObjectObject.html#FileObject">FileObject findFile(final FileObject baseFile, final String uri, final FileSystemOptions fileSystemOptions)
60              throws FileSystemException {
61          final StringBuilder buf = new StringBuilder(BUFFER_SIZE);
62          UriParser.extractScheme(VFS.getManager().getSchemes(), uri, buf);
63          final String resourceName = buf.toString();
64  
65          ClassLoader classLoader = ResourceFileSystemConfigBuilder.getInstance().getClassLoader(fileSystemOptions);
66          if (classLoader == null) {
67              classLoader = getClass().getClassLoader();
68          }
69          FileSystemException.requireNonNull(classLoader, "vfs.provider.url/badly-formed-uri.error", uri);
70          final URL url = classLoader.getResource(resourceName);
71  
72          FileSystemException.requireNonNull(url, "vfs.provider.url/badly-formed-uri.error", uri);
73  
74          return getContext().getFileSystemManager().resolveFile(url.toExternalForm());
75      }
76  
77      @Override
78      public FileSystemConfigBuilder getConfigBuilder() {
79          return org.apache.commons.vfs2.provider.res.ResourceFileSystemConfigBuilder.getInstance();
80      }
81  
82      @Override
83      public void closeFileSystem(final FileSystem filesystem) {
84          // no filesystem created here - so nothing to do
85      }
86  
87      @Override
88      public Collection<Capability> getCapabilities() {
89          return capabilities;
90      }
91  }