KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.commons.vfs.provider.jar;
17
18 import org.apache.commons.vfs.FileName;
19 import org.apache.commons.vfs.FileSystemException;
20 import org.apache.commons.vfs.provider.zip.ZipFileObject;
21
22 import java.io.IOException JavaDoc;
23 import java.security.cert.Certificate JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.jar.Attributes JavaDoc;
28 import java.util.jar.JarEntry JavaDoc;
29 import java.util.jar.JarFile JavaDoc;
30 import java.util.jar.Manifest JavaDoc;
31 import java.util.zip.ZipEntry JavaDoc;
32
33 /**
34  * A file in a Jar file system.
35  *
36  * @author <a HREF="mailto:brian@mmmanager.org">Brian Olsen</a>
37  */

38 public class JarFileObject extends ZipFileObject
39 {
40     private Attributes JavaDoc attributes;
41
42     final JarFileSystem fs;
43
44     protected JarFileObject(final FileName name,
45                             final ZipEntry JavaDoc entry,
46                             final JarFileSystem fs,
47                             final boolean zipExists) throws FileSystemException
48     {
49         super(name, entry, fs, zipExists);
50         this.fs = fs;
51
52         try
53         {
54             getAttributes(); // early get the attributes as the zip file might be closed
55
}
56         catch (IOException JavaDoc e)
57         {
58             throw new FileSystemException(e);
59         }
60     }
61
62     /**
63      * Returns the Jar manifest.
64      */

65     Manifest JavaDoc getManifest() throws IOException JavaDoc
66     {
67         if (fs.getZipFile() == null)
68         {
69             return null;
70         }
71
72         return ((JarFile JavaDoc) fs.getZipFile()).getManifest();
73     }
74
75     /**
76      * Returns the attributes of this file.
77      */

78     Attributes JavaDoc getAttributes() throws IOException JavaDoc
79     {
80         if (attributes == null)
81         {
82             if (entry == null)
83             {
84                 attributes = new Attributes JavaDoc(1);
85             }
86             else
87             {
88                 attributes = ((JarEntry JavaDoc) entry).getAttributes();
89                 if (attributes == null)
90                 {
91                     attributes = new Attributes JavaDoc(1);
92                 }
93             }
94         }
95
96         return attributes;
97     }
98
99     /**
100      * Returns the value of an attribute.
101      */

102     protected Map JavaDoc doGetAttributes()
103         throws Exception JavaDoc
104     {
105         final Map JavaDoc attrs = new HashMap JavaDoc();
106
107         // Add the file system's attributes first
108
final JarFileSystem fs = (JarFileSystem) getFileSystem();
109         addAll(fs.getAttributes(), attrs);
110
111         // Add this file's attributes
112
addAll(getAttributes(), attrs);
113
114         return attrs;
115     }
116
117     /**
118      * Adds the source attributes to the destination map.
119      */

120     private void addAll(final Attributes JavaDoc src, final Map JavaDoc dest)
121     {
122         for (Iterator JavaDoc iterator = src.entrySet().iterator(); iterator.hasNext();)
123         {
124             final Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iterator.next();
125             final String JavaDoc name = entry.getKey().toString().toLowerCase();
126             dest.put(name, entry.getValue());
127         }
128     }
129
130     /**
131      * Return the certificates of this JarEntry.
132      */

133     protected Certificate JavaDoc[] doGetCertificates()
134     {
135         if (entry == null)
136         {
137             return null;
138         }
139
140         return ((JarEntry JavaDoc) entry).getCertificates();
141     }
142 }
143
Popular Tags