KickJava   Java API By Example, From Geeks To Geeks.

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


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: Client.java,v 1.4 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.util.Hashtable JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.Vector JavaDoc;
35
36 import javax.xml.parsers.ParserConfigurationException JavaDoc;
37
38 import org.w3c.dom.Document JavaDoc;
39 import org.xml.sax.SAXException JavaDoc;
40
41 import org.objectweb.jonas_client.deployment.api.ClientContainerDeploymentDesc;
42 import org.objectweb.jonas_client.deployment.lib.ClientDeploymentDescManager;
43
44 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
45 import org.objectweb.jonas_lib.deployment.api.EjbRefDesc;
46 import org.objectweb.jonas_lib.genbase.GenBaseException;
47 import org.objectweb.jonas_lib.genbase.utils.XMLUtils;
48 import org.objectweb.jonas_lib.loader.ClientClassLoader;
49
50 import org.objectweb.jonas_ws.deployment.api.ServiceRefDesc;
51
52 import org.objectweb.util.monolog.api.BasicLevel;
53
54 /**
55  * Client represnets a J2EE Client Archive.
56  *
57  * @author Guillaume Sauthier
58  */

59 public class Client extends J2EEArchive implements EjbRefModule, WsClient {
60
61     /** Container application (can be null) */
62     private Application app = null;
63
64     /** Client Deployment Desc */
65     private ClientContainerDeploymentDesc clientDD = null;
66
67     /** service-ref list */
68     private List JavaDoc sRefs;
69
70     /**
71      * ejb-ref list
72      */

73     private List JavaDoc ejbRefs;
74
75     /** jonas client descriptors */
76     private Map JavaDoc descriptors;
77
78     /**
79      * jonas-client.xml
80      */

81     private Document JavaDoc jclientDoc;
82
83     /**
84      * Create a new Client not contained in Application
85      *
86      * @param archive the archive file
87      *
88      * @throws GenBaseException When Init fails.
89      */

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

106     public Client(Archive archive, Application app) throws GenBaseException {
107         super(archive);
108         setApplication(app);
109         if (getLogger().isLoggable(BasicLevel.DEBUG)) {
110             getLogger().log(BasicLevel.DEBUG, "Wrapping '" + archive.getName() + "' in Client");
111         }
112         init();
113     }
114
115     /**
116      * Initialize the Client module.
117      *
118      * @throws GenBaseException When client classloader cannot be created or when
119      * client deployment desc cannot be parsed.
120      */

121     private void init() throws GenBaseException {
122         loadDescriptors();
123     }
124
125     /**
126      * Load Deployment Descriptor of a Client.
127      *
128      * @throws GenBaseException when jonas-client.xml cannot be parsed
129      */

130     private void loadDescriptors() throws GenBaseException {
131
132         descriptors = new Hashtable JavaDoc();
133         try {
134             InputStream JavaDoc jcis = getJonasClientInputStream();
135             if (jcis != null) {
136                 jclientDoc = XMLUtils.newDocument(jcis, "META-INF/jonas-client.xml", isDTDsAllowed());
137                 descriptors.put("META-INF/jonas-client.xml", jclientDoc);
138             }
139         } catch (SAXException JavaDoc saxe) {
140             String JavaDoc err = getI18n().getMessage("Client.loadDescriptors.parseError");
141             throw new GenBaseException(err, saxe);
142         } catch (ParserConfigurationException JavaDoc pce) {
143             String JavaDoc err = getI18n().getMessage("Client.loadDescriptors.prepare");
144             throw new GenBaseException(err, pce);
145         } catch (IOException JavaDoc ioe) {
146             String JavaDoc err = getI18n().getMessage("Client.loadDescriptors.parseError");
147             throw new GenBaseException(err, ioe);
148         }
149     }
150
151     /**
152      * Set the container application.
153      *
154      * @param app the container application.
155      */

156     public void setApplication(Application app) {
157         this.app = app;
158     }
159
160     /**
161      * Returns the container application (can be null).
162      *
163      * @return the container application (can be null).
164      */

165     public Application getApplication() {
166         return app;
167     }
168
169     /**
170      * Returns the list of service-ref elements contained by a module.
171      *
172      * @return the list of service-ref elements contained by a module.
173      */

174     public List JavaDoc getServiceRefDescs() {
175         return sRefs;
176     }
177
178     /**
179      * add *.class from directory in the archive.
180      *
181      * @param classes directory with classes.
182      */

183     public void addClasses(File JavaDoc classes) {
184         addDirectory(classes);
185     }
186
187     /**
188      * Returns a Map of name to Document for each modified Descriptor of the
189      * archive.
190      *
191      * @return a Map of name to Document
192      */

193     public Map JavaDoc getDescriptors() {
194         return descriptors;
195     }
196
197     /**
198      * Returns true if filename must be omitted in the archive.
199      *
200      * @param name filename to be tested
201      *
202      * @return true if filename must be omitted.
203      */

204     public boolean omit(String JavaDoc name) {
205         return (name.equals("META-INF/jonas-client.xml") || name.equals("META-INF\\jonas-client.xml"));
206     }
207
208     /**
209      * Returns the Document of the jonas-client.xml file.
210      *
211      * @return the Document of the jonas-client.xml file.
212      */

213     public Document JavaDoc getJonasClientDoc() {
214         return jclientDoc;
215     }
216
217     /**
218      * Returns the InputStream of the jonas-client.xml file.
219      *
220      * @return the InputStream of the jonas-client.xml file.
221      *
222      * @throws IOException When InputStream of jonas-client.xml cannot be
223      * returned.
224      */

225     public InputStream JavaDoc getJonasClientInputStream() throws IOException JavaDoc {
226         InputStream JavaDoc is = null;
227
228         if (isPacked()) {
229             is = getInputStream("META-INF/jonas-client.xml");
230         } else {
231             is = getInputStream("META-INF" + File.separator + "jonas-client.xml");
232         }
233
234         return is;
235     }
236
237     /**
238      * Initialize the Archive.
239      * @throws GenBaseException When initialization fails.
240      */

241     public void initialize() throws GenBaseException {
242         // if client in application, clientClassLoader includes ejbClassLoader
243
// too
244

245         try {
246             if (app == null) {
247                 // simple client case
248
setModuleClassloader(new ClientClassLoader(getArchive().getRootFile().toURL(), Thread.currentThread()
249                         .getContextClassLoader()));
250             } else {
251                 // embedded client case
252
setModuleClassloader(new ClientClassLoader(getArchive().getRootFile().toURL(), app.getEJBClassLoader()));
253             }
254         } catch (IOException JavaDoc ioe) {
255             String JavaDoc err = getI18n().getMessage("Client.init.loader", getArchive().getRootFile());
256             throw new GenBaseException(err, ioe);
257         }
258
259         try {
260             clientDD = ClientDeploymentDescManager.getInstance(getRootFile().getAbsolutePath(), getModuleClassloader());
261         } catch (DeploymentDescException dde) {
262             throw new GenBaseException(dde);
263         }
264
265         // we want a List of service-ref
266
sRefs = new Vector JavaDoc();
267         ServiceRefDesc[] refs = clientDD.getServiceRefDesc();
268         for (int i = 0; i < refs.length; i++) {
269             sRefs.add(refs[i]);
270         }
271
272         // List of ejb-refs
273
ejbRefs = new Vector JavaDoc();
274         EjbRefDesc[] refDesc = clientDD.getEjbRefDesc();
275
276         for (int i = 0; i < refDesc.length; i++) {
277             ejbRefs.add(refDesc[i]);
278         }
279
280     }
281
282     /**
283      * Returns the list of ejb-ref elements contained by a module.
284      * @return the list of ejb-ref elements contained by a module.
285      */

286     public List JavaDoc getEjbRefDescs() {
287         return ejbRefs;
288     }
289
290     /**
291      * Close this archive
292      */

293     public void close() {
294         super.close();
295         ejbRefs = null;
296         clientDD = null;
297         app = null;
298         descriptors = null;
299         jclientDoc = null;
300     }
301 }
Popular Tags