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.File;
20  import java.io.IOException;
21  import java.util.Collection;
22  import java.util.jar.Attributes;
23  import java.util.jar.Attributes.Name;
24  import java.util.jar.JarFile;
25  import java.util.jar.Manifest;
26  import java.util.zip.ZipEntry;
27  import java.util.zip.ZipFile;
28  
29  import org.apache.commons.vfs2.Capability;
30  import org.apache.commons.vfs2.FileObject;
31  import org.apache.commons.vfs2.FileSystemException;
32  import org.apache.commons.vfs2.FileSystemOptions;
33  import org.apache.commons.vfs2.provider.AbstractFileName;
34  import org.apache.commons.vfs2.provider.zip.ZipFileObject;
35  import org.apache.commons.vfs2.provider.zip.ZipFileSystem;
36  
37  /**
38   * A read-only file system for Jar files.
39   */
40  public class JarFileSystem extends ZipFileSystem {
41      private Attributes attributes;
42  
43      /**
44       * Constructs a new instance.
45       *
46       * @param rootFileName The root file name of this file system.
47       * @param parentLayer The parent layer of this file system.
48       * @param fileSystemOptions Options to build this file system.
49       * @throws FileSystemException If the parent layer does not exist, or on error replicating the file.
50       */
51      protected JarFileSystem(final AbstractFileName rootFileName, final FileObject parentLayer, final FileSystemOptions fileSystemOptions)
52              throws FileSystemException {
53          super(rootFileName, parentLayer, fileSystemOptions);
54      }
55  
56      // @Override
57      // protected FileObject createFile(AbstractFileName name) throws FileSystemException
58      // {
59      // return new JarFileObject(name, null, this, false);
60      // }
61  
62      /**
63       * Returns the capabilities of this file system.
64       */
65      @Override
66      protected void addCapabilities(final Collection<Capability> caps) {
67          // super.addCapabilities(caps);
68          caps.addAll(JarFileProvider.CAPABILITIES);
69      }
70  
71      @Override
72      protected ZipFile createZipFile(final File file) throws FileSystemException {
73          try {
74              return new JarFile(file);
75          } catch (final IOException ioe) {
76              throw new FileSystemException("vfs.provider.jar/open-jar-file.error", file, ioe);
77          }
78      }
79  
80      @Override
81      protected ZipFileObject createZipFileObject(final AbstractFileName name, final ZipEntry entry)
82              throws FileSystemException {
83          return new JarFileObject(name, entry, this, true);
84      }
85  
86      Object getAttribute(final Name attrName) throws FileSystemException {
87          try {
88              final Attributes attr = getAttributes();
89              return attr.getValue(attrName);
90          } catch (final IOException ioe) {
91              throw new FileSystemException(attrName.toString(), ioe);
92          }
93      }
94  
95      /**
96       * Retrieves the attribute with the specified name. The default implementation simply throws an exception.
97       *
98       * @param attrName The attribute's name.
99       * @return The value of the attribute.
100      * @throws FileSystemException if an error occurs.
101      */
102     @Override
103     public Object getAttribute(final String attrName) throws FileSystemException {
104         final Name name = lookupName(attrName);
105         return getAttribute(name);
106     }
107 
108     Attributes getAttributes() throws IOException {
109         if (attributes == null) {
110             final Manifest man = ((JarFile) getZipFile()).getManifest();
111             if (man == null) {
112                 attributes = new Attributes(1);
113             } else {
114                 attributes = man.getMainAttributes();
115                 if (attributes == null) {
116                     attributes = new Attributes(1);
117                 }
118             }
119         }
120 
121         return attributes;
122     }
123 
124     @Override
125     protected ZipFile getZipFile() throws FileSystemException {
126         // make accessible
127         return super.getZipFile();
128     }
129 
130     Name lookupName(final String attrName) {
131         if (Name.CLASS_PATH.toString().equals(attrName)) {
132             return Name.CLASS_PATH;
133         }
134         if (Name.CONTENT_TYPE.toString().equals(attrName)) {
135             return Name.CONTENT_TYPE;
136         }
137         if (Name.EXTENSION_INSTALLATION.toString().equals(attrName)) {
138             return Name.EXTENSION_INSTALLATION;
139         }
140         if (Name.EXTENSION_LIST.toString().equals(attrName)) {
141             return Name.EXTENSION_LIST;
142         }
143         if (Name.EXTENSION_NAME.toString().equals(attrName)) {
144             return Name.EXTENSION_NAME;
145         }
146         if (Name.IMPLEMENTATION_TITLE.toString().equals(attrName)) {
147             return Name.IMPLEMENTATION_TITLE;
148         }
149         if (Name.IMPLEMENTATION_URL.toString().equals(attrName)) {
150             return Name.IMPLEMENTATION_URL;
151         }
152         if (Name.IMPLEMENTATION_VENDOR.toString().equals(attrName)) {
153             return Name.IMPLEMENTATION_VENDOR;
154         }
155         if (Name.IMPLEMENTATION_VENDOR_ID.toString().equals(attrName)) {
156             return Name.IMPLEMENTATION_VENDOR_ID;
157         }
158         if (Name.IMPLEMENTATION_VERSION.toString().equals(attrName)) {
159             return Name.IMPLEMENTATION_VENDOR;
160         }
161         if (Name.MAIN_CLASS.toString().equals(attrName)) {
162             return Name.MAIN_CLASS;
163         }
164         if (Name.MANIFEST_VERSION.toString().equals(attrName)) {
165             return Name.MANIFEST_VERSION;
166         }
167         if (Name.SEALED.toString().equals(attrName)) {
168             return Name.SEALED;
169         }
170         if (Name.SIGNATURE_VERSION.toString().equals(attrName)) {
171             return Name.SIGNATURE_VERSION;
172         }
173         if (Name.SPECIFICATION_TITLE.toString().equals(attrName)) {
174             return Name.SPECIFICATION_TITLE;
175         }
176         if (Name.SPECIFICATION_VENDOR.toString().equals(attrName)) {
177             return Name.SPECIFICATION_VENDOR;
178         }
179         if (Name.SPECIFICATION_VERSION.toString().equals(attrName)) {
180             return Name.SPECIFICATION_VERSION;
181         }
182         return new Name(attrName);
183     }
184 
185 }