KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_lib > genbase > archive > Application


1 /**
2  * JOnAS : Java(TM) OpenSource Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: Application.java,v 1.5 2005/04/28 16:53:00 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_lib.genbase.archive;
27
28 import java.io.File JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.net.URLClassLoader JavaDoc;
33 import java.util.Hashtable JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.List JavaDoc;
36 import java.util.Map JavaDoc;
37 import java.util.Vector JavaDoc;
38 import java.util.jar.Attributes JavaDoc;
39 import java.util.jar.JarFile JavaDoc;
40
41 import javax.xml.parsers.ParserConfigurationException JavaDoc;
42
43 import org.w3c.dom.Document JavaDoc;
44 import org.xml.sax.SAXException JavaDoc;
45
46 import org.objectweb.jonas_ear.deployment.api.EarDeploymentDesc;
47 import org.objectweb.jonas_ear.deployment.lib.EarDeploymentDescManager;
48 import org.objectweb.jonas_ear.deployment.xml.Web;
49
50 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
51 import org.objectweb.jonas_lib.files.FileUtils;
52 import org.objectweb.jonas_lib.files.FileUtilsException;
53 import org.objectweb.jonas_lib.genbase.GenBaseException;
54 import org.objectweb.jonas_lib.genbase.utils.TempRepository;
55 import org.objectweb.jonas_lib.genbase.utils.XMLUtils;
56 import org.objectweb.jonas_lib.loader.EjbJarClassLoader;
57
58 import org.objectweb.util.monolog.api.BasicLevel;
59
60 /**
61  * Application is a wrapper around an ear (packaged as a jar or as an unpacked
62  * jar).
63  *
64  * @author Guillaume Sauthier
65  */

66 public class Application extends J2EEArchive {
67
68     /** list of embded clients archive. */
69     private List JavaDoc clients;
70
71     /** list of embded webapps archive. */
72     private List JavaDoc webapps;
73
74     /** list of embded ejbjars archive. */
75     private List JavaDoc ejbjars;
76
77     /** Application DeploymentDesc */
78     private EarDeploymentDesc earDD = null;
79
80     /** name to document map */
81     private Map JavaDoc descriptors;
82
83     /** application Descriptor */
84     private Document JavaDoc app;
85
86     /** EJB Jar ClassLoader */
87     private URLClassLoader JavaDoc ejbCL = null;
88
89     /** Common Libs ClassLoader */
90     private URLClassLoader JavaDoc commonCL = null;
91
92     /** libs path File */
93     private List JavaDoc pathFiles;
94
95     /** application archive filename */
96     private String JavaDoc appFilename;
97
98     /**
99      * Creates a new Application archive.
100      *
101      * @param archive the file containing the application archive.
102      *
103      * @throws GenBaseException When Init fails
104      */

105     public Application(Archive archive) throws GenBaseException {
106         super(archive);
107         appFilename = archive.getRootFile().getName();
108         if (getLogger().isLoggable(BasicLevel.DEBUG)) {
109             getLogger().log(BasicLevel.DEBUG, "Wrapping '" + archive.getName() + "' in Application");
110         }
111         init();
112     }
113
114     /**
115      * Initialize the Application. Creates modules lists, unpack if not
116      * unpacked.
117      *
118      * @throws GenBaseException When application unpack fails or when Decriptors
119      * cannot be parsed or found.
120      */

121     protected void init() throws GenBaseException {
122
123         pathFiles = new Vector JavaDoc();
124
125         ejbjars = new Vector JavaDoc();
126         webapps = new Vector JavaDoc();
127         clients = new Vector JavaDoc();
128
129         if (isPacked()) {
130
131             try {
132                 // unpack data
133
JarFile JavaDoc jf = new JarFile JavaDoc(getArchive().getRootFile());
134                 TempRepository tr = TempRepository.getInstance();
135                 File JavaDoc unpacked = tr.createDir();
136                 FileUtils.unpack(jf, unpacked);
137                 jf.close();
138                 setArchive(new FileArchive(unpacked));
139             } catch (FileUtilsException fue) {
140                 String JavaDoc err = getI18n().getMessage("Application.init.unpackException", getArchive().getRootFile());
141                 throw new GenBaseException(err, fue);
142             } catch (IOException JavaDoc ioe) {
143                 String JavaDoc err = getI18n().getMessage("Application.init.unpackException", getArchive().getRootFile());
144                 throw new GenBaseException(err, ioe);
145             }
146         }
147
148         // load META-INF/application.xml
149
try {
150             earDD =
151                 EarDeploymentDescManager.getDeploymentDesc(getRootFile().getAbsolutePath(),
152                     Thread.currentThread().getContextClassLoader());
153         } catch (DeploymentDescException dde) {
154             String JavaDoc err =
155                 getI18n().getMessage("Application.init.earDDExc",
156                     getArchive().getRootFile());
157             throw new GenBaseException(err, dde);
158         }
159
160         // add EjbJars
161
String JavaDoc[] ejbs = earDD.getEjbTags();
162
163         for (int i = 0; i < ejbs.length; i++) {
164             File JavaDoc ejbFile = new File JavaDoc(getRootFile(), ejbs[i]);
165             Archive ejbArch = null;
166
167             if (ejbFile.isDirectory()) {
168                 // Unpacked Jar
169
ejbArch = new FileArchive(ejbFile);
170             } else {
171                 // Packed Jar
172
ejbArch = new JarArchive(ejbFile);
173             }
174             ejbjars.add(new EjbJar(ejbArch, this));
175
176             // add entries for Class-Path
177
addClassPathEntry(ejbArch);
178         }
179
180         // add WebApps
181
Web[] webs = earDD.getWebTags();
182
183         for (int i = 0; i < webs.length; i++) {
184             File JavaDoc webFile = new File JavaDoc(getRootFile(), webs[i].getWebUri());
185
186             if (webFile.isDirectory()) {
187                 // Unpacked Jar
188
webapps.add(new WebApp(new FileArchive(webFile), this));
189             } else {
190                 // Packed Jar
191
webapps.add(new WebApp(new JarArchive(webFile), this));
192             }
193         }
194
195         // add Clients
196
String JavaDoc[] clts = earDD.getClientTags();
197
198         for (int i = 0; i < clts.length; i++) {
199             File JavaDoc clientFile = new File JavaDoc(getRootFile(), clts[i]);
200
201             if (clientFile.isDirectory()) {
202                 // Unpacked Jar
203
clients.add(new Client(new FileArchive(clientFile), this));
204             } else {
205                 // Packed Jar
206
clients.add(new Client(new JarArchive(clientFile), this));
207             }
208         }
209
210         // Create ear ClassLoader (from MANIFEST/Class-Path general entry)
211
setModuleClassloader(createEARClassLoader());
212
213         // Create EJB ClassLoader
214
ejbCL = createEJBClassLoader();
215
216         loadDescriptors();
217     }
218
219     /**
220      * Load Deployment Descriptor of an Application.
221      *
222      * @throws GenBaseException When parsing of application.xml fails
223      */

224     protected void loadDescriptors() throws GenBaseException {
225         try {
226             app = XMLUtils.newDocument(getApplicationInputStream(), "META-INF/application.xml", isDTDsAllowed());
227         } catch (SAXException JavaDoc saxe) {
228             String JavaDoc err = getI18n().getMessage("Application.loadDescriptors.parseError");
229             throw new GenBaseException(err, saxe);
230         } catch (ParserConfigurationException JavaDoc pce) {
231             String JavaDoc err = getI18n().getMessage("Application.loadDescriptors.prepare");
232             throw new GenBaseException(err, pce);
233         } catch (IOException JavaDoc ioe) {
234             String JavaDoc err = getI18n().getMessage("Application.loadDescriptors.parseError");
235             throw new GenBaseException(err, ioe);
236         }
237
238         descriptors = new Hashtable JavaDoc();
239         descriptors.put("META-INF/application.xml", app);
240     }
241
242     /**
243      * Initialize the Archive.
244      * @throws GenBaseException When initialization fails.
245      */

246     public void initialize() throws GenBaseException {
247
248         // init ejbjars
249
for (Iterator JavaDoc i = ejbjars.iterator(); i.hasNext();) {
250             EjbJar ejb = (EjbJar) i.next();
251             ejb.initialize();
252         }
253
254         // init webapps
255
for (Iterator JavaDoc i = webapps.iterator(); i.hasNext();) {
256             WebApp web = (WebApp) i.next();
257             web.initialize();
258         }
259
260         // init clients
261
for (Iterator JavaDoc i = clients.iterator(); i.hasNext();) {
262             Client client = (Client) i.next();
263             client.initialize();
264         }
265
266     }
267
268     /**
269      * Returns the name of the Archive. Overrides J2EEArchive.getName();
270      *
271      * @see org.objectweb.jonas_lib.genbase.archive.J2EEArchive#getName()
272      *
273      * @return the name of the Archive.
274      */

275     public String JavaDoc getName() {
276         return appFilename;
277     }
278
279     /**
280      * Construct a ClassLoader for EJBs inside an application.
281      *
282      * @return the Ejb-Jar ClassLoader
283      *
284      * @throws GenBaseException When URLClassLoader cannot be created.
285      */

286     private URLClassLoader JavaDoc createEJBClassLoader() throws GenBaseException {
287
288         URL JavaDoc[] urls = new URL JavaDoc[pathFiles.size()];
289         int index = 0;
290         for (Iterator JavaDoc i = pathFiles.iterator(); i.hasNext(); index++) {
291             File JavaDoc f = (File JavaDoc) i.next();
292             try {
293                 urls[index] = f.toURL();
294             } catch (IOException JavaDoc ioe) {
295                 String JavaDoc err = "Cannot convert " + f + " to URL.";
296                 throw new GenBaseException(err, ioe);
297             }
298         }
299
300         commonCL = new URLClassLoader JavaDoc(urls, getModuleClassloader());
301
302         urls = new URL JavaDoc[ejbjars.size()];
303         index = 0;
304         for (Iterator JavaDoc i = ejbjars.iterator(); i.hasNext(); index++) {
305             try {
306                 urls[index] = ((EjbJar) i.next()).getRootFile().toURL();
307             } catch (IOException JavaDoc ioe) {
308                 String JavaDoc err = "Cannot transform as a URL : " + ioe.getMessage();
309                 throw new GenBaseException(err, ioe);
310             }
311         }
312
313         try {
314             return new EjbJarClassLoader(urls, commonCL);
315         } catch (IOException JavaDoc ioe) {
316             String JavaDoc err = "Cannot create EjbJarClassLoader";
317             throw new GenBaseException(err, ioe);
318         }
319
320     }
321
322     /**
323      * Construct a ClassLoader for the application.
324      *
325      * @return the Application ClassLoader
326      *
327      * @throws GenBaseException When URLClassLoader cannot be created.
328      */

329     private URLClassLoader JavaDoc createEARClassLoader() throws GenBaseException {
330
331         // find parent ClassLoader
332
ClassLoader JavaDoc parent = Thread.currentThread().getContextClassLoader();
333
334         // get Manifest Attributes if any
335
String JavaDoc classpath = getManifest().getMainAttributes().getValue(Attributes.Name.CLASS_PATH);
336         URL JavaDoc[] urls = new URL JavaDoc[0];
337         if (classpath != null) {
338             // Lookup specified files.
339
String JavaDoc[] paths = classpath.split(",");
340             urls = new URL JavaDoc[paths.length];
341             for (int i = 0; i < paths.length; i++) {
342                 try {
343                     URL JavaDoc path = new File JavaDoc(getRootFile(), paths[i]).toURL();
344                     urls[i] = path;
345                 } catch (IOException JavaDoc ioe) {
346                     String JavaDoc err = "Cannot transform '" + paths[i] + "' as a URL";
347                     throw new GenBaseException(err, ioe);
348                 }
349             }
350         }
351
352         return new URLClassLoader JavaDoc(urls, parent);
353     }
354
355     /**
356      * Search the given Archive for ClassPath Manifest entry and add the entries
357      * in the EAR classpath.
358      *
359      * @param a the Archive to explore
360      *
361      * @throws GenBaseException When a path cannot be added in the EAR classpath.
362      */

363     private void addClassPathEntry(Archive a) throws GenBaseException {
364         // get Manifest Attributes if any
365
String JavaDoc classpath = a.getManifest().getMainAttributes().getValue(Attributes.Name.CLASS_PATH);
366
367         if (classpath != null) {
368             // Lookup specified files.
369
String JavaDoc[] paths = classpath.split(",");
370             for (int i = 0; i < paths.length; i++) {
371                 try {
372                     File JavaDoc path = new File JavaDoc(a.getRootFile().getParentFile(), paths[i]).getCanonicalFile();
373                     if (!pathFiles.contains(path)) {
374                         pathFiles.add(path);
375                     }
376                 } catch (IOException JavaDoc ioe) {
377                     String JavaDoc err = "Cannot add in EAR classpath :" + paths[i];
378                     throw new GenBaseException(err, ioe);
379                 }
380             }
381
382         }
383
384     }
385
386     /**
387      * Returns the Document of the application.xml file.
388      *
389      * @return the Document of the application.xml file.
390      */

391     public Document JavaDoc getApplicationDoc() {
392         return app;
393     }
394
395     /**
396      * Returns the InputStream of the application.xml file.
397      *
398      * @return the InputStream of the application.xml file.
399      *
400      * @throws IOException When InputStream of application.xml cannot be
401      * returned
402      */

403     public InputStream JavaDoc getApplicationInputStream() throws IOException JavaDoc {
404         InputStream JavaDoc is = null;
405
406         if (isPacked()) {
407             is = getInputStream("META-INF/application.xml");
408         } else {
409             is = getInputStream("META-INF" + File.separator + "application.xml");
410         }
411
412         return is;
413     }
414
415     /**
416      * Add a new EjbJar in the Application.
417      *
418      * @param ejbjar the added EjbJar
419      */

420     public void addEjbJar(EjbJar ejbjar) {
421         ejbjars.add(ejbjar);
422         // add module in application.xml
423
XMLUtils.addEjb(app, ejbjar);
424     }
425
426     /**
427      * Add a new Client in the Application.
428      *
429      * @param client the added Client
430      */

431     public void addClient(Client client) {
432         clients.add(client);
433         // add module in application.xml
434
XMLUtils.addClient(app, client);
435     }
436
437     /**
438      * Add a new WebApp in the Application.
439      *
440      * @param webapp the added webapp
441      * @param context context of the webapp
442      */

443     public void addWebApp(WebApp webapp, String JavaDoc context) {
444         webapps.add(webapp);
445         // add module in application.xml
446
XMLUtils.addWebApp(app, webapp, context);
447         addFile(webapp.getRootFile(), webapp.getName());
448     }
449
450     /**
451      * Returns the Iterator of EjbJar contained in this Application.
452      *
453      * @return the Iterator of EjbJar contained in this Application.
454      */

455     public Iterator JavaDoc getEjbJars() {
456         return ejbjars.iterator();
457     }
458
459     /**
460      * Returns the Iterator of WebApp contained in this Application.
461      *
462      * @return the Iterator of WebApp contained in this Application.
463      */

464     public Iterator JavaDoc getWebApps() {
465         return webapps.iterator();
466     }
467
468     /**
469      * Returns the Iterator of WebApp contained in this Application.
470      *
471      * @return the Iterator of WebApp contained in this Application.
472      */

473     public Iterator JavaDoc getClients() {
474         return clients.iterator();
475     }
476
477     /**
478      * Returns the ClassLoader of this ear archive.
479      *
480      * @return the ClassLoader of this ear archive.
481      */

482     public URLClassLoader JavaDoc getEARClassLoader() {
483         return commonCL;
484     }
485
486     /**
487      * Returns the ClassLoader of the ejbs within this archive.
488      *
489      * @return the ClassLoader of the ejbs within this archive.
490      */

491     public URLClassLoader JavaDoc getEJBClassLoader() {
492         return ejbCL;
493     }
494
495     /**
496      * Returns a Map of name to Document for each modified Descriptor of the
497      * archive.
498      *
499      * @return a Map of name to Document
500      */

501     public Map JavaDoc getDescriptors() {
502         return descriptors;
503     }
504
505     /**
506      * Returns true if filename must be omitted in the archive.
507      *
508      * @param name filename to be tested
509      *
510      * @return true if filename must be omitted.
511      */

512     public boolean omit(String JavaDoc name) {
513         return (name.equals("META-INF/application.xml") || name.equals("META-INF\\application.xml"));
514     }
515
516     /**
517      * @param clients The clients to set.
518      */

519     public void setClients(List JavaDoc clients) {
520         this.clients = clients;
521     }
522
523     /**
524      * @param ejbjars The ejbjars to set.
525      */

526     public void setEjbjars(List JavaDoc ejbjars) {
527         this.ejbjars = ejbjars;
528     }
529
530     /**
531      * @param webapps The webapps to set.
532      */

533     public void setWebapps(List JavaDoc webapps) {
534         this.webapps = webapps;
535     }
536
537     /**
538      * @return Returns the app.
539      */

540     public Document JavaDoc getApp() {
541         return app;
542     }
543
544     /**
545      * Close this archive
546      */

547     public void close() {
548         super.close();
549         // Remove ear deployment desc
550
earDD = null;
551         // reset fields
552
descriptors = null;
553         app = null;
554         ejbCL = null;
555         commonCL = null;
556         pathFiles = null;
557
558         // reset
559
for (Iterator JavaDoc i = getEjbJars(); i.hasNext();) {
560             EjbJar ejbjar = (EjbJar) i.next();
561             ejbjar.close();
562         }
563
564         // fill webapp list
565
for (Iterator JavaDoc i = getWebApps(); i.hasNext();) {
566             WebApp webapp = (WebApp) i.next();
567             webapp.close();
568         }
569
570         // fill client list
571
for (Iterator JavaDoc i = getClients(); i.hasNext();) {
572             Client client = (Client) i.next();
573             client.close();
574         }
575
576         // reset internal lists
577
clients = null;
578         webapps = null;
579         ejbjars = null;
580
581
582     }
583 }
Popular Tags