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      private static final int BUFFER_SIZE = 80;
43  
44      public ResourceFileProvider() {
45          setFileNameParser(ResourceFileNameParser.getInstance());
46      }
47  
48      /**
49       * Locates a file object, by absolute URI.
50       *
51       * @param baseFile The base file.
52       * @param uri The URI of the file to locate.
53       * @param fileSystemOptions The FileSystem options.
54       * @return the FileObject.
55       * @throws FileSystemException if an error occurs.
56       */
57      @Override
58      public FileObjectObject.html#FileObject">FileObject findFile(final FileObject baseFile, final String uri, final FileSystemOptions fileSystemOptions)
59              throws FileSystemException {
60          final FileName fileName;
61          if (baseFile != null) {
62              fileName = parseUri(baseFile.getName(), uri);
63          }
64          else {
65              fileName = parseUri(null, uri);
66          }
67          final String resourceName = fileName.getPath();
68  
69          ClassLoader classLoader = ResourceFileSystemConfigBuilder.getInstance().getClassLoader(fileSystemOptions);
70          if (classLoader == null) {
71              classLoader = getClass().getClassLoader();
72          }
73          FileSystemException.requireNonNull(classLoader, "vfs.provider.url/badly-formed-uri.error", uri);
74          final URL url = classLoader.getResource(resourceName);
75  
76          FileSystemException.requireNonNull(url, "vfs.provider.url/badly-formed-uri.error", uri);
77  
78          return getContext().getFileSystemManager().resolveFile(url.toExternalForm());
79      }
80  
81      @Override
82      public FileSystemConfigBuilder getConfigBuilder() {
83          return org.apache.commons.vfs2.provider.res.ResourceFileSystemConfigBuilder.getInstance();
84      }
85  
86      @Override
87      public void closeFileSystem(final FileSystem filesystem) {
88          // no filesystem created here - so nothing to do
89      }
90  
91      @Override
92      public Collection<Capability> getCapabilities() {
93          return capabilities;
94      }
95  }