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      protected JarFileSystem(final AbstractFileName rootName, final FileObject file,
44              final FileSystemOptions fileSystemOptions) throws FileSystemException {
45          super(rootName, file, fileSystemOptions);
46      }
47  
48      // @Override
49      // protected FileObject createFile(AbstractFileName name) throws FileSystemException
50      // {
51      // return new JarFileObject(name, null, this, false);
52      // }
53  
54      @Override
55      protected ZipFile createZipFile(final File file) throws FileSystemException {
56          try {
57              return new JarFile(file);
58          } catch (final IOException ioe) {
59              throw new FileSystemException("vfs.provider.jar/open-jar-file.error", file, ioe);
60          }
61      }
62  
63      @Override
64      protected ZipFileObject createZipFileObject(final AbstractFileName name, final ZipEntry entry)
65              throws FileSystemException {
66          return new JarFileObject(name, entry, this, true);
67      }
68  
69      /**
70       * Returns the capabilities of this file system.
71       */
72      @Override
73      protected void addCapabilities(final Collection<Capability> caps) {
74          // super.addCapabilities(caps);
75          caps.addAll(JarFileProvider.CAPABILITIES);
76      }
77  
78      Attributes getAttributes() throws IOException {
79          if (attributes == null) {
80              final Manifest man = ((JarFile) getZipFile()).getManifest();
81              if (man == null) {
82                  attributes = new Attributes(1);
83              } else {
84                  attributes = man.getMainAttributes();
85                  if (attributes == null) {
86                      attributes = new Attributes(1);
87                  }
88              }
89          }
90  
91          return attributes;
92      }
93  
94      Object getAttribute(final Name attrName) throws FileSystemException {
95          try {
96              final Attributes attr = getAttributes();
97              return attr.getValue(attrName);
98          } catch (final IOException ioe) {
99              throw new FileSystemException(attrName.toString(), ioe);
100         }
101     }
102 
103     Name lookupName(final String attrName) {
104         if (Name.CLASS_PATH.toString().equals(attrName)) {
105             return Name.CLASS_PATH;
106         }
107         if (Name.CONTENT_TYPE.toString().equals(attrName)) {
108             return Name.CONTENT_TYPE;
109         }
110         if (Name.EXTENSION_INSTALLATION.toString().equals(attrName)) {
111             return Name.EXTENSION_INSTALLATION;
112         }
113         if (Name.EXTENSION_LIST.toString().equals(attrName)) {
114             return Name.EXTENSION_LIST;
115         }
116         if (Name.EXTENSION_NAME.toString().equals(attrName)) {
117             return Name.EXTENSION_NAME;
118         }
119         if (Name.IMPLEMENTATION_TITLE.toString().equals(attrName)) {
120             return Name.IMPLEMENTATION_TITLE;
121         }
122         if (Name.IMPLEMENTATION_URL.toString().equals(attrName)) {
123             return Name.IMPLEMENTATION_URL;
124         }
125         if (Name.IMPLEMENTATION_VENDOR.toString().equals(attrName)) {
126             return Name.IMPLEMENTATION_VENDOR;
127         }
128         if (Name.IMPLEMENTATION_VENDOR_ID.toString().equals(attrName)) {
129             return Name.IMPLEMENTATION_VENDOR_ID;
130         }
131         if (Name.IMPLEMENTATION_VERSION.toString().equals(attrName)) {
132             return Name.IMPLEMENTATION_VENDOR;
133         }
134         if (Name.MAIN_CLASS.toString().equals(attrName)) {
135             return Name.MAIN_CLASS;
136         }
137         if (Name.MANIFEST_VERSION.toString().equals(attrName)) {
138             return Name.MANIFEST_VERSION;
139         }
140         if (Name.SEALED.toString().equals(attrName)) {
141             return Name.SEALED;
142         }
143         if (Name.SIGNATURE_VERSION.toString().equals(attrName)) {
144             return Name.SIGNATURE_VERSION;
145         }
146         if (Name.SPECIFICATION_TITLE.toString().equals(attrName)) {
147             return Name.SPECIFICATION_TITLE;
148         }
149         if (Name.SPECIFICATION_VENDOR.toString().equals(attrName)) {
150             return Name.SPECIFICATION_VENDOR;
151         }
152         if (Name.SPECIFICATION_VERSION.toString().equals(attrName)) {
153             return Name.SPECIFICATION_VERSION;
154         }
155         return new Name(attrName);
156     }
157 
158     /**
159      * Retrives the attribute with the specified name. The default implementation simply throws an exception.
160      *
161      * @param attrName The attiribute's name.
162      * @return The value of the attribute.
163      * @throws FileSystemException if an error occurs.
164      */
165     @Override
166     public Object getAttribute(final String attrName) throws FileSystemException {
167         final Name name = lookupName(attrName);
168         return getAttribute(name);
169     }
170 
171     @Override
172     protected ZipFile getZipFile() throws FileSystemException {
173         // make accessible
174         return super.getZipFile();
175     }
176 
177 }