KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > provider > jar > JarFileSystem


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * Copyright 2002-2005 The Apache Software Foundation.
18  *
19  * Licensed under the Apache License, Version 2.0 (the "License");
20  * you may not use this file except in compliance with the License.
21  * You may obtain a copy of the License at
22  *
23  * http://www.apache.org/licenses/LICENSE-2.0
24  *
25  * Unless required by applicable law or agreed to in writing, software
26  * distributed under the License is distributed on an "AS IS" BASIS,
27  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28  * See the License for the specific language governing permissions and
29  * limitations under the License.
30  */

31 package org.apache.commons.vfs.provider.jar;
32
33 import org.apache.commons.vfs.FileName;
34 import org.apache.commons.vfs.FileObject;
35 import org.apache.commons.vfs.FileSystemException;
36 import org.apache.commons.vfs.FileSystemOptions;
37 import org.apache.commons.vfs.provider.zip.ZipFileObject;
38 import org.apache.commons.vfs.provider.zip.ZipFileSystem;
39
40 import java.io.File JavaDoc;
41 import java.io.IOException JavaDoc;
42 import java.util.Collection JavaDoc;
43 import java.util.jar.Attributes JavaDoc;
44 import java.util.jar.Attributes.Name;
45 import java.util.jar.JarFile JavaDoc;
46 import java.util.jar.Manifest JavaDoc;
47 import java.util.zip.ZipEntry JavaDoc;
48 import java.util.zip.ZipFile JavaDoc;
49
50 /**
51  * A read-only file system for Jar files.
52  *
53  * @author <a HREF="mailto:brian@mmmanager.org">Brian Olsen</a>
54  */

55 public class JarFileSystem
56     extends ZipFileSystem
57 {
58     private Attributes JavaDoc attributes;
59
60     protected JarFileSystem(final FileName rootName,
61                             final FileObject file,
62                             final FileSystemOptions fileSystemOptions) throws FileSystemException
63     {
64         super(rootName, file, fileSystemOptions);
65     }
66
67     protected ZipFile JavaDoc createZipFile(File JavaDoc file) throws FileSystemException
68     {
69         try
70         {
71             return new JarFile JavaDoc(file);
72         }
73         catch (IOException JavaDoc ioe)
74         {
75             throw new FileSystemException("vfs.provider.jar/open-jar-file.error", file, ioe);
76         }
77     }
78
79     protected ZipFileObject createZipFileObject(FileName name,
80                                                 ZipEntry JavaDoc entry) throws FileSystemException
81     {
82         return new JarFileObject(name, entry, this, true);
83     }
84
85     /**
86      * Returns the capabilities of this file system.
87      */

88     protected void addCapabilities(final Collection JavaDoc caps)
89     {
90         // super.addCapabilities(caps);
91
caps.addAll(JarFileProvider.capabilities);
92     }
93
94     Attributes JavaDoc getAttributes() throws IOException JavaDoc
95     {
96         if (attributes == null)
97         {
98             final Manifest JavaDoc man = ((JarFile JavaDoc) getZipFile()).getManifest();
99             if (man == null)
100             {
101                 attributes = new Attributes JavaDoc(1);
102             }
103             else
104             {
105                 attributes = man.getMainAttributes();
106                 if (attributes == null)
107                 {
108                     attributes = new Attributes JavaDoc(1);
109                 }
110             }
111         }
112
113         return attributes;
114     }
115
116     Object JavaDoc getAttribute(Name attrName)
117         throws FileSystemException
118     {
119         try
120         {
121             final Attributes JavaDoc attr = getAttributes();
122             final String JavaDoc value = attr.getValue(attrName);
123             return value;
124         }
125         catch (IOException JavaDoc ioe)
126         {
127             throw new FileSystemException(attrName.toString(), ioe);
128         }
129     }
130
131     Name lookupName(String JavaDoc attrName)
132     {
133         if (Name.CLASS_PATH.equals(attrName))
134         {
135             return Name.CLASS_PATH;
136         }
137         else if (Name.CONTENT_TYPE.equals(attrName))
138         {
139             return Name.CONTENT_TYPE;
140         }
141         else if (Name.EXTENSION_INSTALLATION.equals(attrName))
142         {
143             return Name.EXTENSION_INSTALLATION;
144         }
145         else if (Name.EXTENSION_LIST.equals(attrName))
146         {
147             return Name.EXTENSION_LIST;
148         }
149         else if (Name.EXTENSION_NAME.equals(attrName))
150         {
151             return Name.EXTENSION_NAME;
152         }
153         else if (Name.IMPLEMENTATION_TITLE.equals(attrName))
154         {
155             return Name.IMPLEMENTATION_TITLE;
156         }
157         else if (Name.IMPLEMENTATION_URL.equals(attrName))
158         {
159             return Name.IMPLEMENTATION_URL;
160         }
161         else if (Name.IMPLEMENTATION_VENDOR.equals(attrName))
162         {
163             return Name.IMPLEMENTATION_VENDOR;
164         }
165         else if (Name.IMPLEMENTATION_VENDOR_ID.equals(attrName))
166         {
167             return Name.IMPLEMENTATION_VENDOR_ID;
168         }
169         else if (Name.IMPLEMENTATION_VERSION.equals(attrName))
170         {
171             return Name.IMPLEMENTATION_VENDOR;
172         }
173         else if (Name.MAIN_CLASS.equals(attrName))
174         {
175             return Name.MAIN_CLASS;
176         }
177         else if (Name.MANIFEST_VERSION.equals(attrName))
178         {
179             return Name.MANIFEST_VERSION;
180         }
181         else if (Name.SEALED.equals(attrName))
182         {
183             return Name.SEALED;
184         }
185         else if (Name.SIGNATURE_VERSION.equals(attrName))
186         {
187             return Name.SIGNATURE_VERSION;
188         }
189         else if (Name.SPECIFICATION_TITLE.equals(attrName))
190         {
191             return Name.SPECIFICATION_TITLE;
192         }
193         else if (Name.SPECIFICATION_VENDOR.equals(attrName))
194         {
195             return Name.SPECIFICATION_VENDOR;
196         }
197         else if (Name.SPECIFICATION_VERSION.equals(attrName))
198         {
199             return Name.SPECIFICATION_VERSION;
200         }
201         else
202         {
203             return new Name(attrName);
204         }
205     }
206
207     /**
208      * Retrives the attribute with the specified name. The default
209      * implementation simply throws an exception.
210      */

211     public Object JavaDoc getAttribute(String JavaDoc attrName) throws FileSystemException
212     {
213         final Name name = lookupName(attrName);
214         return getAttribute(name);
215     }
216
217
218     protected ZipFile JavaDoc getZipFile() throws FileSystemException
219     {
220         return super.getZipFile();
221     }
222 }
223
Popular Tags