KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > tool > archive > EarBuilder


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  * Paul Mahar
22  *
23  */

24 package org.enhydra.tool.archive;
25
26 // Build
27
//import org.enhydra.ant.taskdefs.EJBClient;
28

29 // ToolBox
30
import org.enhydra.tool.common.PathHandle;
31 import org.enhydra.tool.common.FileUtil;
32 import org.enhydra.tool.common.ResUtil;
33 import org.enhydra.tool.archive.Descriptor;
34 import org.enhydra.tool.archive.xml.EarDescriptorHandler;
35
36 // JDK
37
import java.io.BufferedInputStream JavaDoc;
38 import java.io.BufferedOutputStream JavaDoc;
39 import java.io.ByteArrayInputStream JavaDoc;
40 import java.io.ByteArrayOutputStream JavaDoc;
41 import java.io.InputStream JavaDoc;
42 import java.io.IOException JavaDoc;
43 import java.io.File JavaDoc;
44 import java.io.FileInputStream JavaDoc;
45 import java.io.FileOutputStream JavaDoc;
46 import java.util.Arrays JavaDoc;
47 import java.util.ArrayList JavaDoc;
48 import java.util.Enumeration JavaDoc;
49 import java.util.ResourceBundle JavaDoc;
50 import java.util.jar.Attributes JavaDoc;
51 import java.util.jar.JarFile JavaDoc;
52 import java.util.jar.JarEntry JavaDoc;
53 import java.util.jar.JarInputStream JavaDoc;
54 import java.util.jar.JarOutputStream JavaDoc;
55 import java.util.jar.Manifest JavaDoc;
56
57 //
58
public class EarBuilder extends JarBuilder {
59
60     // not to be resourced
61
private final String JavaDoc APPLICATION_XML =
62         "META-INF/application.xml"; // nores
63
private InputStream JavaDoc[] mergeDescriptors = new InputStream JavaDoc[0];
64     private PathHandle tmpDir = null;
65
66     //
67
//
68
public EarBuilder() {}
69
70     public EarPlan getEarPlan() {
71         EarPlan ep = null;
72
73         if (getPlan() instanceof EarPlan) {
74             ep = (EarPlan) getPlan();
75         }
76         return ep;
77     }
78
79     public File JavaDoc buildArchive() throws ArchiveException {
80         File JavaDoc jar = null;
81         String JavaDoc root = null;
82         File JavaDoc[] files = new File JavaDoc[0];
83         Descriptor[] dd = new Descriptor[0];
84         boolean userAppDD = false;
85
86         root = getPlan().getClassRoot();
87         dd = getPlan().getDescriptors();
88         files = getPlan().getClassFileArray();
89         jar = openJarStream(getEarPlan().getArchivePath());
90         addModules();
91         addLibraries();
92         for (int i = 0; i < dd.length; i++) {
93             addDescriptor(dd[i], root, files);
94             if (dd[i].getArchivePath().equalsIgnoreCase(APPLICATION_XML)) {
95                 userAppDD = true;
96             }
97         }
98         if (!userAppDD) {
99             addApplicationXML();
100         }
101         closeJarStream();
102         cleanTmp();
103         return jar;
104     }
105
106     protected Manifest JavaDoc buildManifest(Manifest JavaDoc m) {
107         Manifest JavaDoc manifest = super.buildManifest(m);
108         Boolean JavaDoc b = null;
109         String JavaDoc[] eMods = new String JavaDoc[0];
110         Descriptor[] metaDD = new Descriptor[0];
111         WebApplication[] webApps = new WebApplication[0];
112
113         // app name
114
manifest.getMainAttributes().put(new Attributes.Name JavaDoc(APP_NAME),
115                                          getEarPlan().getAppName());
116
117         // description
118
manifest.getMainAttributes().put(new Attributes.Name JavaDoc(APP_DESC),
119                                          getEarPlan().getDescription());
120
121         // copy client
122
b = new Boolean JavaDoc(getEarPlan().isCopyClient());
123         manifest.getMainAttributes().put(new Attributes.Name JavaDoc(COPY_CLIENT),
124                                          b.toString());
125
126         // alt dd
127
b = new Boolean JavaDoc(getEarPlan().isAlt());
128         manifest.getMainAttributes().put(new Attributes.Name JavaDoc(ALT_DD),
129                                          b.toString());
130
131         // enterprise modules
132
eMods = getEarPlan().getEnterpriseModules();
133         for (int i = 0; i < eMods.length; i++) {
134             manifest.getMainAttributes().put(new Attributes.Name JavaDoc(ENTERPRISE_MODULE
135                     + i), eMods[i]);
136         }
137
138         // web apps
139
webApps = getEarPlan().getWebApplications();
140         for (int i = 0; i < webApps.length; i++) {
141             manifest.getMainAttributes().put(new Attributes.Name JavaDoc(WEBAPP_MODULE
142                     + i), webApps[i].getArchive());
143             manifest.getMainAttributes().put(new Attributes.Name JavaDoc(WEBAPP_CONTEXT
144                     + i), webApps[i].getContextRoot());
145         }
146
147         // meta dd
148
metaDD = getEarPlan().getDescriptors();
149         for (int i = 0; i < metaDD.length; i++) {
150             manifest.getMainAttributes().put(new Attributes.Name JavaDoc(APP_META
151                     + i), metaDD[i].getPath());
152         }
153         return manifest;
154     }
155
156     private void addModules() throws ArchiveException {
157         Module[] mods = getEarPlan().getAllModules();
158
159         if (getEarPlan().isCopyClient()) {
160             mods = copyClients(mods);
161         }
162         for (int i = 0; i < mods.length; i++) {
163             addModule(mods[i]);
164         }
165     }
166
167     private void addLibraries() throws ArchiveException {
168         File JavaDoc[] files = getEarPlan().getLibFileArray();
169
170         for (int i = 0; i < files.length; i++) {
171             addLibFile(files[i]);
172         }
173     }
174
175     private void extractModules(Module mod) throws ArchiveException {
176         JarFile JavaDoc jar = null;
177         JarEntry JavaDoc entry = null;
178         InputStream JavaDoc stream = null;
179         Enumeration JavaDoc entries = null;
180         ArrayList JavaDoc list = null;
181
182         try {
183             jar = new JarFile JavaDoc(new File JavaDoc(mod.getArchive()));
184             entries = jar.entries();
185             while (entries.hasMoreElements()) {
186                 entry = (JarEntry JavaDoc) entries.nextElement();
187                 if (isJar(entry.getName())) {
188                     getEarPlan().addEnterpriseModule(entry.getName());
189                     stream = jar.getInputStream(entry);
190                     createEntry(stream, entry.getName());
191                     stream = jar.getInputStream(entry);
192                     extractDD(new JarInputStream JavaDoc(stream), entry.getName());
193                 } else if (isWar(entry.getName())) {
194                     getEarPlan().addWebApplication(entry.getName());
195                     stream = jar.getInputStream(entry);
196                     createEntry(stream, entry.getName());
197                     stream = jar.getInputStream(entry);
198                     extractDD(new JarInputStream JavaDoc(stream), entry.getName());
199                 }
200             }
201             entry = jar.getJarEntry(APPLICATION_XML);
202             list = new ArrayList JavaDoc(Arrays.asList(mergeDescriptors));
203             list.add(jar.getInputStream(entry));
204             list.trimToSize();
205             mergeDescriptors = new InputStream JavaDoc[list.size()];
206             mergeDescriptors = (InputStream JavaDoc[]) list.toArray(mergeDescriptors);
207             list.clear();
208         } catch (IOException JavaDoc e) {
209             throw new ArchiveException(e,
210                                        "Unable to extract modules from: "
211                                        + mod.getName());
212         }
213     }
214
215     private void extractDD(JarInputStream JavaDoc stream,
216                            String JavaDoc jarName) throws ArchiveException {
217         JarEntry JavaDoc entry = null;
218         String JavaDoc dd = new String JavaDoc();
219         StringBuffer JavaDoc entryPath = new StringBuffer JavaDoc();
220         ByteArrayOutputStream JavaDoc outStream = null;
221
222         //
223
if (isWar(jarName)) {
224             dd = Descriptor.getArchivePath(Descriptor.WEB);
225         } else {
226             dd = Descriptor.getArchivePath(Descriptor.EJB);
227         }
228         entryPath.append(jarName);
229         entryPath.append('/');
230         entryPath.append(dd);
231         try {
232             entry = stream.getNextJarEntry();
233             while (entry != null) {
234                 if (entry.getName().equalsIgnoreCase(dd)) {
235                     byte[] bytes = new byte[0];
236
237                     outStream = new ByteArrayOutputStream JavaDoc();
238                     FileUtil.copy(stream, outStream);
239                     bytes = outStream.toByteArray();
240                     outStream.close();
241                     createEntry(new ByteArrayInputStream JavaDoc(bytes),
242                                 entryPath.toString());
243                     break;
244                 }
245                 entry = stream.getNextJarEntry();
246             }
247             stream.close();
248         } catch (IOException JavaDoc e) {
249             throw new ArchiveException(e,
250                                        "Unable to extract deployment descriptor from: "
251                                        + jarName);
252         }
253     }
254
255     private void extractDD(Module mod) throws ArchiveException {
256         JarFile JavaDoc jar = null;
257         JarEntry JavaDoc entry = null;
258         InputStream JavaDoc stream = null;
259         String JavaDoc dd = new String JavaDoc();
260         StringBuffer JavaDoc entryPath = new StringBuffer JavaDoc();
261
262         //
263
if (isWar(mod.getName())) {
264             dd = Descriptor.getArchivePath(Descriptor.WEB);
265         } else {
266             dd = Descriptor.getArchivePath(Descriptor.EJB);
267         }
268         entryPath.append(mod.getName());
269         entryPath.append('/');
270         entryPath.append(dd);
271         try {
272             jar = new JarFile JavaDoc(mod.getArchive());
273             entry = jar.getJarEntry(dd);
274             stream = jar.getInputStream(entry);
275             createEntry(stream, entryPath.toString());
276         } catch (NullPointerException JavaDoc e) {
277             throw new ArchiveException(e,
278                                        "Unable to extract deployment descriptor from: "
279                                        + mod.getName());
280         } catch (IOException JavaDoc e) {
281             throw new ArchiveException(e,
282                                        "Unable to extract deployment descriptor from: "
283                                        + mod.getName());
284         }
285     }
286
287     private boolean isWar(String JavaDoc path) {
288         return path.toString().toLowerCase().endsWith(".war");
289     }
290
291     private boolean isJar(String JavaDoc path) {
292         return path.toString().toLowerCase().endsWith(".jar");
293     }
294
295     private boolean isEar(String JavaDoc path) {
296         return path.toString().toLowerCase().endsWith(".ear");
297     }
298
299     private void addModule(Module mod) throws ArchiveException {
300         String JavaDoc entryPath = new String JavaDoc();
301
302         //
303
entryPath = mod.getName();
304         if (getEarPlan().isAlt()) {
305             if (isWar(entryPath)) {
306                 extractDD(mod);
307             } else if (isJar(entryPath)) {
308                 extractDD(mod);
309             } else if (isEar(entryPath)) {
310                 extractModules(mod);
311             }
312         }
313         if (!isEar(entryPath)) {
314
315             //
316
try {
317                 createEntry(new File JavaDoc(mod.getArchive()), entryPath);
318             } catch (IOException JavaDoc e) {
319                 String JavaDoc exMess = res.getString("EarBuilder_Unable_to");
320
321                 exMess = ResUtil.format(exMess, mod.getName());
322                 throw new ArchiveException(e, exMess);
323             }
324         }
325     }
326
327     private void addLibFile(File JavaDoc source) throws ArchiveException {
328         StringBuffer JavaDoc entryPath = new StringBuffer JavaDoc();
329
330         //
331
entryPath.append("lib");
332         entryPath.append('/');
333         entryPath.append(source.getName());
334
335         //
336
try {
337             createEntry(source, entryPath.toString());
338         } catch (IOException JavaDoc e) {
339             String JavaDoc exMess = res.getString("EarBuilder_Unable_to");
340
341             exMess = ResUtil.format(exMess, source);
342             throw new ArchiveException(e, exMess);
343         }
344     }
345
346     private void addApplicationXML() throws ArchiveException {
347         EarDescriptorHandler prep = new EarDescriptorHandler();
348         StringBuffer JavaDoc entryPath = new StringBuffer JavaDoc();
349         String JavaDoc classRoot = null;
350         String JavaDoc relative = null;
351
352         prep.setAppName(getEarPlan().getAppName());
353         prep.setDescription(getEarPlan().getDescription());
354         prep.setAllModules(getEarPlan().getAllModules());
355         prep.setAlt(getEarPlan().isAlt());
356         prep.setMergeDescriptors(mergeDescriptors);
357         prep.prep();
358         entryPath.append(APPLICATION_XML);
359         try {
360             createEntry(prep.getInputStream(), entryPath.toString());
361         } catch (IOException JavaDoc e) {
362             throw new ArchiveException(e,
363                                        ResUtil.format(res.getString("EjbBuilder_Unable_to2"),
364                                                       APPLICATION_XML));
365         }
366     }
367
368     private Module[] copyClients(Module[] mods) {
369         ArrayList JavaDoc list = new ArrayList JavaDoc();
370         String JavaDoc[] clients = new String JavaDoc[0];
371         PathHandle ph = null;
372 // EJBClient task = new EJBClient();
373

374         try {
375             tmpDir = getTempDir();
376
377             // create clients
378
for (int i = 0; i < mods.length; i++) {
379                 ph = PathHandle.createPathHandle(mods[i].getArchive());
380                 if (ph.isFile() && ph.hasExtension("jar")) {
381 // task.setJarname(ph.getPath());
382
ph = createTempClientPH(ph);
383 // task.setClientjarname(ph.getPath());
384
// task.execute();
385
if (ph.isFile()) {
386                         list.add(ph.getPath());
387                     }
388                 }
389             }
390             list.trimToSize();
391             clients = new String JavaDoc[list.size()];
392             clients = (String JavaDoc[]) list.toArray(clients);
393             list.clear();
394             if (clients.length > 0) {
395
396                 // add clients to wars
397
for (int i = 0; i < mods.length; i++) {
398                     if (mods[i] instanceof WebApplication) {
399                         ph = addClients(mods[i], clients);
400                         if (ph.isFile()) {
401                             mods[i].setArchive(ph.getPath());
402                         }
403                     }
404                 }
405             }
406 // task = null;
407
ph = null;
408
409             // delete clients
410
for (int i = 0; i < clients.length; i++) {
411                 File JavaDoc client = new File JavaDoc(clients[i]);
412
413                 clients[i] = null;
414                 if (client.isFile()) {
415                     sleep(10);
416                     client.deleteOnExit();
417                     client.delete();
418                     client = null;
419                 }
420             }
421         } catch (IOException JavaDoc e) {
422             e.printStackTrace(System.err);
423         }
424         return mods;
425     }
426
427     private PathHandle addClients(Module mod,
428                                   String JavaDoc[] clients) throws IOException JavaDoc {
429         Enumeration JavaDoc entries = null;
430         PathHandle ph = null;
431         JarFile JavaDoc source = null;
432         JarOutputStream JavaDoc out = null;
433         File JavaDoc dest = null;
434         Manifest JavaDoc manifest = null;
435
436         ph = PathHandle.createPathHandle(mod.getArchive());
437         dest = File.createTempFile("tmp", ".kelp", tmpDir.getFile());
438         source = new JarFile JavaDoc(ph.getFile());
439         manifest = source.getManifest();
440         out = new JarOutputStream JavaDoc(new FileOutputStream JavaDoc(dest));
441         entries = source.entries();
442         while (entries.hasMoreElements()) {
443             JarEntry JavaDoc entry = null;
444
445             entry = (JarEntry JavaDoc) entries.nextElement();
446             out.putNextEntry(entry);
447             FileUtil.copy(source.getInputStream(entry), out);
448             out.flush();
449             out.closeEntry();
450         }
451         for (int i = 0; i < clients.length; i++) {
452             JarEntry JavaDoc entry = null;
453             FileInputStream JavaDoc in = null;
454
455             ph = PathHandle.createPathHandle(clients[i]);
456             in = new FileInputStream JavaDoc(ph.getFile());
457             ph.setExtension("jar");
458             entry = new JarEntry JavaDoc("lib/" + ph.getFile().getName());
459             entry.setTime(System.currentTimeMillis());
460             out.putNextEntry(entry);
461             FileUtil.copy(in, out);
462             in.close();
463             out.flush();
464             out.closeEntry();
465             in = null;
466         }
467         source.close();
468         out.flush();
469         out.close();
470         return PathHandle.createPathHandle(dest);
471     }
472
473     private PathHandle getTempDir() throws IOException JavaDoc {
474         File JavaDoc tmp = null;
475
476         tmp = File.createTempFile("tmp", ".kelp");
477         return PathHandle.createPathHandle(tmp.getParentFile());
478     }
479
480     private PathHandle createTempClientPH(PathHandle ejb) throws IOException JavaDoc {
481         File JavaDoc tmpFile = null;
482         PathHandle ph = null;
483         String JavaDoc tmpName = ejb.getFile().getName();
484
485         tmpName = tmpName.substring(0, tmpName.indexOf('.'));
486         tmpName = tmpName + "Client";
487         tmpFile = File.createTempFile(tmpName, ".kelp", tmpDir.getFile());
488         ph = PathHandle.createPathHandle(tmpFile);
489         tmpFile.delete();
490         return ph;
491     }
492
493     private void cleanTmp() {
494         PathHandle ph;
495
496         if (tmpDir != null) {
497             sleep(10);
498             File JavaDoc[] files = tmpDir.getFile().listFiles();
499
500             sleep(10);
501             for (int i = 0; i < files.length; i++) {
502                 ph = PathHandle.createPathHandle(files[i]);
503                 if (ph.hasExtension("kelp")) {
504                     ph.getFile().deleteOnExit();
505                     ph.getFile().delete();
506                 }
507                 sleep(10);
508             }
509             sleep(10);
510             tmpDir = null;
511         }
512     }
513
514     private void sleep(int m) {
515         try {
516             Thread.sleep(m);
517         } catch (InterruptedException JavaDoc e) {
518             e.printStackTrace(System.err);
519         }
520     }
521
522 }
523
Popular Tags