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              final String value = attr.getValue(attrName);
98              return value;
99          } catch (final IOException ioe) {
100             throw new FileSystemException(attrName.toString(), ioe);
101         }
102     }
103 
104     Name lookupName(final String attrName) {
105         if (Name.CLASS_PATH.toString().equals(attrName)) {
106             return Name.CLASS_PATH;
107         } else if (Name.CONTENT_TYPE.toString().equals(attrName)) {
108             return Name.CONTENT_TYPE;
109         } else if (Name.EXTENSION_INSTALLATION.toString().equals(attrName)) {
110             return Name.EXTENSION_INSTALLATION;
111         } else if (Name.EXTENSION_LIST.toString().equals(attrName)) {
112             return Name.EXTENSION_LIST;
113         } else if (Name.EXTENSION_NAME.toString().equals(attrName)) {
114             return Name.EXTENSION_NAME;
115         } else if (Name.IMPLEMENTATION_TITLE.toString().equals(attrName)) {
116             return Name.IMPLEMENTATION_TITLE;
117         } else if (Name.IMPLEMENTATION_URL.toString().equals(attrName)) {
118             return Name.IMPLEMENTATION_URL;
119         } else if (Name.IMPLEMENTATION_VENDOR.toString().equals(attrName)) {
120             return Name.IMPLEMENTATION_VENDOR;
121         } else if (Name.IMPLEMENTATION_VENDOR_ID.toString().equals(attrName)) {
122             return Name.IMPLEMENTATION_VENDOR_ID;
123         } else if (Name.IMPLEMENTATION_VERSION.toString().equals(attrName)) {
124             return Name.IMPLEMENTATION_VENDOR;
125         } else if (Name.MAIN_CLASS.toString().equals(attrName)) {
126             return Name.MAIN_CLASS;
127         } else if (Name.MANIFEST_VERSION.toString().equals(attrName)) {
128             return Name.MANIFEST_VERSION;
129         } else if (Name.SEALED.toString().equals(attrName)) {
130             return Name.SEALED;
131         } else if (Name.SIGNATURE_VERSION.toString().equals(attrName)) {
132             return Name.SIGNATURE_VERSION;
133         } else if (Name.SPECIFICATION_TITLE.toString().equals(attrName)) {
134             return Name.SPECIFICATION_TITLE;
135         } else if (Name.SPECIFICATION_VENDOR.toString().equals(attrName)) {
136             return Name.SPECIFICATION_VENDOR;
137         } else if (Name.SPECIFICATION_VERSION.toString().equals(attrName)) {
138             return Name.SPECIFICATION_VERSION;
139         } else {
140             return new Name(attrName);
141         }
142     }
143 
144     /**
145      * Retrives the attribute with the specified name. The default implementation simply throws an exception.
146      *
147      * @param attrName The attiribute's name.
148      * @return The value of the attribute.
149      * @throws FileSystemException if an error occurs.
150      */
151     @Override
152     public Object getAttribute(final String attrName) throws FileSystemException {
153         final Name name = lookupName(attrName);
154         return getAttribute(name);
155     }
156 
157     @Override
158     protected ZipFile getZipFile() throws FileSystemException {
159         // make accessible
160         return super.getZipFile();
161     }
162 
163 }