KickJava   Java API By Example, From Geeks To Geeks.

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


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: WebApp.java,v 1.9 2005/07/26 13:04:41 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.util.Hashtable JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Vector JavaDoc;
35 import java.util.jar.JarFile JavaDoc;
36
37 import javax.xml.parsers.ParserConfigurationException JavaDoc;
38
39 import org.w3c.dom.Document JavaDoc;
40 import org.xml.sax.SAXException JavaDoc;
41
42 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
43 import org.objectweb.jonas_lib.deployment.api.EjbRefDesc;
44 import org.objectweb.jonas_lib.files.FileUtils;
45 import org.objectweb.jonas_lib.files.FileUtilsException;
46 import org.objectweb.jonas_lib.genbase.GenBaseException;
47 import org.objectweb.jonas_lib.genbase.utils.TempRepository;
48 import org.objectweb.jonas_lib.genbase.utils.XMLUtils;
49 import org.objectweb.jonas_lib.loader.WebappClassLoader;
50
51 import org.objectweb.jonas_web.deployment.api.WebContainerDeploymentDesc;
52 import org.objectweb.jonas_web.deployment.lib.WebDeploymentDescManager;
53
54 import org.objectweb.jonas_ws.deployment.api.ServiceRefDesc;
55 import org.objectweb.jonas_ws.deployment.api.WSDeploymentDesc;
56 import org.objectweb.jonas_ws.deployment.lib.WSDeploymentDescManager;
57
58 import org.objectweb.util.monolog.api.BasicLevel;
59
60 /**
61  * A <code>WebApp</code> is a wrapper class around a Web Archive.
62  *
63  * @author Guillaume Sauthier
64  */

65 public class WebApp extends J2EEArchive implements EjbRefModule, WsClient, WsEndpoint {
66
67     /** Application containing the webapp */
68     private Application app = null;
69
70     /** webapp archive filename */
71     private String JavaDoc webFilename;
72
73     /** Web deployment descriptor */
74     private WebContainerDeploymentDesc webDD;
75
76     /** WebServices deployment descriptor */
77     private WSDeploymentDesc wsDD = null;
78
79     /** service-ref list */
80     private List JavaDoc sRefs;
81
82     /** web-app */
83     private Document JavaDoc webApp = null;
84
85     /** jonas-web-app */
86     private Document JavaDoc jonasWebApp = null;
87
88     /** webservices */
89     private Document JavaDoc webservices = null;
90
91     /** context */
92     private Document JavaDoc context = null;
93
94     /** webjetty */
95     private Document JavaDoc webjetty = null;
96
97     /** web descriptors */
98     private Map JavaDoc descriptors;
99
100     /**
101      * ejb-ref list
102      */

103     private List JavaDoc ejbRefs;
104
105     /**
106      * Create an alone WebApp (not in an ear).
107      *
108      * @param archive file archive
109      *
110      * @throws GenBaseException When Init fails
111      */

112     public WebApp(Archive archive) throws GenBaseException {
113         super(archive);
114         webFilename = archive.getName();
115         if (getLogger().isLoggable(BasicLevel.DEBUG)) {
116             getLogger().log(BasicLevel.DEBUG, "Wrapping '" + archive.getName() + "' in WebApp");
117         }
118         init();
119     }
120
121     /**
122      * Create an embded WebApp.
123      *
124      * @param archive Web Archive
125      * @param app container application
126      *
127      * @throws GenBaseException When init fails
128      */

129     public WebApp(Archive archive, Application app) throws GenBaseException {
130         super(archive);
131         webFilename = archive.getName();
132         setApplication(app);
133         if (getLogger().isLoggable(BasicLevel.DEBUG)) {
134             getLogger().log(BasicLevel.DEBUG, "Wrapping '" + archive.getName() + "' in WebApp");
135         }
136         init();
137     }
138
139     /**
140      * Initialize the WebApp module.
141      *
142      * @throws GenBaseException When classloader cannot be created or when
143      * decriptor loading fails.
144      */

145     private void init() throws GenBaseException {
146         // load Deployment Descs
147
loadDescriptors();
148     }
149
150     /**
151      * Load Deployment Descriptor of a WebApp.
152      *
153      * @throws GenBaseException When descriptor cannot be parsed
154      */

155     private void loadDescriptors() throws GenBaseException {
156         try {
157             webApp = XMLUtils.newDocument(getWebInputStream(), "WEB-INF/web.xml", isDTDsAllowed());
158
159             // jonas-web.xml (optionnal)
160
InputStream JavaDoc is = getJonasWebInputStream();
161
162             if (is != null) {
163                 jonasWebApp = XMLUtils.newDocument(is, "WEB-INF/jonas-web.xml", isDTDsAllowed());
164             }
165
166             // webservices.xml (optionnal)
167
InputStream JavaDoc isWS = getWebservicesInputStream();
168
169             if (isWS != null) {
170                 webservices = XMLUtils.newDocument(isWS, "WEB-INF/webservices.xml", isDTDsAllowed());
171             }
172
173             // context.xml (optional)
174
InputStream JavaDoc isContext = getContextInputStream();
175             if (isContext != null) {
176                 context = XMLUtils.newDocument (isContext, "META-INF/context.xml", isDTDsAllowed(), false);
177             }
178
179             //web-jetty.xml (optional)
180
InputStream JavaDoc isWebJetty = getWebJettyInputStream();
181             if (isWebJetty != null) {
182                 // Jetty needs web-jetty.xml to have a dtd
183
webjetty = XMLUtils.newDocument (isWebJetty, "WEB-INF/web-jetty.xml", true, true);
184             }
185
186         } catch (SAXException JavaDoc saxe) {
187             String JavaDoc err = getI18n().getMessage("WebApp.loadDescriptors.parseError");
188             throw new GenBaseException(err, saxe);
189         } catch (ParserConfigurationException JavaDoc pce) {
190             String JavaDoc err = getI18n().getMessage("WebApp.loadDescriptors.prepare");
191             throw new GenBaseException(err, pce);
192         } catch (IOException JavaDoc ioe) {
193             String JavaDoc err = getI18n().getMessage("WebApp.loadDescriptors.parseError");
194             throw new GenBaseException(err, ioe);
195         }
196
197         descriptors = new Hashtable JavaDoc();
198         descriptors.put("WEB-INF/web.xml", webApp);
199
200         if (jonasWebApp != null) {
201             descriptors.put("WEB-INF/jonas-web.xml", jonasWebApp);
202         }
203
204         if (webservices != null) {
205             descriptors.put("WEB-INF/webservices.xml", webservices);
206         }
207
208         if (context != null) {
209             descriptors.put("META-INF/context.xml", context);
210         }
211
212         if (webjetty != null) {
213             descriptors.put("WEB-INF/web-jetty.xml", webjetty);
214         }
215     }
216
217     /**
218      * Returns the name of the Archive. Overrides J2EEArchive.getName();
219      *
220      * @see org.objectweb.jonas_lib.genbase.archive.J2EEArchive#getName()
221      *
222      * @return the name of the Archive.
223      */

224     public String JavaDoc getName() {
225         return webFilename;
226     }
227
228     /**
229      * Set the container application.
230      *
231      * @param app the container application.
232      */

233     public void setApplication(Application app) {
234         this.app = app;
235     }
236
237     /**
238      * Returns the container application (can be null).
239      *
240      * @return the container application (can be null).
241      */

242     public Application getApplication() {
243         return app;
244     }
245
246     /**
247      * Returns the list of service-ref elements contained by a module.
248      *
249      * @return the list of service-ref elements contained by a module.
250      */

251     public List JavaDoc getServiceRefDescs() {
252         return sRefs;
253     }
254
255     /**
256      * Returns the list of webservice-description elements contained by a
257      * module.
258      *
259      * @return the list of webservice-description elements contained by a
260      * module.
261      */

262     public List JavaDoc getServiceDescs() {
263         if (wsDD != null) {
264             return wsDD.getServiceDescs();
265         } else {
266             return new Vector JavaDoc();
267         }
268     }
269
270     /**
271      * Add Archive classes.
272      *
273      * @param classes root directory containing classes.
274      */

275     public void addClasses(File JavaDoc classes) {
276         addDirectoryIn("WEB-INF/classes", classes);
277     }
278
279     /**
280      * Returns the Document of the web.xml file.
281      *
282      * @return the Document of the web.xml file.
283      */

284     public Document JavaDoc getWebAppDoc() {
285         return webApp;
286     }
287
288     /**
289      * Returns the Document of the jonas-web.xml file.
290      *
291      * @return the Document of the jonas-web.xml file.
292      */

293     public Document JavaDoc getJonasWebAppDoc() {
294         return jonasWebApp;
295     }
296
297     /**
298      * Returns the Document of the webservices.xml file.
299      *
300      * @return the Document of the webservices.xml file.
301      */

302     public Document JavaDoc getWebservicesDoc() {
303         return webservices;
304     }
305
306     /**
307      * Returns the Document of the context.xml file
308      *
309      * @return the Document of the context.xml file.
310      */

311     public Document JavaDoc getContextDoc() {
312         return context;
313     }
314
315     /**
316      * Returns the Document of the web-=jetty.xml file
317      *
318      * @return the document of the web-jetty.xml file
319      */

320     public Document JavaDoc getWebJettyDoc() {
321         return webjetty;
322     }
323
324     /**
325      * Returns the InputStream of the web.xml file.
326      *
327      * @return the InputStream of the web.xml file.
328      *
329      * @throws IOException When InputStream cannot be returned.
330      */

331     private InputStream JavaDoc getWebInputStream() throws IOException JavaDoc {
332         InputStream JavaDoc is = null;
333
334         if (isPacked()) {
335             is = getInputStream("WEB-INF/web.xml");
336         } else {
337             is = getInputStream("WEB-INF" + File.separator + "web.xml");
338         }
339
340         return is;
341     }
342
343     /**
344      * Returns the InputStream of the jonas-web.xml file.
345      *
346      * @return the InputStream of the jonas-web.xml file.
347      *
348      * @throws IOException When InputStream cannot be returned.
349      */

350     private InputStream JavaDoc getJonasWebInputStream() throws IOException JavaDoc {
351         InputStream JavaDoc is = null;
352
353         if (isPacked()) {
354             is = getInputStream("WEB-INF/jonas-web.xml");
355         } else {
356             is = getInputStream("WEB-INF" + File.separator + "jonas-web.xml");
357         }
358
359         return is;
360     }
361
362     /**
363      * Returns the InputStream of the webservices.xml file.
364      *
365      * @return the InputStream of the webservices.xml file.
366      *
367      * @throws IOException When InputStream cannot be returned.
368      */

369     private InputStream JavaDoc getWebservicesInputStream() throws IOException JavaDoc {
370         InputStream JavaDoc is = null;
371
372         if (isPacked()) {
373             is = getInputStream("WEB-INF/webservices.xml");
374         } else {
375             is = getInputStream("WEB-INF" + File.separator + "webservices.xml");
376         }
377
378         return is;
379     }
380
381     /**
382      * Returns the InputStream of the context.xml file.
383      *
384      * @return the InputStream of the context.xml file.
385      * @throws IOException When InputStream cannot be returned
386      */

387     private InputStream JavaDoc getContextInputStream() throws IOException JavaDoc {
388         InputStream JavaDoc is = null;
389         if (isPacked()) {
390             is = getInputStream("META-INF/context.xml");
391         } else {
392             is = getInputStream("META-INF" + File.separator + "context.xml");
393         }
394
395         return is;
396     }
397
398     /**
399      * Returns the InputStream of the web-jetty.xml file
400      *
401      * @return the InputStream of the web-jetty.xml file
402      * @throws IOException When InputStream cannot be returned
403      */

404     private InputStream JavaDoc getWebJettyInputStream() throws IOException JavaDoc {
405         InputStream JavaDoc is = null;
406         if (isPacked()) {
407             is = getInputStream("WEB-INF/web-jetty.xml");
408         } else {
409             is = getInputStream("WEB-INF" + File.separator + "web-jetty.xml");
410         }
411
412         return is;
413     }
414
415     /**
416      * Returns a Map of name to Document for each modified Descriptor of the
417      * archive.
418      *
419      * @return a Map of name to Document
420      */

421     public Map JavaDoc getDescriptors() {
422         return descriptors;
423     }
424
425     /**
426      * Returns true if filename must be omitted in the archive.
427      *
428      * @param name filename to be tested
429      *
430      * @return true if filename must be omitted.
431      */

432     public boolean omit(String JavaDoc name) {
433         return (name.equals("WEB-INF/web.xml") || name.equals("WEB-INF\\web.xml")
434                 || name.equals("WEB-INF/jonas-web.xml") || name.equals("WEB-INF\\jonas-web.xml")
435                 || name.equals("WEB-INF/webservices.xml") || name.equals("WEB-INF\\webservices.xml")
436                 || name.equals("META-INF/context.xml") || name.equals("META-INF\\context.xml")
437                 || name.equals("WEB-INF/web-jetty.xml") || name.equals("WEB-INF\\web-jetty.xml"));
438     }
439
440     /**
441      * Initialize the Archive.
442      * @throws GenBaseException When initialization fails.
443      */

444     public void initialize() throws GenBaseException {
445         File JavaDoc webappUnpackDir = null;
446         try {
447
448             if (getArchive().isPacked()) {
449                 JarFile JavaDoc jf = new JarFile JavaDoc(getRootFile());
450                 TempRepository tr = TempRepository.getInstance();
451                 webappUnpackDir = tr.createDir();
452                 FileUtils.unpack(jf, webappUnpackDir);
453                 jf.close();
454                 setArchive(new FileArchive(webappUnpackDir));
455             }
456
457             if (app == null) {
458                 // simple webapp case
459
setModuleClassloader(new WebappClassLoader(webappUnpackDir.toURL(), Thread.currentThread()
460                         .getContextClassLoader()));
461             } else {
462                 // embedded webapp case
463
setModuleClassloader(new WebappClassLoader(webappUnpackDir.toURL(), app.getEJBClassLoader()));
464             }
465         } catch (IOException JavaDoc ioe) {
466             String JavaDoc err = getI18n().getMessage("WebApp.init.loader", getArchive().getRootFile());
467             throw new GenBaseException(err, ioe);
468         } catch (FileUtilsException fue) {
469             String JavaDoc err = getI18n().getMessage("WebApp.init.loader", getArchive().getRootFile());
470             throw new GenBaseException(err, fue);
471         }
472
473         try {
474             webDD = WebDeploymentDescManager.getDeploymentDesc(webappUnpackDir.getAbsolutePath(), getModuleClassloader());
475         } catch (DeploymentDescException dde) {
476             throw new GenBaseException(dde);
477         }
478
479         try {
480             wsDD = WSDeploymentDescManager.getDeploymentDesc(webappUnpackDir.getAbsolutePath(), getModuleClassloader());
481         } catch (DeploymentDescException dde) {
482             throw new GenBaseException(dde);
483         }
484
485         // we want a List of service-ref
486
sRefs = new Vector JavaDoc();
487
488         ServiceRefDesc[] refs = webDD.getServiceRefDesc();
489
490         for (int i = 0; i < refs.length; i++) {
491             sRefs.add(refs[i]);
492         }
493
494         // List of ejb-refs
495
ejbRefs = new Vector JavaDoc();
496         EjbRefDesc[] refDesc = webDD.getEjbRefDesc();
497
498         for (int i = 0; i < refDesc.length; i++) {
499             ejbRefs.add(refDesc[i]);
500         }
501
502     }
503
504     /**
505      * @return Returns the context-root to use for this group of Services.
506      */

507     public String JavaDoc getContextRoot() {
508         return this.wsDD.getContextRoot();
509     }
510
511     /**
512      * Returns the list of ejb-ref elements contained by a module.
513      * @return the list of ejb-ref elements contained by a module.
514      */

515     public List JavaDoc getEjbRefDescs() {
516         return ejbRefs;
517     }
518
519     /**
520      * Close this archive
521      */

522     public void close() {
523         sRefs = null;
524         ejbRefs = null;
525         webDD = null;
526         wsDD = null;
527         webApp = null;
528         app = null;
529         descriptors = null;
530         jonasWebApp = null;
531         webFilename = null;
532         webservices = null;
533         context = null;
534         webjetty = null;
535     }
536 }
Popular Tags