KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_client > deployment > lib > ClientDeploymentDescManager


1 /**
2  * JOnAS: Java(TM) Open 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  * Initial developer(s): Florent BENOIT
22  * --------------------------------------------------------------------------
23  * $Id: ClientDeploymentDescManager.java,v 1.11 2004/07/06 12:59:10 sauthieg Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas_client.deployment.lib;
28
29 //import java
30
import java.io.File JavaDoc;
31 import java.io.FileInputStream JavaDoc;
32 import java.io.FileNotFoundException JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.io.InputStreamReader JavaDoc;
36 import java.io.Reader JavaDoc;
37 import java.net.MalformedURLException JavaDoc;
38 import java.net.URL JavaDoc;
39 import java.net.URLClassLoader JavaDoc;
40 import java.util.Enumeration JavaDoc;
41 import java.util.Hashtable JavaDoc;
42 import java.util.List JavaDoc;
43 import java.util.StringTokenizer JavaDoc;
44 import java.util.jar.JarFile JavaDoc;
45 import java.util.zip.ZipEntry JavaDoc;
46
47 import org.objectweb.jonas_client.deployment.api.ClientContainerDeploymentDesc;
48 import org.objectweb.jonas_client.deployment.api.ClientContainerDeploymentDescException;
49 import org.objectweb.jonas_client.deployment.rules.ApplicationClientRuleSet;
50 import org.objectweb.jonas_client.deployment.rules.JonasClientRuleSet;
51 import org.objectweb.jonas_client.deployment.xml.ApplicationClient;
52 import org.objectweb.jonas_client.deployment.xml.JonasClient;
53
54 import org.objectweb.jonas_ejb.deployment.api.DeploymentDesc;
55 import org.objectweb.jonas_ejb.deployment.lib.EjbDeploymentDescManager;
56
57 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
58 import org.objectweb.jonas_lib.deployment.api.EjbRefDesc;
59 import org.objectweb.jonas_lib.deployment.api.MessageDestinationRefDesc;
60 import org.objectweb.jonas_lib.deployment.digester.JDigester;
61 import org.objectweb.jonas_lib.deployment.lib.AbsDeploymentDescManager;
62 import org.objectweb.jonas_lib.deployment.xml.JonasMessageDestination;
63 import org.objectweb.jonas_lib.loader.WebappClassLoader;
64
65 import org.objectweb.jonas_ws.deployment.api.PortComponentDesc;
66 import org.objectweb.jonas_ws.deployment.api.PortComponentRefDesc;
67 import org.objectweb.jonas_ws.deployment.api.ServiceDesc;
68 import org.objectweb.jonas_ws.deployment.api.ServiceRefDesc;
69 import org.objectweb.jonas_ws.deployment.api.WSDeploymentDesc;
70 import org.objectweb.jonas_ws.deployment.lib.WSDeploymentDescManager;
71
72 /**
73  * This class provide a way for managing the ClientContainerDeploymentDesc.
74  * @author Florent Benoit
75  */

76 public class ClientDeploymentDescManager extends AbsDeploymentDescManager {
77
78     /**
79      * The unique instance of the ClientDeploymentDescManager.
80      */

81     private static ClientDeploymentDescManager unique;
82
83     /**
84      * Reference on the EjbDeploymentDescManager.
85      */

86     private EjbDeploymentDescManager ejbDDManager = null;
87
88     /**
89      * Associate a ear classLoader to an hashtable which contains association
90      * between Urls of wars and their optional alt-dd
91      */

92     private Hashtable JavaDoc earCLAltDDBindings = null;
93
94     /**
95      * The path to the application-client.xml file.
96      */

97     public static final String JavaDoc CLIENT_FILE_NAME = "META-INF/application-client.xml";
98
99     /**
100      * The path to the jonas-client.xml file.
101      */

102     public static final String JavaDoc JONAS_CLIENT_FILE_NAME = "META-INF/jonas-client.xml";
103
104     /**
105      * Digester used to parse application-client.xml
106      */

107     private static JDigester appClientDigester = null;
108
109     /**
110      * Digester use to parse jonas-client.xml
111      */

112     private static JDigester jonasAppClientDigester = null;
113
114     /**
115      * Flag for parser validation
116      */

117     private static boolean parsingWithValidation = true;
118
119     /**
120      * Rules to parse the application-client.xml
121      */

122     private static ApplicationClientRuleSet appClientRuleSet = new ApplicationClientRuleSet();
123
124     /**
125      * Rules to parse the jonas-client.xml
126      */

127     private static JonasClientRuleSet jonasAppClientRuleSet = new JonasClientRuleSet();
128
129     /**
130      * Contructs a unique ClientDeploymentDescManager
131      */

132     private ClientDeploymentDescManager() {
133         ejbDDManager = EjbDeploymentDescManager.getInstance();
134         earCLAltDDBindings = new Hashtable JavaDoc();
135     }
136
137     /**
138      * Get an instance of the ClientDeploymentDescManager.
139      * @return the instance of the ClientDeploymentDescManager.
140      */

141     public static ClientDeploymentDescManager getInstance() {
142         if (unique == null) {
143             unique = new ClientDeploymentDescManager();
144         }
145         return unique;
146     }
147
148     /**
149      * Get the specified web deployment descriptor.
150      * @param url the url where to load xml deployment descriptors.
151      * @param loaderForCls classloader used to load web classes.
152      * @param earLoader the ear classloader.
153      * @return ClientContainerDeploymentDesc the web deployment descriptor.
154      * @throws DeploymentDescException when
155      * ClientContainerDeploymentDesc cannot be created with the given
156      * files.
157      */

158     public ClientContainerDeploymentDesc getDeploymentDesc(URL JavaDoc url, ClassLoader JavaDoc loaderForCls, ClassLoader JavaDoc earLoader)
159             throws DeploymentDescException {
160
161         // Check if the jar exists ...
162
if (!new File JavaDoc(url.getFile()).exists()) {
163             String JavaDoc err = "Cannot get the deployment descriptor for ";
164             err = err + "'" + url.getFile() + "'. The file doesn't exist.";
165             throw new ClientContainerDeploymentDescException(err);
166         }
167
168         //url used to load an alternate DDesc in the EAR case
169
URL JavaDoc altDDUrl = null;
170
171         //check if it's an Ear case or not
172
Hashtable JavaDoc urlAltddBindings = null;
173         if (earLoader != null) {
174             //Mapping ?
175
urlAltddBindings = (Hashtable JavaDoc) earCLAltDDBindings.get(earLoader);
176             if (urlAltddBindings == null) {
177                 //If there is no mapping, the setAltDD function was badly
178
// called
179
String JavaDoc err = "Cannot find if there is alt-dd for '" + url.getFile()
180                         + "', the setAltDD function was badly called";
181                 throw new ClientContainerDeploymentDescException(err);
182             }
183             //Now we can get the optional alt-dd url file
184
altDDUrl = (URL JavaDoc) urlAltddBindings.get(url);
185         }
186
187         // ... and get the instance of the ClientContainerDeploymentDesc.
188
// If there is an alternate url for the application-client.xml, call the
189
// method with this param.
190
ClientContainerDeploymentDesc clientDD = null;
191         try {
192             if (altDDUrl != null) {
193                 clientDD = getInstance(url.getFile(), loaderForCls, altDDUrl.getFile());
194             } else {
195                 clientDD = getInstance(url.getFile(), loaderForCls);
196             }
197         } catch (org.objectweb.jonas_lib.deployment.api.DeploymentDescException dde) {
198             throw new ClientContainerDeploymentDescException(dde);
199         }
200
201         // Resolve the ejb-link for ejb-ref
202
EjbRefDesc[] ejbRef = clientDD.getEjbRefDesc();
203         for (int i = 0; i < ejbRef.length; i++) {
204             if (ejbRef[i].getJndiName() == null) {
205                 String JavaDoc ejbLink = ejbRef[i].getEjbLink();
206                 String JavaDoc ejbRefType = ejbRef[i].getEjbRefType();
207                 if (ejbLink != null) {
208                     if (earLoader == null) {
209                         throw new ClientContainerDeploymentDescException(
210                                 "Ejb-link is not authorized from a single client jar. The client jar must be in an ear.");
211                     } else {
212                         String JavaDoc jndiName = getJndiName(url, ejbLink, earLoader, ejbRefType);
213                         ejbRef[i].setJndiName(jndiName);
214                     }
215                 }
216             }
217         }
218         // Resolve the port-component-link for service-ref
219
ServiceRefDesc[] serviceRef = clientDD.getServiceRefDesc();
220
221         for (int i = 0; i < serviceRef.length; i++) {
222
223             List JavaDoc pcRefs = serviceRef[i].getPortComponentRefs();
224             for (int j = 0; j < pcRefs.size(); j++) {
225                 // for each service portComponents : resolve links
226
PortComponentRefDesc pcr = (PortComponentRefDesc) pcRefs.get(j);
227                 String JavaDoc pclink = pcr.getPortComponentLink();
228                 if (pclink != null) {
229                     // a pc link is defined, we resolve it
230
PortComponentDesc pcDesc = getPCDesc(url, pclink, earLoader);
231                     pcr.setPortComponentDesc(pcDesc);
232                 }
233             }
234         }
235
236
237         // Resolve the message-destination-link for message-destination-ref
238
MessageDestinationRefDesc[] mdRef = clientDD.getMessageDestinationRefDesc();
239         for (int i = 0; i < mdRef.length; i++) {
240             if (mdRef[i].getJndiName() == null) {
241                 String JavaDoc jndiName = mdRef[i].getJndiName();
242                 String JavaDoc mdLink = mdRef[i].getMessageDestinationLink();
243                 String JavaDoc mdType = mdRef[i].getMessageDestinationType();
244                 String JavaDoc mdUsage = mdRef[i].getMessageDestinationUsage();
245                 if (mdLink != null) {
246                     if (earLoader == null) {
247                         throw new ClientContainerDeploymentDescException(
248                                 "Message-destination-link is not authorized from a single client jar. The client jar must be in an ear.");
249                     } else {
250                         String JavaDoc mdName = getMDJndiName(url, mdLink, mdType, mdUsage, earLoader);
251                         // String jndiName = getMdJndiName(url, mdLink,
252
// earLoader, mdType);
253
mdRef[i].setJndiName(jndiName);
254                     }
255                 }
256             }
257         }
258         return clientDD;
259     }
260     /**
261      * Return the port component desc from the pcLink string. pcLink format :
262      * filename.[jar or war]#portComponentName in the same Ear File
263      * @param warURL the url of the war being parsed. This is needed because
264      * pcLink is relative. With the url and the pcLink, we can know where
265      * the file is locate.
266      * @param pcLink the pcLink tag of an port-component-ref.
267      * @param earLoader the classloader of the ear.
268      * @return the pcLink portComponent.
269      * @throws DeploymentDescException when it failed
270      */

271     private PortComponentDesc getPCDesc(URL JavaDoc warURL, String JavaDoc pcLink, ClassLoader JavaDoc earLoader)
272             throws DeploymentDescException {
273
274         // Extract from the pc link
275
// - the name of the file
276
// - the name of the bean
277
String JavaDoc moduleLink = null;
278         String JavaDoc pcNameLink = null;
279
280         // Check the format of the pc-link. It must contains .jar# or .war#
281
if ((pcLink.toLowerCase().indexOf(".war" + LINK_SEPARATOR) == -1)
282                 && (pcLink.toLowerCase().indexOf(".jar" + LINK_SEPARATOR) == -1)) {
283             // the pc link is not in war or jar file
284
String JavaDoc err = "PC-link " + pcLink
285                     + " has a bad format. Correct format : filename.(jar|war)#portComponentName";
286             throw new DeploymentDescException(err);
287         }
288         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(pcLink, LINK_SEPARATOR);
289
290         // We must have only two elements after this step, one for the fileName
291
// before the # and the name of the bean after the # char
292
if (st.countTokens() != 2 || pcLink.startsWith(LINK_SEPARATOR) || pcLink.endsWith(LINK_SEPARATOR)) {
293             String JavaDoc err = "PC-link " + pcLink
294                     + " has a bad format. Correct format : filename.[jar or war]#portComponentName";
295             throw new DeploymentDescException(err);
296         }
297
298         //Get the token
299
moduleLink = st.nextToken();
300         pcNameLink = st.nextToken();
301
302         // Now construct the URL from the absolute path from the url module and
303
// the relative path from moduleJarLink
304
URL JavaDoc moduleLinkUrl = null;
305         try {
306             moduleLinkUrl = new File JavaDoc(new File JavaDoc(warURL.getFile()).getParent() + File.separator + moduleLink)
307                     .getCanonicalFile().toURL();
308         } catch (MalformedURLException JavaDoc mue) {
309             String JavaDoc err = "Error when creating an url for the module filename. Error :" + mue.getMessage();
310             throw new DeploymentDescException(err);
311         } catch (IOException JavaDoc ioe) {
312             String JavaDoc err = "Error when creating/accessing a file. Error :" + ioe.getMessage();
313             throw new DeploymentDescException(err);
314         }
315
316         // Check if the jar exist.
317
if (!new File JavaDoc(moduleLinkUrl.getFile()).exists()) {
318             String JavaDoc err = "Cannot get the deployment descriptor for '" + moduleLinkUrl.getFile()
319                     + "'. The file doesn't exist.";
320             throw new DeploymentDescException(err);
321         }
322
323         // We've got the url
324
// Now, We can ask the Deployment Descriptor of this url
325
ClassLoader JavaDoc loaderForCls = null;
326         ClassLoader JavaDoc current = Thread.currentThread().getContextClassLoader();
327         if (moduleLink.toLowerCase().endsWith(".war")) {
328             try {
329                 // webservice in webapp
330
loaderForCls = new WebappClassLoader(moduleLinkUrl, current);
331             } catch (IOException JavaDoc ioe) {
332                 throw new DeploymentDescException("Unable to create Web ClassLoader", ioe);
333             }
334         } else {
335             // webservice in ejbjar
336
loaderForCls = current;
337         }
338
339         WSDeploymentDesc wsDD = null;
340
341         try {
342             wsDD = WSDeploymentDescManager.getInstance().getDeploymentDesc(moduleLinkUrl, loaderForCls, earLoader);
343         } catch (DeploymentDescException e) {
344             String JavaDoc err = "Cannot get the deployment descriptor for '" + moduleLinkUrl.getFile() + "'.";
345             throw new DeploymentDescException(err, e);
346         }
347         if (wsDD == null) {
348             // the module doesn't contain port components.
349
String JavaDoc err = "Port component link " + pcNameLink + " not found in " + moduleLinkUrl.getFile();
350             throw new DeploymentDescException(err);
351         }
352
353         //get port component desc //pcNameLink
354
List JavaDoc sdl = wsDD.getServiceDescs();
355         boolean isFound = false;
356         PortComponentDesc pcd = null;
357         for (int i = 0; (i < sdl.size()) && !isFound; i++) {
358             if (sdl.get(i) != null) {
359                 pcd = ((ServiceDesc) sdl.get(i)).getPortComponent(pcNameLink);
360                 isFound = (pcd != null);
361                 // we stop when we have found the good portComponent
362
}
363         }
364         if (!isFound) {
365             // the module doesn't contain port components.
366
String JavaDoc err = "the port component link " + pcNameLink + " doesn't exist in " + moduleLinkUrl.getFile();
367             throw new DeploymentDescException(err);
368         }
369
370         return pcd;
371     }
372
373
374     /**
375      * Get an instance of a Client deployment descriptor by parsing the
376      * application-client.xml and jonas-client.xml deployment descriptors.
377      * @param clientFileName the fileName of the client file for the deployment
378      * descriptors.
379      * @param classLoaderForCls the classloader for the classes.
380      * @return a Client deployment descriptor by parsing the
381      * application-client.xml and jonas-client.xml deployment
382      * descriptors.
383      * @throws DeploymentDescException if the deployment descriptors are
384      * corrupted.
385      */

386     public static ClientContainerDeploymentDesc getInstance(String JavaDoc clientFileName, ClassLoader JavaDoc classLoaderForCls)
387             throws DeploymentDescException {
388
389         return getInstance(clientFileName, classLoaderForCls, null);
390     }
391
392     /**
393      * Get an instance of a Client deployment descriptor by parsing the
394      * application-client.xml and jonas-client.xml deployment descriptors.
395      * @param clientFileName the fileName of the client file for the deployment
396      * descriptors.
397      * @param classLoaderForCls the classloader for the classes.
398      * @param altClientXmlFilename the fileName to the application-client.xml
399      * for the alt-dd tag in the Ear Case. This is used for specify an
400      * alternate DDesc file.
401      * @return a Client deployment descriptor by parsing the
402      * application-client.xml and jonas-client.xml deployment
403      * descriptors.
404      * @throws DeploymentDescException if the deployment descriptors are
405      * corrupted.
406      */

407     public static ClientContainerDeploymentDesc getInstance(String JavaDoc clientFileName, ClassLoader JavaDoc classLoaderForCls,
408             String JavaDoc altClientXmlFilename)
409
410     throws DeploymentDescException {
411
412         // clientjar file
413
JarFile JavaDoc clientFile = null;
414
415         // Input streams
416
InputStream JavaDoc applicationClientInputStream = null;
417         InputStream JavaDoc jonasClientInputStream = null;
418
419         // ZipEntry
420
ZipEntry JavaDoc applicationClientZipEntry = null;
421         ZipEntry JavaDoc jonasClientZipEntry = null;
422
423         // Clients
424
ApplicationClient applicationClient;
425         JonasClient jonasClient;
426
427         // Init xml contents values;
428
String JavaDoc xmlContent = "";
429         String JavaDoc jonasXmlContent = "";
430
431         // Build the file
432
File JavaDoc fClient = new File JavaDoc(clientFileName);
433
434         //Check if the file exists.
435
if (!(fClient.exists())) {
436             String JavaDoc err = "' " + clientFileName + "' was not found.";
437             throw new ClientContainerDeploymentDescException(err);
438         }
439
440         //Check if the Alt deploymentDesc file exists.
441
//But only if it's a non null value because it's optionnal.
442
if ((altClientXmlFilename != null) && (!new File JavaDoc(altClientXmlFilename).exists())) {
443             String JavaDoc err = "The file for the altdd tag for the EAR case '" + altClientXmlFilename + "' was not found.";
444             throw new ClientContainerDeploymentDescException(err);
445         }
446
447         // load the application-client deployment descriptor data
448
// (META-INF/application-client.xml
449
// and META-INF/jonas-client.xml)
450
//No alt-dd case
451
if (altClientXmlFilename == null) {
452             try {
453                 clientFile = new JarFile JavaDoc(clientFileName);
454
455                 //Lookup in the JAR
456
//Check the client entry
457
applicationClientZipEntry = clientFile.getEntry(CLIENT_FILE_NAME);
458                 if (applicationClientZipEntry == null) {
459                     throw new ClientContainerDeploymentDescException("The entry '" + CLIENT_FILE_NAME
460                             + "' was not found in the file '" + clientFileName + "'.");
461                 }
462                 //Get the stream
463
applicationClientInputStream = clientFile.getInputStream(applicationClientZipEntry);
464                 xmlContent = xmlContent(applicationClientInputStream);
465                 // necessary to have a not empty InputStream !!!
466
applicationClientInputStream = clientFile.getInputStream(applicationClientZipEntry);
467             } catch (Exception JavaDoc e) {
468                 if (clientFile != null) {
469                     try {
470                         clientFile.close();
471                     } catch (IOException JavaDoc ioe) {
472                         //We can't close the file
473
}
474                 }
475                 throw new ClientContainerDeploymentDescException(
476                         "Can not read the XML deployment descriptors of the client jar file '" + clientFileName + "'.",
477                         e);
478             }
479         } else {
480             try {
481                 applicationClientInputStream = new FileInputStream JavaDoc(altClientXmlFilename);
482                 xmlContent = xmlContent(applicationClientInputStream);
483                 // necessary to have a not empty InputStream !!!
484
applicationClientInputStream = new FileInputStream JavaDoc(altClientXmlFilename);
485             } catch (FileNotFoundException JavaDoc ioe) {
486                 throw new ClientContainerDeploymentDescException("The altDD file '" + altClientXmlFilename
487                         + "' was not found.");
488             } catch (Exception JavaDoc e) {
489                 if (applicationClientInputStream != null) {
490                     try {
491                         applicationClientInputStream.close();
492                     } catch (IOException JavaDoc ioe) {
493                         // Can't close the file
494
}
495                 }
496                 throw new ClientContainerDeploymentDescException("Cannot read the XML deployment descriptors of the client jar file '"
497                         + clientFileName + "'.", e);
498             }
499         }
500
501         applicationClient = loadApplicationClient(new InputStreamReader JavaDoc(applicationClientInputStream), CLIENT_FILE_NAME);
502
503         try {
504             clientFile = new JarFile JavaDoc(clientFileName);
505
506             // Lookup in the JAR
507
// Check the client entry
508
jonasClientZipEntry = clientFile.getEntry(JONAS_CLIENT_FILE_NAME);
509             if (jonasClientZipEntry != null) {
510                 //Get the stream
511
jonasClientInputStream = clientFile.getInputStream(jonasClientZipEntry);
512                 jonasXmlContent = xmlContent(jonasClientInputStream);
513                 // necessary to have a not empty InputStream !!!
514
jonasClientInputStream = clientFile.getInputStream(jonasClientZipEntry);
515             }
516         } catch (Exception JavaDoc e) {
517             if (clientFile != null) {
518                 try {
519                     clientFile.close();
520                 } catch (IOException JavaDoc ioe) {
521                     //We can't close the file
522
}
523             }
524             throw new ClientContainerDeploymentDescException(
525                     "Can not read the XML deployment descriptors of the client jar file '" + clientFileName + "'.", e);
526         }
527
528         // load jonas-client deployment descriptor data
529
// (META-INF/jonas-client.xml)
530
if (jonasClientInputStream != null) {
531             jonasClient = loadJonasClient(new InputStreamReader JavaDoc(jonasClientInputStream), JONAS_CLIENT_FILE_NAME);
532             try {
533                 jonasClientInputStream.close();
534             } catch (IOException JavaDoc e) {
535                 // Nothing to do
536
}
537         } else {
538             jonasClient = new JonasClient();
539         }
540
541         // instantiate client deployment descriptor
542
ClientContainerDeploymentDesc clientDD = new ClientContainerDeploymentDesc(classLoaderForCls, applicationClient, jonasClient);
543         clientDD.setXmlContent(xmlContent);
544         clientDD.setJOnASXmlContent(jonasXmlContent);
545         return clientDD;
546     }
547
548     /**
549      * Load the application-client.xml file.
550      * @param reader the reader of the XML file.
551      * @param fileName the name of the file (application-client.xml).
552      * @return a structure containing the result of the application-client.xml
553      * parsing.
554      * @throws DeploymentDescException if the deployment descriptor is
555      * corrupted.
556      */

557     public static ApplicationClient loadApplicationClient(Reader JavaDoc reader, String JavaDoc fileName)
558             throws DeploymentDescException {
559
560         ApplicationClient appc = new ApplicationClient();
561
562         // Create if null
563
if (appClientDigester == null) {
564             // Create and initialize the digester
565
appClientDigester = new JDigester(appClientRuleSet, getParsingWithValidation(), true, new AppClientDTDs(),
566                     new AppClientSchemas());
567         }
568
569         try {
570             appClientDigester.parse(reader, fileName, appc);
571         } catch (DeploymentDescException e) {
572             throw e;
573         } finally {
574             appClientDigester.push(null);
575         }
576         return appc;
577     }
578
579     /**
580      * Load the jonas-client.xml file.
581      * @param reader the stream of the XML file.
582      * @param fileName the name of the file (jonas-client.xml).
583      * @return a structure containing the result of the jonas-client.xml
584      * parsing.
585      * @throws DeploymentDescException if the deployment descriptor is
586      * corrupted.
587      */

588     public static JonasClient loadJonasClient(Reader JavaDoc reader, String JavaDoc fileName) throws DeploymentDescException {
589
590         JonasClient jc = new JonasClient();
591
592         // Create if null
593
if (jonasAppClientDigester == null) {
594             jonasAppClientDigester = new JDigester(jonasAppClientRuleSet, getParsingWithValidation(), true,
595                     new JonasAppClientDTDs(), new JonasAppClientSchemas());
596         }
597
598         try {
599             jonasAppClientDigester.parse(reader, fileName, jc);
600
601         } catch (DeploymentDescException e) {
602             throw e;
603         } finally {
604             jonasAppClientDigester.push(null);
605         }
606         return jc;
607     }
608
609     /**
610      * Return the JNDI name from the ejbLink string. ejbLink format :
611      * filename.jar#beanName in the same Ear File
612      * @param clientURL the url of the jar being parsed. This is needed because
613      * ejbLink is relative. With the url and the ejbLink, we can know
614      * where the file is locate.
615      * @param ejbLink the ejbLink tag of an ejb-ref.
616      * @param earLoader the classloader of the ear.
617      * @param ejbType the type of the referenced ejb in the ejb-ref tag.
618      * @return the JNDI name if found, null otherwise
619      * @throws DeploymentDescException when it failed
620      */

621     private String JavaDoc getJndiName(URL JavaDoc clientURL, String JavaDoc ejbLink, ClassLoader JavaDoc earLoader, String JavaDoc ejbType)
622             throws DeploymentDescException {
623         // Now ask EJB deployment Desc manager
624
// Last arg is always true as it is always ejb ref and not local ref from a client
625
return ejbDDManager.getJndiName(clientURL, ejbLink, earLoader, ejbType, null, true);
626     }
627
628     /**
629      * Make a cleanup of the cache of deployment descriptor. This method must
630      * Return the JNDI name from the mdLink string. mdLink format :
631      * filename.jar#mdName in the same Ear File
632      * @param clientURL the url of the jar being parsed. This is needed because
633      * mdLink is relative. With the url and the mdLink, we can know where
634      * the file is locate.
635      * @param mdLink the mdLink tag of a message-destination-ref
636      * @param mdType the type of the referenced mdb in the
637      * message-destination-ref tag.
638      * @param mdUsage the usage of the referenced mdb in the
639      * message-destination-ref tag.
640      * @param earLoader the classloader of the ear.
641      * @return the JNDI name if found, null otherwise
642      * @throws ClientContainerDeploymentDescException when it failed
643      */

644     private String JavaDoc getMDJndiName(URL JavaDoc clientURL, String JavaDoc mdLink, String JavaDoc mdType, String JavaDoc mdUsage, ClassLoader JavaDoc earLoader)
645             throws ClientContainerDeploymentDescException {
646
647         // Extract from the mdb link
648
// - the name of the file
649
// - the name of the destination
650
String JavaDoc ejbJarLink = null;
651         String JavaDoc destNameLink = null;
652         DeploymentDesc dd = null;
653
654         // Check the format of the ejb-link. It must contains .jar#
655
if (mdLink.toLowerCase().indexOf(".jar#") == -1) {
656             String JavaDoc err = "Message-destination-link " + mdLink
657                     + " has a bad format. Correct format : filename.jar#messageDestinationName";
658             throw new ClientContainerDeploymentDescException(err);
659         }
660
661         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(mdLink, LINK_SEPARATOR);
662
663         // We must have only two elements after this step, one for the fileName
664
// before the # and the name of the message-destination after the # char
665
if (st.countTokens() != 2 || mdLink.startsWith(LINK_SEPARATOR) || mdLink.endsWith(LINK_SEPARATOR)) {
666
667             String JavaDoc err = "Message-destination-link " + mdLink
668                     + " has a bad format. Correct format : filename.jar#messageDestinationName.";
669             throw new ClientContainerDeploymentDescException(err);
670         }
671
672         //Get the token
673
ejbJarLink = st.nextToken();
674         destNameLink = st.nextToken();
675
676         //Check if ejbJarLink is a jar or not
677
if (!ejbJarLink.endsWith(".jar")) {
678             String JavaDoc err = "Ejbjar filename " + ejbJarLink + " from the message-destination-link " + mdLink
679                     + " has a bad format. Correct format : filename.jar";
680             throw new ClientContainerDeploymentDescException(err);
681         }
682
683         // Now construct the URL from the absolute path from the url clientURL
684
// and
685
// the relative path from ejbJarLink
686
URL JavaDoc ejbJarLinkUrl = null;
687         try {
688             ejbJarLinkUrl = new File JavaDoc(new File JavaDoc(clientURL.getFile()).getParent() + File.separator + ejbJarLink)
689                     .getCanonicalFile().toURL();
690         } catch (MalformedURLException JavaDoc mue) {
691             String JavaDoc err = "Error when creating an url for the ejb jar filename. Error :" + mue.getMessage();
692             throw new ClientContainerDeploymentDescException(err);
693         } catch (IOException JavaDoc ioe) {
694             String JavaDoc err = "Error when creating/accessing a file. Error :" + ioe.getMessage();
695             throw new ClientContainerDeploymentDescException(err);
696         }
697
698         // Check if the jar exist.
699
if (!new File JavaDoc(ejbJarLinkUrl.getFile()).exists()) {
700             String JavaDoc err = "Cannot get the deployment descriptor for '" + ejbJarLinkUrl.getFile()
701                     + "'. The file doesn't exist.";
702             throw new ClientContainerDeploymentDescException(err);
703         }
704
705         // We've got the url
706
// Now, We can ask the Deployment Descriptor of this url
707
URL JavaDoc[] ddURL = new URL JavaDoc[1];
708         ddURL[0] = ejbJarLinkUrl;
709         URLClassLoader JavaDoc loaderForClsEjb = new URLClassLoader JavaDoc(ddURL, earLoader);
710         try {
711             dd = ejbDDManager.getDeploymentDesc(ejbJarLinkUrl, loaderForClsEjb, earLoader);
712         } catch (DeploymentDescException e) {
713             String JavaDoc err = "Cannot get the deployment descriptor for '" + ejbJarLinkUrl.getFile() + "'.";
714             throw new ClientContainerDeploymentDescException(err, e);
715         }
716
717         JonasMessageDestination md = dd.getJonasMessageDestination(mdLink);
718
719         if (md == null) {
720             String JavaDoc err = "No message-destination-link was found for '" + mdLink + "' in the file "
721                     + clientURL.getFile() + " specified.";
722             throw new ClientContainerDeploymentDescException(err);
723         }
724
725         //Check if the type & usage of the message-destination-ref is correct.
726
//For now checkTypeUsage(clientURL, mdType, mdUsage, dd);
727

728         return md.getJndiName();
729     }
730
731     /**
732      * Make a cleanup of the cache of deployment descriptor. This method must be
733      * invoked after the ear deployment by the EAR service.
734      * @param earClassLoader the ClassLoader of the ear application to remove
735      * from the cache.
736      */

737     public void removeCache(ClassLoader JavaDoc earClassLoader) {
738         //Remove the altdd mapping
739
earCLAltDDBindings.remove(earClassLoader);
740
741         //Then remove the cache of the ejb dd manager
742
ejbDDManager.removeCache(earClassLoader);
743     }
744
745     /**
746      * Set the alt deployment desc which are used instead of the web.xml file
747      * which is in the war file. The alt-dd tag is in the application.xml file
748      * of the ear files and is used ony in the EAR case. ie : deployment of wars
749      * packaged into EAR applications. alt-dd tag is optionnal
750      * @param earClassLoader the ear classloader which is used for mapped the
751      * URLs of the wars to the Alt dd.
752      * @param urls the urls of the wars
753      * @param altDDs the alt-dd name for the specified war URLs
754      */

755     public void setAltDD(ClassLoader JavaDoc earClassLoader, URL JavaDoc[] urls, URL JavaDoc[] altDDs) {
756
757         //Associate an url to a altDD url
758
Hashtable JavaDoc urlAltddBindings = new Hashtable JavaDoc();
759
760         //Fill the hashtable for each url
761
for (int i = 0; i < urls.length; i++) {
762             if (altDDs[i] != null) {
763                 urlAltddBindings.put(urls[i], altDDs[i]);
764             }
765         }
766
767         //Bind the hashtable
768
earCLAltDDBindings.put(earClassLoader, urlAltddBindings);
769
770     }
771
772     /**
773      * Get the size of the cache (number of entries in the cache). This method
774      * is used only for the tests.
775      * @return the size of the cache (number of entries in the cache).
776      */

777     public int getCacheSize() {
778         int bufferSize = 0;
779
780         Enumeration JavaDoc keys = earCLAltDDBindings.keys();
781         while (keys.hasMoreElements()) {
782             ClassLoader JavaDoc loader = (ClassLoader JavaDoc) keys.nextElement();
783             Hashtable JavaDoc hashtab = (Hashtable JavaDoc) earCLAltDDBindings.get(loader);
784             bufferSize = bufferSize + hashtab.size();
785         }
786
787         return bufferSize;
788     }
789
790     /**
791      * Controls whether the parser is reporting all validity errors.
792      * @return if true, all external entities will be read.
793      */

794     public static boolean getParsingWithValidation() {
795         return parsingWithValidation;
796     }
797
798     /**
799      * Controls whether the parser is reporting all validity errors.
800      * @param validation if true, all external entities will be read.
801      */

802     public static void setParsingWithValidation(boolean validation) {
803         ClientDeploymentDescManager.parsingWithValidation = validation;
804     }
805 }
Popular Tags