KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > deploy > shared > InputJarArchive


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.deployment.deploy.shared;
25
26 import java.io.*;
27 import java.util.*;
28 import java.util.jar.*;
29 import java.util.zip.*;
30 import java.util.logging.Level JavaDoc;
31 import java.net.URI JavaDoc;
32
33 import com.sun.enterprise.deployment.util.DOLUtils;
34 import com.sun.enterprise.deployment.deploy.shared.Archive;
35 import com.sun.enterprise.util.io.FileUtils;
36 import com.sun.enterprise.util.i18n.StringManager;
37
38 /**
39  * This implementation of the AbstractArchive deal with reading
40  * jar files either from a JarFile or from a JarInputStream
41  *
42  * @author Jerome Dochez
43  */

44 public class InputJarArchive extends AbstractArchive {
45     
46     
47     // the file we are currently mapped to
48
protected JarFile jarFile=null;
49     
50     // in case this abstraction is dealing with a jar file
51
// within a jar file, the jarFile will be null and this
52
// JarInputStream will contain the
53
protected JarInputStream jarIS=null;
54     
55     // the archive Uri
56
private String JavaDoc archiveUri;
57
58     // parent jar file for embedded jar
59
private InputJarArchive parentArchive=null;
60
61     private StringManager localStrings = StringManager.getManager(getClass());
62
63
64     /**
65      * @return the archive uri
66      */

67     public String JavaDoc getArchiveUri() {
68         return archiveUri;
69     }
70     
71     /**
72      * Get the size of the archive
73      * @return tje the size of this archive or -1 on error
74      */

75     public long getArchiveSize() throws NullPointerException JavaDoc, SecurityException JavaDoc {
76         if(getArchiveUri() == null) {
77             return -1;
78         }
79         File tmpFile = new File(getArchiveUri());
80         return(tmpFile.length());
81     }
82     
83     /** @returns an @see java.io.OutputStream for a new entry in this
84      * current abstract archive.
85      * @param the entry name
86      */

87     public OutputStream addEntry(String JavaDoc name) throws IOException {
88         throw new UnsupportedOperationException JavaDoc("Cannot write to an JAR archive open for reading");
89     }
90     
91     /**
92      * close the abstract archive
93      */

94     public void close() throws IOException {
95         if (jarFile!=null) {
96             jarFile.close();
97             jarFile=null;
98         }
99         if (jarIS!=null) {
100             jarIS.close();
101             jarIS=null;
102         }
103     }
104         
105     /**
106      * creates a new abstract archive with the given path
107      *
108      * @param the path to create the archive
109      */

110     public void create(String JavaDoc path) throws IOException {
111         throw new UnsupportedOperationException JavaDoc("Cannot write to an JAR archive open for reading");
112     }
113         
114     /**
115      * @return an @see java.util.Enumeration of entries in this abstract
116      * archive
117      */

118     public Enumeration entries() {
119         Vector entries = new Vector();
120  
121         if (parentArchive!=null) {
122             try {
123                 // reopen the embedded archive and position the input stream
124
// at the beginning of the desired element
125
jarIS = new JarInputStream(parentArchive.jarFile.getInputStream(parentArchive.jarFile.getJarEntry(archiveUri)));
126                 JarEntry ze;
127                 do {
128                     ze = jarIS.getNextJarEntry();
129                     if (ze!=null && !ze.isDirectory()) {
130                         entries.add(ze.getName());
131                     }
132                 } while (ze!=null);
133                 jarIS.close();
134                 jarIS = null;
135             } catch(IOException ioe) {
136                 return null;
137             }
138         } else {
139             try {
140                 if (jarFile==null) {
141                     getJarFile(archiveUri);
142                 }
143             } catch(IOException ioe) {
144                 return entries.elements();
145             }
146             if (jarFile==null) {
147                 return entries.elements();
148             }
149             for (Enumeration e = jarFile.entries();e.hasMoreElements();) {
150                 ZipEntry ze = (ZipEntry) e.nextElement();
151                 if (!ze.isDirectory() && !ze.getName().equals(JarFile.MANIFEST_NAME)) {
152                     entries.add(ze.getName());
153                 }
154             }
155         }
156         return entries.elements();
157     }
158     
159     /**
160      * @return an @see java.util.Enumeration of entries in this abstract
161      * archive, providing the list of embedded archive to not count their
162      * entries as part of this archive
163      */

164      public Enumeration entries(Enumeration embeddedArchives) {
165     // jar file are not recursive
166
return entries();
167     }
168     
169     /**
170      * @return a @see java.io.InputStream for an existing entry in
171      * the current abstract archive
172      * @param the entry name
173      */

174     public InputStream getEntry(String JavaDoc entryName) throws IOException {
175         if (jarFile!=null) {
176             ZipEntry ze = jarFile.getEntry(entryName);
177             if (ze!=null) {
178                 return new BufferedInputStream(jarFile.getInputStream(ze));
179             } else {
180                 return null;
181             }
182         } else
183     if ((parentArchive != null) && (parentArchive.jarFile != null)) {
184             JarEntry je;
185             // close the current input stream
186
if (jarIS!=null) {
187                 jarIS.close();
188             }
189             
190             // reopen the embedded archive and position the input stream
191
// at the beginning of the desired element
192
JarEntry archiveJarEntry = (archiveUri != null)? parentArchive.jarFile.getJarEntry(archiveUri) : null;
193         if (archiveJarEntry == null) {
194         return null;
195         }
196             jarIS = new JarInputStream(parentArchive.jarFile.getInputStream(archiveJarEntry));
197             do {
198                 je = jarIS.getNextJarEntry();
199             } while (je!=null && !je.getName().equals(entryName));
200             if (je!=null) {
201                 return new BufferedInputStream(jarIS);
202             } else {
203                 return null;
204             }
205         } else {
206         return null;
207     }
208     }
209     
210     /** Open an abstract archive
211      * @param the path to the archive
212      */

213     public void open(String JavaDoc path) throws IOException {
214         archiveUri = path;
215         jarFile = getJarFile(path);
216     }
217     
218     /**
219      * @return a JarFile instance for a file path
220      */

221     protected JarFile getJarFile(String JavaDoc path) throws IOException {
222         jarFile = null;
223         try {
224             File file = new File(path);
225             if (file.exists()) {
226                 jarFile = new JarFile(file);
227             }
228         } catch(IOException e) {
229             DOLUtils.getDefaultLogger().log(Level.SEVERE, "enterprise.deployment.backend.fileOpenFailure",
230                     new Object JavaDoc[]{path});
231             // add the additional information about the path
232
// since the IOException from jdk doesn't include that info
233
String JavaDoc additionalInfo = localStrings.getString(
234                 "enterprise.deployment.invalid_zip_file", path);
235             IOException ioe = new IOException(e.getMessage() + " -- " + additionalInfo);
236             ioe.initCause(e);
237             throw ioe;
238         }
239         return jarFile;
240     }
241     
242     
243     /**
244      * @return the manifest information for this abstract archive
245      */

246     public Manifest getManifest() throws IOException {
247         if (jarFile!=null) {
248             return jarFile.getManifest();
249         }
250         if (parentArchive!=null) {
251             // close the current input stream
252
if (jarIS!=null) {
253                 jarIS.close();
254             }
255             // reopen the embedded archive and position the input stream
256
// at the beginning of the desired element
257
if (jarIS==null) {
258                 jarIS = new JarInputStream(parentArchive.jarFile.getInputStream(parentArchive.jarFile.getJarEntry(archiveUri)));
259             }
260             Manifest m = jarIS.getManifest();
261             if (m==null) {
262                java.io.InputStream JavaDoc is = getEntry(java.util.jar.JarFile.MANIFEST_NAME);
263                if (is!=null) {
264                     m = new Manifest();
265                     m.read(is);
266                     is.close();
267                }
268             }
269             return m;
270         }
271         return null;
272     }
273     
274     /**
275      * @return true if this abstract archive maps to an existing
276      * jar file
277      */

278     public boolean exists() {
279         return jarFile!=null;
280     }
281     
282     /**
283      * deletes the underlying jar file
284      */

285     public boolean delete() {
286         if (jarFile==null) {
287             return false;
288         }
289         try {
290             jarFile.close();
291             jarFile = null;
292         } catch (IOException ioe) {
293             return false;
294         }
295         return FileUtils.deleteFile(new File(archiveUri));
296     }
297     
298     /**
299      * rename the underlying jar file
300      */

301     public boolean renameTo(String JavaDoc name) {
302         if (jarFile==null) {
303             return false;
304         }
305         try {
306             jarFile.close();
307             jarFile = null;
308         } catch (IOException ioe) {
309             return false;
310         }
311         return FileUtils.renameFile(new File(archiveUri), new File(name));
312     }
313     
314     /**
315      * @return an AbstractArchive for an embedded archive indentified with
316      * the name parameter
317      */

318     public AbstractArchive getEmbeddedArchive(String JavaDoc name) throws IOException {
319         if (jarFile!=null) {
320             // for now, I only support one level down embedded archives
321
InputJarArchive ija = new InputJarArchive();
322             JarEntry je = jarFile.getJarEntry(name);
323             if (je!=null) {
324                 JarInputStream jis = new JarInputStream(new BufferedInputStream(jarFile.getInputStream(je)));
325                 ija.archiveUri = name;
326                 ija.jarIS = jis;
327                 ija.parentArchive = this;
328                 return ija;
329             }
330         }
331         return null;
332     }
333     
334     /** close a previously returned @see java.io.OutputStream returned
335      * by an addEntry call
336      * @param the output stream to close
337      */

338     public void closeEntry(AbstractArchive os) throws IOException {
339         throw new UnsupportedOperationException JavaDoc("Cannot write to an JAR archive open for reading");
340     }
341             
342     public void closeEntry() {
343         throw new UnsupportedOperationException JavaDoc("Cannot write to an JAR archive open for reading");
344     }
345     
346     public URI JavaDoc getURI() {
347         try {
348             return ArchiveFactory.prepareArchiveURI(getArchiveUri());
349         } catch(java.net.URISyntaxException JavaDoc e) {
350             return null;
351         } catch (UnsupportedEncodingException uee) {
352             return null;
353         } catch (IOException ioe) {
354             return null;
355         }
356     }
357     
358     public OutputStream putNextEntry(String JavaDoc name) throws java.io.IOException JavaDoc {
359         throw new UnsupportedOperationException JavaDoc("Cannot write to an JAR archive open for reading");
360     }
361     
362 }
363
Popular Tags