KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > container > EJBServiceImpl


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: EJBServiceImpl.java,v 1.116 2005/07/28 13:35:38 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.container;
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.util.ArrayList JavaDoc;
34 import java.util.Enumeration JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.HashSet JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.Map JavaDoc;
40 import java.util.Properties JavaDoc;
41 import java.util.Set JavaDoc;
42 import java.util.StringTokenizer JavaDoc;
43 import java.util.Vector JavaDoc;
44 import java.util.jar.Attributes JavaDoc;
45 import java.util.jar.JarFile JavaDoc;
46 import java.util.jar.Manifest JavaDoc;
47
48 import javax.management.MBeanException JavaDoc;
49 import javax.management.MBeanServer JavaDoc;
50 import javax.management.ObjectName JavaDoc;
51 import javax.management.OperationsException JavaDoc;
52 import javax.management.modelmbean.ModelMBean JavaDoc;
53 import javax.naming.Context JavaDoc;
54 import javax.naming.InitialContext JavaDoc;
55 import javax.naming.NamingException JavaDoc;
56
57 import org.apache.commons.modeler.ManagedBean;
58 import org.apache.commons.modeler.Registry;
59
60 import org.objectweb.transaction.jta.TransactionManager;
61
62 import org.objectweb.carol.util.configuration.ConfigurationRepository;
63 import org.objectweb.carol.util.configuration.ProtocolConfiguration;
64
65 import org.objectweb.jonas_ejb.container.BeanFactory;
66 import org.objectweb.jonas_ejb.container.Container;
67 import org.objectweb.jonas_ejb.container.JContainer;
68 import org.objectweb.jonas_ejb.container.JEntityFactory;
69 import org.objectweb.jonas_ejb.container.JMdbEndpointFactory;
70 import org.objectweb.jonas_ejb.container.JMdbFactory;
71 import org.objectweb.jonas_ejb.container.JSessionFactory;
72 import org.objectweb.jonas_ejb.container.JStatelessFactory;
73 import org.objectweb.jonas_ejb.container.PermissionManager;
74 import org.objectweb.jonas_ejb.deployment.api.BeanDesc;
75 import org.objectweb.jonas_ejb.deployment.api.DeploymentDesc;
76 import org.objectweb.jonas_ejb.deployment.api.EntityBmpDesc;
77 import org.objectweb.jonas_ejb.deployment.api.EntityCmpDesc;
78 import org.objectweb.jonas_ejb.deployment.api.SessionStatefulDesc;
79 import org.objectweb.jonas_ejb.deployment.api.SessionStatelessDesc;
80 import org.objectweb.jonas_ejb.deployment.lib.wrapper.EjbManagerWrapper;
81
82 import org.objectweb.jonas_lib.JWorkManager;
83 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
84 import org.objectweb.jonas_lib.deployment.work.CleanerException;
85 import org.objectweb.jonas_lib.deployment.work.DeployerLog;
86 import org.objectweb.jonas_lib.deployment.work.DeployerLogException;
87 import org.objectweb.jonas_lib.deployment.work.FileManager;
88 import org.objectweb.jonas_lib.deployment.work.JarCleanTask;
89 import org.objectweb.jonas_lib.deployment.work.WorkCleaner;
90 import org.objectweb.jonas_lib.files.FileUtils;
91 import org.objectweb.jonas_lib.loader.EjbJarClassLoader;
92 import org.objectweb.jonas_lib.version.Version;
93
94 import org.objectweb.jonas_jms.api.JmsManager;
95
96 import org.objectweb.jonas.common.JModule;
97 import org.objectweb.jonas.common.JProp;
98 import org.objectweb.jonas.common.Log;
99 import org.objectweb.jonas.ear.EarServiceImpl;
100 import org.objectweb.jonas.jms.JmsService;
101 import org.objectweb.jonas.jmx.J2eeObjectName;
102 import org.objectweb.jonas.jmx.JmxService;
103 import org.objectweb.jonas.jmx.JonasObjectName;
104 import org.objectweb.jonas.jtm.TransactionService;
105 import org.objectweb.jonas.management.JonasMBeanTools;
106 import org.objectweb.jonas.naming.CompNamingContext;
107 import org.objectweb.jonas.naming.NamingManager;
108 import org.objectweb.jonas.security.SecurityService;
109 import org.objectweb.jonas.server.LoaderManager;
110 import org.objectweb.jonas.server.WorkManagerMBean;
111 import org.objectweb.jonas.service.AbsServiceImpl;
112 import org.objectweb.jonas.service.ServiceException;
113 import org.objectweb.jonas.service.ServiceManager;
114
115 import org.objectweb.util.monolog.api.BasicLevel;
116 import org.objectweb.util.monolog.api.Logger;
117
118 /**
119  * Implementation of the EJB Container Service for jonas. This class works only
120  * with jonas_ejb classes.
121  * @author Philippe Coq
122  * @author Jeff Mesnil (Security)
123  * @author Markus Karg (Novell port)
124  * @author Christophe Ney (for making easier Enhydra integration)
125  * @author Adriana Danes (complete management methods)
126  * @author Florent Benoit & Ludovic Bert (Ear service, deployJars, undeployJars)
127  * @author Benjamin Bonnet (max thread pool size)
128  * @author Michel-Ange Anton (JSR77 MBean : EJBModule)
129  * @author Adriana Danes (JSR77)
130  */

131 public class EJBServiceImpl extends AbsServiceImpl implements EJBService, EJBServiceImplMBean {
132
133     /** server logger */
134     private static Logger servlog = null;
135
136     /** loader logger */
137     private static Logger loaderlog = null;
138
139     /**
140      * Transaction manager reference (tm can be accessed directly from
141      * everywhere in the package.)
142      */

143     private TransactionManager tm = null;
144
145     /** JMS manager reference */
146     private JmsManager jms = null;
147
148     /** JMX Server */
149     private MBeanServer JavaDoc mbeanServer = null;
150
151     /** Security service reference */
152     private SecurityService securityService = null;
153
154     /**
155      * Reference to the cleaner
156      */

157     private static WorkCleaner workCleaner = null;
158
159     private Registry oRegistry = null;
160
161     /**
162      * List of the ejb names to load when starting the EJB Container Service
163      */

164     private Vector JavaDoc ejbNames = new Vector JavaDoc();
165
166     /** We have a Container by ejb-jar file. */
167     private Vector JavaDoc containers = new Vector JavaDoc();
168
169     /** WorkManager thread pool size */
170     public static final String JavaDoc MINWORKTHREADS = "jonas.service.ejb.minworkthreads";
171     private static final int DEFAULT_MINWORKTHREADS = 3;
172     public static final String JavaDoc MAXWORKTHREADS = "jonas.service.ejb.maxworkthreads ";
173     private static final int DEFAULT_MAXWORKTHREADS = 80;
174     public static final String JavaDoc THREADWAITTIMEOUT = "jonas.service.ejb.threadwaittimeout";
175     private static final int DEFAULT_THREADWAITTIMEOUT = 60;
176     private JWorkManager workManager;
177
178     /**
179      * The name of the JONAS_BASE directory.
180      */

181     protected static final String JavaDoc JONAS_BASE = JProp.getJonasBase();
182
183     /**
184      * The name of the directory containing the ejb-jar files.
185      */

186     protected static final String JavaDoc EJBJARS_DIR = JONAS_BASE + File.separator + "ejbjars";
187
188     /**
189      * List of autoloaded directories.
190      */

191     private ArrayList JavaDoc autoloadDirectories = new ArrayList JavaDoc();
192
193     /** base for properties */
194     public static final String JavaDoc JONAS_EJB = "jonas.service.ejb";
195
196     /** autoloaddir property */
197     public static final String JavaDoc AUTOLOADDIR = JONAS_EJB + ".autoloaddir";
198
199     /** parsing with validation property */
200     public static final String JavaDoc PARSINGWITHVALIDATION = JONAS_EJB + ".parsingwithvalidation";
201
202     /** descriptors property */
203     public static final String JavaDoc DESCRIPTORS = JONAS_EJB + ".descriptors";
204
205     /** EJB service class */
206     public static final String JavaDoc CLASS = JONAS_EJB + ".class";
207
208     /** BeanManaged Management properties */
209     public static final String JavaDoc BMP = "Bean-Managed";
210
211     /** ContainerManaged Management properties */
212     public static final String JavaDoc CMP = "Container-Managed";
213
214     /** applications parent classloader */
215     private ClassLoader JavaDoc appsClassLoader = null;
216
217     /**
218      * The name of the working directory.
219      */

220     private static final String JavaDoc WORK_DIR = JProp.getWorkDir();
221
222     /**
223      * The name of the working ejbjars directory.
224      */

225     private static final String JavaDoc WORK_EJBJARS_DIR = WORK_DIR + File.separator + "ejbjars";
226
227     /**
228      *
229      */

230     private static String JavaDoc ejbjarsDir = null;
231
232     /**
233      * Reference on the DeployerLog which is the class that manage the accesses
234      * to the log file (to remove the jar).
235      */

236     private DeployerLog jarDeployerLog = null;
237
238     // -------------------------------------------------------------------
239
// JOnAS Service Implementation
240
// -------------------------------------------------------------------
241

242     /**
243      * Init the Service. Configuration information is passed thru a Context
244      * object.
245      */

246     public void doInit(Context JavaDoc ctx) throws ServiceException {
247         servlog = Log.getLogger(Log.JONAS_SERVER_PREFIX);
248         loaderlog = Log.getLogger(Log.JONAS_LOADER_PREFIX);
249
250         if (servlog.isLoggable(BasicLevel.DEBUG)) {
251             servlog.log(BasicLevel.DEBUG, "");
252         }
253
254         // get apps ClassLoader
255
try {
256             LoaderManager lm = LoaderManager.getInstance();
257             appsClassLoader = lm.getAppsLoader();
258         } catch (Exception JavaDoc e) {
259             servlog.log(BasicLevel.ERROR, "Cannot get the Applications ClassLoader from EJB Container Service");
260             throw new ServiceException("Cannot get the Applications ClassLoader from EJB Container Service", e);
261         }
262
263         ServiceManager serviceManager = null;
264         try {
265             serviceManager = ServiceManager.getInstance();
266         } catch (Exception JavaDoc e) {
267             servlog.log(BasicLevel.ERROR, "Cannot initialize the EJB Container Service");
268             throw new ServiceException("Cannot initialize the EJB Container Service", e);
269         }
270         // Get the used services
271
try {
272             // Get the Transaction Manager via the jtm service
273
TransactionService jtmService = (TransactionService) serviceManager.getTransactionService();
274             tm = jtmService.getTransactionManager();
275         } catch (ServiceException e) {
276             servlog.log(BasicLevel.ERROR, "Cannot initialize the EJB Container Service");
277             throw new ServiceException("Cannot initialize the EJB Container Service", e);
278         }
279
280         try {
281             // Get the Security service
282
securityService = (SecurityService) serviceManager.getSecurityService();
283         } catch (ServiceException se) {
284             // The Security service may not be started
285
securityService = null;
286         }
287
288         try {
289             // Get the JMS manager via the JMS service
290
JmsService jmsService = (JmsService) serviceManager.getJmsService();
291             jms = jmsService.getJmsManager();
292         } catch (ServiceException e) {
293             // the JMS service may not be started
294
jms = null;
295         }
296
297         try {
298             // Get the JMX Server via JMX Service
299
JmxService jmxService = (JmxService) serviceManager.getJmxService();
300             mbeanServer = jmxService.getJmxServer();
301         } catch (ServiceException e) {
302             // the JMX service may not be started
303
mbeanServer = null;
304         }
305         oRegistry = JonasMBeanTools.getRegistry();
306
307         // Set the XML parsing mode to no validation
308
String JavaDoc parsingMode;
309         try {
310             parsingMode = (String JavaDoc) ctx.lookup(PARSINGWITHVALIDATION);
311         } catch (NamingException JavaDoc e) {
312             // No problem if there is no value for 'parsingwithvalidation'
313
// (false by default)
314
parsingMode = "false";
315         }
316         if ("false".equalsIgnoreCase(parsingMode)) {
317             EjbManagerWrapper.setParsingWithValidation(false);
318             if (servlog.isLoggable(BasicLevel.DEBUG)) {
319                 servlog.log(BasicLevel.DEBUG, "XML parsing without validation");
320             }
321         } else {
322             if (servlog.isLoggable(BasicLevel.DEBUG)) {
323                 servlog.log(BasicLevel.DEBUG, "XML parsing with validation");
324             }
325         }
326
327         // Init the ejb names to be loaded when starting
328
String JavaDoc descsValue;
329         try {
330             descsValue = (String JavaDoc) ctx.lookup(DESCRIPTORS);
331         } catch (NamingException JavaDoc e) {
332             // No problem if there is no value for 'descriptors' (no ejb to
333
// load)
334
descsValue = null;
335         }
336         if (descsValue != null) {
337             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(descsValue, ",");
338             while (st.hasMoreTokens()) {
339                 String JavaDoc fileName = st.nextToken().trim();
340                 ejbNames.add(fileName);
341             }
342         }
343
344         // Add the jars of the jonas.service.ejb.autoloaddir property
345
String JavaDoc dirValue;
346         ArrayList JavaDoc autoDirs = new ArrayList JavaDoc();
347         try {
348             dirValue = (String JavaDoc) ctx.lookup(AUTOLOADDIR);
349         } catch (NamingException JavaDoc e) {
350             // No problem if there is no value for 'autoloaddir'
351
//(no ejb to load)
352
dirValue = null;
353         }
354         if (dirValue != null) {
355             StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(dirValue, ",");
356             while (st.hasMoreTokens()) {
357                 String JavaDoc dirName = st.nextToken().trim();
358                 addEjbjars(dirName);
359                 autoDirs.add(dirName);
360             }
361         }
362         // Build autoload directories
363
File JavaDoc oFile;
364         for (int i = 0; i < autoDirs.size(); i++) {
365             try {
366                 oFile = new File JavaDoc(EJBJARS_DIR, autoDirs.get(i).toString());
367                 if (oFile.exists()) {
368                     autoloadDirectories.add(oFile.getCanonicalPath());
369                 } else {
370                     oFile = new File JavaDoc(autoDirs.get(i).toString());
371                 }
372             } catch (Exception JavaDoc e) {
373                 String JavaDoc err = "Error when trying to verify Ejbjar autoload directory : " + autoDirs.get(i);
374                 servlog.log(BasicLevel.ERROR, err, e);
375             }
376         }
377
378         // Create the WorkManager (thread Pool)
379
int minworkthreads = DEFAULT_MINWORKTHREADS;
380         try {
381             String JavaDoc plz = (String JavaDoc) ctx.lookup(MINWORKTHREADS);
382             minworkthreads = (new Integer JavaDoc(plz)).intValue();
383         } catch (NamingException JavaDoc e) {
384             // default value will be used.
385
}
386         int maxworkthreads = DEFAULT_MAXWORKTHREADS;
387         try {
388             String JavaDoc mxplz = (String JavaDoc) ctx.lookup(MAXWORKTHREADS);
389             maxworkthreads = (new Integer JavaDoc(mxplz)).intValue();
390         } catch (NamingException JavaDoc e) {
391             // default value will be used.
392
}
393         int threadwaittimeout = DEFAULT_THREADWAITTIMEOUT;
394         try {
395             String JavaDoc tto = (String JavaDoc) ctx.lookup(THREADWAITTIMEOUT);
396             threadwaittimeout = (new Integer JavaDoc(tto)).intValue();
397         } catch (NamingException JavaDoc e) {
398             // default value will be used.
399
}
400         workManager = new JWorkManager(minworkthreads, maxworkthreads, tm, threadwaittimeout);
401
402         // Create and register the MBean for this WorkManager
403
ObjectName JavaDoc mbn;
404         try {
405             mbn = JonasObjectName.workManager();
406             ManagedBean oManaged = oRegistry.findManagedBean("WorkManager");
407             ModelMBean JavaDoc oMBean = oManaged.createMBean(new WorkManagerMBean(workManager));
408             mbeanServer.registerMBean(oMBean, mbn);
409         } catch (Exception JavaDoc e) {
410             servlog.log(BasicLevel.ERROR, "Cannot create MBean", e);
411         }
412
413         if (servlog.isLoggable(BasicLevel.DEBUG)) {
414             servlog.log(BasicLevel.DEBUG, "EJB Container Service initialized");
415         }
416     }
417     
418     public JWorkManager getWorkManager() {
419         return workManager;
420     }
421
422     /**
423      * Start the Service Initialization of the service is already done.
424      */

425     public void doStart() throws ServiceException {
426
427         ejbjarsDir = WORK_EJBJARS_DIR + File.separator + getJonasServerName();
428
429         URL JavaDoc ejbjarsUrl = null;
430         try {
431             ejbjarsUrl = new File JavaDoc(ejbjarsDir).toURL();
432         } catch (MalformedURLException JavaDoc mue) {
433             throw new ServiceException("Error when trying to get the URL of the jonasroot/apps directory", mue);
434         }
435
436         File JavaDoc fLog = new File JavaDoc(ejbjarsUrl.getFile() + File.separator + getJonasServerName() + ".log");
437         if (!fLog.exists()) {
438             try {
439                 //create
440
fLog.getParentFile().mkdirs();
441                 fLog.createNewFile();
442             } catch (IOException JavaDoc e) {
443                 throw new ServiceException("cannot create the log file" + fLog, e);
444             }
445         }
446
447         //get the logger
448
try {
449             jarDeployerLog = new DeployerLog(fLog);
450         } catch (DeployerLogException e) {
451             throw new ServiceException("Can not get an EarDeployerLog", e);
452         }
453
454         // create the jar deployment work file control task
455
JarCleanTask jarCleanTask = new JarCleanTask(jarDeployerLog);
456
457         //get the cleaner ref
458
workCleaner = WorkCleaner.getInstance();
459
460         // add the jar deployment work file control task
461
try {
462             workCleaner.registerTask(jarCleanTask);
463         } catch (CleanerException ce) {
464             throw new ServiceException("Cannot register the JAR clean task", ce);
465
466         }
467
468         // force the cleaning immediately
469
workCleaner.executeTasks();
470
471         // Creates all predefined containers
472
// 1 container per ejbjar file.
473
String JavaDoc fileName = null;
474         Context JavaDoc contctx = null;
475         for (int i = 0; i < ejbNames.size(); i++) {
476             fileName = (String JavaDoc) ejbNames.elementAt(i);
477             try {
478                 contctx = new CompNamingContext(fileName);
479                 contctx.rebind("filename", fileName);
480             } catch (NamingException JavaDoc ne) {
481                 servlog.log(BasicLevel.WARN, "Cannot create container for " + fileName
482                         + " because of a NamingException : " + ne.toString());
483             }
484
485             try {
486                 createContainer(contctx);
487             } catch (Exception JavaDoc e) {
488                 servlog.log(BasicLevel.WARN, "Cannot create container for " + fileName, e);
489                 // Close naming context
490
try {
491                     contctx.close();
492                 } catch (NamingException JavaDoc nne) {
493                     if (servlog.isLoggable(BasicLevel.DEBUG)) {
494                         servlog.log(BasicLevel.DEBUG, "Cannot close deploy context for " + fileName, nne);
495                     }
496                 }
497             }
498         }
499
500         // Register EJBService MBean : EJBServiceImplMBean
501
if (mbeanServer != null) {
502             try {
503                 mbeanServer.registerMBean(this, JonasObjectName.ejbService());
504             } catch (MBeanException JavaDoc mbe) {
505                 // registration failed because a MBeanException
506
servlog.log(BasicLevel.WARN, "Cannot register the EJB Container Service in the MBean Server.");
507                 servlog.log(BasicLevel.WARN, "Regstration failure cause : " + mbe.getTargetException().toString());
508             } catch (OperationsException JavaDoc me) {
509                 // registration failed because a OperationsException JMX
510
// Exception
511
servlog.log(BasicLevel.WARN, "Cannot register the EJB Container Service in the MBean Server.", me);
512             }
513         }
514     }
515
516     /**
517      * Stop the service: - Remove all JOnAS Containers. - Unbinds all the
518      * EJBHome names from JNDI
519      */

520     public void doStop() {
521
522         Enumeration JavaDoc lc = containers.elements();
523         while (lc.hasMoreElements()) {
524             Container cont = (Container) lc.nextElement();
525             removeContainer(cont);
526         }
527         if (mbeanServer != null) {
528             try {
529                 // unregister EJBService MBean : EJBServiceImplMBean
530
mbeanServer.unregisterMBean(JonasObjectName.ejbService());
531             } catch (Exception JavaDoc e) {
532                 servlog.log(BasicLevel.ERROR, "Cannot stop the EJB Container Service", e);
533             }
534         }
535
536         if (servlog.isLoggable(BasicLevel.DEBUG)) {
537             servlog.log(BasicLevel.DEBUG, "EJB Container Service stopped");
538         }
539     }
540
541     // -------------------------------------------------------------------
542
// EJBService Implementation
543
// -------------------------------------------------------------------
544

545     /**
546      * Create a JOnAS Container for all the beans that are described in a .xml
547      * file, or belong to .jar file.
548      * @return The ObjectName of the MBean associated to the container (i.e. to
549      * the deployed module)
550      * @throws Exception if an error occur during the creation of the container.
551      */

552     public String JavaDoc createContainer(Context JavaDoc ctx) throws Exception JavaDoc {
553         if (servlog.isLoggable(BasicLevel.DEBUG)) {
554             servlog.log(BasicLevel.DEBUG, "");
555         }
556
557         // Get the file name
558
String JavaDoc fileName = (String JavaDoc) ctx.lookup("filename");
559         File JavaDoc f = new File JavaDoc(fileName).getCanonicalFile();
560
561         boolean isEjbJar = fileName.toLowerCase().endsWith(".jar");
562
563         // check if the file exists
564
// In case of the name is a ejb-jar file name, check also in
565
// the JONAS_BASE/ejbjars directory
566
try {
567             if (!f.exists() && isEjbJar) {
568                 String JavaDoc ejbjarFileName = EJBJARS_DIR + File.separator + fileName;
569                 f = new File JavaDoc(ejbjarFileName).getCanonicalFile();
570                 if (!f.exists()) {
571                     servlog.log(BasicLevel.ERROR, "createContainer: " + fileName + " not found");
572                     throw new NamingException JavaDoc(fileName + " not found");
573                 }
574             }
575         } catch (IOException JavaDoc e) {
576             String JavaDoc err = "Invalid ejbjar file name '" + fileName;
577             servlog.log(BasicLevel.ERROR, err);
578             throw new Exception JavaDoc(err, e);
579         }
580         boolean isEjbJarFile = isEjbJar && f.isFile();
581         boolean isEjbJarXml = fileName.toLowerCase().endsWith(".xml") && f.isFile();
582
583         // Force fileName with slash to avoid conflict under Windows (slash and
584
// back-slash)
585
try {
586             fileName = f.toURL().getPath();
587         } catch (MalformedURLException JavaDoc e) {
588             servlog.log(BasicLevel.ERROR, "Invalid ejb-jar file name '" + fileName + "'", e);
589         }
590
591         if (f.isFile()) {
592             if (!isEjbJar && !isEjbJarXml) {
593                 throw new ServiceException("The ejbjar to deploy is not a jar file nor an xml file");
594             }
595         }
596
597         // Check if container already exists
598
if (getContainer(fileName) != null) {
599             servlog.log(BasicLevel.ERROR, "createContainer: " + fileName + " already exists");
600             throw new Exception JavaDoc("Container already exists");
601         }
602
603         // In order to avoid lock problem after undeploy
604
// the jar file is copied under the working directory before its loading
605
// ! only if the ejbjars isn't included in an ear
606
boolean isInEar = true;
607         try {
608             ctx.lookup("earClassLoader");
609         } catch (NamingException JavaDoc ne) {
610             isInEar = false;
611         }
612
613         // if need, call Genic. Only in single ejb-jar mode.
614
// In ear case, this has been checked by ear service.
615
if (!isInEar) {
616             checkGenIC(fileName);
617         }
618
619
620         String JavaDoc wkFileName = null;
621
622         if (!isInEar && isEjbJarFile) {
623             // Get the destination file
624
wkFileName = ejbjarsDir + File.separator + FileManager.fileToTimeStampDir(f.toURL(), ".jar");
625
626             if (servlog.isLoggable(BasicLevel.DEBUG)) {
627                 servlog.log(BasicLevel.DEBUG, "filename=" + fileName);
628                 servlog.log(BasicLevel.DEBUG, "wkFilename=" + wkFileName);
629             }
630
631             // copy the file
632
FileUtils.copyFile(fileName, wkFileName);
633
634             // add an entry in the log file
635
try {
636                 jarDeployerLog.addEntry(f, new File JavaDoc(wkFileName));
637             } catch (DeployerLogException e) {
638                 String JavaDoc err = "Error while adding the " + fileName + " entry in the log file";
639                 servlog.log(BasicLevel.ERROR, err + " : " + e.getMessage());
640                 throw new Exception JavaDoc(err, e);
641             }
642
643             if (servlog.isLoggable(BasicLevel.DEBUG)) {
644                 servlog.log(BasicLevel.DEBUG, "Create wk file :" + wkFileName);
645             }
646
647         } else {
648             // in this case, no needs to copy the file
649
wkFileName = fileName;
650             if (servlog.isLoggable(BasicLevel.DEBUG)) {
651                 servlog.log(BasicLevel.DEBUG, "No needs wk file :" + wkFileName);
652             }
653         }
654
655         // Classloader
656
URL JavaDoc[] url = null;
657         try {
658             url = new URL JavaDoc[1];
659             url[0] = (new File JavaDoc(wkFileName)).toURL();
660         } catch (MalformedURLException JavaDoc e) {
661             servlog.log(BasicLevel.ERROR, "Invalid ejb-jar file name '" + wkFileName + "'", e);
662         }
663
664         // Get the classloaders
665
URLClassLoader JavaDoc ejbClassLoader = null;
666         URLClassLoader JavaDoc earClassLoader = null;
667         try {
668             earClassLoader = (URLClassLoader JavaDoc) ctx.lookup("earClassLoader");
669             ejbClassLoader = (URLClassLoader JavaDoc) ctx.lookup("ejbClassLoader");
670             if (loaderlog.isLoggable(BasicLevel.DEBUG)) {
671                 loaderlog.log(BasicLevel.DEBUG, "earClassLoader=" + earClassLoader);
672             }
673         } catch (NamingException JavaDoc ne) {
674             // no ear classloader => ejb application case (not ear case).
675
if (isEjbJar) {
676                 ejbClassLoader = new EjbJarClassLoader(url, appsClassLoader);
677             } else {
678                 ejbClassLoader = (URLClassLoader JavaDoc) appsClassLoader;
679             }
680             if (loaderlog.isLoggable(BasicLevel.DEBUG)) {
681                 loaderlog.log(BasicLevel.DEBUG, "parent Loader=" + appsClassLoader);
682             }
683         }
684         if (loaderlog.isLoggable(BasicLevel.DEBUG)) {
685             loaderlog.log(BasicLevel.DEBUG, "ejbClassLoader=" + ejbClassLoader);
686         }
687
688         // Get the deployment descriptor from file
689
DeploymentDesc dd = null;
690         try {
691             // ejb-jar file
692
dd = EjbManagerWrapper.getDeploymentDesc(url[0], ejbClassLoader, earClassLoader);
693         } catch (DeploymentDescException e) {
694             String JavaDoc err = "Cannot read the deployment descriptors '" + fileName + "'";
695             servlog.log(BasicLevel.ERROR, err);
696             throw new ServiceException(err, e);
697         }
698
699         // container name
700
String JavaDoc cname = buildEJBModuleName(fileName);
701         if (cname == null) {
702             //use displayName
703
cname = "EJB container ";
704             if (dd.getDisplayName() != null) {
705                 cname += dd.getDisplayName();
706             }
707         }
708
709         // Create the Container Object
710
JContainer cont = new JContainer(cname, fileName, wkFileName, ejbClassLoader);
711         cont.setContainerNaming(NamingManager.getInstance());
712         cont.setTransactionManager(tm);
713         cont.setPrincipalFactory(new PrincipalFactoryImpl());
714         cont.setJmsManager(jms);
715
716         // Set the name of the ear application containing this container
717
// in the case of an ear application.
718
String JavaDoc earFileName;
719         try {
720             earFileName = (String JavaDoc) ctx.lookup("earFileName");
721