KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ws > deployment > lib > WSDeploymentDescManager


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 : Delplanque Xavier & Sauthier Guillaume
22  * --------------------------------------------------------------------------
23  * $Id: WSDeploymentDescManager.java,v 1.19 2005/04/28 08:43:24 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas_ws.deployment.lib;
28
29 import java.io.File JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.InputStream JavaDoc;
32 import java.io.InputStreamReader JavaDoc;
33 import java.io.Reader JavaDoc;
34 import java.net.MalformedURLException JavaDoc;
35 import java.net.URL JavaDoc;
36 import java.net.URLClassLoader JavaDoc;
37 import java.net.URLConnection JavaDoc;
38 import java.util.Enumeration JavaDoc;
39 import java.util.HashMap JavaDoc;
40 import java.util.Hashtable JavaDoc;
41 import java.util.Iterator JavaDoc;
42 import java.util.List JavaDoc;
43 import java.util.Map JavaDoc;
44 import java.util.StringTokenizer JavaDoc;
45 import java.util.Vector JavaDoc;
46
47 import org.objectweb.jonas_ejb.deployment.api.BeanDesc;
48 import org.objectweb.jonas_ejb.deployment.api.DeploymentDesc;
49 import org.objectweb.jonas_ejb.deployment.api.SessionStatelessDesc;
50 import org.objectweb.jonas_ejb.deployment.lib.EjbDeploymentDescManager;
51
52 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
53 import org.objectweb.jonas_lib.deployment.digester.JDigester;
54 import org.objectweb.jonas_lib.deployment.lib.AbsDeploymentDescManager;
55
56 import org.objectweb.jonas_web.deployment.api.WebContainerDeploymentDesc;
57 import org.objectweb.jonas_web.deployment.api.WebContainerDeploymentDescException;
58 import org.objectweb.jonas_web.deployment.lib.WebDeploymentDescManager;
59
60 import org.objectweb.jonas_ws.deployment.api.PortComponentDesc;
61 import org.objectweb.jonas_ws.deployment.api.ServiceDesc;
62 import org.objectweb.jonas_ws.deployment.api.WSDeploymentDesc;
63 import org.objectweb.jonas_ws.deployment.api.WSDeploymentDescException;
64 import org.objectweb.jonas_ws.deployment.rules.JonasWebservicesRuleSet;
65 import org.objectweb.jonas_ws.deployment.rules.WebservicesRuleSet;
66 import org.objectweb.jonas_ws.deployment.xml.JonasWebservices;
67 import org.objectweb.jonas_ws.deployment.xml.Webservices;
68
69 import org.objectweb.jonas.common.Log;
70
71 import org.objectweb.util.monolog.api.BasicLevel;
72 import org.objectweb.util.monolog.api.Logger;
73
74 /**
75  * This class provide a way for managing the WSDeploymentDesc. Note that there
76  * is 1 instance of the WSDeploymentDescManager on each JOnAS server.
77  * @author Guillaume Sauthier
78  * @author Xavier Delplanque
79  * @author Helene Joanin
80  */

81 public class WSDeploymentDescManager extends AbsDeploymentDescManager {
82
83     /**
84      * ejb-jar.xml filename
85      */

86     public static final String JavaDoc WS_EJBJAR_FILE_NAME = "META-INF/webservices.xml";
87
88     /**
89      * jonas-ejb-jar.xml filename
90      */

91     public static final String JavaDoc JONAS_WS_EJBJAR_FILE_NAME = "META-INF/jonas-webservices.xml";
92
93     /**
94      * ejb-jar.xml filename
95      */

96     public static final String JavaDoc WS_WEBAPP_FILE_NAME = "WEB-INF/webservices.xml";
97
98     /**
99      * jonas-ejb-jar.xml filename
100      */

101     public static final String JavaDoc JONAS_WS_WEBAPP_FILE_NAME = "WEB-INF/jonas-webservices.xml";
102
103     /**
104      * Digester used to parse webservices.xml
105      */

106     private static JDigester wsDigester = null;
107
108     /**
109      * Digester used to parse jonas-webservices.xml
110      */

111     private static JDigester jwsDigester = null;
112
113     /**
114      * Rules to parse the webservices.xml
115      */

116     private static WebservicesRuleSet wsRuleSet = new WebservicesRuleSet();
117
118     /**
119      * Rules to parse the jonas-webservices.xml
120      */

121     private static JonasWebservicesRuleSet jwsRuleSet = new JonasWebservicesRuleSet();
122
123     /**
124      * Flag for parser validation
125      */

126     private static boolean parsingWithValidation = true;
127
128     /**
129      * The unique instance of the WSDeploymentDescManager
130      */

131     private static WSDeploymentDescManager unique = null;
132
133     /**
134      * is the manager instanciated ?
135      */

136     private static boolean isInstanciated = false;
137
138     /**
139      * Logger for the deployment desc manager.
140      */

141     private static Logger logger = Log.getLogger(Log.JONAS_WS_PREFIX);
142
143     /**
144      * Associates module or webservices dd URL and a WSDeploymentDesc
145      */

146     private HashMap JavaDoc urlWsddBindings;
147
148     /**
149      * Associates a given ClassLaoder (ear/ejb/web) to a list of URL containing
150      * (or not) WSDD
151      */

152     private Map JavaDoc classLoader2URLs;
153
154     /**
155      * EJB Descriptor Manager
156      */

157     private EjbDeploymentDescManager ejbManager = null;
158
159     /**
160      * Web Descriptor Manager
161      */

162     private WebDeploymentDescManager webManager = null;
163
164     /**
165      * Constructor : creates a WSDeploymentDescManager object. Used only when
166      * loading files in the running JOnAS instance.
167      */

168     private WSDeploymentDescManager() {
169         urlWsddBindings = new HashMap JavaDoc();
170         classLoader2URLs = new Hashtable JavaDoc();
171
172         ejbManager = EjbDeploymentDescManager.getInstance();
173         webManager = WebDeploymentDescManager.getInstance();
174     }
175
176     /**
177      * Get the unique instance of the WSDeploymentDescManager.
178      * @return the instance of the WSDeploymentDescManager.
179      */

180     public static WSDeploymentDescManager getInstance() {
181         if (!isInstanciated) {
182             isInstanciated = true;
183             unique = new WSDeploymentDescManager();
184
185         }
186         return unique;
187     }
188
189     /**
190      * Get the specified WebService deployment descriptor. Used by WsGen.
191      * @param file module (ejbjar or war). It is required that file is not a
192      * directory.
193      * @param jarCL the classloader where classes are stored.
194      * @return the module webservices deployment descriptor if it exists, null
195      * else.
196      * @throws WSDeploymentDescException when WSDeploymentDesc cannot be created
197      * with the given files.
198      */

199     public static WSDeploymentDesc getDeploymentDesc(String JavaDoc file, ClassLoader JavaDoc jarCL) throws WSDeploymentDescException {
200
201         URL JavaDoc fileUrl = null;
202         try {
203             fileUrl = new File JavaDoc(file).toURL();
204         } catch (MalformedURLException JavaDoc mue) {
205             throw new WSDeploymentDescException(mue);
206         }
207
208         WSDeploymentDesc wsdd = loadDeploymentDesc(fileUrl, jarCL);
209
210         // webservices.xml not present in file, no resolution needed
211
if (wsdd == null) {
212             return null;
213
214         }
215
216         // Resolve ejb-link and servlet-link elements
217
List JavaDoc sdl = wsdd.getServiceDescs();
218
219         for (int i = 0; i < sdl.size(); i++) {
220             List JavaDoc pcdl = ((ServiceDesc) sdl.get(i)).getPortComponents();
221
222             for (int j = 0; j < pcdl.size(); j++) {
223                 // Resolve the ejb-link and servlet-link of each port components
224
PortComponentDesc pcd = (PortComponentDesc) pcdl.get(j);
225                 String JavaDoc sibLink = pcd.getSibLink();
226
227                 // a link is defined, we resolve it
228
if (pcd.hasBeanImpl()) {
229                     SessionStatelessDesc desc = getBeanDesc(file, sibLink, jarCL);
230                     pcd.setDesc(desc);
231
232                 } else if (pcd.hasJaxRpcImpl()) {
233                     WebContainerDeploymentDesc desc = getWebDesc(file, sibLink, jarCL);
234                     pcd.setDesc(desc);
235
236                 }
237             }
238         }
239
240         return wsdd;
241     }
242
243     /**
244      * Get the specified ws deployment descriptor and put it in the cache if it
245      * is not in.
246      * @param url module (ejbjar or war). It can be a directory or a jar file.
247      * @param jarCL classloader used to load bean classes.
248      * @param earCL the parent classloader (the ear classloader). Null when not
249      * in the case of an ear application.
250      * @return the module webservices deployment descriptor if it exists, null
251      * else.
252      * @throws WSDeploymentDescException when WSDeploymentDesc cannot be created
253      * with the given files.
254      */

255     public WSDeploymentDesc getDeploymentDesc(URL JavaDoc url, ClassLoader JavaDoc jarCL, ClassLoader JavaDoc earCL)
256             throws WSDeploymentDescException {
257         return getDeploymentDesc(url, null, jarCL, earCL);
258     }
259
260     /**
261      * Get the specified ws deployment descriptor and put it in the cache if it
262      * is not in.
263      * @param url module (ejbjar or war). It can be a directory or a jar file.
264      * @param unpackedURL Unpacked URL of the module archive
265      * @param jarCL classloader used to load bean classes.
266      * @param earCL the parent classloader (the ear classloader). Null when not
267      * in the case of an ear application.
268      * @return the module webservices deployment descriptor if it exists, null
269      * else.
270      * @throws WSDeploymentDescException when WSDeploymentDesc cannot be created
271      * with the given files.
272      */

273     public WSDeploymentDesc getDeploymentDesc(URL JavaDoc url, URL JavaDoc unpackedURL, ClassLoader JavaDoc jarCL, ClassLoader JavaDoc earCL)
274             throws WSDeploymentDescException {
275
276         WSDeploymentDesc wsdd = null;
277         File JavaDoc moduleFile = new File JavaDoc(url.getFile());
278
279         if (moduleFile.exists()) {
280             // if the module has yet been loaded
281
if (!urlWsddBindings.containsKey(url)) {
282                 wsdd = getDeploymentDescriptor(url, unpackedURL, jarCL, earCL);
283             } else { // module already loaded
284
// Can be null if no webservices.xml has been found in specified
285
// module
286
wsdd = (WSDeploymentDesc) urlWsddBindings.get(url);
287             }
288         } else {
289             throw new WSDeploymentDescException("'" + moduleFile.getName() + "' doesn't exists");
290         }
291
292         // Store binding
293
ClassLoader JavaDoc keyCL;
294         if (earCL != null) {
295             keyCL = earCL;
296         } else {
297             keyCL = jarCL;
298         }
299         List JavaDoc urls = (List JavaDoc) classLoader2URLs.get(keyCL);
300         if (urls == null) {
301             urls = new Vector JavaDoc();
302             classLoader2URLs.put(keyCL, urls);
303         }
304         urls.add(url);
305
306         return wsdd;
307     }
308
309     /**
310      * return the specified webservices deployment descriptor and put it in
311      * cache.
312      * @param url an existing module (ejbjar or war). It can be a directory or a
313      * jar file
314      * @param unpackedURL Unpacked URL of the module archive
315      * @param jarCL classloader used to load war or ejbjar classes.
316      * @param earCL the parent classloader (the ear classloader). Null when not
317      * in the case of an ear application.
318      * @return the module webservices deployment descriptor if it exists, null
319      * else.
320      * @throws WSDeploymentDescException when WSDeploymentDesc cannot be created
321      * with the given files.
322      */

323     private WSDeploymentDesc getDeploymentDescriptor(URL JavaDoc url, URL JavaDoc unpackedURL, ClassLoader JavaDoc jarCL, ClassLoader JavaDoc earCL)
324             throws WSDeploymentDescException {
325
326         URL JavaDoc baseURL = url;
327         if (unpackedURL != null) {
328             baseURL = unpackedURL;
329         }
330         WSDeploymentDesc wsdd = loadDeploymentDesc(baseURL, jarCL);
331
332         // add in cache the deployment descriptor and return it
333
urlWsddBindings.put(url, wsdd);
334
335         // No WSDeploymentDesc, return null
336
if (wsdd == null) {
337             return null;
338         }
339
340         // Resolve ejb-link and servlet-link elements
341
List JavaDoc sdl = wsdd.getServiceDescs();
342
343         for (int i = 0; i < sdl.size(); i++) {
344             List JavaDoc pcdl = ((ServiceDesc) sdl.get(i)).getPortComponents();
345
346             for (int j = 0; j < pcdl.size(); j++) {
347                 // Resolve the ejb-link and servlet-link of each port components
348
PortComponentDesc pcd = (PortComponentDesc) pcdl.get(j);
349                 String JavaDoc sibLink = pcd.getSibLink();
350
351                 // a link is defined, we resolve it
352
if (pcd.hasBeanImpl()) {
353                     SessionStatelessDesc desc = getBeanDesc(url.getFile(), sibLink, jarCL, earCL);
354                     pcd.setDesc(desc);
355
356                 } else if (pcd.hasJaxRpcImpl()) {
357                     WebContainerDeploymentDesc desc = getWebDesc(url.getFile(), sibLink, jarCL, earCL);
358                     pcd.setDesc(desc);
359
360                 }
361             }
362         }
363
364         return wsdd;
365     }
366
367     /**
368      * Load webservices.xml in the WSDeploymentDesc structure (null if
369      * webservices.xml not found). Can take as parameter a directory (like an
370      * unpacked jar root directory), a XML file name (path to
371      * (META-INF|WEB-INF)/webservices.xml) or a packed jar.
372      * @param url the url to load (can be a directory, xml, jar).
373      * @param cl the classLoader to use for loading files.
374      * @return WSDeploymentDesc object.
375      * @throws WSDeploymentDescException when parsing error.
376      */

377     private static WSDeploymentDesc loadDeploymentDesc(URL JavaDoc url, ClassLoader JavaDoc cl) throws WSDeploymentDescException {
378
379         URLClassLoader JavaDoc ucl = (URLClassLoader JavaDoc) cl;
380
381         // get the Webservices object for this module
382
InputStream JavaDoc isWebservices = null;
383
384         // get the JonasWebservices object for this module
385
InputStream JavaDoc isJonasWebservices = null;
386
387         URL JavaDoc wsXml = null;
388         URL JavaDoc jwsXml = null;
389
390         try {
391
392             boolean foundInEjb = false;
393             for (Enumeration JavaDoc ejbWebservices = ucl.findResources("META-INF/webservices.xml"); ejbWebservices
394                     .hasMoreElements()
395                     && !foundInEjb;) {
396                 wsXml = (URL JavaDoc) ejbWebservices.nextElement();
397                 if (isResourceInFile(url, wsXml)) {
398                     foundInEjb = true;
399                 }
400             }
401
402             // if DD found in Ejb
403
if (foundInEjb) {
404                 isWebservices = openInputStream(wsXml);
405
406                 foundInEjb = false;
407                 // try load jonas DD
408
for (Enumeration JavaDoc ejbJonasWebservices = ucl.findResources("META-INF/jonas-webservices.xml"); ejbJonasWebservices
409                         .hasMoreElements()
410                         && !foundInEjb;) {
411                     jwsXml = (URL JavaDoc) ejbJonasWebservices.nextElement();
412                     if (isResourceInFile(url, jwsXml)) {
413                         foundInEjb = true;
414                     }
415                 }
416                 if (foundInEjb) {
417                     isJonasWebservices = openInputStream(jwsXml);
418
419                 }
420             }
421
422             boolean foundInWeb = false;
423             for (Enumeration JavaDoc webWebservices = ucl.findResources("WEB-INF/webservices.xml"); webWebservices
424                     .hasMoreElements()
425                     && !foundInWeb;) {
426                 wsXml = (URL JavaDoc) webWebservices.nextElement();
427                 if (isResourceInFile(url, wsXml)) {
428                     foundInWeb = true;
429                 }
430             }
431
432             // if DD found in Web
433
if (foundInWeb) {
434                 isWebservices = openInputStream(wsXml);
435
436                 foundInWeb = false;
437                 // try load jonas DD
438
for (Enumeration JavaDoc webJonasWebservices = ucl.findResources("WEB-INF/jonas-webservices.xml"); webJonasWebservices
439                         .hasMoreElements()
440                         && !foundInWeb;) {
441                     jwsXml = (URL JavaDoc) webJonasWebservices.nextElement();
442                     if (isResourceInFile(url, jwsXml)) {
443                         foundInWeb = true;
444                     }
445                 }
446                 if (foundInWeb) {
447                     isJonasWebservices = openInputStream(jwsXml);
448                 }
449             }
450         } catch (IOException JavaDoc ioe) {
451             String JavaDoc err = "Cannot open Streams on WebServices Deployment Descriptor for '" + url + "'";
452             throw new WSDeploymentDescException(err, ioe);
453         }
454
455         // webservices.xml not found in module, return null
456
if (isWebservices == null) {
457             return null;
458         }
459
460         // Load and Parse files
461
Webservices webservices = loadWebservices(new InputStreamReader JavaDoc(isWebservices), wsXml.getFile());
462         JonasWebservices jonasWebservices = null;
463
464         if (isJonasWebservices != null) {
465             jonasWebservices = loadJonasWebservices(new InputStreamReader JavaDoc(isJonasWebservices), jwsXml.getFile());
466         }
467
468         try {
469             if (isWebservices != null) {
470                 isWebservices.close();
471             }
472             if (isJonasWebservices != null) {
473                 isJonasWebservices.close();
474             }
475         } catch (IOException JavaDoc ioe) {
476             // nothing to do, just log a warning
477
String JavaDoc err = "Cannot close InputStreams for '" + url + "'";
478             logger.log(BasicLevel.WARN, err);
479         }
480
481         // Create DeploymentDesc
482
WSDeploymentDesc wsdd = new WSDeploymentDesc(cl, logger, webservices, jonasWebservices);
483
484         return wsdd;
485
486     }
487
488     /**
489      * @param xml URL
490      * @return InputStream of the URL Content
491      * @throws IOException
492      */

493     private static InputStream JavaDoc openInputStream(URL JavaDoc xml) throws IOException JavaDoc {
494         URLConnection JavaDoc conn = xml.openConnection();
495         conn.setUseCaches(false);
496         conn.setDefaultUseCaches(false);
497         InputStream JavaDoc is = conn.getInputStream();
498
499         return is;
500     }
501
502     /**
503      * Returns true if the URL resource is located in the specified file.
504      * @param file file to search in
505      * @param resource resource name to search
506      * @return true if the URL resource is located in the specified file.
507      */

508     private static boolean isResourceInFile(URL JavaDoc file, URL JavaDoc resource) {
509
510         boolean within = false;
511
512         File JavaDoc f = new File JavaDoc(file.getFile());
513         if (f.isDirectory()) {
514             // test directory
515
// resource need to be of the form :
516
// <resource> = <file>/META-INF/webservices.xml for example
517
if (resource.toString().startsWith(file.toString())) {
518                 within = true;
519                 if (logger.isLoggable(BasicLevel.DEBUG)) {
520                     logger.log(BasicLevel.DEBUG, "Directory case. found '" + resource + "' in '" + file + "'");
521                 }
522             }
523         } else if (f.isFile()) {
524             if (f.getPath().endsWith(".xml")) {
525                 // test XML
526
// resource need to be of the form :
527
// <file> = <resource>
528
if (file.equals(resource)) {
529                     within = true;
530                     if (logger.isLoggable(BasicLevel.DEBUG)) {
531                         logger.log(BasicLevel.DEBUG, "XML case. found '" + resource + "' in '" + file + "'");
532                     }
533                 }
534             } else if ((f.getPath().endsWith(".war")) || (f.getPath().endsWith(".jar"))) {
535                 // test JarFile
536
// resource need to be of the form :
537
// <resource> = jar:<file>!/META-INF/webservices.xml for example
538
int index = resource.toString().indexOf("!/");
539                 if (index != -1) {
540                     if (resource.toString().startsWith(("jar:" + file.toString()))) {
541                         within = true;
542                         if (logger.isLoggable(BasicLevel.DEBUG)) {
543                             logger.log(BasicLevel.DEBUG, "Archive case. found '" + resource + "' in '" + file + "'");
544                         }
545                     }
546                 }
547             }
548         }
549         return within;
550     }
551
552     /**
553      * return a 'xml' object containing webservices.xml informations
554      * @param reader webservices.xml file input stream reader
555      * @param fileName webservices.xml file name
556      * @return a 'xml' object containing webservices.xml informations
557      * @throws WSDeploymentDescException when Webservices cannot be created with
558      * the given files.
559      */

560     public static Webservices loadWebservices(Reader JavaDoc reader, String JavaDoc fileName) throws WSDeploymentDescException {
561
562         Webservices ws = new Webservices();
563
564         // Create if wsDigester is null
565
if (wsDigester == null) {
566             try {
567                 // Create and initialize the digester
568
wsDigester = new JDigester(wsRuleSet, getParsingWithValidation(), true, null, new WsSchemas());
569             } catch (DeploymentDescException e) {
570                 throw new WSDeploymentDescException(e);
571             }
572         }
573
574         try {
575             wsDigester.parse(reader, fileName, ws);
576         } catch (DeploymentDescException e) {
577             throw new WSDeploymentDescException(e);
578         } finally {
579             wsDigester.push(null);
580         }
581
582         return ws;
583     }
584
585     /**
586      * return a 'xml' object containing jonas-webservices.xml informations
587      * @param reader jonas-webservices.xml file input stream reader
588      * @param fileName jonas-webservices.xml file name
589      * @return a 'xml' object containing jonas-webservices.xml informations
590      * @throws WSDeploymentDescException when JonasWebservices cannot be created
591      * with the given files.
592      */

593     public static JonasWebservices loadJonasWebservices(Reader JavaDoc reader, String JavaDoc fileName)
594             throws WSDeploymentDescException {
595
596         JonasWebservices jws = new JonasWebservices();
597
598         // Create if wsDigester is null
599
if (jwsDigester == null) {
600             try {
601                 // Create and initialize the digester
602
jwsDigester = new JDigester(jwsRuleSet, getParsingWithValidation(), true, null, new JonasWsSchemas());
603             } catch (DeploymentDescException e) {
604                 throw new WSDeploymentDescException(e);
605             }
606         }
607
608         try {
609             jwsDigester.parse(reader, fileName, jws);
610         } catch (DeploymentDescException e) {
611             throw new WSDeploymentDescException(e);
612         } finally {
613             jwsDigester.push(null);
614         }
615
616         return jws;
617
618     }
619
620     /**
621      * Get the size of the cache (number of entries in the cache). Used only for
622      * debugging.
623      * @return the size of the cache (number of entries in the cache).
624      */

625     public int getCacheSize() {
626         return urlWsddBindings.size();
627
628     }
629
630     /**
631      * Clear the cache.
632      */

633     public void clearCache() {
634         urlWsddBindings = new HashMap JavaDoc();
635     }
636
637     /**
638      * Remove the DD cache for the specified ClassLoader
639      * @param cl the Key ClassLoader
640      */

641     public void removeCache(ClassLoader JavaDoc cl) {
642         List JavaDoc urls = (List JavaDoc) classLoader2URLs.remove(cl);
643         if (urls != null) {
644             for (Iterator JavaDoc i = urls.iterator(); i.hasNext();) {
645                 URL JavaDoc url = (URL JavaDoc) i.next();
646                 urlWsddBindings.remove(url);
647             }
648         }
649     }
650
651     /**
652      * Return a string representation of the cache. (Used only for debugging).
653      * @return a string representation of the cache.
654      */

655     public String JavaDoc toString() {
656         return "" + getCacheSize();
657
658     }
659
660     /**
661      * Return a StatelessSessionDesc found in filename with the name link. Used
662      * by WsGen only. (No JOnAS running).
663      * @param filename the ejb-jar file to load.
664      * @param link the ejb-name to retrieve.
665      * @param cl the classLoader to use.
666      * @return he StatelessSessionDesc associated with this port component
667      * @throws WSDeploymentDescException if ejb-link not found or something went
668      * wrong when parsing ejb deployment desc.
669      */

670     private static SessionStatelessDesc getBeanDesc(String JavaDoc filename, String JavaDoc link, ClassLoader JavaDoc cl)
671             throws WSDeploymentDescException {
672
673         BeanDesc bd = null;
674
675         try {
676             // Note : Because we are in the static getDeploymentDesc
677
// we have the right (accordingly to classloader hierarchie) to use
678
// real EjbJar Manager
679
// ---------------------------------------------------
680
// Do not add import on EjbDeploymentDescManager !!!!
681
DeploymentDesc dd = org.objectweb.jonas_ejb.deployment.lib.EjbDeploymentDescManager.getDeploymentDesc(
682                     filename, cl);
683             bd = dd.getBeanDesc(link);
684
685         } catch (DeploymentDescException dde) {
686             throw new WSDeploymentDescException("DeploymentDescException when searching '" + link + "' in '" + filename
687                     + "'", dde);
688         }
689
690         if (bd == null) {
691             throw new WSDeploymentDescException("Unable to find the ejb-link '" + link + "' in '" + filename + "'");
692         }
693
694         if (!(bd instanceof SessionStatelessDesc)) {
695             throw new WSDeploymentDescException("ejb-link '" + link + "' must be a Stateless Session Bean not a '"
696                     + bd.getClass().getName() + "'");
697         }
698
699         return (SessionStatelessDesc) bd;
700
701     }
702
703     /**
704      * Return a StatelessSessionDesc found in filename with the name link. Used
705      * when JOnAS is running.
706      * @param filename the ejb-jar file to load.
707      * @param link the ejb-name to retrieve.
708      * @param jarCL the loader for classes.
709      * @param earCL the EAR classLoader .
710      * @return the StatelessSessionDesc associated with this port component
711      * @throws WSDeploymentDescException if ejb-link not found or something went
712      * wrong when parsing ejb deployment desc.
713      */

714     private SessionStatelessDesc getBeanDesc(String JavaDoc filename, String JavaDoc link, ClassLoader JavaDoc jarCL, ClassLoader JavaDoc earCL)
715             throws WSDeploymentDescException {
716         URL JavaDoc url;
717
718         try {
719             url = new File JavaDoc(filename).toURL();
720
721         } catch (MalformedURLException JavaDoc mue) {
722             throw new WSDeploymentDescException("Url Error with '" + filename + "'", mue);
723         }
724
725         BeanDesc bd = null;
726
727         try {
728             DeploymentDesc dd = ejbManager.getDeploymentDesc(url, jarCL, earCL);
729             bd = dd.getBeanDesc(link);
730
731         } catch (DeploymentDescException dde) {
732             throw new WSDeploymentDescException("Unable to load EjbJar '" + url + "'", dde);
733         }
734
735         // Ejb Link not found
736
if (bd == null) {
737             throw new WSDeploymentDescException("Unable to find the ejb-link '" + link + "' in '" + filename + "'");
738         }
739
740         if (!(bd instanceof SessionStatelessDesc)) {
741             throw new WSDeploymentDescException("ejb-link '" + link + "' must be a Stateless Session Bean");
742         }
743
744         return (SessionStatelessDesc) bd;
745
746     }
747
748     /**
749      * Return the Web Deployment Desc found in filename with the name link.
750      * @param filename the war file to load.
751      * @param link the servlet-name to retrieve.
752      * @param cl the classLoader to use.
753      * @return the Web Deployment Desc associated with this port component
754      * @throws WSDeploymentDescException if servlet-link not found or something
755      * went wrong when parsing web deployment desc.
756      */

757     private static WebContainerDeploymentDesc getWebDesc(String JavaDoc filename, String JavaDoc link, ClassLoader JavaDoc cl)
758             throws WSDeploymentDescException {
759
760         WebContainerDeploymentDesc wd = null;
761
762         try {
763             // Note : Because we are in the static getDeploymentDesc
764
// we have the right (accordingly to classloader hierarchie) to use
765
// real Web Manager
766
// ---------------------------------------------------
767
// Do not add import on WebDeploymentDescManager !!!!
768
wd = org.objectweb.jonas_web.deployment.lib.WebDeploymentDescManager.getDeploymentDesc(filename, cl);
769         } catch (WebContainerDeploymentDescException wcdde) {
770             throw new WSDeploymentDescException("Error while reading/parsing " + filename, wcdde);
771         }
772
773         // link not found in this deployment desc
774
if (wd.getServletClassname(link) == null) {
775             throw new WSDeploymentDescException("Unable to find the servlet-link '" + link + "' in '" + filename + "'");
776         }
777
778         return wd;
779
780     }
781
782     /**
783      * Return the Web Deployment Desc found in filename with the name link. Used
784      * when JOnAS is up and running.
785      * @param filename the war file to load.
786      * @param link the servlet-name to retrieve.
787      * @param jarCL the classLoader to us.
788      * @param earCL the EAR classLoader.
789      * @return the Web Deployment Desc associated with this port component
790      * @throws WSDeploymentDescException if servlet-link not found or something
791      * went wrong when parsing web deployment desc.
792      */

793     private WebContainerDeploymentDesc getWebDesc(String JavaDoc filename, String JavaDoc link, ClassLoader JavaDoc jarCL, ClassLoader JavaDoc earCL)
794             throws WSDeploymentDescException {
795
796         URL JavaDoc url;
797
798         try {
799             url = new File JavaDoc(filename).toURL();
800         } catch (MalformedURLException JavaDoc mue) {
801             throw new WSDeploymentDescException("Url Error with '" + filename + "'", mue);
802         }
803
804         WebContainerDeploymentDesc wd = null;
805
806         try {
807             wd = webManager.getDeploymentDesc(url, jarCL, earCL);
808         } catch (DeploymentDescException wcdde) {
809             throw new WSDeploymentDescException("Error while reading/parsing " + filename, wcdde);
810         }
811
812         // link not found in this deployment desc
813
if (wd.getServletClassname(link) == null) {
814             throw new WSDeploymentDescException("Unable to find the servlet-link '" + link + "' in '" + filename + "'");
815         }
816
817         return wd;
818
819     }
820
821     /**
822      * Controls whether the parser is reporting all validity errors.
823      * @return if true, all external entities will be read.
824      */

825     public static boolean getParsingWithValidation() {
826         return parsingWithValidation;
827     }
828
829     /**
830      * Controls whether the parser is reporting all validity errors.
831      * @param validation if true, all external entities will be read.
832      */

833     public static void setParsingWithValidation(boolean validation) {
834         WSDeploymentDescManager.parsingWithValidation = validation;
835     }
836
837     /**
838      * Return the port component desc from the pcLink string. pcLink format :
839      * filename.[jar or war]#portComponentName in the same Ear File
840      * @param callerURL the url of the module being parsed. This is needed because
841      * pcLink is relative. With the url and the pcLink, we can know where
842      * the file is locate.
843      * @param portComponentLinkName the pcLink tag of an port-component-ref.
844      * @param earLoader the classloader of the ear.
845      * @param moduleLoader classlaoder of the current module
846      * @return the pcLink portComponent.
847      * @throws WSDeploymentDescException when it failed
848      */

849     public PortComponentDesc getPortComponentDesc(URL JavaDoc callerURL, String JavaDoc portComponentLinkName, ClassLoader JavaDoc moduleLoader, ClassLoader JavaDoc earLoader)
850             throws WSDeploymentDescException {
851
852         // Extract from the pc link
853
// - the name of the file
854
// - the name of the bean
855
String JavaDoc moduleLink = null;
856         String JavaDoc pcNameLink = null;
857         URL JavaDoc moduleLinkUrl = null;
858         URLClassLoader JavaDoc loaderForCls = null;
859
860         // port-component-link can use directly the portComponentName without #
861
// See Bug #300592
862
if (!portComponentLinkName.matches(".*#.*")) {
863             // no xxx#yyy
864
// we just have the port link
865
pcNameLink = portComponentLinkName;
866
867             moduleLinkUrl = callerURL;
868             loaderForCls = (URLClassLoader JavaDoc) moduleLoader;
869         } else {
870             // xxx#yyy
871

872             // Check the format of the pc-link. It must contains .jar# or .war#
873
if ((portComponentLinkName.toLowerCase().indexOf(".war" + AbsDeploymentDescManager.LINK_SEPARATOR) == -1) && (portComponentLinkName.toLowerCase().indexOf(".jar" + AbsDeploymentDescManager.LINK_SEPARATOR) == -1)) {
874                 // the pc link is not in war or jar file
875
String JavaDoc err = "PC-link " + portComponentLinkName + " has a bad format. Correct format : filename.[jar or war]#portComponentName or just portComponentName";
876                 throw new WSDeploymentDescException(err);
877             }
878             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(portComponentLinkName, LINK_SEPARATOR);
879
880             // We must have only two elements after this step, one for the fileName
881
// before the # and the name of the bean after the # char
882
if (st.countTokens() != 2 || portComponentLinkName.startsWith(AbsDeploymentDescManager.LINK_SEPARATOR)
883                 || portComponentLinkName.endsWith(AbsDeploymentDescManager.LINK_SEPARATOR)) {
884                 String JavaDoc err = "PC-link " + portComponentLinkName + " has a bad format. Correct format : filename.[jar or war]#portComponentName or just portComponentName";
885                 throw new WSDeploymentDescException(err);
886             }
887
888             //Get the token
889
moduleLink = st.nextToken();
890             pcNameLink = st.nextToken();
891
892             // Now construct the URL from the absolute path from the url module and
893
// the relative path from moduleJarLink
894
try {
895                 moduleLinkUrl = new File JavaDoc(new File JavaDoc(callerURL.getFile()).getParent() + File.separator + moduleLink).getCanonicalFile().toURL();
896             } catch (MalformedURLException JavaDoc mue) {
897                 String JavaDoc err = "Error when creating an url for the module filename. Error :" + mue.getMessage();
898                 throw new WSDeploymentDescException(err, mue);
899             } catch (IOException JavaDoc ioe) {
900                 String JavaDoc err = "Error when creating/accessing a file. Error :" + ioe.getMessage();
901                 throw new WSDeploymentDescException(err, ioe);
902             }
903
904             // Check if the jar exist.
905
if (!new File JavaDoc(moduleLinkUrl.getFile()).exists()) {
906                 String JavaDoc err = "Cannot get the deployment descriptor for '" + moduleLinkUrl.getFile() + "'. The file doesn't exist.";
907                 throw new WSDeploymentDescException(err);
908             }
909
910             URL JavaDoc[] ddURL = new URL JavaDoc[1];
911             ddURL[0] = moduleLinkUrl;
912             loaderForCls = new URLClassLoader JavaDoc(ddURL, earLoader);
913
914         }
915
916         // We've got the url
917
// Now, We can ask the Deployment Descriptor of this url
918
WSDeploymentDesc wsDD = null;
919
920         try {
921             wsDD = getDeploymentDesc(moduleLinkUrl, loaderForCls, earLoader);
922         } catch (DeploymentDescException e) {
923             String JavaDoc err = "Cannot get the deployment descriptor for '" + moduleLinkUrl.getFile() + "'.";
924             throw new WSDeploymentDescException(err, e);
925         }
926         if (wsDD == null) {
927             // the module doesn't contain port components.
928
String JavaDoc err = "Port component link " + pcNameLink + " not found in " + moduleLinkUrl.getFile();
929             throw new WSDeploymentDescException(err);
930         }
931
932         //get port component desc //pcNameLink
933
List JavaDoc sdl = wsDD.getServiceDescs();
934         boolean isFound = false;
935         PortComponentDesc pcd = null;
936         for (int i = 0; (i < sdl.size()) && !isFound; i++) {
937             if (sdl.get(i) != null) {
938                 pcd = ((ServiceDesc) sdl.get(i)).getPortComponent(pcNameLink);
939                 isFound = (pcd != null);
940                 // we stop when we have found the good portComponent
941
}
942         }
943         if (!isFound) {
944             // the module doesn't contain port components.
945
String JavaDoc err = "the port component link " + pcNameLink + " doesn't exist in " + moduleLinkUrl.getFile();
946             throw new WSDeploymentDescException(err);
947         }
948
949         return pcd;
950     }
951 }
Popular Tags