KickJava   Java API By Example, From Geeks To Geeks.

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


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: EjbJar.java,v 1.4 2005/07/18 23:53:30 mwringe 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.util.Hashtable JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35 import java.util.Vector 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_ejb.deployment.api.BeanDesc;
43 import org.objectweb.jonas_ejb.deployment.api.DeploymentDesc;
44 import org.objectweb.jonas_ejb.deployment.lib.EjbDeploymentDescManager;
45
46 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
47 import org.objectweb.jonas_lib.genbase.GenBaseException;
48 import org.objectweb.jonas_lib.genbase.utils.XMLUtils;
49 import org.objectweb.jonas_lib.loader.EjbJarClassLoader;
50
51 import org.objectweb.jonas_ws.deployment.api.WSDeploymentDesc;
52 import org.objectweb.jonas_ws.deployment.lib.WSDeploymentDescManager;
53
54 import org.objectweb.util.monolog.api.BasicLevel;
55
56 /**
57  * EjbJar represents an EjbJar J2EE Archive.
58  *
59  * @author Guillaume Sauthier
60  */

61 public class EjbJar extends J2EEArchive implements WsEndpoint {
62
63     /** Application containing the ejbjar */
64     private Application app;
65
66     /** WebServices deployment descriptor */
67     private WSDeploymentDesc wsDD = null;
68
69     /** EjbJar deployment descriptor */
70     private DeploymentDesc ejbDD = null;
71
72     /** inner EJBs List */
73     private List JavaDoc ejbs;
74
75     /** descriptors list */
76     private Map JavaDoc descriptors;
77
78     /** jonas-ejb-jar document */
79     private Document JavaDoc jEjbJar;
80
81     /** jonas-webservices document */
82     private Document JavaDoc jWebservices = null;
83
84     /**
85      * Create a new EjbJar not contained in Application
86      *
87      * @param archive
88      * the archive file
89      *
90      * @throws GenBaseException
91      * When init fails
92      */

93     public EjbJar(Archive archive) throws GenBaseException {
94         super(archive);
95         if (getLogger().isLoggable(BasicLevel.DEBUG)) {
96             getLogger().log(BasicLevel.DEBUG, "Wrapping '" + archive.getName() + "' in EjbJar");
97         }
98         init();
99     }
100
101     /**
102      * Create a new EjbJar contained in Application
103      *
104      * @param archive
105      * the archive file
106      * @param app
107      * container application
108      *
109      * @throws GenBaseException
110      * When init fails
111      */

112     public EjbJar(Archive archive, Application app) throws GenBaseException {
113         super(archive);
114         setApplication(app);
115         if (getLogger().isLoggable(BasicLevel.DEBUG)) {
116             getLogger().log(BasicLevel.DEBUG, "Wrapping '" + archive.getName() + "' in EjbJar");
117         }
118         init();
119     }
120
121     /**
122      * Initialize the EjbJar module.
123      *
124      * @throws GenBaseException
125      * When ejb classlaoder cannot be created or when Descriptor
126      * cannot be loaded.
127      */

128     private void init() throws GenBaseException {
129         loadDescriptors();
130     }
131
132
133     /**
134      * Initialize the Archive.
135      * @throws GenBaseException When initialization fails.
136      */

137     public void initialize() throws GenBaseException {
138
139         try {
140             if (app == null) {
141                 // simple ejb case
142
setModuleClassloader(new EjbJarClassLoader(new URL JavaDoc[] {getArchive()
143                         .getRootFile().toURL()}, Thread.currentThread()
144                         .getContextClassLoader()));
145             } else {
146                 // embedded ejb case
147
setModuleClassloader(app.getEJBClassLoader());
148             }
149         } catch (IOException JavaDoc ioe) {
150             String JavaDoc err = getI18n().getMessage("EjbJar.init.loader", getArchive()
151                     .getRootFile());
152             throw new GenBaseException(err, ioe);
153         }
154
155         try {
156             ejbDD = EjbDeploymentDescManager.getDeploymentDesc(getRootFile()
157                     .getAbsolutePath(), getModuleClassloader());
158         } catch (DeploymentDescException dde) {
159             throw new GenBaseException(dde);
160         }
161
162         try {
163             wsDD = WSDeploymentDescManager.getDeploymentDesc(getRootFile()
164                     .getAbsolutePath(), getModuleClassloader());
165         } catch (DeploymentDescException dde) {
166             throw new GenBaseException(dde);
167         }
168
169         // create inner EJBs
170
ejbs = new Vector JavaDoc();
171
172         BeanDesc[] bd = ejbDD.getBeanDesc();
173
174         for (int i = 0; i < bd.length; i++) {
175             ejbs.add(new Ejb(bd[i], XMLUtils.getBeanElement(jEjbJar
176                     .getDocumentElement(), bd[i].getEjbName())));
177         }
178
179     }
180
181
182     /**
183      * Load Deployment Descriptor of an EjbJar.
184      *
185      * @throws GenBaseException
186      * When parsing fails
187      */

188     private void loadDescriptors() throws GenBaseException {
189         try {
190
191             jEjbJar = XMLUtils.newDocument(getJonasEjbJarInputStream(), "META-INF/jonas-ejb-jar.xml", isDTDsAllowed());
192
193             InputStream JavaDoc isJws = getJonasWebservicesInputStream();
194             if (isJws != null) {
195                 jWebservices = XMLUtils.newDocument(isJws,
196                     "META-INF/jonas-webservices.xml", isDTDsAllowed());
197             }
198
199         } catch (SAXException JavaDoc saxe) {
200             String JavaDoc err = getI18n().getMessage("EjbJar.loadDescriptors.parseError");
201             throw new GenBaseException(err, saxe);
202         } catch (ParserConfigurationException JavaDoc pce) {
203             String JavaDoc err = getI18n().getMessage("EjbJar.loadDescriptors.prepare");
204             throw new GenBaseException(err, pce);
205         } catch (IOException JavaDoc ioe) {
206             String JavaDoc err = getI18n().getMessage("EjbJar.loadDescriptors.parseError");
207             throw new GenBaseException(err, ioe);
208         }
209
210         descriptors = new Hashtable JavaDoc();
211         descriptors.put("META-INF/jonas-ejb-jar.xml", jEjbJar);
212     }
213
214     /**
215      * Returns the List of Ejb contained in this EjbJar.
216      *
217      * @return the List of Ejb contained in this EjbJar.
218      */

219     public List JavaDoc getEjbs() {
220         return ejbs;
221     }
222
223     /**
224      * Returns the list of webservice-description elements contained by a
225      * module.
226      *
227      * @return the list of webservice-description elements contained by a
228      * module.
229      */

230     public List JavaDoc getServiceDescs() {
231         if (wsDD != null) {
232             return wsDD.getServiceDescs();
233         } else {
234             return new Vector JavaDoc();
235         }
236     }
237
238     /**
239      * Add *.class from directory in the archive.
240      *
241      * @param classes
242      * directory containing classes files.
243      */

244     public void addClasses(File JavaDoc classes) {
245         addDirectory(classes);
246     }
247
248     /**
249      * @return Returns the desired war filename (jonas-webservices/war)
250      */

251     public String JavaDoc getWarName() {
252        if (wsDD != null) {
253            return wsDD.getWarFile();
254        } else {
255            return null;
256        }
257     }
258
259     /**
260      * @return Returns the desired context-root (jonas-webservices/context-root)
261      */

262     public String JavaDoc getContextRoot() {
263         // compute context root name from ejbjar name
264
String JavaDoc archiveName = this.getArchive().getName();
265         String JavaDoc root = null;
266         if (archiveName.toLowerCase().endsWith(".jar")) {
267             root = archiveName.toLowerCase().substring(0, archiveName.length() - ".jar".length());
268         } else {
269             root = archiveName;
270         }
271         if (wsDD != null) {
272             String JavaDoc croot = wsDD.getContextRoot();
273             if (croot != null) {
274                 root = croot;
275             }
276         }
277         if (getLogger().isLoggable(BasicLevel.DEBUG)) {
278             getLogger().log(BasicLevel.DEBUG, "Computed context root : " + root);
279         }
280         return root;
281     }
282
283     /**
284      * Set the container application of this EjbJar
285      *
286      * @param app
287      * container application
288      */

289     public void setApplication(Application app) {
290         this.app = app;
291     }
292
293     /**
294      * Return the container application of this EjbJar
295      *
296      * @return the container application of this EjbJar
297      */

298     public Application getApplication() {
299         return app;
300     }
301
302     /**
303      * Returns a Map of name to Document for each modified Descriptor of the
304      * archive.
305      *
306      * @return a Map of name to Document
307      */

308     public Map JavaDoc getDescriptors() {
309         return descriptors;
310     }
311
312     /**
313      * Returns true if filename must be omitted in the archive.
314      *
315      * @param name
316      * filename to be tested
317      *
318      * @return true if filename must be omitted.
319      */

320     public boolean omit(String JavaDoc name) {
321         return (name.equals("META-INF/jonas-ejb-jar.xml")
322                 || name.equals("META-INF\\jonas-ejb-jar.xml"));
323     }
324
325     /**
326      * Returns the Document of the jonas-ejb-jar.xml file.
327      *
328      * @return the Document of the jonas-ejb-jar.xml file.
329      */

330     public Document JavaDoc getJonasEjbJarDoc() {
331         return jEjbJar;
332     }
333
334     /**
335      * Return the Document of the the jonas-webservices.xml
336      *
337      * @return the Document of the jonas-webservices.xml file
338      */

339     public Document JavaDoc getJonasWebservicesDoc() {
340         return jWebservices;
341     }
342
343     /**
344      * Returns the InputStream of the jonas-ejb-jar.xml file.
345      *
346      * @return the InputStream of the jonas-ejb-jar.xml file.
347      *
348      * @throws IOException
349      * When InputStream of jonas-ejb-jar.xml cannot be returned
350      */

351     public InputStream JavaDoc getJonasEjbJarInputStream() throws IOException JavaDoc {
352         InputStream JavaDoc is = null;
353
354         if (isPacked()) {
355             is = getInputStream("META-INF/jonas-ejb-jar.xml");
356         } else {
357             is = getInputStream("META-INF" + File.separator
358                     + "jonas-ejb-jar.xml");
359         }
360         if (is == null) {
361             throw new IOException JavaDoc("Cannot read jonas specific DD for EJB as the entry is not present");
362         }
363         return is;
364     }
365
366     /**
367      * Returns the InputStream of the jonas-webservices.xml file.
368      *
369      * @return the InputStream of the jonas-webservices.xml file.
370      * @throws IOException When InputStream of jonas-webservices.xml cannot be returned
371      */

372     public InputStream JavaDoc getJonasWebservicesInputStream() throws IOException JavaDoc {
373         InputStream JavaDoc is = null;
374
375         if (isPacked()) {
376             is = getInputStream("META-INF/jonas-webservices.xml");
377         } else {
378             is = getInputStream("META-INF" + File.separator
379                        + "jonas-webservices.xml");
380         }
381         return is;
382     }
383
384
385
386
387
388 }
389
Popular Tags