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.provider.jar;
    18
    19  import org.apache.commons.vfs.FileName;
    20  import org.apache.commons.vfs.FileSystemException;
    21  import org.apache.commons.vfs.provider.zip.ZipFileObject;
    22
    23  import java.io.IOException;
    24  import java.security.cert.Certificate;
    25  import java.util.HashMap;
    26  import java.util.Iterator;
    27  import java.util.Map;
    28  import java.util.jar.Attributes;
    29  import java.util.jar.JarEntry;
    30  import java.util.jar.JarFile;
    31  import java.util.jar.Manifest;
    32  import java.util.zip.ZipEntry;
    33
    34  /***
    35   * A file in a Jar file system.
    36   *
    37   * @author <a href="mailto:brian@mmmanager.org">Brian Olsen</a>
    38   * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
    39   */
    40  public class JarFileObject extends ZipFileObject
    41  {
    42      private Attributes attributes;
    43
    44      final JarFileSystem fs;
    45
    46      protected JarFileObject(final FileName name,
    47                              final ZipEntry entry,
    48                              final JarFileSystem fs,
    49                              final boolean zipExists) throws FileSystemException
    50      {
    51          super(name, entry, fs, zipExists);
    52          this.fs = fs;
    53
    54          try
    55          {
    56              getAttributes(); // early get the attributes as the zip file might be closed
    57          }
    58          catch (IOException e)
    59          {
    60              throw new FileSystemException(e);
    61          }
    62      }
    63
    64      /***
    65       * Returns the Jar manifest.
    66       */
    67      Manifest getManifest() throws IOException
    68      {
    69          if (fs.getZipFile() == null)
    70          {
    71              return null;
    72          }
    73
    74          return ((JarFile) fs.getZipFile()).getManifest();
    75      }
    76
    77      /***
    78       * Returns the attributes of this file.
    79       */
    80      Attributes getAttributes() throws IOException
    81      {
    82          if (attributes == null)
    83          {
    84              if (entry == null)
    85              {
    86                  attributes = new Attributes(1);
    87              }
    88              else
    89              {
    90                  attributes = ((JarEntry) entry).getAttributes();
    91                  if (attributes == null)
    92                  {
    93                      attributes = new Attributes(1);
    94                  }
    95              }
    96          }
    97
    98          return attributes;
    99      }
    100
    101     /***
    102      * Returns the value of an attribute.
    103      */
    104     protected Map doGetAttributes()
    105         throws Exception
    106     {
    107         final Map attrs = new HashMap();
    108
    109         // Add the file system's attributes first
    110         final JarFileSystem fs = (JarFileSystem) getFileSystem();
    111         addAll(fs.getAttributes(), attrs);
    112
    113         // Add this file's attributes
    114         addAll(getAttributes(), attrs);
    115
    116         return attrs;
    117     }
    118
    119     /***
    120      * Adds the source attributes to the destination map.
    121      */
    122     private void addAll(final Attributes src, final Map dest)
    123     {
    124         for (Iterator iterator = src.entrySet().iterator(); iterator.hasNext();)
    125         {
    126             final Map.Entry entry = (Map.Entry) iterator.next();
    127             final String name = entry.getKey().toString().toLowerCase();
    128             dest.put(name, entry.getValue());
    129         }
    130     }
    131
    132     /***
    133      * Return the certificates of this JarEntry.
    134      */
    135     protected Certificate[] doGetCertificates()
    136     {
    137         if (entry == null)
    138         {
    139             return null;
    140         }
    141
    142         return ((JarEntry) entry).getCertificates();
    143     }
    144 }