KickJava   Java API By Example, From Geeks To Geeks.

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


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.Manifest JavaDoc;
29 import java.util.zip.*;
30 import java.net.URI JavaDoc;
31
32 import com.sun.enterprise.deployment.util.DOLUtils;
33
34 /**
35  * Provides an implementation of the AbstractArchive that maps to
36  * a Jar file @see java.util.jar.JarFile
37 *
38  * @author Jerome Dochez
39  */

40 public class OutputJarArchive extends AbstractArchive {
41     
42     // the archiveUri
43
private String JavaDoc archiveUri;
44     
45     // the file we are currently mapped to (if open for writing)
46
protected ZipOutputStream jos=null;
47     
48     private Manifest JavaDoc manifest=null;
49     
50     // list of entries already written to this ouput
51
private Vector entries = new Vector();
52     
53     
54     /**
55      * @return the archive uri
56      */

57     public String JavaDoc getArchiveUri() {
58         return archiveUri;
59     }
60         
61     /**
62      * Get the size of the archive
63      * @return -1 because this is getting created
64      */

65     public long getArchiveSize() throws NullPointerException JavaDoc, SecurityException JavaDoc {
66         return -1;
67     }
68     
69     /** close the abstract archive
70      */

71     public void close() throws IOException {
72         if (jos!=null) {
73             jos.flush();
74             jos.finish();
75             jos.close();
76             jos=null;
77         }
78     }
79         
80     /** creates a new abstract archive with the given path
81      * @param the path to create the archive
82      */

83     public void create(String JavaDoc path) throws IOException {
84         archiveUri = path;
85         File file = new File(path);
86         // if teh file exists, we delete it first
87
if (file.exists()) {
88             file.delete();
89         }
90         FileOutputStream fos = new FileOutputStream(file);
91         BufferedOutputStream bos = new BufferedOutputStream(fos);
92         jos = new ZipOutputStream(bos);
93     }
94         
95     /** @return an @see java.util.Enumeration of entries in this abstract
96      * archive
97      */

98     public Enumeration entries() {
99         return entries.elements();
100     }
101     
102     /**
103      * @return an @see java.util.Enumeration of entries in this abstract
104      * archive, providing the list of embedded archive to not count their
105      * entries as part of this archive
106      */

107      public Enumeration entries(Enumeration embeddedArchives) {
108     return entries();
109      }
110     
111     /** @return a @see java.io.InputStream for an existing entry in
112      * the current abstract archive
113      * @param the entry name
114      */

115     public InputStream getEntry(String JavaDoc name) throws IOException {
116         throw new UnsupportedOperationException JavaDoc("Cannot read from a JAR archive open for writing");
117     }
118     
119     /** Open an abstract archive
120      * @param the path to the archive
121      */

122     public void open(String JavaDoc path) throws IOException {
123         throw new UnsupportedOperationException JavaDoc("Cannot read jar files");
124     }
125     
126     /** @return the manifest information for this abstract archive
127      */

128     public Manifest JavaDoc getManifest() throws IOException {
129         if (manifest!=null) {
130             manifest = new Manifest JavaDoc();
131         }
132         return manifest;
133     }
134     
135     public boolean exists() {
136         throw new UnsupportedOperationException JavaDoc("Cannot read from a JAR archive open for writing");
137     }
138     
139     public boolean delete() {
140         throw new UnsupportedOperationException JavaDoc("Cannot read from a JAR archive open for writing");
141     }
142     
143     public boolean renameTo(String JavaDoc name) {
144         throw new UnsupportedOperationException JavaDoc("Cannot read from a JAR archive open for writing");
145     }
146     
147     public AbstractArchive getEmbeddedArchive(String JavaDoc name) throws IOException {
148         OutputStream os = putNextEntry(name);
149         ZipOutputStream jos = new ZipOutputStream(os);
150         OutputJarArchive ja = new OutputJarArchive();
151         ja.archiveUri = name;
152         ja.jos = jos;
153         return ja;
154     }
155     
156     /** close a previously returned @see java.io.OutputStream returned
157      * by an addEntry call
158      * @param the output stream to close
159      */

160     public void closeEntry(AbstractArchive os) throws IOException {
161         if (os instanceof OutputJarArchive) {
162             ((OutputJarArchive) os).jos.flush();
163             ((OutputJarArchive) os).jos.finish();
164         }
165         jos.closeEntry();
166     }
167     
168        
169     /** @returns an @see java.io.OutputStream for a new entry in this
170      * current abstract archive.
171      * @param the entry name
172      */

173     public OutputStream putNextEntry(String JavaDoc name) throws java.io.IOException JavaDoc {
174         if (jos!=null) {
175             ZipEntry ze = new ZipEntry(name);
176             jos.putNextEntry(ze);
177             entries.add(name);
178         }
179         return jos;
180     }
181
182     
183     /**
184      * closes the current entry
185      */

186     public void closeEntry() throws IOException {
187         if (jos!=null) {
188             jos.flush();
189             jos.closeEntry();
190         }
191     }
192     
193     public java.net.URI JavaDoc getURI() {
194         try {
195             return ArchiveFactory.prepareArchiveURI(getArchiveUri());
196         } catch(java.net.URISyntaxException JavaDoc e) {
197             return null;
198         } catch (UnsupportedEncodingException uee) {
199             return null;
200         } catch (IOException ioe) {
201             return null;
202         }
203     }
204     
205     
206 }
207
Popular Tags