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.FileName;
26  import org.apache.commons.vfs2.FileObject;
27  import org.apache.commons.vfs2.FileSystem;
28  import org.apache.commons.vfs2.FileSystemConfigBuilder;
29  import org.apache.commons.vfs2.FileSystemException;
30  import org.apache.commons.vfs2.FileSystemOptions;
31  import org.apache.commons.vfs2.provider.AbstractFileProvider;
32  
33  /**
34   * The Resource provider.
35   */
36  public class ResourceFileProvider extends AbstractFileProvider {
37  
38      /** The provider's capabilities */
39      protected static final Collection<Capability> capabilities = Collections
40              .unmodifiableCollection(Arrays.asList(Capability.DISPATCHER));
41  
42      /**
43       * Constructs a new instance.
44       */
45      public ResourceFileProvider() {
46          setFileNameParser(ResourceFileNameParser.getInstance());
47      }
48  
49      @Override
50      public void closeFileSystem(final FileSystem filesystem) {
51          // no filesystem created here - so nothing to do
52      }
53  
54      /**
55       * Locates a file object, by absolute URI.
56       *
57       * @param baseFile The base file.
58       * @param uri The URI of the file to locate.
59       * @param fileSystemOptions The FileSystem options.
60       * @return the FileObject.
61       * @throws FileSystemException if an error occurs.
62       */
63      @Override
64      public FileObject findFile(final FileObject baseFile, final String uri, final FileSystemOptions fileSystemOptions)
65          throws FileSystemException {
66          final FileName fileName;
67          if (baseFile != null) {
68              fileName = parseUri(baseFile.getName(), uri);
69          } else {
70              fileName = parseUri(null, uri);
71          }
72          final String resourceName = fileName.getPath();
73  
74          ClassLoader classLoader = ResourceFileSystemConfigBuilder.getInstance().getClassLoader(fileSystemOptions);
75          if (classLoader == null) {
76              classLoader = getClass().getClassLoader();
77          }
78          FileSystemException.requireNonNull(classLoader, "vfs.provider.url/badly-formed-uri.error", uri);
79          final URL url = classLoader.getResource(resourceName);
80  
81          FileSystemException.requireNonNull(url, "vfs.provider.url/badly-formed-uri.error", uri);
82  
83          return getContext().getFileSystemManager().resolveFile(url.toExternalForm());
84      }
85  
86      @Override
87      public Collection<Capability> getCapabilities() {
88          return capabilities;
89      }
90  
91      @Override
92      public FileSystemConfigBuilder getConfigBuilder() {
93          return ResourceFileSystemConfigBuilder.getInstance();
94      }
95  }