KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > web > AbsJWebContainerServiceImpl


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: AbsJWebContainerServiceImpl.java,v 1.65 2005/06/09 09:55:22 sauthieg Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.web;
27
28 import java.io.File JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.net.MalformedURLException JavaDoc;
31 import java.net.URL JavaDoc;
32 import java.net.URLClassLoader JavaDoc;
33 import java.rmi.RemoteException JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Enumeration JavaDoc;
36 import java.util.HashSet JavaDoc;
37 import java.util.Hashtable JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.Set JavaDoc;
41 import java.util.StringTokenizer JavaDoc;
42 import java.util.Vector JavaDoc;
43
44 import javax.management.InstanceAlreadyExistsException JavaDoc;
45 import javax.management.MBeanRegistrationException JavaDoc;
46 import javax.management.MBeanServer JavaDoc;
47 import javax.management.NotCompliantMBeanException JavaDoc;
48 import javax.naming.Context JavaDoc;
49 import javax.naming.LinkRef JavaDoc;
50 import javax.naming.NamingException JavaDoc;
51 import javax.naming.Reference JavaDoc;
52 import javax.naming.StringRefAddr JavaDoc;
53
54 import org.objectweb.jonas_lib.deployment.api.EjbLocalRefDesc;
55 import org.objectweb.jonas_lib.deployment.api.EjbRefDesc;
56 import org.objectweb.jonas_lib.deployment.api.EnvEntryDesc;
57 import org.objectweb.jonas_lib.deployment.api.MessageDestinationRefDesc;
58 import org.objectweb.jonas_lib.deployment.api.ResourceEnvRefDesc;
59 import org.objectweb.jonas_lib.deployment.api.ResourceRefDesc;
60 import org.objectweb.jonas_lib.loader.SimpleWebappClassLoader;
61 import org.objectweb.jonas_lib.loader.WebappClassLoader;
62 import org.objectweb.jonas_lib.naming.ContainerNaming;
63 import org.objectweb.jonas_lib.security.PermissionManagerException;
64
65 import org.objectweb.jonas_web.deployment.api.WebContainerDeploymentDesc;
66 import org.objectweb.jonas_web.deployment.api.WebContainerDeploymentDescException;
67 import org.objectweb.jonas_web.deployment.lib.wrapper.WebManagerWrapper;
68
69 import org.objectweb.jonas_ws.deployment.api.ServiceRefDesc;
70
71 import org.objectweb.jonas.common.JModule;
72 import org.objectweb.jonas.common.JProp;
73 import org.objectweb.jonas.common.Log;
74 import org.objectweb.jonas.ear.EarServiceImpl;
75 import org.objectweb.jonas.jmx.JmxService;
76 import org.objectweb.jonas.jmx.JonasObjectName;
77 import org.objectweb.jonas.naming.CompNamingContext;
78 import org.objectweb.jonas.naming.NamingManager;
79 import org.objectweb.jonas.server.LoaderManager;
80 import org.objectweb.jonas.service.AbsServiceImpl;
81 import org.objectweb.jonas.service.ServiceException;
82 import org.objectweb.jonas.service.ServiceManager;
83 import org.objectweb.jonas.web.lib.JarTools;
84 import org.objectweb.jonas.web.lib.PermissionManager;
85 import org.objectweb.jonas.ws.JServiceFactory;
86 import org.objectweb.jonas.ws.JServiceFactoryFinder;
87 import org.objectweb.jonas.ws.WebServicesService;
88
89 import org.objectweb.util.monolog.api.BasicLevel;
90 import org.objectweb.util.monolog.api.Logger;
91
92 /**
93  * This abstract class provides an implementation for a dynamic
94  * JWebContainerService service.
95  * @author Florent Benoit
96  * @author Ludovic Bert (J2EE 1.3)
97  * @author Nicolas Van Caneghem (exploded ear)
98  * @author Michel-Ange Anton (contributor)
99  */

100 public abstract class AbsJWebContainerServiceImpl extends AbsServiceImpl implements JWebContainerService,
101         AbsJWebContainerServiceImplMBean {
102
103     /**
104      * The name of the JONAS_BASE directory.
105      */

106     protected static final String JavaDoc JONAS_BASE = JProp.getJonasBase();
107
108     /**
109      * The name of the webapps directory.
110      */

111     protected static final String JavaDoc WEBAPPS_DIR = JONAS_BASE + File.separator + "webapps";
112
113     /**
114      * The name of the working directory.
115      */

116     protected static final String JavaDoc WORK_DIR = JProp.getWorkDir();
117
118     /**
119      * The name of the working apps directory.
120      */

121     protected static final String JavaDoc WORK_WEBAPPS_DIR = WORK_DIR + File.separator + "webapps";
122
123     /**
124      * Web service configuration properties : Files deployed
125      */

126     public static final String JavaDoc DESCRIPTORS = "jonas.service.web.descriptors";
127
128     /**
129      * Web service configuration properties : Autdeployed the files in these
130      * directories
131      */

132     public static final String JavaDoc AUTOLOADDIR = "jonas.service.web.autoloaddir";
133
134     /**
135      * Web service configuration properties : Xml parsing with validation
136      */

137     public static final String JavaDoc PARSINGWITHVALIDATION = "jonas.service.web.parsingwithvalidation";
138
139     /**
140      * Length of the extension .war
141      */

142     private static final int WAR_EXTENSION_LENGTH = 4;
143
144     /**
145      * Web service configuration properties : Implementation of the web
146      * container
147      */

148     public static final String JavaDoc CLASS = "jonas.service.web.class";
149
150     /**
151      * Logger for this service.
152      */

153     private static Logger logger = null;
154
155     /**
156      * The name of the current jonas server
157      */

158     private static String JavaDoc nameOfServer = null;
159
160     /**
161      * Reference on the NamingManager.
162      */

163     private ContainerNaming naming;
164
165     /**
166      * Associates an URL of an unpacked WAR file to its classloader.
167      */

168     private Hashtable JavaDoc warLoaders = new Hashtable JavaDoc();
169
170     /**
171      * Associates an URL of a deployed WAR file to its classloader.
172      */

173     private Hashtable JavaDoc warBindings = new Hashtable JavaDoc();
174
175     /**
176      * Reference to a MBean server.
177      */

178     private MBeanServer JavaDoc mbeanServer = null; // Bug Tomcat 5 doesn't remove MBean
179
// WebModule, normally the type is
180
// "private"
181

182     /**
183      * List of the war names to load when starting the Web container Service.
184      */

185     private Vector JavaDoc warNames = new Vector JavaDoc();
186
187     /**
188      * List of the war deployed by the Web container Service.
189      */

190     private Vector JavaDoc warDeployed = new Vector JavaDoc();
191
192     /**
193      * List of autoloaded directories.
194      */

195     private List JavaDoc autoloadDirectories = new ArrayList JavaDoc();
196
197     /**
198      * Name of the server
199      */

200     private String JavaDoc serverName = null;
201
202     /**
203      * Version of the server
204      */

205     private String JavaDoc serverVersion = null;
206
207     /**
208      * Reference to the WebServices service
209      */

210     private WebServicesService wsService = null;
211
212     /**
213      * Application Classloader
214      */

215     private ClassLoader JavaDoc appsClassLoader;
216
217     /**
218      * Initialize the service.
219      * @param ctx the configuration context of the service.
220      * @throws ServiceException if the initialization failed.
221      */

222     protected void doInit(Context JavaDoc ctx) throws ServiceException {
223
224         // Init the logger
225
logger = Log.getLogger(Log.JONAS_WEB_PREFIX);
226
227         // get apps ClassLoader
228
try {
229             LoaderManager lm = LoaderManager.getInstance();
230             appsClassLoader = lm.getAppsLoader();
231         } catch (Exception JavaDoc e) {
232             logger.log(BasicLevel.ERROR, "Cannot get the Applications ClassLoader from Web Container Service: " + e);
233             throw new ServiceException("Cannot get the Applications ClassLoader from Web Container Service", e);
234         }
235
236         ServiceManager sm = null;
237         try {
238             sm = ServiceManager.getInstance();
239         } catch (Exception JavaDoc e) {
240             String JavaDoc err = "Cannot get ServiceManager instance.";
241             logger.log(BasicLevel.ERROR, err);
242             throw new ServiceException(err, e);
243         }
244
245         // Get the JMX Server via JMX Service
246
try {
247             mbeanServer = ((JmxService) sm.getJmxService()).getJmxServer();
248         } catch (ServiceException e) {
249             // the JMX service may not be started
250
mbeanServer = null;
251         }
252
253         // Get the WebServices service
254
try {
255             wsService = (WebServicesService) sm.getWebServicesService();
256         } catch (ServiceException e) {
257             if (logger.isLoggable(BasicLevel.DEBUG)) {
258                 logger.log(BasicLevel.DEBUG, "WebServices service not started");
259             }
260             //not started
261
wsService = null;
262         }
263
264         // Set the XML parsing mode to no validation
265
String JavaDoc parsingMode = "false";
266         try {
267             parsingMode = (String JavaDoc) ctx.lookup(PARSINGWITHVALIDATION);
268         } catch (NamingException JavaDoc e) {
269             if (logger.isLoggable(BasicLevel.DEBUG)) {
270                 logger.log(BasicLevel.DEBUG, "No value for parsingWithValidation");
271             }
272             // No problem if there is no value for 'parsingwithvalidation'
273
// (false by default)
274
}
275
276         WebManagerWrapper.setParsingWithValidation("true".equalsIgnoreCase(parsingMode));
277         if (logger.isLoggable(BasicLevel.DEBUG)) {
278             if ("false".equalsIgnoreCase(parsingMode)) {
279                 logger.log(BasicLevel.DEBUG, "Web XML parsing without validation");
280             } else {
281                 logger.log(BasicLevel.DEBUG, "Web XML parsing with validation");
282             }
283         }
284
285         // Init the war names to be loaded when starting
286
String JavaDoc descsValue = null;
287         try {
288             descsValue = (String JavaDoc) ctx.lookup(DESCRIPTORS);
289         } catch (NamingException JavaDoc e) {
290             if (logger.isLoggable(BasicLevel.DEBUG)) {
291                 logger.log(BasicLevel.DEBUG, "No " + DESCRIPTORS);
292             }
293         }
294         if (descsValue != null) {
295             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(descsValue, ",");
296             while (st.hasMoreTokens()) {
297                 String JavaDoc fileName = st.nextToken().trim();
298                 warNames.add(fileName);
299             }
300         }
301         // Add the wars of the jonas.service.web.autoloaddir property
302
String JavaDoc dirValue = null;
303         ArrayList JavaDoc autoDirs = new ArrayList JavaDoc();
304         try {
305             dirValue = (String JavaDoc) ctx.lookup(AUTOLOADDIR);
306         } catch (NamingException JavaDoc e) {
307             if (logger.isLoggable(BasicLevel.DEBUG)) {
308                 logger.log(BasicLevel.DEBUG, "No " + AUTOLOADDIR);
309             }
310         }
311         if (dirValue != null) {
312             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(dirValue, ",");
313             while (st.hasMoreTokens()) {
314                 String JavaDoc dirName = st.nextToken().trim();
315                 addWars(dirName);
316                 autoDirs.add(dirName);
317             }
318         }
319         // Build autoload directories
320
File JavaDoc oFile;
321         Iterator JavaDoc it = autoDirs.iterator();
322         while (it.hasNext()) {
323             String JavaDoc dirName = (String JavaDoc) it.next();
324             try {
325                 oFile = new File JavaDoc(WEBAPPS_DIR, dirName);
326                 if (!oFile.exists()) {
327                     oFile = new File JavaDoc(dirName);
328                 }
329                 if (oFile.exists()) {
330                     autoloadDirectories.add(oFile.getCanonicalPath());
331                 }
332             } catch (Exception JavaDoc e) {
333                 String JavaDoc err = "Error when trying to verify Webapps autoload directory : " + dirName;
334                 logger.log(BasicLevel.ERROR, err + " " + e.getMessage());
335             }
336         }
337
338         //Init the naming manager
339
try {
340             naming = NamingManager.getInstance();
341         } catch (NamingException JavaDoc e) {
342             throw new ServiceException("Error when getting the reference to the Naming manager");
343         }
344
345         JProp jp = null;
346         try {
347             jp = JProp.getInstance();
348         } catch (Exception JavaDoc e) {
349             String JavaDoc err = "Error when trying to get jonas name ";
350             logger.log(BasicLevel.ERROR, err + " " + e.getMessage());
351             throw new ServiceException(err, e);
352         }
353         AbsJWebContainerServiceImpl.nameOfServer = jp.getValue("jonas.name", "jonas");
354
355     }
356
357     /**
358      * Start the service.
359      * @throws ServiceException if the startup failed.
360      */

361     protected void doStart() throws ServiceException {
362         // Deploy all wars which are in descriptors section
363
for (Enumeration JavaDoc e = warNames.elements(); e.hasMoreElements();) {
364             String JavaDoc fileName = (String JavaDoc) e.nextElement();
365
366             URL JavaDoc warURL = checkWarFile(fileName);
367
368             Context JavaDoc contctx = null;
369             try {
370                 contctx = new CompNamingContext(fileName);
371                 contctx.rebind("warURL", warURL);
372                 // No context root to rebind because not in ear case.
373
// => no rebind("contextRoot", ...)
374
} catch (NamingException JavaDoc ne) {
375                 String JavaDoc err = "Cannot start the WebContainerService";
376                 throw new ServiceException(err, ne);
377             }
378             try {
379                 registerWar(contctx);
380             } catch (JWebContainerServiceException jwcse) {
381                 String JavaDoc err = "Cannot deploy the file '" + warURL + "' : " + jwcse.getMessage();
382                 logger.log(BasicLevel.WARN, err);
383             }
384         }
385
386         try {
387             // Register JWebContainerService MBean :
388
// JWebContainerServiceImplMBean
389
if (mbeanServer != null) {
390                 mbeanServer.registerMBean(this, JonasObjectName.webContainerService());
391             }
392         } catch (InstanceAlreadyExistsException JavaDoc iae) {
393             String JavaDoc err = "Cannot start the WebContainerService Already Exists";
394             throw new ServiceException(err, iae);
395         } catch (MBeanRegistrationException JavaDoc mbre) {
396             throw new ServiceException("Cannot start the WebContainerService (MBean registration error)", mbre);
397         } catch (NotCompliantMBeanException JavaDoc ncmbe) {
398             throw new ServiceException("Cannot start the WebContainerService (MBean Not compliant error)", ncmbe);
399         }
400     }
401
402     /**
403      * Stop the service.
404      * @throws ServiceException if the stop failed.
405      */

406     protected void doStop() throws ServiceException {
407
408         // Get all the deployed war which are not in an ear application file
409
// and undeploy them.
410
for (int i = warDeployed.size() - 1; i >= 0; i--) {
411             War war = (War) warDeployed.elementAt(i);
412             URL JavaDoc warURL = war.getWarURL();
413             String JavaDoc fileName = warURL.getFile();
414             try {
415                 // Test if the war is in an ear application.
416
if (!war.isInEarCase()) {
417                     Context JavaDoc ctx = new CompNamingContext(fileName);
418                     ctx.rebind("warURL", warURL);
419                     ctx.rebind("isEarCase", new Boolean JavaDoc(false));
420                     unRegisterWar(ctx);
421                 }
422             } catch (Exception JavaDoc e) {
423                 //We don't stop the process of undeploying the wars.
424
//We only display an error in the logger.
425
String JavaDoc err = "Error when undeploying the war :" + fileName;
426                 logger.log(BasicLevel.ERROR, err + e.getMessage());
427             }
428         }
429
430         if (mbeanServer != null) {
431             try {
432                 // unregister AbsJWebContainerServiceImpl MBean
433
mbeanServer.unregisterMBean(JonasObjectName.webContainerService());
434             } catch (Exception JavaDoc e) {
435                 logger.log(BasicLevel.ERROR, "Cannot stop the WebContainerService: " + e);
436             }
437         }
438         if (logger.isLoggable(BasicLevel.DEBUG)) {
439             logger.log(BasicLevel.DEBUG, "WebContainerService stopped");
440         }
441     }
442
443     /**
444      * Create the environment and delegate the operation to the implementation
445      * of the web container.
446      * @param ctx the context which contains the configuration in order to
447      * deploy a WAR.
448      * @throws JWebContainerServiceException if the registration of the WAR
449      * failed.
450      */

451     protected abstract void doRegisterWar(Context JavaDoc ctx) throws JWebContainerServiceException;
452
453     /**
454      * Delegate the unregistration to the implementation of the web container.
455      * @param ctx the context which contains the configuration in order to
456      * undeploy a WAR.
457      * @throws JWebContainerServiceException if the unregistration failed.
458      */

459     protected abstract void doUnRegisterWar(Context JavaDoc ctx) throws JWebContainerServiceException;
460
461     /**
462      * Return the URL where warURL has been unpacked.
463      * @param warURL the URL of the war
464      * @param earAppName EAR Application name (can be null if not in EAR case)
465      * @return the URL where warURL has been unpacked.
466      * @throws JWebContainerServiceException when it is impossible to retrieve
467      * the unpacked URL.
468      */

469     protected URL JavaDoc getUnpackDir(URL JavaDoc warURL, String JavaDoc earAppName) throws JWebContainerServiceException {
470
471         String JavaDoc destDir = WORK_WEBAPPS_DIR + File.separator + nameOfServer + File.separator;
472
473         // Two cases :
474
// war is alone --> unpack it in :
475
// WEBAPPS_DIR/servername/filename_without_extension_dir/
476
// war come from an ear --> unpack it in :
477
// WEBAPPS_DIR/servername/ear_AppName/filename_without_extension_dir/
478

479         // location of unpacked War
480
URL JavaDoc unpackedWarURL = null;
481
482         String JavaDoc fileName = warURL.getFile();
483
484         // War file
485
if (new File JavaDoc(fileName).isFile()) {
486             // Note : if the war is alone, earAppName is null.
487
// ear case
488
if (earAppName != null) {
489                 destDir += earAppName + File.separator;
490             }
491
492             String JavaDoc fTemp = new File JavaDoc(fileName).getName();
493             destDir += fTemp.substring(0, fTemp.length() - WAR_EXTENSION_LENGTH);
494
495             // Unpack the war into a directory
496
JarTools.unpack(fileName, destDir);
497         } else {
498             // Directory (No unpack)
499
destDir = fileName;
500         }
501
502         try {
503             unpackedWarURL = new File JavaDoc(destDir).toURL();
504         } catch (MalformedURLException JavaDoc mue) {
505             throw new JWebContainerServiceException("Error", mue);
506         }
507
508         return unpackedWarURL;
509
510     }
511
512     /**
513      * Return the class loader of the given warURL. Unpack the associated war
514      * and build the loader if it's not in the cache.
515      * @param warURL the url of the war we want to get the loader
516      * @param earAppName the name of the ear application containing the war. May
517      * be null in non ear case.
518      * @param parentLoader the ejb class loader of the ear. May be null in non
519      * ear case.
520      * @return the class loader of the given warURL.
521      * @throws JWebContainerServiceException if the process failed.
522      */

523     public URLClassLoader JavaDoc getClassLoader(URL JavaDoc warURL, String JavaDoc earAppName, ClassLoader JavaDoc parentLoader)
524             throws JWebContainerServiceException {
525
526         URLClassLoader JavaDoc loaderForCls = null;
527         try {
528             WebLoaderHolder holder = (WebLoaderHolder) warLoaders.get(warURL);
529             if (holder != null) {
530                 loaderForCls = holder.getJonasWebLoader();
531             }
532         } catch (Exception JavaDoc e) {
533             throw new JWebContainerServiceException("Error when getting '" + warURL + "' in cache", e);
534         }
535
536         if (loaderForCls == null) {
537             // the war is not already used
538

539             // Use the unpacked directory
540
URL JavaDoc unpackedWarURL = getUnpackDir(warURL, earAppName);
541
542             try {
543                 if (parentLoader != null) {
544                     // ear case.
545
loaderForCls = new WebappClassLoader(unpackedWarURL, parentLoader);
546                 } else {
547                     //Case of non-ear application : only a single war
548
loaderForCls = new WebappClassLoader(unpackedWarURL, appsClassLoader);
549                 }
550             } catch (IOException JavaDoc ioe) {
551                 throw new JWebContainerServiceException(
552                         "Cannot create WebAppClassLoader from '" + unpackedWarURL + "'", ioe);
553             }
554
555             // add the class loader in cache.
556
try {
557                 WebLoaderHolder holder = new WebLoaderHolder(loaderForCls, null);
558                 warLoaders.put(warURL, holder);
559             } catch (Exception JavaDoc e) {
560                 throw new JWebContainerServiceException("Error when adding '" + warURL + "' in cache", e);
561             }
562         }
563
564         return loaderForCls;
565
566     }
567
568     /**
569      * @param warURL the URL of the webapp
570      * @return Returns the ClassLoader used to link a JNDI environnment to a webapp
571      */

572     public ClassLoader JavaDoc getContextLinkedClassLoader(URL JavaDoc warURL) {
573         WebLoaderHolder holder = (WebLoaderHolder) warLoaders.get(warURL);
574         if (holder != null) {
575             return holder.getEnvWebLoader();
576         }
577         return null;
578     }
579
580     /**
581      * Create the environment and delegate the operation to the implementation
582      * of the web container.
583      * @param ctx the context which contains the configuration in order to
584      * deploy a WAR.
585      * @throws JWebContainerServiceException if the registration of the WAR
586      * failed.
587      */

588     private void registerWar(Context JavaDoc ctx) throws JWebContainerServiceException {
589         // There are 6 possible parameters :
590
// - warURL is the URL of the war to deploy (required param).
591
// - parentClassLoader is the parent classloader of
592
// the web classloader (optional param).
593
// - earClassLoader is the ear classloader (optional param).
594
// - earURL is the URL of the ear (optional parameter :
595
// if earURL is set it means that we are in the ear case, else it
596
// means that we are in the non ear case).
597
// - altDD is the optional deployment descriptor (optional param).
598
// - contextRoot is the context root for the Web application
599
// (optional param).
600

601         // Get the URL of the ear application file in the case of an
602
// ear application, null otherwise.
603
URL JavaDoc earURL = null;
604         String JavaDoc contextRoot = null;
605         String JavaDoc earAppName = null;
606         try {
607             earURL = (URL JavaDoc) ctx.lookup("earURL");
608             contextRoot = (String JavaDoc) ctx.lookup("contextRoot");
609             earAppName = EarServiceImpl.buildJ2eeApplicationName(earURL);
610         } catch (NamingException JavaDoc e) {
611             if (earURL != null || contextRoot != null) {
612                 String JavaDoc err = "Error while getting parameter from context param :" + e.getMessage();
613                 logger.log(BasicLevel.ERROR, err);
614                 throw new JWebContainerServiceException(err, e);
615             } // else nothing to do : non-ear case.
616
}
617
618         // Get the URL of the war to deploy ...
619
URL JavaDoc warURL = null;
620         try {
621             warURL = (URL JavaDoc) ctx.lookup("warURL");
622         } catch (NamingException JavaDoc e) {
623             String JavaDoc err = "Error while getting parameter from context param :" + e.getMessage();
624             logger.log(BasicLevel.ERROR, err);
625             throw new JWebContainerServiceException(err, e);
626         }
627
628         // ... and check if the war to deploy exists.
629
File JavaDoc warFile = new File JavaDoc(warURL.getFile());
630         if (!warFile.exists()) {
631             String JavaDoc err = "registerWar: '" + warFile.getPath() + "' not found";
632             logger.log(BasicLevel.ERROR, err);
633             throw new JWebContainerServiceException(err);
634         }
635
636         // Check if the war to deploy is not already deployed.
637
War war = getWar(warURL);
638         if (war != null) {
639             // The war is already deployed.
640
String JavaDoc err = "Cannot deploy war '" + warURL.getFile() + "' is already deployed."
641                     + " You must undeploy the war before a new deployment.";
642             throw new JWebContainerServiceException(err);
643         }
644
645         URLClassLoader JavaDoc parentLoader = null;
646         URLClassLoader JavaDoc earClassLoader = null;
647         try {
648             parentLoader = (URLClassLoader JavaDoc) ctx.lookup("parentClassLoader");
649             earClassLoader = (URLClassLoader JavaDoc) ctx.lookup("earClassLoader");
650         } catch (NamingException JavaDoc ne) {
651             if (logger.isLoggable(BasicLevel.DEBUG)) {
652                 logger.log(BasicLevel.DEBUG, "Not an ear case");
653             }
654             // exception occurs when the earClassLoader is not found.
655
}
656
657         // get the war class loader.
658
URLClassLoader JavaDoc loaderForCls = getClassLoader(warURL, earAppName, parentLoader);
659
660         // get the directory where the war have been unpacked
661
URL JavaDoc unpackedWarURL = getUnpackDir(warURL, earAppName);
662
663         // Only if the WebServicesService is started
664
// And in non EAR case
665
if (wsService != null && earClassLoader == null) {
666             try {
667                 CompNamingContext contctx = null;
668                 try {
669                     contctx = new CompNamingContext(unpackedWarURL.getFile());
670                     contctx.rebind("unpackDir", unpackedWarURL.toExternalForm());
671                     contctx.rebind("jarUrls", new URL JavaDoc[0]);
672                     contctx.rebind("warUrls", new URL JavaDoc[] {warURL});
673                     if (parentLoader != null) {
674                         contctx.rebind("ejbClassLoader", parentLoader);
675                     }
676                 } catch (NamingException JavaDoc e) {
677                     String JavaDoc err = "Can not bind params for the WebServices service, "
678                             + "Can't deploy Web Services Endpoint";
679                     throw new JWebContainerServiceException(err, e);
680                 }
681                 wsService.deployWebServices(contctx);
682             } catch (ServiceException se) {
683                 String JavaDoc err = "Error during the deployment of the WebServices of the War file '" + warURL + "'";
684                 logger.log(BasicLevel.ERROR, err + " : " + se.getMessage());
685                 throw new JWebContainerServiceException(err, se);
686             }
687         }
688
689         // Get the deployment descriptor from file
690
WebContainerDeploymentDesc webDD = null;
691         try {
692             webDD = WebManagerWrapper.getDeploymentDesc(warURL, loaderForCls, earClassLoader);
693         } catch (WebContainerDeploymentDescException e) {
694             String JavaDoc err = "Cannot read the deployment descriptors '" + warURL.getFile() + "'";
695             logger.log(BasicLevel.ERROR, err + ": " + e);
696             e.printStackTrace(System.err);
697             throw new JWebContainerServiceException(err, e);
698         }
699
700         // Populate the java:comp/env (ENC) environment.
701
URLClassLoader JavaDoc webClassLoader = null;
702         try {
703             if (logger.isLoggable(BasicLevel.DEBUG)) {
704                 logger.log(BasicLevel.DEBUG, "Populating environment of the file " + warURL.getFile());
705             }
706             Context JavaDoc ctxParam = new CompNamingContext(unpackedWarURL.getFile());
707             ctxParam.rebind("DeploymentDesc", webDD);
708             ctxParam.rebind("warName", unpackedWarURL.getFile());
709             if (parentLoader == null) {
710                 webClassLoader = new SimpleWebappClassLoader(unpackedWarURL, appsClassLoader);
711             } else {
712                 webClassLoader = new SimpleWebappClassLoader(unpackedWarURL, parentLoader);
713             }
714             ctxParam.rebind("parentCL", webClassLoader);
715             // Do the populating of the java:comp/env (ENC) environment.
716
setWebEnvironment(ctxParam);
717         } catch (Exception JavaDoc e) {
718             //populating environment failed.
719
String JavaDoc err = "Error when populating ";
720             logger.log(BasicLevel.ERROR, err + e.getMessage());
721             throw new JWebContainerServiceException(err, e);
722         }
723
724         // TODO maybe to be removed ?
725
WebLoaderHolder holder = (WebLoaderHolder) warLoaders.get(warURL);
726         holder.setEnvWebLoader(webClassLoader);
727
728         // Set the right context root for the web application, the priority is
729
// the following :
730
// 1 - context-root of application.xml
731
// 2 - context-root of jonas-web.xml
732
// 3 - context-root is the name of the file without .war.
733
if (earClassLoader == null && contextRoot == null) {
734             String JavaDoc cRoot = webDD.getContextRoot();
735             if (cRoot == null) {
736                 String JavaDoc file = new File JavaDoc(unpackedWarURL.getFile()).getName();
737                 if (file.toLowerCase().endsWith(".war")) {
738                     contextRoot = file.substring(0, file.length() - WAR_EXTENSION_LENGTH);
739                 } else {
740                     //It's a directory which is deployed
741
contextRoot = file.substring(0, file.length());
742                 }
743             } else {
744                 contextRoot = cRoot;
745             }
746         }
747
748         // Set the name of the host where to deploy the war if it is
749
// specified in the jonas-web.xml.
750
String JavaDoc hostName = webDD.getHost();
751
752         // Check if the context to deploy is not already deployed.
753
List JavaDoc deployedWars = getWar(contextRoot);
754         for (Iterator JavaDoc itDeployed = deployedWars.iterator(); itDeployed.hasNext();) {
755             War deployedWar = (War) itDeployed.next();
756             String JavaDoc hostDeployed = deployedWar.getHostName();
757             if ((hostDeployed == null && hostName == null) || (hostDeployed != null && hostDeployed.equals(hostName))) {
758                 // The war is already deployed.
759
String JavaDoc err = "Cannot deploy war '" + warURL.getFile() + "' is already deployed with the context '"
760                         + contextRoot + "'." + " You must undeploy the war before a new deployment.";
761                 throw new JWebContainerServiceException(err);
762             }
763         }
764
765         // Context classloader must follow the java2 delegation model ?
766
boolean java2DelegationModel = webDD.getJava2DelegationModel();
767
768         // Create War
769
war = new War(warURL, earURL, hostName, contextRoot, java2DelegationModel, webDD.getXmlContent(), webDD
770                 .getJOnASXmlContent(), webDD.getServletsName());
771
772         // Configure JACC
773
PermissionManager permissionManager = null;
774         try {
775             permissionManager = new PermissionManager(webDD, war.getContextId());
776             permissionManager.translateServletDeploymentDescriptor();
777             // if not in ear case, commit the policy configuration, else it is done
778
// by EAR service after linking all policy configuration objects
779
if (earClassLoader == null) {
780                 permissionManager.commit();
781             }
782         } catch (Exception JavaDoc e) {
783             e.printStackTrace();
784             String JavaDoc err = "Cannot build permission manager object for the webapp '" + unpackedWarURL + "' : ";
785             logger.log(BasicLevel.ERROR, err + e.getMessage());
786             throw new JWebContainerServiceException(err, e);
787         }
788
789         // Configure the context to give to doRegisterWar ...
790
Context JavaDoc ctxParam = null;
791         try {
792             ctxParam = new CompNamingContext(unpackedWarURL.getFile());
793             ctxParam.rebind("warURL", warURL);
794             if (earURL != null) {
795                 ctxParam.rebind("earURL", earURL);
796             }
797             ctxParam.rebind("unpackedWarURL", unpackedWarURL);
798             ctxParam.rebind("parentCL", webClassLoader);
799             if (hostName != null) {
800                 ctxParam.rebind("hostName", hostName);
801             }
802             ctxParam.rebind("contextRoot", contextRoot);
803             if (earAppName != null) {
804                 ctxParam.rebind("earAppName", earAppName);
805             }
806             ctxParam.rebind("jonasDD", webDD.getJOnASXmlContent());
807             ctxParam.rebind("java2DelegationModel", new Boolean JavaDoc(java2DelegationModel));
808             ctxParam.rebind("permissionManager", permissionManager);
809         } catch (NamingException JavaDoc e) {
810             String JavaDoc err = "Error when deploying the war '" + unpackedWarURL.getFile() + "'";
811             logger.log(BasicLevel.ERROR, err + e.getMessage());
812             throw new JWebContainerServiceException(err, e);
813         }
814
815         war.setPermissionManager(permissionManager);
816
817         // ... and delegate the registration of the war to the wrapper.
818
doRegisterWar(ctxParam);
819
820         // Finaly indicates that the war is deployed.
821
warDeployed.addElement(war);
822         warBindings.put(warURL, webClassLoader);
823
824         try {
825             // Register Ear MBean : EarMBean
826
if (mbeanServer != null) {
827                 mbeanServer.registerMBean(war, JonasObjectName.war(warURL.getFile()));
828             }
829         } catch (Exception JavaDoc ware) {
830             logger.log(BasicLevel.ERROR, "Can not register the MBean for the war" + unpackedWarURL + " :"
831                     + ware.getMessage());
832         }
833
834         // Complete the Deployment of the WebServices
835
// Only if the WebServicesService is started
836
// And in non EAR case
837
if (wsService != null && earClassLoader == null) {
838             try {
839                 CompNamingContext contctx = null;
840                 try {
841                     contctx = new CompNamingContext(unpackedWarURL.getFile());
842                     contctx.rebind(WebServicesService.CLASSLOADER_CTX_PARAM, loaderForCls);
843                     contctx.rebind(WebServicesService.PARENT_OBJECTNAME_CTX_PARAM , ctxParam.lookup("WebModule"));
844                     contctx.rebind(WebServicesService.ISINEAR_CTX_PARAM, Boolean.FALSE);
845                 } catch (NamingException JavaDoc e) {
846                     String JavaDoc err = "Can not bind params for the WebServices service, "
847                             + "can't complete deployment of Web Services Endpoints";
848                     throw new JWebContainerServiceException(err, e);
849                 }
850                 wsService.completeWSDeployment(contctx);
851             } catch (ServiceException se) {
852                 String JavaDoc err = "Error during the deployment of the WebServices of the War file '" + warURL + "'";
853                 logger.log(BasicLevel.ERROR, err + " : " + se.getMessage());
854                 throw new JWebContainerServiceException(err, se);
855             }
856         }
857
858         StringBuffer JavaDoc txtInfo = new StringBuffer JavaDoc("War " + warURL.getFile() + " available at the context ");
859         if (!contextRoot.startsWith("/")) {
860             txtInfo.append("/");
861         }
862         txtInfo.append(contextRoot);
863
864         if (hostName != null) {
865             txtInfo.append(" on the host ");
866             txtInfo.append(hostName);
867         }
868         txtInfo.append(".");
869         logger.log(BasicLevel.INFO, txtInfo.toString());
870
871         // Remove the DD cache (WebServices)
872
if (wsService != null && earClassLoader == null) {
873             wsService.removeCache(loaderForCls);
874         }
875     }
876
877     /**
878      * Register a WAR by delegating the operation to the registerWar() method.
879      * This is used for JMX management.
880      * @param fileName the name of the war to deploy.
881      * @throws RemoteException if rmi call failed.
882      * @throws JWebContainerServiceException if the registration failed.
883      */

884     public void registerWarMBean(String JavaDoc fileName) throws RemoteException JavaDoc, JWebContainerServiceException {
885         // convert the file name to the appropriate url and check if the file
886
// exists.
887
URL JavaDoc warURL = checkWarFile(fileName);
888
889         // create the context to call the registerWar method.
890
Context JavaDoc ctx = null;
891         try {
892             ctx = new CompNamingContext(fileName);
893             ctx.rebind("warURL", warURL);
894         } catch (NamingException JavaDoc e) {
895             String JavaDoc err = "Error when deploying the war '" + fileName + "'";
896             logger.log(BasicLevel.ERROR, err + e.getMessage());
897             throw new JWebContainerServiceException(err, e);
898         }
899
900         // call the registerWar method.
901
registerWar(ctx);
902     }
903
904     /**
905      * Set the environment of the web container inside the given context.
906      * @param ctxParam the java:comp/env/ environment where is stored the values
907      * of the web container environment.
908      * @throws JWebContainerServiceException if the populating of the
909      * environment failed.
910      */

911     private void setWebEnvironment(Context JavaDoc ctxParam) throws JWebContainerServiceException {
912
913         WebContainerDeploymentDesc dd = null;
914         String JavaDoc warName = null;
915         ClassLoader JavaDoc parentClassLoader = null;
916
917         /*
918          * get the parameters - Deployment desc of xml - Name of the war -
919          * Parent Class loader
920          */

921         try {
922             dd = (WebContainerDeploymentDesc) ctxParam.lookup("DeploymentDesc");
923             warName = (String JavaDoc) ctxParam.lookup("warName");
924         } catch (NamingException JavaDoc e) {
925             String JavaDoc err = "Error while getting parameter from context param ";
926             logger.log(BasicLevel.ERROR, err + e.getMessage());
927             throw new JWebContainerServiceException(err, e);
928         }
929
930         //Get the parentCL
931
try {
932             parentClassLoader = (ClassLoader JavaDoc) ctxParam.lookup("parentCL");
933         } catch (NamingException JavaDoc e) {
934             String JavaDoc err = "Error while getting parameter from context param ";
935             logger.log(BasicLevel.ERROR, err + e.getMessage());
936             throw new JWebContainerServiceException(err, e);
937         }
938
939         //Create the java:comp/env entry and bind the entries
940
try {
941             // Create a JNDI context for this war java:comp/env
942
Context JavaDoc javaCtx = naming.createEnvironmentContext(warName);
943             naming.setComponentContext(javaCtx, parentClassLoader);
944             Context JavaDoc envCtx = javaCtx.createSubcontext("comp/env");
945
946             // War Environment entries
947
EnvEntryDesc[] envt = dd.getEnvEntryDesc();
948             for (int i = 0; i < envt.length; i++) {
949                 // get information in descriptor
950
String JavaDoc name = envt[i].getName();
951                 Object JavaDoc obj = envt[i].getValue();
952
953                 // register object in JNDI
954
if (logger.isLoggable(BasicLevel.DEBUG)) {
955                     logger.log(BasicLevel.DEBUG, warName + ": Binding object " + name + " -> " + obj);
956                 }
957                 envCtx.rebind(name, obj);
958             }
959
960             // Resource References
961
ResourceRefDesc[] resref = dd.getResourceRefDesc();
962             for (int i = 0; i < resref.length; i++) {
963                 // get information in descriptor
964
String JavaDoc name = resref[i].getName();
965                 String JavaDoc type = resref[i].getTypeName();
966                 String JavaDoc resname = resref[i].getJndiName();
967                 // register object in JNDI
968
if (logger.isLoggable(BasicLevel.DEBUG)) {
969                     logger.log(BasicLevel.DEBUG, warName + ": Linking resource " + name + " -> " + resname);
970                 }
971
972                 if (type.equalsIgnoreCase("java.net.URL")) {
973                     // Specify the factory to use with the right URL
974
Reference JavaDoc ref = new Reference JavaDoc("java.net.URL", "org.objectweb.jonas_lib.naming.factory.URLFactory",
975                             null);
976                     StringRefAddr JavaDoc refAddr = new StringRefAddr JavaDoc("url", resname);
977                     ref.add(refAddr);
978                     envCtx.rebind(name, ref);
979                 } else {
980                     // build the LinkRef that will be registered:
981
// FactoryClassName = null, size = 1, refAddr = resname.
982
LinkRef JavaDoc lref = new LinkRef JavaDoc(resname);
983                     envCtx.rebind(name, lref);
984                 }
985             }
986
987             // Resource Environment References
988
ResourceEnvRefDesc[] resEnvref = dd.getResourceEnvRefDesc();
989             for (int i = 0; i < resEnvref.length; i++) {
990                 // get information in descriptor
991
String JavaDoc name = resEnvref[i].getName();
992                 String JavaDoc resname = resEnvref[i].getJndiName();
993                 LinkRef JavaDoc lref = new LinkRef JavaDoc(resname);
994
995                 if (logger.isLoggable(BasicLevel.DEBUG)) {
996                     logger.log(BasicLevel.DEBUG, warName + ": Linking resource environment " + name + " -> " + resname);
997                 }
998                 envCtx.rebind(name, lref);
999             }
1000
1001            // EJB References
1002
EjbRefDesc[] ejbref = dd.getEjbRefDesc();
1003            for (int i = 0; i < ejbref.length; i++) {
1004                // get information in descriptor
1005
String JavaDoc name = ejbref[i].getEjbRefName();
1006                String JavaDoc ejbname = null;
1007                ejbname = ejbref[i].getJndiName();
1008
1009                LinkRef JavaDoc lref = new LinkRef JavaDoc(ejbname);
1010
1011                if (logger.isLoggable(BasicLevel.DEBUG)) {
1012                    logger.log(BasicLevel.DEBUG, warName + ": Linking ejb " + name + " -> " + ejbname);
1013                }
1014                envCtx.rebind(name, lref);
1015            }
1016
1017            // EJB Local Refs
1018
// We use here ejb-link tag. This should be used also for
1019
// ejb-ref when we are able to manage references to
1020
// another jar file.
1021
EjbLocalRefDesc[] ejblocalref = dd.getEjbLocalRefDesc();
1022            for (int i = 0; i < ejblocalref.length; i++) {
1023                String JavaDoc name = ejblocalref[i].getEjbRefName();
1024                String JavaDoc ejbname = ejblocalref[i].getJndiLocalName();
1025                LinkRef JavaDoc lref = new LinkRef JavaDoc(ejbname);
1026                if (logger.isLoggable(BasicLevel.DEBUG)) {
1027                    logger.log(BasicLevel.DEBUG, warName + ": Linking ejb " + name + " -> " + ejbname);
1028                }
1029                envCtx.rebind(name, lref);
1030            }
1031
1032            // Message Destination References
1033
MessageDestinationRefDesc[] mdref = dd.getMessageDestinationRefDesc();
1034            for (int i = 0; i < mdref.length; i++) {
1035                // get information in descriptor
1036
String JavaDoc name = mdref[i].getMessageDestinationRefName();
1037                String JavaDoc mdname = null;
1038                mdname = mdref[i].getJndiName();
1039
1040                LinkRef JavaDoc lref = new LinkRef JavaDoc(mdname);
1041
1042                if (logger.isLoggable(BasicLevel.DEBUG)) {
1043                    logger.log(BasicLevel.DEBUG, warName + ": Linking message-destination " + name + " -> " + mdname);
1044                }
1045                envCtx.rebind(name, lref);
1046            }
1047
1048            // Service Ref
1049
// We bind a Reference when full configuration is provided
1050
// Otherwise we bind only a LinkRef in JNDI
1051

1052            // get the JServiceFactory
1053
JServiceFactory factory = null;
1054
1055            ServiceRefDesc[] serviceRefs = dd.getServiceRefDesc();
1056            for (int i = 0; i < serviceRefs.length; i++) {
1057
1058                if (factory == null) {
1059                    factory = JServiceFactoryFinder.getJOnASServiceFactory();
1060                }
1061
1062                // Create the Service from the ServiceRef description
1063
String JavaDoc name = serviceRefs[i].getServiceRefName();
1064                // create a full Reference
1065
Reference JavaDoc ref = factory.getServiceReference(serviceRefs[i], parentClassLoader);
1066                envCtx.rebind(name, ref);
1067                if (logger.isLoggable(BasicLevel.DEBUG)) {
1068                    logger.log(BasicLevel.DEBUG, "Adding service-ref 'java:comp/env/" + name + "'");
1069                }
1070            }
1071
1072        } catch (NamingException JavaDoc e) {
1073            String JavaDoc err = "Error while populating environment of the war file " + warName;
1074            logger.log(BasicLevel.ERROR, err + " :" + e.getMessage());
1075            throw new JWebContainerServiceException(err, e);
1076        }
1077    }
1078
1079    /**
1080     * Delegate the unregistration to the implementation of the web container
1081     * and delete the environment associated to the WAR file.
1082     * @param ctx the context which contains the configuration in order to
1083     * undeploy a WAR.
1084     * @throws JWebContainerServiceException if the unregistration failed.
1085     */

1086    private void unRegisterWar(Context JavaDoc ctx) throws JWebContainerServiceException {
1087
1088        // Gets the 2 parameters :
1089
// warURL, isEarCase, hostName and contextRoot.
1090
URL JavaDoc warURL = null;
1091        boolean isEarCase = true;
1092        try {
1093            warURL = (URL JavaDoc) ctx.lookup("warURL");
1094            isEarCase = ((Boolean JavaDoc) ctx.lookup("isEarCase")).booleanValue();
1095        } catch (NamingException JavaDoc e) {
1096            String JavaDoc err = "Error while getting parameter from context param.";
1097            logger.log(BasicLevel.ERROR, err + e.getMessage());
1098            throw new JWebContainerServiceException(err, e);
1099        }
1100
1101        // get the file name for displaying message.
1102
String JavaDoc fileName = warURL.getFile();
1103
1104        // Check if the war is deployed.
1105
War war = null;
1106        war = getWar(warURL);
1107        if (war == null) {
1108            String JavaDoc err = "Cannot undeploy war: '" + fileName + "' is not deployed.";
1109            logger.log(BasicLevel.ERROR, err);
1110            throw new JWebContainerServiceException(err);
1111        }
1112
1113        // Check if the war can be undeployed (2 case authorized) :
1114
// - case EAR and war is in an EAR.
1115
// - case non EAR and war isn't in an EAR.
1116
if (isEarCase != war.isInEarCase()) {
1117            String JavaDoc err = "Cannot undeploy war: '" + fileName
1118                    + "' it is in an ear application. You must undeploy the ear associated.";
1119            logger.log(BasicLevel.ERROR, err);
1120            throw new JWebContainerServiceException(err);
1121        }
1122
1123        // Call the specific method of the implementation of the web container.
1124
try {
1125            //ctx.rebind("hostName", war.getHostName());
1126
ctx.rebind("contextRoot", war.getContextRoot());
1127            ctx.rebind("webClassLoader", warBindings.get(warURL));
1128        } catch (NamingException JavaDoc e) {
1129            String JavaDoc err = "Error when undeploying the war '" + fileName + "'";
1130            logger.log(BasicLevel.ERROR, err + e.getMessage());
1131            throw new JWebContainerServiceException(err, e);
1132        }
1133
1134        // Remove permission manager
1135
PermissionManager permissionManager = war.getPermissionManager();
1136        try {
1137            permissionManager.delete();
1138            permissionManager = null;
1139        } catch (PermissionManagerException pme) {
1140            logger.log(BasicLevel.ERROR, "Cannot remove permission manager for file '" + fileName + "'.", pme);
1141        }
1142
1143        doUnRegisterWar(ctx);
1144
1145        // undeploy webservices
1146
if (wsService != null) {
1147            wsService.undeployWebServices(ctx);
1148        }
1149
1150        // Remove the context.
1151
URLClassLoader JavaDoc loader = (URLClassLoader JavaDoc) warBindings.remove(warURL);
1152        naming.unSetComponentContext(loader);
1153
1154        // Remove classloader
1155
warLoaders.remove(warURL);
1156
1157        // Indicates that the war is not in the deployed war.
1158
// (here we are sure that the war is not null and isn't in an ear).
1159
warDeployed.removeElement(war);
1160
1161        //unregister mbean
1162
if (mbeanServer != null) {
1163            try {
1164                // unregister Ear MBean : EarMBean
1165
mbeanServer.unregisterMBean(JonasObjectName.war(fileName));
1166            } catch (Exception JavaDoc e) {
1167                logger
1168                        .log(BasicLevel.ERROR, "Cannot remove the MBean for the war " + fileName + " : "
1169                                + e.getMessage());
1170            }
1171        }
1172
1173        logger.log(BasicLevel.INFO, "War " + fileName + " no longer available");
1174    }
1175
1176    /**
1177     * Unregister a WAR by delegating the operation to the unRegisterWar()
1178     * method. This is used for JMX management.
1179     * @param fileName the name of the war to undeploy.
1180     * @throws RemoteException if rmi call failed.
1181     * @throws JWebContainerServiceException if the unregistration failed.
1182     */

1183    public void unRegisterWarMBean(String JavaDoc fileName) throws RemoteException JavaDoc, JWebContainerServiceException {
1184
1185        // Convert the given file name to an url and check if the war is
1186
// deployed ...
1187
URL JavaDoc warURL = checkWarDeployed(fileName);
1188
1189        // ... and do the undeployment.
1190
Context JavaDoc ctx = null;
1191        try {
1192            ctx = new CompNamingContext(fileName);
1193            ctx.rebind("warURL", warURL);
1194            ctx.rebind("isEarCase", new Boolean JavaDoc(false));
1195        } catch (NamingException JavaDoc e) {
1196            String JavaDoc err = "Error when undeploying the war file '" + fileName + "'";
1197            logger.log(BasicLevel.ERROR, err + e.getMessage());
1198            throw new JWebContainerServiceException(err, e);
1199        }
1200        unRegisterWar(ctx);
1201    }
1202
1203    /**
1204     * Deploy the given wars of an ear file with the specified parent
1205     * classloader (ejb classloader or ear classloader). (This method is only
1206     * used for the ear applications, not for the web applications).
1207     * @param ctx the context containing the configuration to deploy the wars.
1208     * <BR>This context contains the following parameters :<BR>- urls
1209     * the list of the urls of the wars to deploy. <BR>- earURL the URL
1210     * of the ear application file. <BR>- parentClassLoader the parent
1211     * classLoader of the wars. <BR>- earClassLoader the ear classLoader
1212     * of the j2ee app. <BR>- altDDs the optional URI of deployment
1213     * descriptor. <BR>- contextRoots the optional context root of the
1214     * wars. <BR>
1215     * @throws JWebContainerServiceException if an error occurs during the
1216     * deployment.
1217     */

1218    public void deployWars(Context JavaDoc ctx) throws JWebContainerServiceException {
1219        // Gets the parameters from the context :
1220
// - urls the list of the urls of the wars to deploy.
1221
// - earURL the URL of the ear application file.
1222
// - parentClassLoader the parent classLoader of the wars.
1223
// - earClassLoader the ear classLoader of the j2ee app.
1224
// - altDDs the optional URI of deployment descriptor.
1225
// - contextRoots the optional context root of the wars.
1226
URL JavaDoc[] urls = null;
1227        URL JavaDoc earURL = null;
1228        ClassLoader JavaDoc parentClassLoader = null;
1229        ClassLoader JavaDoc earClassLoader = null;
1230        URL JavaDoc[] altDDs = null;
1231        String JavaDoc[] contextRoots = null;
1232        try {
1233            urls = (URL JavaDoc[]) ctx.lookup("urls");
1234            earURL = (URL JavaDoc) ctx.lookup("earURL");
1235            parentClassLoader = (ClassLoader JavaDoc) ctx.lookup("parentClassLoader");
1236            earClassLoader = (ClassLoader JavaDoc) ctx.lookup("earClassLoader");
1237            altDDs = (URL JavaDoc[]) ctx.lookup("altDDs");
1238            contextRoots = (String JavaDoc[]) ctx.lookup("contextRoots");
1239        } catch (NamingException JavaDoc e) {
1240            String JavaDoc err = "Error while getting parameter from context param ";
1241            logger.log(BasicLevel.ERROR, err + e.getMessage());
1242            throw new JWebContainerServiceException(err, e);
1243        }
1244        //webDDManager.setAltDD(earClassLoader, urls, altDDs);
1245

1246        // Deploy all the wars of the ear application.
1247
for (int i = 0; i < urls.length; i++) {
1248            // Get the name of a war to deploy.
1249
String JavaDoc fileName = urls[i].getFile();
1250            if (logger.isLoggable(BasicLevel.DEBUG)) {
1251                logger.log(BasicLevel.DEBUG, "Deploy war '" + fileName + "' for the ear service");
1252            }
1253
1254            // The context to give for the creation of the container
1255
// associated to the ejb-jar.
1256
Context JavaDoc contctx = null;
1257            try {
1258                contctx = new CompNamingContext(fileName);
1259                contctx.rebind("warURL", urls[i]);
1260                contctx.rebind("parentClassLoader", parentClassLoader);
1261                contctx.rebind("earClassLoader", earClassLoader);
1262                contctx.rebind("earURL", earURL);
1263                if (altDDs[i] != null) {
1264                    contctx.rebind("altDD", altDDs[i]);
1265                }
1266                if (contextRoots[i] != null) {
1267                    contctx.rebind("contextRoot", contextRoots[i]);
1268                }
1269                registerWar(contctx);
1270            } catch (Exception JavaDoc e) {
1271                // A war is corrupted so undeploy all the deployed war
1272
// of the ear application.
1273
logger.log(BasicLevel.ERROR, "Error when deploying '" + fileName + "'");
1274                logger.log(BasicLevel.ERROR, e.getMessage());
1275                logger.log(BasicLevel.ERROR, "Undeploy war of the ear application");
1276
1277                for (int j = 0; j < i; j++) {
1278                    String JavaDoc warFileName = urls[j].getFile();
1279                    try {
1280                        // Try to undeploy a war of the ear application.
1281
CompNamingContext context = new CompNamingContext(warFileName);
1282                        context.rebind("warURL", urls[j]);
1283                        context.rebind("isEarCase", new Boolean JavaDoc(true));
1284                        unRegisterWar(context);
1285                    } catch (Exception JavaDoc ex) {
1286                        // Cannot undeploy a war of the ear application
1287
// So there is an error message.
1288
logger.log(BasicLevel.ERROR, "Error when undeploying '" + warFileName + "'");
1289                        logger.log(BasicLevel.ERROR, ex.getMessage());
1290                        logger.log(BasicLevel.ERROR, "Cannot undeploy war of the ear application");
1291                    }
1292
1293                }
1294                throw new JWebContainerServiceException("Error during the deployment", e);
1295            }
1296        }
1297    }
1298
1299    /**
1300     * Undeploy the given wars of an ear file with the specified parent
1301     * classloader (ejb classloader or ear classloader). (This method is only
1302     * used for the ear applications, not for the war applications).
1303     * @param urls the list of the urls of the wars to undeploy.
1304     */

1305    public void unDeployWars(URL JavaDoc[] urls) {
1306        for (int i = 0; i < urls.length; i++) {
1307            String JavaDoc warFileName = urls[i].getFile();
1308            try {
1309                // Try to undeploy a war of the ear application.
1310
CompNamingContext context = new CompNamingContext(warFileName);
1311                context.rebind("warURL", urls[i]);
1312                context.rebind("isEarCase", new Boolean JavaDoc(true));
1313                unRegisterWar(context);
1314            } catch (Exception JavaDoc ex) {
1315                // Cannot undeploy a war of the ear application
1316
// So there is an error message.
1317
logger.log(BasicLevel.ERROR, "Error when undeploying '" + warFileName + "'");
1318                logger.log(BasicLevel.ERROR, ex.getMessage());
1319                logger.log(BasicLevel.ERROR, "Cannot undeploy war of the ear application");
1320            }
1321        }
1322    }
1323
1324    /**
1325     * Get the war identified by its URL (.war).
1326     * @param url the URL of the war to get.
1327     * @return the war indentified by its URL, or null if the war is not found.
1328     */

1329    public War getWar(URL JavaDoc url) {
1330        Enumeration JavaDoc wars = warDeployed.elements();
1331        while (wars.hasMoreElements()) {
1332            War war = (War) wars.nextElement();
1333            if (war.getWarURL().equals(url)) {
1334                return war;
1335            }
1336        }
1337        return null;
1338    }
1339
1340    /**
1341     * Get a list of wars identified by their Context.
1342     * @param pContext the context of the war to get.
1343     * @return the list of wars indentified by their Context, or an empty list
1344     * if no wars are found.
1345     */

1346    private List JavaDoc getWar(String JavaDoc pContext) {
1347        List JavaDoc checkDeployed = new ArrayList JavaDoc();
1348        Enumeration JavaDoc wars = warDeployed.elements();
1349        while (wars.hasMoreElements()) {
1350            War war = (War) wars.nextElement();
1351            if (war.getContextRoot().equals(pContext)) {
1352                checkDeployed.add(war);
1353            }
1354        }
1355        return checkDeployed;
1356    }
1357
1358    /**
1359     * Make a cleanup of the cache of deployment descriptor. This method must be
1360     * invoked after the ear deployment by the EAR service.
1361     * @param earClassLoader the ClassLoader of the ear application to remove
1362     * from the cache.
1363     */

1364    public void removeCache(ClassLoader JavaDoc earClassLoader) {
1365        WebManagerWrapper.removeCache(earClassLoader);
1366    }
1367
1368    /**
1369     * Check if the specified file name correspond to a file which is located
1370     * relatively to where the JOnAS server is launched or in the
1371     * $JONAS_BASE/web-apps.
1372     * @param fileName the file to check if it exists.
1373     * @return the URL of the file found (either relatively to the JOnAS server
1374     * or to the $JONAS_BASE/web-apps).
1375     * @throws JWebContainerServiceException if the specified file does't
1376     * exists.
1377     */

1378    private URL JavaDoc checkWarFile(String JavaDoc fileName) throws JWebContainerServiceException {
1379
1380        File JavaDoc f = null;
1381        try {
1382            f = new File JavaDoc(fileName).getCanonicalFile();
1383            if (!f.exists()) {
1384                boolean found = false;
1385                String JavaDoc warFileName = null;
1386                // Check also in JONAS_BASE/webapps directory
1387
warFileName = WEBAPPS_DIR + File.separator + fileName;
1388                f = new File JavaDoc(warFileName).getCanonicalFile();
1389                found = f.exists();
1390                if (found) {
1391                    fileName = f.getPath();
1392                } else {
1393                    String JavaDoc err = "File '" + f.getPath() + "' not found";
1394                    logger.log(BasicLevel.ERROR, err);
1395                    throw new JWebContainerServiceException(err);
1396                }
1397            }
1398        } catch (IOException JavaDoc e) {
1399            String JavaDoc err = "Invalid war file name '" + fileName;
1400            logger.log(BasicLevel.ERROR, err);
1401            throw new JWebContainerServiceException(err, e);
1402        }
1403
1404        URL JavaDoc warURL = null;
1405        try {
1406            warURL = f.toURL();
1407        } catch (MalformedURLException JavaDoc e) {
1408            String JavaDoc err = "Invalid war file name '" + fileName + "'.";
1409            logger.log(BasicLevel.ERROR, err + e.getMessage());
1410            throw new JWebContainerServiceException(err, e);
1411        }
1412        return warURL;
1413    }
1414
1415    /**
1416     * Check if the specified file is already deployed in the JOnAS server and
1417     * return the URL of this deployed war file.
1418     * @param fileName the name of the WAR file to check.
1419     * @return the URL of the deployed war file.
1420     * @throws JWebContainerServiceException if the file name doesn't correspond
1421     * to a deployed war.
1422     */

1423    private URL JavaDoc checkWarDeployed(String JavaDoc fileName) throws JWebContainerServiceException {
1424
1425        URL JavaDoc url = null;
1426        try {
1427            Enumeration JavaDoc wars = warDeployed.elements();
1428            while (wars.hasMoreElements()) {
1429                War war = (War) wars.nextElement();
1430                url = (new File JavaDoc(fileName).getCanonicalFile()).toURL();
1431                if (war.getWarURL().equals(url)) {
1432                    return url;
1433                }
1434            }
1435            String JavaDoc warFileName = WEBAPPS_DIR + File.separator + fileName;
1436            wars = warDeployed.elements();
1437            while (wars.hasMoreElements()) {
1438                War war = (War) wars.nextElement();
1439                url = (new File JavaDoc(warFileName).getCanonicalFile()).toURL();
1440                if (war.getWarURL().equals(url)) {
1441                    return url;
1442                }
1443            }
1444
1445            String JavaDoc err = "Cannot undeploy war: '" + fileName + "' is not deployed.";
1446            logger.log(BasicLevel.ERROR, err);
1447            throw new JWebContainerServiceException(err);
1448
1449        } catch (MalformedURLException JavaDoc e) {
1450            String JavaDoc err = "Invalid war file name '" + fileName + "'.";
1451            logger.log(BasicLevel.ERROR, err + e.getMessage());
1452            throw new JWebContainerServiceException(err, e);
1453        } catch (IOException JavaDoc e) {
1454            String JavaDoc err = "Invalid war file name '" + fileName;
1455            logger.log(BasicLevel.ERROR, err);
1456            throw new JWebContainerServiceException(err, e);
1457        }
1458    }
1459
1460    /**
1461     * @return current number of wars deployed in the JOnAS server
1462     */

1463    public Integer JavaDoc getCurrentNumberOfWars() {
1464        return new Integer JavaDoc(warDeployed.size());
1465    }
1466
1467    /**
1468     * Return the list of installed web applications. The WAR files or the
1469     * directories with expanded web application are searched in
1470     * JONAS_BASE/webapps and all webapps directories 'autoload'.
1471     * @return The list of WAR files or the directories with expanded web
1472     * application found
1473     * @throws Exception if the list can't be retrieved
1474     */

1475    public List JavaDoc getInstalledWars() throws Exception JavaDoc {
1476        // get WAR files found in JONAS_BASE/webapps
1477
ArrayList JavaDoc al = JModule.getInstalledContainersInDir(WEBAPPS_DIR, JModule.WAR_EXTENSION, JModule.WAR_CHILD_DIR,
1478                JModule.WAR_CONFIRM_FILE);
1479        // get WAR files found in all autoload directories
1480
for (Iterator JavaDoc it = autoloadDirectories.iterator(); it.hasNext();) {
1481            al.addAll(JModule.getInstalledContainersInDir((String JavaDoc) it.next(), JModule.WAR_EXTENSION,
1482                    JModule.WAR_CHILD_DIR, JModule.WAR_CONFIRM_FILE));
1483        }
1484        return al;
1485    }
1486
1487    /**
1488     * This method is added temporarily. It will disapear when Wars will have
1489     * their associated MBeans (when Wars will become manageable)
1490     * @return the names of the wars currently deployed in the JOnAS server
1491     */

1492    public Set JavaDoc getWarNames() {
1493        HashSet JavaDoc names = new HashSet JavaDoc();
1494        for (Enumeration JavaDoc wars = warDeployed.elements(); wars.hasMoreElements();) {
1495            War war = (War) wars.nextElement();
1496            URL JavaDoc warURL = war.getWarURL();
1497            names.add(warURL.getFile());
1498        }
1499        return names;
1500    }
1501
1502    /**
1503     * Add wars from a directory (war can be directories with WEB-INF/
1504     * directories) If the dir has a relative path, this path is relative from
1505     * where the Application Server is launched. If the dir is not found it will
1506     * be searched in $JONAS_BASE/webapps/ directory.
1507     * @param dirPath the path to the directory containing the wars to load.
1508     */

1509    private void addWars(String JavaDoc dirPath) {
1510        boolean found = false;
1511
1512        // Look the directory relative to the $WEBAPPS_DIR directory
1513
File JavaDoc dir = new File JavaDoc(WEBAPPS_DIR + File.separator + dirPath);
1514        found = dir.isDirectory();
1515
1516        if (found) {
1517            addWarsFrom(dir);
1518        } else {
1519            String JavaDoc err = "Warning: Cannot load dir: '" + dirPath + "' ";
1520            err += "is not a directory or directory doesn't exist";
1521            logger.log(BasicLevel.WARN, err);
1522        }
1523    }
1524
1525    /**
1526     * Add the wars of the specified directory.
1527     * @param dir the directory from which the wars are loaded.
1528     * @throws JWebContainerServiceException if the argument is not a directory
1529     */

1530    private void addWarsFrom(File JavaDoc dir) throws JWebContainerServiceException {
1531        try {
1532            if (dir.isDirectory()) {
1533                File JavaDoc[] files = dir.listFiles();
1534                for (int i = 0; i < files.length; i++) {
1535                    if (files[i].isFile()) {
1536                        if (files[i].getPath().toLowerCase().endsWith(".war")) {
1537                            // War file
1538
warNames.add(files[i].getCanonicalPath());
1539                        }
1540                    } else {
1541                        // Directory webapp
1542
warNames.add(files[i].getCanonicalPath());
1543                    }
1544                }
1545            }
1546        } catch (IOException JavaDoc e) {
1547            String JavaDoc err = "Invalid file name '" + dir.getPath();
1548            logger.log(BasicLevel.ERROR, err);
1549            throw new JWebContainerServiceException(err, e);
1550        }
1551    }
1552
1553    /**
1554     * Test if the specified filename is already deployed or not
1555     * @param fileName the name of the war file.
1556     * @return true if the war is deployed, else false.
1557     */

1558    public boolean isWarLoaded(String JavaDoc fileName) {
1559
1560        URL JavaDoc url = null;
1561        boolean isLoaded = false;
1562        try {
1563            // Absolute filename
1564
try {
1565                url = new File JavaDoc(fileName).getCanonicalFile().toURL();
1566                //Check if the war is already deployed or not
1567
if (getWar(url) != null) {
1568                    isLoaded = true;
1569                } else {
1570                    // Not found force to test in relative Webapps directory
1571
url = null;
1572                }
1573            } catch (Exception JavaDoc e) {
1574                url = null;
1575            }
1576            // Relative filename
1577
if (url == null) {
1578                url = new File JavaDoc(WEBAPPS_DIR + File.separator + fileName).getCanonicalFile().toURL();
1579                //Check if the war is already deployed or not
1580
if (getWar(url) != null) {
1581                    isLoaded = true;
1582                }
1583            }
1584        } catch (Exception JavaDoc e) {
1585            String JavaDoc err = "Can not found if the war is deployed or not";
1586            logger.log(BasicLevel.ERROR, err);
1587            return false;
1588        }
1589
1590        return isLoaded;
1591    }
1592
1593    /**
1594     * Return the list of all loaded web applications.
1595     * @return The list of deployed web applications
1596     */

1597    public List JavaDoc getDeployedWars() {
1598        ArrayList JavaDoc al = new ArrayList JavaDoc();
1599        for (Enumeration JavaDoc wars = warDeployed.elements(); wars.hasMoreElements();) {
1600            War war = (War) wars.nextElement();
1601            URL JavaDoc warURL = war.getWarURL();
1602            al.add(warURL.getFile());
1603        }
1604        return al;
1605    }
1606
1607    /**
1608     * Return the list of installed web applications ready to deploy.
1609     * @return The list of deployable web applications
1610     * @throws Exception if the list can't be retrieved
1611     */

1612    public List JavaDoc getDeployableWars() throws Exception JavaDoc {
1613        List JavaDoc al = getInstalledWars();
1614        al.removeAll(getDeployedWars());
1615        return al;
1616    }
1617
1618    /**
1619     * Return the list of "autoload" directories for web applications.
1620     * @return The list of all "autoload" directories
1621     */

1622    public List JavaDoc getAutoloadDirectories() {
1623        ArrayList JavaDoc al = new ArrayList JavaDoc();
1624        for (Iterator JavaDoc it = autoloadDirectories.iterator(); it.hasNext();) {
1625            try {
1626                al.add(new File JavaDoc((String JavaDoc) it.next()).toURL().getPath());
1627            } catch (Exception JavaDoc e) {
1628                if (logger.isLoggable(BasicLevel.DEBUG)) {
1629                    logger.log(BasicLevel.DEBUG, "Can't get autoload directories " + e.getMessage());
1630                }
1631                // none
1632
}
1633        }
1634        return al;
1635    }
1636
1637    /**
1638     * Return the WebApps directory.
1639     * @return The WebApps directory
1640     */

1641    public String JavaDoc getWebappsDirectory() {
1642        String JavaDoc sRet = null;
1643        try {
1644            sRet = (new File JavaDoc(WEBAPPS_DIR)).toURL().getPath();
1645        } catch (Exception JavaDoc e) {
1646            if (logger.isLoggable(BasicLevel.DEBUG)) {
1647                logger.log(BasicLevel.DEBUG, "Can't get webapps directory " + e.getMessage());
1648            }
1649            // none
1650
}
1651        return sRet;
1652    }
1653
1654    /**
1655     * Gets the name of the server which is the web container
1656     * @return the name of the server which is the web container
1657     */

1658    public String JavaDoc getServerName() {
1659        if (serverName == null) {
1660            updateServerInfos();
1661        }
1662        return serverName;
1663    }
1664
1665    /**
1666     * Gets the version of the server which is the web container
1667     * @return the version of the server which is the web container
1668     */

1669    public String JavaDoc getServerVersion() {
1670        if (serverVersion == null) {
1671            updateServerInfos();
1672        }
1673        return serverVersion;
1674    }
1675
1676    /**
1677     * Update info of the serverName and serverVersion
1678     */

1679    protected abstract void updateServerInfos();
1680
1681    /**
1682     * Return the Default host name of the web container.
1683     * @return the Default host name of the web container.
1684     * @throws JWebContainerServiceException when it is impossible to get the
1685     * Default Host.
1686     */

1687    public abstract String JavaDoc getDefaultHost() throws JWebContainerServiceException;
1688
1689    /**
1690     * Return the Default HTTP port number of the web container (can be null if
1691     * multiple HTTP connector has been set).
1692     * @return the Default HTTP port number of the web container.
1693     * @throws JWebContainerServiceException when it is impossible to get the
1694     * Default Http port.
1695     */

1696    public abstract String JavaDoc getDefaultHttpPort() throws JWebContainerServiceException;
1697
1698    /**
1699     * Return the Default HTTPS port number of the web container (can be null if
1700     * multiple HTTPS connector has been set).
1701     * @return the Default HTTPS port number of the web container.
1702     * @throws JWebContainerServiceException when it is impossible to get the
1703     * Default Https port.
1704     */

1705    public abstract String JavaDoc getDefaultHttpsPort() throws JWebContainerServiceException;
1706
1707    /**
1708     * @return Returns the logger.
1709     */

1710    protected static Logger getLogger() {
1711        return logger;
1712    }
1713
1714    /**
1715     * @return Returns the naming.
1716     */

1717    protected ContainerNaming getNaming() {
1718        return naming;
1719    }
1720
1721    /**
1722     * @return Returns the mbeanServer.
1723     */

1724    protected MBeanServer JavaDoc getMbeanServer() {
1725        return mbeanServer;
1726    }
1727
1728    /**
1729     * @param serverName The serverName to set.
1730     */

1731    protected void setServerName(String JavaDoc serverName) {
1732        this.serverName = serverName;
1733    }
1734
1735    /**
1736     * @param serverVersion The serverVersion to set.
1737     */

1738    protected void setServerVersion(String JavaDoc serverVersion) {
1739        this.serverVersion = serverVersion;
1740    }
1741
1742    /**
1743     * Holds the ClassLoader used to retrieve the WebApp JNDI Context and the
1744     * JOnAS Webapp ClasLoader
1745     * @author Guillaume Sauthier
1746     */

1747    public class WebLoaderHolder {
1748
1749        /**
1750         * Web ClassLoader used by JOnAS.
1751         */

1752        private URLClassLoader JavaDoc jonasWebLoader;
1753
1754        /**
1755         * Web ClassLoader used to retrieve the JNDI Context of the webapp.
1756         */

1757        private ClassLoader JavaDoc envWebLoader;
1758
1759        /**
1760         * Constructs a WebLoaderHolder with jonas Loader and en Loader.
1761         * @param jonas JOnAS Webapp Loader
1762         * @param env Environnement ClassLoader
1763         */

1764        public WebLoaderHolder(final URLClassLoader JavaDoc jonas, final ClassLoader JavaDoc env) {
1765            jonasWebLoader = jonas;
1766            envWebLoader = env;
1767        }
1768
1769        /**
1770         * @return Returns the jonasWebLoader.
1771         */

1772        public URLClassLoader JavaDoc getJonasWebLoader() {
1773            return jonasWebLoader;
1774        }
1775
1776        /**
1777         * @return Returns the envWebLoader.
1778         */

1779        public ClassLoader JavaDoc getEnvWebLoader() {
1780            return envWebLoader;
1781        }
1782
1783        /**
1784         * @param envWebLoader The envWebLoader to set.
1785         */

1786        public void setEnvWebLoader(ClassLoader JavaDoc envWebLoader) {
1787            this.envWebLoader = envWebLoader;
1788        }
1789
1790        /**
1791         * @param jonasWebLoader The jonasWebLoader to set.
1792         */

1793        public void setJonasWebLoader(URLClassLoader JavaDoc jonasWebLoader) {
1794            this.jonasWebLoader = jonasWebLoader;
1795        }
1796    }
1797
1798}
1799
Popular Tags