KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.net.URI JavaDoc;
30 import java.util.Vector JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import java.util.jar.JarFile JavaDoc;
33 import java.util.jar.Manifest JavaDoc;
34 import java.util.zip.ZipEntry JavaDoc;
35 import com.sun.enterprise.util.io.FileUtils;
36
37 /**
38  * This AbstractArchive offers an abstraction for jsr88
39  * deployment plan as defined for the SJES Application
40  * Server.
41  *
42  * @author Jerome Dochez
43  */

44 public class DeploymentPlanArchive extends AbstractArchive {
45
46     // the deployment plan jar file...
47
JarFile JavaDoc jarFile = null;
48     
49     // original archive uri
50
String JavaDoc archiveUri = null;
51     
52     // cached list of elements
53
Vector JavaDoc elements=null;
54     
55     String JavaDoc subArchiveUri=null;
56     
57     /** Creates a new instance of DeploymentPlanArchive
58      * package private
59      */

60     public DeploymentPlanArchive() {
61     }
62     
63     /** Open an existing DeploymentPlan archive and return
64      * a abstraction for reading from it.
65      * @param the path to the archive
66      */

67     public void open(String JavaDoc path) throws IOException JavaDoc {
68         archiveUri = path;
69         File JavaDoc f = new File JavaDoc(path);
70         if (f.exists()) {
71             jarFile = new JarFile JavaDoc(f);
72         }
73     }
74     
75     /**
76      * Get the size of the archive
77      * @return tje the size of this archive or -1 on error
78      */

79     public long getArchiveSize() throws NullPointerException JavaDoc, SecurityException JavaDoc {
80         if(getArchiveUri() == null) {
81             return -1;
82         }
83         File JavaDoc tmpFile = new File JavaDoc(getArchiveUri());
84         return(tmpFile.length());
85     }
86     
87     /**
88      * Closes the current jar file
89      */

90     public void close() throws java.io.IOException JavaDoc {
91         if (jarFile!=null) {
92             jarFile.close();
93             jarFile=null;
94         }
95     }
96     
97     /**
98      * Closes the output jar file entry
99      */

100     public void closeEntry() throws java.io.IOException JavaDoc {
101         // nothing to do
102
}
103     
104     /**
105      * Closes the output sub archive entry
106      */

107     public void closeEntry(AbstractArchive sub) throws java.io.IOException JavaDoc {
108         // nothing to do...
109
}
110     
111     /**
112      * Deletes the underlying jar file
113      */

114     public boolean delete() {
115         File JavaDoc f = new File JavaDoc(archiveUri);
116         if (f.exists()) {
117             return FileUtils.deleteFile(f);
118         }
119         return false;
120     }
121     
122     /**
123      * @return an Enumeration of entries for this archive
124      */

125     public Enumeration JavaDoc entries() {
126         // Deployment Plan are organized flatly,
127

128         if (elements==null) {
129             synchronized(this) {
130                 elements = new Vector JavaDoc();
131                 for (Enumeration JavaDoc e = jarFile.entries();e.hasMoreElements();) {
132                     ZipEntry JavaDoc ze = (ZipEntry JavaDoc) e.nextElement();
133                     if (!ze.isDirectory() && !ze.getName().equals(
134                             JarFile.MANIFEST_NAME)) {
135                         elements.add(ze.getName());
136                     }
137                 }
138             }
139         }
140
141         Vector JavaDoc entries = new Vector JavaDoc();
142         for (Enumeration JavaDoc e = elements.elements();e.hasMoreElements();) {
143             
144             String JavaDoc entryName = (String JavaDoc) e.nextElement();
145             
146             String JavaDoc mangledName = entryName;
147             String JavaDoc prefix = "META-INF/";
148             if (entryName.indexOf("sun-web.xml")!=-1) {
149                 prefix = "WEB-INF/";
150             }
151             if (subArchiveUri != null && entryName.startsWith(subArchiveUri)) {
152                 mangledName = mangledName.substring(subArchiveUri.length()+1);
153             }
154             if (entryName.endsWith(".dbschema")) {
155                 mangledName = mangledName.replaceAll("#", "/");
156             } else {
157                 mangledName = prefix + mangledName;
158             }
159             
160             if (subArchiveUri==null) {
161                 // top level archive
162
if ((entryName.indexOf(".jar.")!=-1) ||
163                     (entryName.indexOf(".war.")!=-1) ||
164                     (entryName.indexOf(".rar."))!=-1) {
165                     
166                     // this element is in a sub archive
167
continue;
168                 }
169                 entries.add(mangledName);
170             } else {
171                 // this is a sub archive
172
if (entryName.startsWith(subArchiveUri)) {
173                     entries.add(mangledName);
174                 }
175             }
176         }
177         return entries.elements();
178     }
179     
180     /**
181      * @return an Enumeration of entries not including entries
182      * from the subarchives
183      */

184     public Enumeration JavaDoc entries(java.util.Enumeration JavaDoc embeddedArchives) {
185         return entries();
186     }
187     
188     /**
189      * @return true if the underlying archive exists
190      */

191     public boolean exists() {
192         File JavaDoc f = new File JavaDoc(archiveUri);
193         return f.exists();
194     }
195     
196     /**
197      * @return the archive URI
198      */

199     public String JavaDoc getArchiveUri() {
200         return archiveUri;
201     }
202     
203     /**
204      * @return a sub archive giving the name
205      */

206     public AbstractArchive getEmbeddedArchive(String JavaDoc name) throws java.io.IOException JavaDoc {
207         if (jarFile==null) {
208             return null;
209         }
210         DeploymentPlanArchive dpArchive = new DeploymentPlanArchive();
211         dpArchive.jarFile = new JarFile JavaDoc(archiveUri);
212         dpArchive.archiveUri = archiveUri + File.separator + name;
213         dpArchive.subArchiveUri = name;
214         dpArchive.elements = elements;
215         return dpArchive;
216     }
217     
218     /**
219      * @return an input stream giving its entry name
220      */

221     public InputStream JavaDoc getEntry(String JavaDoc name) throws IOException JavaDoc {
222
223         // we are just interested in the file name, not the
224
// relative path
225
if (name.endsWith(".dbschema")) {
226             name = name.replaceAll("/", "#");
227         } else {
228             name = name.substring(name.lastIndexOf('/')+1);
229         }
230         
231         if (subArchiveUri==null) {
232             // we are at the "top level"
233

234             return getElement(name);
235         } else {
236             // we are in a sub archive...
237
// now let's mangle the name...
238
String JavaDoc mangledName = subArchiveUri + "." +
239                 name;
240             return getElement(mangledName);
241             
242         }
243     }
244     
245     private InputStream JavaDoc getElement(String JavaDoc name) throws IOException JavaDoc {
246         if (elements.contains(name)) {
247             return jarFile.getInputStream(jarFile.getEntry(name));
248         } else {
249             return null;
250         }
251     }
252     
253     /**
254      * @return the manifest
255      */

256     public java.util.jar.Manifest JavaDoc getManifest() throws java.io.IOException JavaDoc {
257         // no manifest in DeploymentPlan
258
return new Manifest JavaDoc();
259     }
260     
261     /**
262      * @return the URI for this archive
263      */

264     public java.net.URI JavaDoc getURI() {
265         File JavaDoc f = new File JavaDoc(archiveUri);
266         try {
267             return new URI JavaDoc("jar://" + f.toURI().getSchemeSpecificPart());
268         } catch(java.net.URISyntaxException JavaDoc e) {
269             return null;
270         }
271     }
272     
273     /**
274      * Add an entry to the archive
275      */

276     public java.io.OutputStream JavaDoc putNextEntry(String JavaDoc name) throws java.io.IOException JavaDoc {
277         // not supported for now.
278
throw new UnsupportedOperationException JavaDoc();
279     }
280     
281     /**
282      * rename the underlying archive
283      */

284     public boolean renameTo(String JavaDoc name) {
285         File JavaDoc f = new File JavaDoc(archiveUri);
286         boolean result = FileUtils.renameFile(f, new File JavaDoc(name));
287         if (result) {
288             archiveUri = name;
289         }
290         return result;
291         
292     }
293     
294 }
295
Popular Tags