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.FileObject;
21  import org.apache.commons.vfs.FileSystemException;
22  import org.apache.commons.vfs.FileSystemOptions;
23  import org.apache.commons.vfs.provider.zip.ZipFileObject;
24  import org.apache.commons.vfs.provider.zip.ZipFileSystem;
25  
26  import java.io.File;
27  import java.io.IOException;
28  import java.util.Collection;
29  import java.util.jar.Attributes;
30  import java.util.jar.Attributes.Name;
31  import java.util.jar.JarFile;
32  import java.util.jar.Manifest;
33  import java.util.zip.ZipEntry;
34  import java.util.zip.ZipFile;
35  
36  /***
37   * A read-only file system for Jar files.
38   *
39   * @author <a href="mailto:brian@mmmanager.org">Brian Olsen</a>
40   * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
41   */
42  public class JarFileSystem
43      extends ZipFileSystem
44  {
45      private Attributes attributes;
46  
47      protected JarFileSystem(final FileName rootName,
48                              final FileObject file,
49                              final FileSystemOptions fileSystemOptions) throws FileSystemException
50      {
51          super(rootName, file, fileSystemOptions);
52      }
53  
54      protected ZipFile createZipFile(File file) throws FileSystemException
55      {
56          try
57          {
58              return new JarFile(file);
59          }
60          catch (IOException ioe)
61          {
62              throw new FileSystemException("vfs.provider.jar/open-jar-file.error", file, ioe);
63          }
64      }
65  
66      protected ZipFileObject createZipFileObject(FileName name,
67                                                  ZipEntry entry) throws FileSystemException
68      {
69          return new JarFileObject(name, entry, this, true);
70      }
71  
72      /***
73       * Returns the capabilities of this file system.
74       */
75      protected void addCapabilities(final Collection caps)
76      {
77          // super.addCapabilities(caps);
78          caps.addAll(JarFileProvider.capabilities);
79      }
80  
81      Attributes getAttributes() throws IOException
82      {
83          if (attributes == null)
84          {
85              final Manifest man = ((JarFile) getZipFile()).getManifest();
86              if (man == null)
87              {
88                  attributes = new Attributes(1);
89              }
90              else
91              {
92                  attributes = man.getMainAttributes();
93                  if (attributes == null)
94                  {
95                      attributes = new Attributes(1);
96                  }
97              }
98          }
99  
100         return attributes;
101     }
102 
103     Object getAttribute(Name attrName)
104         throws FileSystemException
105     {
106         try
107         {
108             final Attributes attr = getAttributes();
109             final String value = attr.getValue(attrName);
110             return value;
111         }
112         catch (IOException ioe)
113         {
114             throw new FileSystemException(attrName.toString(), ioe);
115         }
116     }
117 
118     Name lookupName(String attrName)
119     {
120         if (Name.CLASS_PATH.equals(attrName))
121         {
122             return Name.CLASS_PATH;
123         }
124         else if (Name.CONTENT_TYPE.equals(attrName))
125         {
126             return Name.CONTENT_TYPE;
127         }
128         else if (Name.EXTENSION_INSTALLATION.equals(attrName))
129         {
130             return Name.EXTENSION_INSTALLATION;
131         }
132         else if (Name.EXTENSION_LIST.equals(attrName))
133         {
134             return Name.EXTENSION_LIST;
135         }
136         else if (Name.EXTENSION_NAME.equals(attrName))
137         {
138             return Name.EXTENSION_NAME;
139         }
140         else if (Name.IMPLEMENTATION_TITLE.equals(attrName))
141         {
142             return Name.IMPLEMENTATION_TITLE;
143         }
144         else if (Name.IMPLEMENTATION_URL.equals(attrName))
145         {
146             return Name.IMPLEMENTATION_URL;
147         }
148         else if (Name.IMPLEMENTATION_VENDOR.equals(attrName))
149         {
150             return Name.IMPLEMENTATION_VENDOR;
151         }
152         else if (Name.IMPLEMENTATION_VENDOR_ID.equals(attrName))
153         {
154             return Name.IMPLEMENTATION_VENDOR_ID;
155         }
156         else if (Name.IMPLEMENTATION_VERSION.equals(attrName))
157         {
158             return Name.IMPLEMENTATION_VENDOR;
159         }
160         else if (Name.MAIN_CLASS.equals(attrName))
161         {
162             return Name.MAIN_CLASS;
163         }
164         else if (Name.MANIFEST_VERSION.equals(attrName))
165         {
166             return Name.MANIFEST_VERSION;
167         }
168         else if (Name.SEALED.equals(attrName))
169         {
170             return Name.SEALED;
171         }
172         else if (Name.SIGNATURE_VERSION.equals(attrName))
173         {
174             return Name.SIGNATURE_VERSION;
175         }
176         else if (Name.SPECIFICATION_TITLE.equals(attrName))
177         {
178             return Name.SPECIFICATION_TITLE;
179         }
180         else if (Name.SPECIFICATION_VENDOR.equals(attrName))
181         {
182             return Name.SPECIFICATION_VENDOR;
183         }
184         else if (Name.SPECIFICATION_VERSION.equals(attrName))
185         {
186             return Name.SPECIFICATION_VERSION;
187         }
188         else
189         {
190             return new Name(attrName);
191         }
192     }
193 
194     /***
195      * Retrives the attribute with the specified name. The default
196      * implementation simply throws an exception.
197      */
198     public Object getAttribute(String attrName) throws FileSystemException
199     {
200         final Name name = lookupName(attrName);
201         return getAttribute(name);
202     }
203 
204 
205     protected ZipFile getZipFile() throws FileSystemException
206     {
207         return super.getZipFile();
208     }
209 }