KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sape > carbon > core > util > jar > EnhancedJarFile


1 /*
2  * The contents of this file are subject to the Sapient Public License
3  * Version 1.0 (the "License"); you may not use this file except in compliance
4  * with the License. You may obtain a copy of the License at
5  * http://carbon.sf.net/License.html.
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is The Carbon Component Framework.
12  *
13  * The Initial Developer of the Original Code is Sapient Corporation
14  *
15  * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
16  */

17
18 package org.sape.carbon.core.util.jar;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.InputStream JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27 import java.util.jar.JarEntry JavaDoc;
28 import java.util.jar.JarFile JavaDoc;
29 import java.util.jar.Manifest JavaDoc;
30 import java.util.zip.ZipEntry JavaDoc;
31
32 import org.sape.carbon.core.exception.ExceptionUtility;
33 import org.sape.carbon.core.exception.InvalidParameterException;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38 /**
39  * This class enhances functionality of java.util.jar.JarFile.
40  * Additional functionality includes jar entry removal, the ability to list
41  * the entries within a directory within the jar, and the ability to get
42  * an output stream for modifying extisting entries.
43  *
44  * @see java.util.jar.JarFile
45  *
46  * Copyright 2002 Sapient
47  * @since carbon 1.0
48  * @author Doug Voet, April 2002
49  * @version $Revision: 1.11 $ ($Author: dvoet $)
50  */

51 public class EnhancedJarFile {
52     public static final String JavaDoc JAR_DELIMETER = "/";
53
54     /**
55      * Provides a handle to Apache-commons logger
56      */

57     private Log log = LogFactory.getLog(this.getClass());
58
59     private JarFile JavaDoc jar;
60
61     /**
62      * @see java.util.jar.JarFile#JarFile(java.lang.String)
63      */

64     public EnhancedJarFile(String JavaDoc name) throws IOException JavaDoc {
65         if (log.isTraceEnabled()) {
66             log.trace("Openning Jar [" + name + "] " +
67                 ExceptionUtility.captureStackTrace(new Exception JavaDoc()));
68         }
69         this.jar = new JarFile JavaDoc(name);
70     }
71
72     /**
73      * @see java.util.jar.JarFile#JarFile(java.lang.String, boolean)
74      */

75     public EnhancedJarFile(String JavaDoc name, boolean verify) throws IOException JavaDoc {
76         if (log.isTraceEnabled()) {
77             log.trace("Openning Jar [" + name + "] " +
78                 ExceptionUtility.captureStackTrace(new Exception JavaDoc()));
79         }
80         this.jar = new JarFile JavaDoc(name, verify);
81     }
82
83     /**
84      * @see java.util.jar.JarFile#JarFile(java.io.File)
85      */

86     public EnhancedJarFile(File JavaDoc file) throws IOException JavaDoc {
87         if (log.isTraceEnabled()) {
88             log.trace("Openning Jar [" + file.getAbsolutePath() +
89                 "] " + ExceptionUtility.captureStackTrace(new Exception JavaDoc()));
90         }
91         this.jar = new JarFile JavaDoc(file);
92     }
93
94     /**
95      * @see java.util.jar.JarFile#JarFile(java.io.File, boolean)
96      */

97     public EnhancedJarFile(File JavaDoc file, boolean verify) throws IOException JavaDoc {
98         if (log.isTraceEnabled()) {
99             log.trace("Openning Jar [" + file.getAbsolutePath() +
100                 "] " + ExceptionUtility.captureStackTrace(new Exception JavaDoc()));
101         }
102         this.jar = new JarFile JavaDoc(file, verify);
103     }
104
105     /**
106      * @see java.util.jar.JarFile#JarFile(java.io.File, boolean, int)
107      */

108     public EnhancedJarFile(File JavaDoc file, boolean verify, int mode)
109         throws IOException JavaDoc {
110         if (log.isTraceEnabled()) {
111             log.trace("Openning Jar [" + file.getAbsolutePath() +
112                 "] " + ExceptionUtility.captureStackTrace(new Exception JavaDoc()));
113         }
114         this.jar = new JarFile JavaDoc(file, verify, mode);
115     }
116
117     /**
118      * Returns a list of entries that are
119      * immediately below the entry named by entryName in the jar's directory
120      * structure.
121      *
122      * @param entryName the name of the directory entry name
123      * @return List a list of java.util.jar.JarEntry objects that are
124      * immediately below the entry named by entryName in the jar's directory
125      * structure.
126      */

127     public List JavaDoc listSubEntries(String JavaDoc entryName) {
128
129         if (entryName == null) {
130             throw new InvalidParameterException(
131                 this.getClass(),
132                 "Entry name cannot be null");
133         }
134
135
136         Enumeration JavaDoc entries = jar.entries();
137         List JavaDoc subEntries = new ArrayList JavaDoc();
138
139         while(entries.hasMoreElements()) {
140             JarEntry JavaDoc nextEntry = (JarEntry JavaDoc) entries.nextElement();
141
142             if (nextEntry.getName().startsWith(entryName)) {
143                 // the next entry name starts with the entryName so it
144
// is a potential sub entry
145

146                 // tokenize the rest of the next entry name to see how
147
// many tokens exist
148
StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(
149                     nextEntry.getName().substring(entryName.length()),
150                     EnhancedJarFile.JAR_DELIMETER);
151
152                 if (tokenizer.countTokens() == 1) {
153                     // only 1 token exists, so it is a sub-entry
154
subEntries.add(nextEntry);
155                 }
156             }
157         }
158
159         return subEntries;
160     }
161
162     /**
163      * Creates a new output entry stream within the jar. The entry named
164      * will be created if it does not exist within the jar already.
165      *
166      * @param entryName name of the entry for which to create an output
167      * stream.
168      * @return JarEntryOutputStream
169      */

170     public JarEntryOutputStream getEntryOutputStream(String JavaDoc entryName) {
171         return new JarEntryOutputStream(this, entryName);
172     }
173
174     /**
175      * Removes the given entry from the jar. If the entry does not exist,
176      * the method returns without doing anything.
177      *
178      * @param entry entry to be removed
179      * @throws IOException if there is a problem writing the changes
180      * to the jar
181      */

182     public void removeEntry(JarEntry JavaDoc entry) throws IOException JavaDoc {
183         // opens an output stream and closes it without writing anything to it
184
if (entry != null && getEntry(entry.getName()) != null) {
185             JarEntryOutputStream outputStream =
186                 new JarEntryOutputStream(this, entry.getName());
187
188             outputStream.close();
189         }
190     }
191
192     /**
193      * @see java.util.jar.JarFile#entries()
194      */

195     public Enumeration JavaDoc entries() {
196         return this.jar.entries();
197     }
198
199     /**
200      * @see java.util.jar.JarFile#getEntry(java.lang.String)
201      */

202     public ZipEntry JavaDoc getEntry(String JavaDoc arg0) {
203         return this.jar.getEntry(arg0);
204     }
205
206     /**
207      * @see java.util.jar.JarFile#getInputStream(java.util.zip.ZipEntry)
208      */

209     public InputStream JavaDoc getInputStream(ZipEntry JavaDoc arg0) throws IOException JavaDoc {
210         return this.jar.getInputStream(arg0);
211     }
212
213     /**
214      * @see java.util.jar.JarFile#getJarEntry(java.lang.String)
215      */

216     public JarEntry JavaDoc getJarEntry(String JavaDoc arg0) {
217         return this.jar.getJarEntry(arg0);
218     }
219
220     /**
221      * @see java.util.jar.JarFile#getManifest()
222      */

223     public Manifest JavaDoc getManifest() throws IOException JavaDoc {
224         return this.jar.getManifest();
225     }
226
227     /**
228      * @see java.util.zip.ZipFile#close()
229      */

230     public void close() throws IOException JavaDoc {
231         if (log.isTraceEnabled()) {
232             log.trace("Closing Jar [" + this.jar.getName() +
233                 "] " + ExceptionUtility.captureStackTrace(new Exception JavaDoc()));
234         }
235         this.jar.close();
236     }
237
238     /**
239      * @see java.util.zip.ZipFile#getName()
240      */

241     public String JavaDoc getName() {
242         return this.jar.getName();
243     }
244
245     /**
246      * @see java.util.zip.ZipFile#size()
247      */

248     public int size() {
249         return this.jar.size();
250     }
251
252     /**
253      * Utility method used to swap the underlying jar file out for the new one.
254      * This method closes the old jar file, deletes it, moves the new jar
255      * file to the location where the old one used to be and opens it.
256      * <p>
257      * This is used when modifying the jar (removal, addition, or changes
258      * of entries)
259      *
260      * @param newJarFile the file object pointing to the new jar file
261      */

262     void swapJars(File JavaDoc newJarFile) throws IOException JavaDoc {
263         File JavaDoc oldJarFile = new File JavaDoc(getName());
264         this.jar.close();
265         oldJarFile.delete();
266         if (newJarFile.renameTo(oldJarFile)) {
267             this.jar = new JarFile JavaDoc(oldJarFile);
268         } else {
269             throw new IOException JavaDoc();
270         }
271     }
272 }
273
Popular Tags