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.impl;
18  
19  import java.io.IOException;
20  import java.net.URL;
21  import java.util.jar.Attributes;
22  
23  import org.apache.commons.vfs2.FileObject;
24  import org.apache.commons.vfs2.FileSystemException;
25  import org.apache.commons.vfs2.FileUtil;
26  
27  /**
28   * Helper class for VFSClassLoader. This represents a resource loaded with the classloader.
29   *
30   * @see VFSClassLoader
31   */
32  class Resource {
33  
34      private final FileObject root;
35      private final FileObject resource;
36      private final FileObject packageFolder;
37      private final String packageName;
38  
39      /**
40       * Creates a new instance.
41       *
42       * @param root The code source FileObject.
43       * @param resource The resource of the FileObject.
44       */
45      public Resource(final String name, final FileObjectObject.html#FileObject">FileObject root, final FileObject resource) throws FileSystemException {
46          this.root = root;
47          this.resource = resource;
48          packageFolder = resource.getParent();
49          final int pos = name.lastIndexOf('/');
50          if (pos == -1) {
51              packageName = null;
52          } else {
53              packageName = name.substring(0, pos).replace('/', '.');
54          }
55      }
56  
57      /**
58       * Returns the URL of the resource.
59       */
60      public URL getURL() throws FileSystemException {
61          return resource.getURL();
62      }
63  
64      /**
65       * Returns the name of the package containing the resource.
66       */
67      public String getPackageName() {
68          return packageName;
69      }
70  
71      /**
72       * Returns an attribute of the package containing the resource.
73       */
74      public String getPackageAttribute(final Attributes.Name attrName) throws FileSystemException {
75          return (String) packageFolder.getContent().getAttribute(attrName.toString());
76      }
77  
78      /**
79       * Returns the folder for the package containing the resource.
80       */
81      public FileObject getPackageFolder() {
82          return packageFolder;
83      }
84  
85      /**
86       * Returns the FileObject of the resource.
87       */
88      public FileObject getFileObject() {
89          return resource;
90      }
91  
92      /**
93       * Returns the code source as an URL.
94       */
95      public URL getCodeSourceURL() throws FileSystemException {
96          return root.getURL();
97      }
98  
99      /**
100      * Returns the data for this resource as a byte array.
101      */
102     public byte[] getBytes() throws IOException {
103         return FileUtil.getContent(resource);
104     }
105 }