KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_web > deployment > lib > WebDeploymentDescManager


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): Ludovic BERT & Florent BENOIT
22  * --------------------------------------------------------------------------
23  * $Id: WebDeploymentDescManager.java,v 1.16 2004/12/09 10:44:03 sauthieg Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.jonas_web.deployment.lib;
28
29 import java.io.File JavaDoc;
30 import java.io.FileInputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.io.InputStreamReader JavaDoc;
34 import java.io.Reader JavaDoc;
35 import java.net.MalformedURLException JavaDoc;
36 import java.net.URL JavaDoc;
37 import java.net.URLClassLoader JavaDoc;
38 import java.util.Enumeration JavaDoc;
39 import java.util.Hashtable JavaDoc;
40 import java.util.List JavaDoc;
41 import java.util.StringTokenizer JavaDoc;
42 import java.util.jar.JarFile JavaDoc;
43 import java.util.zip.ZipEntry JavaDoc;
44
45 import org.objectweb.jonas_ejb.deployment.lib.EjbDeploymentDescManager;
46
47 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
48 import org.objectweb.jonas_lib.deployment.api.EjbLocalRefDesc;
49 import org.objectweb.jonas_lib.deployment.api.EjbRefDesc;
50 import org.objectweb.jonas_lib.deployment.api.MessageDestinationRefDesc;
51 import org.objectweb.jonas_lib.deployment.digester.JDigester;
52 import org.objectweb.jonas_lib.deployment.lib.AbsDeploymentDescManager;
53 import org.objectweb.jonas_lib.deployment.xml.JonasMessageDestination;
54
55 import org.objectweb.jonas_web.deployment.api.WebContainerDeploymentDesc;
56 import org.objectweb.jonas_web.deployment.api.WebContainerDeploymentDescException;
57 import org.objectweb.jonas_web.deployment.rules.JonasWebAppRuleSet;
58 import org.objectweb.jonas_web.deployment.rules.WebAppRuleSet;
59 import org.objectweb.jonas_web.deployment.xml.JonasWebApp;
60 import org.objectweb.jonas_web.deployment.xml.WebApp;
61
62 import org.objectweb.jonas_ws.deployment.api.PortComponentDesc;
63 import org.objectweb.jonas_ws.deployment.api.PortComponentRefDesc;
64 import org.objectweb.jonas_ws.deployment.api.ServiceRefDesc;
65 import org.objectweb.jonas_ws.deployment.api.WSDeploymentDescException;
66 import org.objectweb.jonas_ws.deployment.lib.WSDeploymentDescManager;
67
68 import org.objectweb.jonas.common.Log;
69
70 import org.objectweb.util.monolog.api.BasicLevel;
71 import org.objectweb.util.monolog.api.Logger;
72
73 /**
74  * This class provide a way for managing the WebContainerDeploymentDesc. Note
75  * that there is an intance of the WebDeploymentDescManager on each JOnAS
76  * server.
77  * @author Ludovic Bert
78  * @author Florent Benoit
79  */

80 public class WebDeploymentDescManager extends AbsDeploymentDescManager {
81
82     /**
83      * The path to the web.xml file.
84      */

85     public static final String JavaDoc WEB_FILE_NAME = "WEB-INF/web.xml";
86
87     /**
88      * The path to the jonas-web.xml file.
89      */

90     public static final String JavaDoc JONAS_WEB_FILE_NAME = "WEB-INF/jonas-web.xml";
91
92     /**
93      * Flag for parser validation
94      */

95     private static boolean parsingWithValidation = true;
96
97     /**
98      * Digester use to parse web.xml
99      */

100     private static JDigester webAppDigester = null;
101
102     /**
103      * Digester use to parse jonas-web.xml
104      */

105     private static JDigester jonasWebAppDigester = null;
106
107     /**
108      * Rules to parse the web.xml
109      */

110     private static WebAppRuleSet webAppRuleSet = new WebAppRuleSet();
111
112     /**
113      * Rules to parse the jonas-web.xml
114      */

115     private static JonasWebAppRuleSet jonasWebAppRuleSet = new JonasWebAppRuleSet();
116
117     /**
118      * The unique instance of the WebDeploymentDescManager.
119      */

120     private static WebDeploymentDescManager unique;
121
122     /**
123      * Reference on the EjbDeploymentDescManager.
124      */

125     private EjbDeploymentDescManager ejbDDManager = null;
126
127     /**
128      * Reference on the WSDeploymentDescManager.
129      */

130     private WSDeploymentDescManager wsDDManager = null;
131
132     /**
133      * logger
134      */

135     private static Logger logger = Log.getLogger(Log.JONAS_WEB_PREFIX);
136
137     /** The cache used when static getDeploymentDesc are called (WsGen case) */
138     private static Hashtable JavaDoc staticCache = new Hashtable JavaDoc();
139
140     /**
141      * Contructs a unique new WebDeploymentDescManager.
142      */

143     private WebDeploymentDescManager() {
144         ejbDDManager = EjbDeploymentDescManager.getInstance();
145         earCLAltDDBindings = new Hashtable JavaDoc();
146     }
147
148     /**
149      * Associate a ear classLoader to an hashtable which contains Association
150      * between Urls of wars and their optional alt-dd
151      */

152     private Hashtable JavaDoc earCLAltDDBindings = null;
153
154     /**
155      * Get an instance of the WebDeploymentDescManager.
156      * @return the instance of the WebDeploymentDescManager.
157      */

158     public static WebDeploymentDescManager getInstance() {
159         if (unique == null) {
160             unique = new WebDeploymentDescManager();
161         }
162         return unique;
163     }
164
165     /**
166      * Get the specified web deployment descriptor.
167      * @param url the url where to load xml deployment descriptors.
168      * @param loaderForCls classloader used to load web classes.
169      * @param earLoader the ear classloader.
170      * @return WebContainerDeploymentDesc the web deployment descriptor.
171      * @throws DeploymentDescException when WebContainerDeploymentDesc cannot be
172      * created with the given files.
173      */

174     public WebContainerDeploymentDesc getDeploymentDesc(URL JavaDoc url, ClassLoader JavaDoc loaderForCls, ClassLoader JavaDoc earLoader)
175             throws DeploymentDescException {
176
177         // load an instance of the WebService Manager
178
if (wsDDManager == null) {
179             wsDDManager = WSDeploymentDescManager.getInstance();
180         }
181
182         // Check if the war exists ...
183
if (!new File JavaDoc(url.getFile()).exists()) {
184             String JavaDoc err = "Cannot get the deployment descriptor for ";
185             err = err + "'" + url.getFile() + "'. The file doesn't exist.";
186             throw new WebContainerDeploymentDescException(err);
187         }
188
189         //url used to load an alternate DDesc in the EAR case
190
URL JavaDoc altDDUrl = null;
191
192         //check if it's an Ear case or not
193
Hashtable JavaDoc urlAltddBindings = null;
194         if (earLoader != null) {
195             //Mapping ?
196
urlAltddBindings = (Hashtable JavaDoc) earCLAltDDBindings.get(earLoader);
197             if (urlAltddBindings == null) {
198                 //If there is no mapping, the setAltDD function was badly
199
// called
200
String JavaDoc err = "Cannot find if there is alt-dd for '" + url.getFile()
201                         + "', the setAltDD function was badly called";
202                 throw new WebContainerDeploymentDescException(err);
203             }
204             //Now we can get the optional alt-dd url file
205
altDDUrl = (URL JavaDoc) urlAltddBindings.get(url);
206         }
207
208         // ... and get the instance of the WebContainerDeploymentDesc.
209
//If there is an alternate url for the web.xml, call the method with
210
// this param.
211
WebContainerDeploymentDesc webDD = null;
212         try {
213             if (altDDUrl != null) {
214                 webDD = getInstance(url.getFile(), loaderForCls, altDDUrl.getFile());
215             } else {
216                 webDD = getInstance(url.getFile(), loaderForCls);
217             }
218         } catch (DeploymentDescException dde) {
219             throw new WebContainerDeploymentDescException(dde);
220         }
221
222         // Resolve the ejb-link for ejb-ref
223
EjbRefDesc[] ejbRef = webDD.getEjbRefDesc();
224         for (int i = 0; i < ejbRef.length; i++) {
225             if (ejbRef[i].getJndiName() == null) {
226                 String JavaDoc ejbLink = ejbRef[i].getEjbLink();
227                 String JavaDoc ejbRefType = ejbRef[i].getEjbRefType();
228                 if (ejbLink != null) {
229                     if (earLoader == null) {
230                         throw new WebContainerDeploymentDescException(
231                                 "Ejb-link is not authorized from a single war. The war must be in an ear.");
232                     } else {
233                         String JavaDoc jndiName = getJndiName(url, ejbLink, earLoader, ejbRefType, true);
234                         ejbRef[i].setJndiName(jndiName);
235                     }
236                 }
237             }
238         }
239
240         // Resolve the ejb-link for ejb-local-ref
241
EjbLocalRefDesc[] ejbLocalRef = webDD.getEjbLocalRefDesc();
242         for (int i = 0; i < ejbLocalRef.length; i++) {
243             String JavaDoc ejblink = ejbLocalRef[i].getEjbLink();
244             if (earLoader == null) {
245                 throw new WebContainerDeploymentDescException(
246                         "Ejb-link is not authorized from a single war. The war must be in an ear.");
247             }
248             String JavaDoc ejbRefType = ejbLocalRef[i].getEjbRefType();
249             String JavaDoc ejbName = getJndiName(url, ejblink, earLoader, ejbRefType, false);
250             ejbLocalRef[i].setJndiLocalName(ejbName);
251         }
252
253         // Resolve the port-component-link for service-ref
254
ServiceRefDesc[] serviceRef = webDD.getServiceRefDesc();
255
256         for (int i = 0; i < serviceRef.length; i++) {
257
258             List JavaDoc pcRefs = serviceRef[i].getPortComponentRefs();
259             for (int j = 0; j < pcRefs.size(); j++) {
260                 // for each service portComponents : resolve links
261
PortComponentRefDesc pcr = (PortComponentRefDesc) pcRefs.get(j);
262                 String JavaDoc pclink = pcr.getPortComponentLink();
263                 if (pclink != null) {
264                     // a pc link is defined, we resolve it
265
PortComponentDesc pcDesc = getPCDesc(url, pclink, loaderForCls, earLoader);
266                     pcr.setPortComponentDesc(pcDesc);
267                 }
268             }
269         }
270
271         // Resolve the message-destination-link for message-destination-ref
272
MessageDestinationRefDesc[] mdRef = webDD.getMessageDestinationRefDesc();
273         for (int i = 0; i < mdRef.length; i++) {
274             if (mdRef[i].getJndiName() == null) {
275                 String JavaDoc jndiName = mdRef[i].getJndiName();
276                 String JavaDoc mdLink = mdRef[i].getMessageDestinationLink();
277                 String JavaDoc mdType = mdRef[i].getMessageDestinationType();
278                 String JavaDoc mdUsage = mdRef[i].getMessageDestinationUsage();
279                 if (mdLink != null) {
280                     if (earLoader == null) {
281                         throw new WebContainerDeploymentDescException(
282                                 "Message-destination-link is not authorized from a single client jar. The client jar must be in an ear.");
283                     } else {
284                         String JavaDoc mdName = getMDJndiName(url, mdLink, mdType, mdUsage, earLoader);
285                         mdRef[i].setJndiName(jndiName);
286                     }
287                 }
288             }
289         }
290
291         return webDD;
292     }
293
294     /**
295      * Return the port component desc from the pcLink string. pcLink format :
296      * filename.[jar or war]#portComponentName in the same Ear File
297      * @param warURL the url of the war being parsed. This is needed because
298      * pcLink is relative. With the url and the pcLink, we can know where
299      * the file is locate.
300      * @param pcLink the pcLink tag of an port-component-ref.
301      * @param earLoader the classloader of the ear.
302      * @param moduleLoader classlaoder of the current module
303      * @return the pcLink portComponent.
304      * @throws WSDeploymentDescException when it failed
305      */

306     private PortComponentDesc getPCDesc(URL JavaDoc warURL, String JavaDoc pcLink, ClassLoader JavaDoc moduleLoader, ClassLoader JavaDoc earLoader)
307             throws WSDeploymentDescException {
308
309         // now ask WS Manager for port-component-desc
310
return wsDDManager.getPortComponentDesc(warURL, pcLink, moduleLoader, earLoader);
311     }
312
313     /**
314      * Return the JNDI name from the ejbLink string. ejbLink format :
315      * filename.jar#beanName in the same Ear File beanName in the same ejb-jar
316      * file.
317      * @param warURL the url of the war being parsed. This is needed because
318      * ejbLink is relative. With the url and the ejbLink, we can know
319      * where the file is locate.
320      * @param ejbLink the ejbLink tag of an ejb-ref.
321      * @param earLoader the classloader of the ear.
322      * @param ejbType the type of the referenced ejb in the ejb-ref tag.
323      * @param isEjbRef true if the jndi name to resolve is an ejb-ref
324      * @return the JNDI name if found, null otherwise
325      * @throws DeploymentDescException when it failed
326      */

327     private String JavaDoc getJndiName(URL JavaDoc warURL, String JavaDoc ejbLink, ClassLoader JavaDoc earLoader, String JavaDoc ejbType, boolean isEjbRef)
328             throws DeploymentDescException {
329
330         // Now ask EJB deployment Desc manager :
331
return ejbDDManager.getJndiName(warURL, ejbLink, earLoader, ejbType, null, isEjbRef);
332     }
333
334     /**
335      * Return the JNDI name from the mdLink string. mdLink format :
336      * filename.jar#mdName in the same Ear File
337      * @param warURL the url of the jar being parsed. This is needed because
338      * mdLink is relative. With the url and the mdLink, we can know where
339      * the file is locate.
340      * @param mdLink the mdLink tag of a message-destination-ref
341      * @param mdType the type of the referenced mdb in the
342      * message-destination-ref tag.
343      * @param mdUsage the usage of the referenced mdb in the
344      * message-destination-ref tag.
345      * @param earLoader the classloader of the ear.
346      * @return the JNDI name if found, null otherwise
347      * @throws WebContainerDeploymentDescException when it failed
348      */

349     private String JavaDoc getMDJndiName(URL JavaDoc warURL, String JavaDoc mdLink, String JavaDoc mdType, String JavaDoc mdUsage, ClassLoader JavaDoc earLoader)
350             throws WebContainerDeploymentDescException {
351
352         // Extract from the mdb link
353
// - the name of the file
354
// - the name of the destination
355
String JavaDoc ejbJarLink = null;
356         String JavaDoc destNameLink = null;
357         org.objectweb.jonas_ejb.deployment.api.DeploymentDesc dd = null;
358
359         // Check the format of the ejb-link. It must contains .jar#
360
if (mdLink.toLowerCase().indexOf(".jar#") == -1) {
361             String JavaDoc err = "Message-destination-link " + mdLink
362                     + " has a bad format. Correct format : filename.jar#messageDestinationName";
363             throw new WebContainerDeploymentDescException(err);
364         }
365
366         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(mdLink, LINK_SEPARATOR);
367
368         // We must have only two elements after this step, one for the fileName
369
// before the # and the name of the message-destination after the # char
370
if (st.countTokens() != 2 || mdLink.startsWith(LINK_SEPARATOR) || mdLink.endsWith(LINK_SEPARATOR)) {
371
372             String JavaDoc err = "Message-destination-link " + mdLink
373                     + " has a bad format. Correct format : filename.jar#messageDestinationName.";
374             throw new WebContainerDeploymentDescException(err);
375         }
376
377         //Get the token
378
ejbJarLink = st.nextToken();
379         destNameLink = st.nextToken();
380
381         //Check if ejbJarLink is a jar or not
382
if (!ejbJarLink.endsWith(".jar")) {
383             String JavaDoc err = "Ejbjar filename " + ejbJarLink + " from the message-destination-link " + mdLink
384                     + " has a bad format. Correct format : filename.jar";
385             throw new WebContainerDeploymentDescException(err);
386         }
387
388         // Now construct the URL from the absolute path from the url warURL and
389
// the relative path from ejbJarLink
390
URL JavaDoc ejbJarLinkUrl = null;
391         try {
392             ejbJarLinkUrl = new File JavaDoc(new File JavaDoc(warURL.getFile()).getParent() + File.separator + ejbJarLink)
393                     .getCanonicalFile().toURL();
394         } catch (MalformedURLException JavaDoc mue) {
395             String JavaDoc err = "Error when creating an url for the ejb jar filename. Error :" + mue.getMessage();
396             throw new WebContainerDeploymentDescException(err);
397         } catch (IOException JavaDoc ioe) {
398             String JavaDoc err = "Error when creating/accessing a file. Error :" + ioe.getMessage();
399             throw new WebContainerDeploymentDescException(err);
400         }
401
402         // Check if the jar exist.
403
if (!new File JavaDoc(ejbJarLinkUrl.getFile()).exists()) {
404             String JavaDoc err = "Cannot get the deployment descriptor for '" + ejbJarLinkUrl.getFile()
405                     + "'. The file doesn't exist.";
406             throw new WebContainerDeploymentDescException(err);
407         }
408
409         // We've got the url
410
// Now, We can ask the Deployment Descriptor of this url
411
URL JavaDoc[] ddURL = new URL JavaDoc[1];
412         ddURL[0] = ejbJarLinkUrl;
413         URLClassLoader JavaDoc loaderForClsEjb = new URLClassLoader JavaDoc(ddURL, earLoader);
414         try {
415             dd = ejbDDManager.getDeploymentDesc(ejbJarLinkUrl, loaderForClsEjb, earLoader);
416         } catch (DeploymentDescException e) {
417             String JavaDoc err = "Cannot get the deployment descriptor for '" + ejbJarLinkUrl.getFile() + "'.";
418             throw new WebContainerDeploymentDescException(err, e);
419         }
420
421         JonasMessageDestination md = dd.getJonasMessageDestination(mdLink);
422
423         if (md == null) {
424             String JavaDoc err = "No message-destination-link was found for '" + mdLink + "' in the file " + warURL.getFile()
425                     + " specified.";
426             throw new WebContainerDeploymentDescException(err);
427         }
428
429         //Check if the type & usage of the message-destination-ref is correct.
430
//For now checkTypeUsage(warURL, mdType, mdUsage, dd);
431

432         return md.getJndiName();
433     }
434
435     /**
436      * Make a cleanup of the cache of deployment descriptor. This method must be
437      * invoked after the ear deployment by the EAR service.
438      * @param earClassLoader the ClassLoader of the ear application to remove
439      * from the cache.
440      */

441     public void removeCache(ClassLoader JavaDoc earClassLoader) {
442         //Remove the altdd mapping
443
earCLAltDDBindings.remove(earClassLoader);
444
445         //Then remove the cache of the ejb dd manager
446
ejbDDManager.removeCache(earClassLoader);
447     }
448
449     /**
450      * Set the alt deployment desc which are used instead of the web.xml file
451      * which is in the war file. The alt-dd tag is in the application.xml file
452      * of the ear files and is used ony in the EAR case. ie : deployment of wars
453      * packaged into EAR applications. alt-dd tag is optionnal
454      * @param earClassLoader the ear classloader which is used for mapped the
455      * URLs of the wars to the Alt dd.
456      * @param urls the urls of the wars
457      * @param altDDs the alt-dd name for the specified war URLs
458      */

459     public void setAltDD(ClassLoader JavaDoc earClassLoader, URL JavaDoc[] urls, URL JavaDoc[] altDDs) {
460
461         //Associate an url to a altDD url
462
Hashtable JavaDoc urlAltddBindings = new Hashtable JavaDoc();
463
464         //Fill the hashtable for each url
465
for (int i = 0; i < urls.length; i++) {
466             if (altDDs[i] != null) {
467                 urlAltddBindings.put(urls[i], altDDs[i]);
468             }
469         }
470
471         //Bind the hashtable
472
earCLAltDDBindings.put(earClassLoader, urlAltddBindings);
473
474     }
475
476     /**
477      * Get the size of the cache (number of entries in the cache). This method
478      * is used only for the tests.
479      * @return the size of the cache (number of entries in the cache).
480      */

481     public int getCacheSize() {
482         int bufferSize = 0;
483
484         Enumeration JavaDoc classLoaders = earCLAltDDBindings.keys();
485         while (classLoaders.hasMoreElements()) {
486             ClassLoader JavaDoc loader = (ClassLoader JavaDoc) classLoaders.nextElement();
487             Hashtable JavaDoc hashtab = (Hashtable JavaDoc) earCLAltDDBindings.get(loader);
488             bufferSize = bufferSize + hashtab.size();
489         }
490
491         return bufferSize;
492     }
493
494     /**
495      * Get the specified web deployment descriptor.
496      * @param filename the filename where to load xml deployment descriptors.
497      * @param loader classloader used to load web classes.
498      * @return WebContainerDeploymentDesc the web deployment descriptor.
499      * @throws WebContainerDeploymentDescException when
500      * WebContainerDeploymentDesc cannot be created with the given
501      * files.
502      */

503     public static WebContainerDeploymentDesc getDeploymentDesc(String JavaDoc filename, ClassLoader JavaDoc loader)
504             throws WebContainerDeploymentDescException {
505
506         WebContainerDeploymentDesc wcdd = null;
507         // if Desc already parsed
508
if (staticCache.containsKey(filename)) {
509             wcdd = (WebContainerDeploymentDesc) staticCache.get(filename);
510         } else {
511             // Check if the war exists ...
512
if (!new File JavaDoc(filename).exists()) {
513                 String JavaDoc err = "Cannot get the deployment descriptor for ";
514                 err += "'" + filename + "'. The file doesn't exist.";
515                 throw new WebContainerDeploymentDescException(err);
516             }
517             // get the DeploymentDesc
518
try {
519                 wcdd = getInstance(filename, loader);
520             } catch (DeploymentDescException dde) {
521                 throw new WebContainerDeploymentDescException(dde);
522             }
523             // put in cache
524
staticCache.put(filename, wcdd);
525         }
526
527         return wcdd;
528     }
529
530     /**
531      * Get an instance of a WEB deployment descriptor by parsing the web.xml and
532      * jonas-web.xml deployment descriptors.
533      * @param warFileName the fileName of the war file for the deployment
534      * descriptors.
535      * @param classLoaderForCls the classloader for the classes.
536      * @param altWebXmlFilename the fileName to the web.xml for the alt-dd tag
537      * in the Ear Case. This is used for specify an alternate DDesc file.
538      * @return a WEB deployment descriptor by parsing the web.xml and
539      * jonas-web.xml deployment descriptors.
540      * @throws DeploymentDescException if the deployment descriptors are
541      * corrupted.
542      */

543     public static WebContainerDeploymentDesc getInstance(String JavaDoc warFileName, ClassLoader JavaDoc classLoaderForCls,
544             String JavaDoc altWebXmlFilename) throws DeploymentDescException {
545
546         // init xml contents values;
547
String JavaDoc xmlContent = "";
548         String JavaDoc jonasXmlContent = "";
549
550         //war file
551
JarFile JavaDoc warFile = null;
552
553         //Input streams
554
InputStream JavaDoc webInputStream = null;
555         InputStream JavaDoc jonasWebInputStream = null;
556
557         //ZipEntry
558
ZipEntry JavaDoc webZipEntry = null;
559         ZipEntry JavaDoc jonasWebZipEntry = null;
560
561         //Webapps
562
WebApp webApp;
563         JonasWebApp jonasWebApp;
564
565         //Build the file
566
File JavaDoc fWar = new File JavaDoc(warFileName);
567
568         //Check if the file exists.
569
if (!(fWar.exists())) {
570             String JavaDoc err = "' " + warFileName + "' was not found.";
571             throw new WebContainerDeploymentDescException(err);
572         }
573
574         //Check if the Alt deploymentDesc file exists.
575
//But only if it's a non null value because it's optionnal.
576
if ((altWebXmlFilename != null) && (!new File JavaDoc(altWebXmlFilename).exists())) {
577             String JavaDoc err = "The file for the altdd tag for the EAR case '" + altWebXmlFilename + "' was not found.";
578             throw new WebContainerDeploymentDescException(err);
579         }
580
581         // load the web-app deployment descriptor data (WEB-INF/web.xml
582
// and WEB-INF/jonas-web.xml)
583
try {
584
585             //No alt-dd case
586
if (altWebXmlFilename == null) {
587
588                 //If warFile is a directory, there is no jar. Check file in the
589
// directory
590
if (fWar.isDirectory()) {
591                     //lookup a WEB-INF/web.xml file
592
File JavaDoc webXmlF = new File JavaDoc(warFileName, WEB_FILE_NAME);
593                     if (!webXmlF.exists()) {
594                         String JavaDoc err = "You have choose to deploy a war directory but there is no " + WEB_FILE_NAME
595                                 + " file in the directory " + warFileName;
596                         throw new WebContainerDeploymentDescException(err);
597                     }
598                     webInputStream = new FileInputStream JavaDoc(webXmlF);
599                     xmlContent = xmlContent(webInputStream);
600                     webInputStream = new FileInputStream JavaDoc(webXmlF);
601                 } else {
602                     warFile = new JarFile JavaDoc(warFileName);
603                     //Lookup in the JAR
604
//Check the web entry
605
webZipEntry = warFile.getEntry(WEB_FILE_NAME);
606                     if (webZipEntry == null) {
607                         throw new WebContainerDeploymentDescException("The entry '" + WEB_FILE_NAME
608                                 + "' was not found in the file '" + warFileName + "'.");
609                     }
610                     //Get the stream
611
webInputStream = warFile.getInputStream(webZipEntry);
612                     xmlContent = xmlContent(webInputStream);
613                     webInputStream = warFile.getInputStream(webZipEntry);
614                 }
615             } else {
616                 webInputStream = new FileInputStream JavaDoc(altWebXmlFilename);
617                 xmlContent = xmlContent(webInputStream);
618                 webInputStream = new FileInputStream JavaDoc(altWebXmlFilename);
619             }
620
621             //This is a directory
622
if (fWar.isDirectory()) {
623                 //lookup a WEB-INF/jonas-web.xml file
624
File JavaDoc webJXmlF = new File JavaDoc(warFileName, JONAS_WEB_FILE_NAME);
625                 if (webJXmlF.exists()) {
626                     jonasWebInputStream = new FileInputStream JavaDoc(webJXmlF);
627                     jonasXmlContent = xmlContent(jonasWebInputStream);
628                     jonasWebInputStream = new FileInputStream JavaDoc(webJXmlF);
629                 }
630             } else {
631                 if (warFile == null) {
632                     warFile = new JarFile JavaDoc(warFileName);
633                 }
634
635                 //Check the jonas web entry
636
jonasWebZipEntry = warFile.getEntry(JONAS_WEB_FILE_NAME);
637
638                 //Get the stream
639
if (jonasWebZipEntry != null) {
640                     jonasWebInputStream = warFile.getInputStream(jonasWebZipEntry);
641                     jonasXmlContent = xmlContent(jonasWebInputStream);
642                     jonasWebInputStream = warFile.getInputStream(jonasWebZipEntry);
643
644                 }
645             }
646
647         } catch (Exception JavaDoc e) {
648             if (warFile != null) {
649                 try {
650                     warFile.close();
651                 } catch (IOException JavaDoc ioe) {
652                     // We can't close the file
653
logger.log(BasicLevel.WARN, "Can't close file '" + warFileName + "'");
654                 }
655             }
656             throw new WebContainerDeploymentDescException(
657                     "Cannot read the XML deployment descriptors of the war file '" + warFileName + "'.", e);
658         }
659
660         webApp = loadWebApp(new InputStreamReader JavaDoc(webInputStream), WEB_FILE_NAME);
661         try {
662             webInputStream.close();
663         } catch (IOException JavaDoc e) {
664             // Nothing to do
665
logger.log(BasicLevel.WARN, "Can't close InputStream of web.xml from '" + warFileName + "'");
666         }
667
668         // load jonas-web-app deployment descriptor data
669
// (WEB-INF/jonas-web.xml)
670
if (jonasWebInputStream != null) {
671             jonasWebApp = loadJonasWebApp(new InputStreamReader JavaDoc(jonasWebInputStream), JONAS_WEB_FILE_NAME);
672             try {
673                 jonasWebInputStream.close();
674             } catch (IOException JavaDoc e) {
675                 // Nothing to do
676
logger.log(BasicLevel.WARN, "Can't close InputStream of jonas-web.xml from '" + warFileName + "'");
677             }
678         } else {
679             jonasWebApp = new JonasWebApp();
680         }
681
682         // close the zip
683
if (warFile != null) {
684             try {
685                 warFile.close();
686             } catch (IOException JavaDoc ioe) {
687                 // We can't close the file
688
logger.log(BasicLevel.WARN, "Can't close file '" + warFileName + "'");
689             }
690         }
691
692         // instantiate web deployment descriptor
693
WebContainerDeploymentDesc webDD = new WebContainerDeploymentDesc(warFileName, classLoaderForCls, webApp,
694                 jonasWebApp);
695         webDD.setXmlContent(xmlContent);
696         webDD.setJOnASXmlContent(jonasXmlContent);
697         return webDD;
698     }
699
700     /**
701      * Get an instance of a WEB deployment descriptor by parsing the web.xml and
702      * jonas-web.xml deployment descriptors.
703      * @param warFileName the fileName of the war file for the deployment
704      * descriptors.
705      * @param classLoaderForCls the classloader for the classes.
706      * @return a WEB deployment descriptor by parsing the web.xml and
707      * jonas-web.xml deployment descriptors.
708      * @throws DeploymentDescException if the deployment descriptors are
709      * corrupted.
710      */

711     public static WebContainerDeploymentDesc getInstance(String JavaDoc warFileName, ClassLoader JavaDoc classLoaderForCls)
712             throws DeploymentDescException {
713
714         return getInstance(warFileName, classLoaderForCls, null);
715     }
716
717     /**
718      * Load the web.xml file.
719      * @param reader the reader of the XML file.
720      * @param fileName the name of the file (web.xml).
721      * @return a structure containing the result of the web.xml parsing.
722      * @throws DeploymentDescException if the deployment descriptor is
723      * corrupted.
724      */

725     public static WebApp loadWebApp(Reader JavaDoc reader, String JavaDoc fileName) throws DeploymentDescException {
726
727         WebApp webApp = new WebApp();
728
729         // Create if null
730
if (webAppDigester == null) {
731             webAppDigester = new JDigester(webAppRuleSet, getParsingWithValidation(), true, new WebAppDTDs(),
732                     new WebAppSchemas());
733         }
734
735         try {
736             webAppDigester.parse(reader, fileName, webApp);
737         } catch (DeploymentDescException e) {
738             throw e;
739         } finally {
740             webAppDigester.push(null);
741         }
742         return webApp;
743     }
744
745     /**
746      * Load the jonas-web.xml file.
747      * @param reader the Reader of the XML file.
748      * @param fileName the name of the file (jonas-web.xml).
749      * @return a structure containing the result of the jonas-web.xml parsing.
750      * @throws DeploymentDescException if the deployment descriptor is
751      * corrupted.
752      */

753     public static JonasWebApp loadJonasWebApp(Reader JavaDoc reader, String JavaDoc fileName) throws DeploymentDescException {
754
755         JonasWebApp jonasWebApp = new JonasWebApp();
756
757         // Create if null
758
if (jonasWebAppDigester == null) {
759             jonasWebAppDigester = new JDigester(jonasWebAppRuleSet, getParsingWithValidation(), true,
760                     new JonasWebAppDTDs(), new JonasWebAppSchemas());
761         }
762
763         try {
764             jonasWebAppDigester.parse(reader, fileName, jonasWebApp);
765         } catch (DeploymentDescException e) {
766             throw e;
767         } finally {
768             jonasWebAppDigester.push(null);
769         }
770         return jonasWebApp;
771     }
772
773     /**
774      * Controls whether the parser is reporting all validity errors.
775      * @return if true, all external entities will be read.
776      */

777     public static boolean getParsingWithValidation() {
778         return parsingWithValidation;
779     }
780
781     /**
782      * Controls whether the parser is reporting all validity errors.
783      * @param validation if true, all external entities will be read.
784      */

785     public static void setParsingWithValidation(boolean validation) {
786         WebDeploymentDescManager.parsingWithValidation = validation;
787     }
788
789 }
Popular Tags