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