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