KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_lib > genbase > archive > JarArchive


1 /**
2  * JOnAS : Java(TM) OpenSource Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JarArchive.java,v 1.2 2005/04/27 12:28:21 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_lib.genbase.archive;
27
28 import java.io.File JavaDoc;
29 import java.io.FileInputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.util.Enumeration JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Vector JavaDoc;
35 import java.util.jar.JarFile JavaDoc;
36 import java.util.zip.ZipEntry JavaDoc;
37
38 import org.objectweb.jonas_lib.genbase.GenBaseException;
39
40
41 /**
42  * A <code>JarArchive</code> is a wrapper for jar file.
43  *
44  * @author Guillaume Sauthier
45  */

46 public class JarArchive extends AbsArchive {
47
48     /** encapsulated Jar File */
49     private JarFile JavaDoc jar;
50
51     /**
52      * Creates a new JarArchive object.
53      *
54      * @param jar the File corresponding to a JarFile
55      *
56      * @throws GenBaseException When Manifest cannot be found
57      */

58     public JarArchive(File JavaDoc jar) throws GenBaseException {
59         super(jar);
60
61         try {
62             this.jar = new JarFile JavaDoc(jar);
63             setManifest(this.jar.getManifest());
64         } catch (IOException JavaDoc ioe) {
65             String JavaDoc err = getI18n().getMessage("JarArchive.constr.jar", jar);
66             throw new GenBaseException(err, ioe);
67         }
68     }
69
70     /**
71      * Returns an InputStream corresponding to the given filename.
72      *
73      * @param filename file name source of the InputStream
74      *
75      * @return the InputStream corresponding to the given filename.
76      *
77      * @throws IOException when InputStream of the filename cannot be found in
78      * the archive
79      */

80     public InputStream JavaDoc getInputStream(String JavaDoc filename) throws IOException JavaDoc {
81         // try get the file from the map
82
File JavaDoc file = (File JavaDoc) getFiles().get(filename);
83
84         if (file == null) {
85             // filename not found in added files
86
// try jar search
87
ZipEntry JavaDoc ze = jar.getEntry(filename);
88
89             // Entry found ?
90
if (ze == null) {
91                 return null;
92             } else {
93                 return jar.getInputStream(ze);
94             }
95         } else {
96             // file exists (in added files)
97
return new FileInputStream JavaDoc(file);
98         }
99     }
100
101     /**
102      * Returns a List of all files contained in this archive. Original files in
103      * jar, added Files are all included as String in this Enumeration.
104      *
105      * @return a List of all files contained in this archive.
106      */

107     public List JavaDoc getContainedFiles() {
108         List JavaDoc list = new Vector JavaDoc(getFiles().keySet());
109
110         // add files of the original archive
111
for (Enumeration JavaDoc e = jar.entries(); e.hasMoreElements();) {
112             ZipEntry JavaDoc ze = (ZipEntry JavaDoc) e.nextElement();
113             list.add(ze.getName());
114         }
115
116         return list;
117     }
118
119     /**
120      * Returns true if archive is packed or false if archive is unpacked.
121      *
122      * @return true if archive is packed or false if archive is unpacked.
123      */

124     public boolean isPacked() {
125         return true;
126     }
127
128     /**
129      * Close this archive
130      */

131     public void close() {
132         super.close();
133         try {
134             jar.close();
135         } catch (IOException JavaDoc ioe) {
136             throw new RuntimeException JavaDoc("Cannot close file '" + jar + "'", ioe);
137         }
138         jar = null;
139     }
140 }
Popular Tags