00001
00026 package org.objectweb.jonas.container;
00027
00028 import java.io.File;
00029 import java.io.IOException;
00030 import java.net.MalformedURLException;
00031 import java.net.URL;
00032 import java.net.URLClassLoader;
00033 import java.util.ArrayList;
00034 import java.util.Enumeration;
00035 import java.util.HashMap;
00036 import java.util.HashSet;
00037 import java.util.Iterator;
00038 import java.util.List;
00039 import java.util.Map;
00040 import java.util.Properties;
00041 import java.util.Set;
00042 import java.util.StringTokenizer;
00043 import java.util.Vector;
00044
00045 import javax.management.MBeanException;
00046 import javax.management.MBeanServer;
00047 import javax.management.ObjectName;
00048 import javax.management.OperationsException;
00049 import javax.management.modelmbean.ModelMBean;
00050 import javax.naming.Context;
00051 import javax.naming.InitialContext;
00052 import javax.naming.NamingException;
00053
00054 import org.apache.commons.modeler.ManagedBean;
00055 import org.apache.commons.modeler.Registry;
00056
00057 import org.objectweb.transaction.jta.TransactionManager;
00058
00059 import org.objectweb.jonas_ejb.container.BeanFactory;
00060 import org.objectweb.jonas_ejb.container.Container;
00061 import org.objectweb.jonas_ejb.container.JContainer;
00062 import org.objectweb.jonas_ejb.container.JEntityFactory;
00063 import org.objectweb.jonas_ejb.container.JMdbEndpointFactory;
00064 import org.objectweb.jonas_ejb.container.JMdbFactory;
00065 import org.objectweb.jonas_ejb.container.JSessionFactory;
00066 import org.objectweb.jonas_ejb.container.JStatelessFactory;
00067 import org.objectweb.jonas_ejb.container.PermissionManager;
00068 import org.objectweb.jonas_ejb.container.ThreadPool;
00069 import org.objectweb.jonas_ejb.deployment.api.BeanDesc;
00070 import org.objectweb.jonas_ejb.deployment.api.DeploymentDesc;
00071 import org.objectweb.jonas_ejb.deployment.api.EntityBmpDesc;
00072 import org.objectweb.jonas_ejb.deployment.api.EntityCmpDesc;
00073 import org.objectweb.jonas_ejb.deployment.api.SessionStatefulDesc;
00074 import org.objectweb.jonas_ejb.deployment.api.SessionStatelessDesc;
00075 import org.objectweb.jonas_ejb.deployment.lib.wrapper.EjbManagerWrapper;
00076
00077 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
00078 import org.objectweb.jonas_lib.deployment.work.CleanerException;
00079 import org.objectweb.jonas_lib.deployment.work.DeployerLog;
00080 import org.objectweb.jonas_lib.deployment.work.DeployerLogException;
00081 import org.objectweb.jonas_lib.deployment.work.FileManager;
00082 import org.objectweb.jonas_lib.deployment.work.JarCleanTask;
00083 import org.objectweb.jonas_lib.deployment.work.WorkCleaner;
00084 import org.objectweb.jonas_lib.loader.EjbJarClassLoader;
00085
00086 import org.objectweb.jonas_jms.api.JmsManager;
00087
00088 import org.objectweb.jonas.common.JModule;
00089 import org.objectweb.jonas.common.JProp;
00090 import org.objectweb.jonas.common.Log;
00091 import org.objectweb.jonas.ear.EarServiceImpl;
00092 import org.objectweb.jonas.jms.JmsService;
00093 import org.objectweb.jonas.jmx.J2eeObjectName;
00094 import org.objectweb.jonas.jmx.JmxService;
00095 import org.objectweb.jonas.jmx.JonasObjectName;
00096 import org.objectweb.jonas.jtm.TransactionService;
00097 import org.objectweb.jonas.management.JonasMBeanTools;
00098 import org.objectweb.jonas.naming.CompNamingContext;
00099 import org.objectweb.jonas.naming.NamingManager;
00100 import org.objectweb.jonas.security.SecurityService;
00101 import org.objectweb.jonas.server.LoaderManager;
00102 import org.objectweb.jonas.service.AbsServiceImpl;
00103 import org.objectweb.jonas.service.ServiceException;
00104 import org.objectweb.jonas.service.ServiceManager;
00105
00106 import org.objectweb.util.monolog.api.BasicLevel;
00107 import org.objectweb.util.monolog.api.Logger;
00108
00122 public class EJBServiceImpl extends AbsServiceImpl implements EJBService, EJBServiceImplMBean {
00123
00125 private static Logger servlog = null;
00126
00128 private static Logger loaderlog = null;
00129
00134 private TransactionManager tm = null;
00135
00137 private JmsManager jms = null;
00138
00140 private MBeanServer mbeanServer = null;
00141
00143 private SecurityService securityService = null;
00144
00148 private static WorkCleaner workCleaner = null;
00149
00153 private Vector ejbNames = new Vector();
00154
00156 private Vector containers = new Vector();
00157
00159 private static final int DEFAULT_THREAD_POOL_SIZE = 0;
00160
00162 private int threadPoolSize;
00163
00165 private int maxthreadPoolSize;
00166
00170 protected static final String JONAS_BASE = JProp.getJonasBase();
00171
00175 protected static final String EJBJARS_DIR = JONAS_BASE + File.separator + "ejbjars";
00176
00180 private ArrayList autoloadDirectories = new ArrayList();
00181
00183 public static final String JONAS_EJB = "jonas.service.ejb";
00184
00186 public static final String AUTOLOADDIR = JONAS_EJB + ".autoloaddir";
00187
00189 public static final String MDBTHREADPOOLSIZE = JONAS_EJB + ".mdbthreadpoolsize";
00190
00192 public static final String MDBMAXTHREADPOOLSIZE = JONAS_EJB + ".mdbmaxthreadpoolsize";
00193
00195 public static final String PARSINGWITHVALIDATION = JONAS_EJB + ".parsingwithvalidation";
00196
00198 public static final String DESCRIPTORS = JONAS_EJB + ".descriptors";
00199
00201 public static final String CLASS = JONAS_EJB + ".class";
00202
00204 public static final String BMP = "Bean-Managed";
00205
00207 public static final String CMP = "Container-Managed";
00208
00210 private ClassLoader appsClassLoader = null;
00211
00215 private static final String WORK_DIR = JProp.getWorkDir();
00216
00220 private static final String WORK_EJBJARS_DIR = WORK_DIR + File.separator + "ejbjars";
00221
00225 private static String ejbjarsDir = null;
00226
00231 private DeployerLog jarDeployerLog = null;
00232
00233
00234
00235
00236
00241 public void doInit(Context ctx) throws ServiceException {
00242 servlog = Log.getLogger(Log.JONAS_SERVER_PREFIX);
00243 loaderlog = Log.getLogger(Log.JONAS_LOADER_PREFIX);
00244
00245 servlog.log(BasicLevel.DEBUG, "");
00246
00247
00248 try {
00249 LoaderManager lm = LoaderManager.getInstance();
00250 appsClassLoader = lm.getAppsLoader();
00251 } catch (Exception e) {
00252 servlog.log(BasicLevel.ERROR, "Cannot get the Applications ClassLoader from EJB Container Service: " + e);
00253 throw new ServiceException("Cannot get the Applications ClassLoader from EJB Container Service", e);
00254 }
00255
00256 ServiceManager serviceManager = null;
00257 try {
00258 serviceManager = ServiceManager.getInstance();
00259 } catch (Exception e) {
00260 servlog.log(BasicLevel.ERROR, "Cannot initialize the EJB Container Service: " + e);
00261 throw new ServiceException("Cannot initialize the EJB Container Service", e);
00262 }
00263
00264 try {
00265
00266 TransactionService jtmService = (TransactionService) serviceManager.getTransactionService();
00267 tm = jtmService.getTransactionManager();
00268 } catch (ServiceException e) {
00269 servlog.log(BasicLevel.ERROR, "Cannot initialize the EJB Container Service: " + e);
00270 throw new ServiceException("Cannot initialize the EJB Container Service", e);
00271 }
00272
00273 try {
00274
00275 securityService = (SecurityService) serviceManager.getSecurityService();
00276 } catch (ServiceException se) {
00277
00278 securityService = null;
00279 }
00280
00281 try {
00282
00283 JmsService jmsService = (JmsService) serviceManager.getJmsService();
00284 jms = jmsService.getJmsManager();
00285 } catch (ServiceException e) {
00286
00287 jms = null;
00288 }
00289
00290 try {
00291
00292 JmxService jmxService = (JmxService) serviceManager.getJmxService();
00293 mbeanServer = jmxService.getJmxServer();
00294 } catch (ServiceException e) {
00295
00296 mbeanServer = null;
00297 }
00298
00299
00300 String parsingMode;
00301 try {
00302 parsingMode = (String) ctx.lookup(PARSINGWITHVALIDATION);
00303 } catch (NamingException e) {
00304
00305
00306 parsingMode = "false";
00307 }
00308 if ("false".equalsIgnoreCase(parsingMode)) {
00309 EjbManagerWrapper.setParsingWithValidation(false);
00310 servlog.log(BasicLevel.DEBUG, "XML parsing without validation");
00311 } else {
00312 servlog.log(BasicLevel.DEBUG, "XML parsing with validation");
00313 }
00314
00315
00316 String descsValue;
00317 try {
00318 descsValue = (String) ctx.lookup(DESCRIPTORS);
00319 } catch (NamingException e) {
00320
00321
00322 descsValue = null;
00323 }
00324 if (descsValue != null) {
00325 StringTokenizer st = new StringTokenizer(descsValue, ",");
00326 while (st.hasMoreTokens()) {
00327 String fileName = st.nextToken().trim();
00328 ejbNames.add(fileName);
00329 }
00330 }
00331
00332
00333 String dirValue;
00334 ArrayList autoDirs = new ArrayList();
00335 try {
00336 dirValue = (String) ctx.lookup(AUTOLOADDIR);
00337 } catch (NamingException e) {
00338
00339
00340 dirValue = null;
00341 }
00342 if (dirValue != null) {
00343 StringTokenizer st = new StringTokenizer(dirValue, ",");
00344 while (st.hasMoreTokens()) {
00345 String dirName = st.nextToken().trim();
00346 addEjbjars(dirName);
00347 autoDirs.add(dirName);
00348 }
00349 }
00350
00351 File oFile;
00352 for (int i = 0; i < autoDirs.size(); i++) {
00353 try {
00354 oFile = new File(EJBJARS_DIR, autoDirs.get(i).toString());
00355 if (oFile.exists()) {
00356 autoloadDirectories.add(oFile.getCanonicalPath());
00357 } else {
00358 oFile = new File(autoDirs.get(i).toString());
00359 }
00360 } catch (Exception e) {
00361 String err = "Error when trying to verify Ejbjar autoload directory : " + autoDirs.get(i);
00362 servlog.log(BasicLevel.ERROR, err + " " + e.getMessage());
00363 }
00364 }
00365
00366
00367 try {
00368 String plz = (String) ctx.lookup(MDBTHREADPOOLSIZE);
00369 threadPoolSize = (new Integer(plz)).intValue();
00370 } catch (NamingException e) {
00371
00372 threadPoolSize = DEFAULT_THREAD_POOL_SIZE;
00373 }
00374
00375 try {
00376 String mxplz = (String) ctx.lookup(MDBMAXTHREADPOOLSIZE);
00377 maxthreadPoolSize = (new Integer(mxplz)).intValue();
00378 } catch (NamingException e) {
00379
00380 maxthreadPoolSize = ThreadPool.DEFAULT_POOL_MAX_SIZE;
00381 }
00382 servlog.log(BasicLevel.DEBUG, "EJB Container Service initialized");
00383
00384 }
00385
00389 public void doStart() throws ServiceException {
00390
00391 ejbjarsDir = WORK_EJBJARS_DIR + File.separator + getJonasServerName();
00392
00393 URL ejbjarsUrl = null;
00394 try {
00395 ejbjarsUrl = new File(ejbjarsDir).toURL();
00396 } catch (MalformedURLException mue) {
00397 throw new ServiceException("Error when trying to get the URL of the jonasroot/apps directory", mue);
00398 }
00399
00400 File fLog = new File(ejbjarsUrl.getFile() + File.separator + getJonasServerName() + ".log");
00401 if (!fLog.exists()) {
00402 try {
00403
00404 fLog.getParentFile().mkdirs();
00405 fLog.createNewFile();
00406 } catch (IOException e) {
00407 throw new ServiceException("cannot create the log file" + fLog);
00408 }
00409 }
00410
00411
00412 try {
00413 jarDeployerLog = new DeployerLog(fLog);
00414 } catch (DeployerLogException e) {
00415 throw new ServiceException("Can not get an EarDeployerLog", e);
00416 }
00417
00418
00419 JarCleanTask jarCleanTask = new JarCleanTask(jarDeployerLog);
00420
00421
00422 workCleaner = WorkCleaner.getInstance();
00423
00424
00425 try {
00426 workCleaner.registerTask(jarCleanTask);
00427 } catch (CleanerException ce) {
00428 throw new ServiceException("Cannot register the JAR clean task", ce);
00429
00430 }
00431
00432
00433 workCleaner.executeTasks();
00434
00435
00436
00437 String fileName = null;
00438 Context contctx = null;
00439 for (int i = 0; i < ejbNames.size(); i++) {
00440 fileName = (String) ejbNames.elementAt(i);
00441 try {
00442 contctx = new CompNamingContext(fileName);
00443 contctx.rebind("filename", fileName);
00444 } catch (NamingException ne) {
00445 servlog.log(BasicLevel.WARN, "Cannot create container for " + fileName
00446 + " because of a NamingException : " + ne.toString());
00447 }
00448
00449 try {
00450 createContainer(contctx);
00451 } catch (Exception e) {
00452 servlog.log(BasicLevel.WARN, "Cannot create container for " + fileName + " because of Exception : "
00453 + e.toString());
00454
00455 try {
00456 contctx.close();
00457 } catch (NamingException nne) {
00458 servlog.log(BasicLevel.DEBUG, "Cannot close deploy context for " + fileName, nne);
00459 }
00460 }
00461 }
00462
00463
00464 if (mbeanServer != null) {
00465 try {
00466 mbeanServer.registerMBean(this, JonasObjectName.ejbService());
00467 } catch (MBeanException mbe) {
00468
00469 servlog.log(BasicLevel.WARN, "Cannot register the EJB Container Service in the MBean Server.");
00470 servlog.log(BasicLevel.WARN, "Regstration failure cause : " + mbe.getTargetException().toString());
00471 } catch (OperationsException me) {
00472
00473
00474 servlog.log(BasicLevel.WARN, "Cannot register the EJB Container Service in the MBean Server.");
00475 servlog.log(BasicLevel.WARN, "Regstration failure cause : " + me.toString());
00476 }
00477 }
00478 }
00479
00484 public void doStop() {
00485
00486 Enumeration lc = containers.elements();
00487 while (lc.hasMoreElements()) {
00488 Container cont = (Container) lc.nextElement();
00489 removeContainer(cont);
00490 }
00491 if (mbeanServer != null) {
00492 try {
00493
00494 mbeanServer.unregisterMBean(JonasObjectName.ejbService());
00495 } catch (Exception e) {
00496 servlog.log(BasicLevel.ERROR, "Cannot stop the EJB Container Service: " + e);
00497 }
00498 }
00499
00500 servlog.log(BasicLevel.DEBUG, "EJB Container Service stopped");
00501 }
00502
00503
00504
00505
00506
00514 public String createContainer(Context ctx) throws Exception {
00515 servlog.log(BasicLevel.DEBUG, "");
00516
00517
00518 String fileName = (String) ctx.lookup("filename");
00519 File f = new File(fileName).getCanonicalFile();
00520 boolean isEjbJar = fileName.toLowerCase().endsWith(".jar");
00521
00522
00523
00524
00525 try {
00526 if (!f.exists() && isEjbJar) {
00527 String ejbjarFileName = EJBJARS_DIR + File.separator + fileName;
00528 f = new File(ejbjarFileName).getCanonicalFile();
00529 if (!f.exists()) {
00530 servlog.log(BasicLevel.ERROR, "createContainer: " + fileName + " not found");
00531 throw new NamingException(fileName + " not found");
00532 }
00533 }
00534 } catch (IOException e) {
00535 String err = "Invalid ejbjar file name '" + fileName;
00536 servlog.log(BasicLevel.ERROR, err);
00537 throw new Exception(err + e.getMessage());
00538 }
00539 boolean isEjbJarFile = isEjbJar && f.isFile();
00540 boolean isEjbJarXml = fileName.toLowerCase().endsWith(".xml") && f.isFile();
00541
00542
00543
00544 try {
00545 fileName = f.toURL().getPath();
00546 } catch (MalformedURLException e) {
00547 servlog.log(BasicLevel.ERROR, "Invalid ejb-jar file name '" + fileName + "': " + e);
00548 }
00549
00550 if (f.isFile()) {
00551 if (!isEjbJar && !isEjbJarXml) {
00552 throw new ServiceException("The ejbjar to deploy is not a jar file nor an xml file");
00553 }
00554 }
00555
00556
00557 if (getContainer(fileName) != null) {
00558 servlog.log(BasicLevel.ERROR, "createContainer: " + fileName + " already exists");
00559 throw new Exception("Container already exists");
00560 }
00561
00562
00563
00564
00565 boolean isInEar = true;
00566 try {
00567 ctx.lookup("earClassLoader");
00568 } catch (NamingException ne) {
00569 isInEar = false;
00570 }
00571
00572 String wkFileName = null;
00573
00574 if (!isInEar && isEjbJarFile) {
00575
00576 wkFileName = ejbjarsDir + File.separator + FileManager.fileToTimeStampDir(f.toURL(), ".jar");
00577
00578 servlog.log(BasicLevel.DEBUG, "filename=" + fileName);
00579 servlog.log(BasicLevel.DEBUG, "wkFilename=" + wkFileName);
00580
00581
00582 FileManager.copyFile(fileName, wkFileName);
00583
00584
00585 try {
00586 jarDeployerLog.addEntry(f, new File(wkFileName));
00587 } catch (DeployerLogException e) {
00588 String err = "Error while adding the " + fileName + " entry in the log file";
00589 servlog.log(BasicLevel.ERROR, err + " : " + e.getMessage());
00590 throw new Exception(err, e);
00591 }
00592
00593 servlog.log(BasicLevel.DEBUG, "Create wk file :" + wkFileName);
00594
00595 } else {
00596
00597 wkFileName = fileName;
00598 servlog.log(BasicLevel.DEBUG, "No needs wk file :" + wkFileName);
00599 }
00600
00601
00602 URL[] url = null;
00603 try {
00604 url = new URL[1];
00605 url[0] = (new File(wkFileName)).toURL();
00606 } catch (MalformedURLException e) {
00607 servlog.log(BasicLevel.ERROR, "Invalid ejb-jar file name '" + wkFileName + "'", e);
00608 }
00609
00610
00611 URLClassLoader ejbClassLoader = null;
00612 URLClassLoader earClassLoader = null;
00613 try {
00614 earClassLoader = (URLClassLoader) ctx.lookup("earClassLoader");
00615 ejbClassLoader = (URLClassLoader) ctx.lookup("ejbClassLoader");
00616 loaderlog.log(BasicLevel.DEBUG, "earClassLoader=" + earClassLoader);
00617 } catch (NamingException ne) {
00618
00619 if (isEjbJar) {
00620 ejbClassLoader = new EjbJarClassLoader(url, appsClassLoader);
00621 } else {
00622 ejbClassLoader = (URLClassLoader) appsClassLoader;
00623 }
00624 loaderlog.log(BasicLevel.DEBUG, "parent Loader=" + appsClassLoader);
00625 }
00626 loaderlog.log(BasicLevel.DEBUG, "ejbClassLoader=" + ejbClassLoader);
00627
00628
00629 DeploymentDesc dd = null;
00630 try {
00631
00632 dd = EjbManagerWrapper.getDeploymentDesc(url[0], ejbClassLoader, earClassLoader);
00633 } catch (DeploymentDescException e) {
00634 servlog.log(BasicLevel.ERROR, "Cannot read the deployment descriptors '" + fileName + "': " + e);
00635 throw new ServiceException(e.getMessage(), e);
00636 }
00637
00638
00639 String cname = buildEJBModuleName(fileName);
00640 if (cname == null) {
00641
00642 cname = "EJB container ";
00643 if (dd.getDisplayName() != null) {
00644 cname += dd.getDisplayName();
00645 }
00646 }
00647
00648
00649 JContainer cont = new JContainer(cname, fileName, wkFileName, ejbClassLoader);
00650 cont.setContainerNaming(NamingManager.getInstance());
00651 cont.setTransactionManager(tm);
00652 cont.setPrincipalFactory(new PrincipalFactoryImpl());
00653 cont.setJmsManager(jms);
00654
00655
00656
00657 String earFileName;
00658 try {
00659 earFileName = (String) ctx.lookup("earFileName");
00660 cont.setEarFileName(earFileName);
00661 } catch (NamingException ne) {
00662 earFileName = null;
00663 }
00664
00665
00666 if (securityService == null) {
00667 servlog.log(BasicLevel.DEBUG, "EJB Container Service: working without Security Service");
00668 cont.setSecurity(false);
00669 } else {
00670 PermissionManager permissionManager = null;
00671 try {
00672 permissionManager = new PermissionManager(dd, cont.getContextId());
00673 permissionManager.translateEjbDeploymentDescriptor();
00674
00675
00676
00677 if (!isInEar) {
00678 permissionManager.commit();
00679 }
00680 } catch (Exception e) {
00681 servlog.log(BasicLevel.ERROR, "Can't build permission manager object for the ejbjar '" + fileName
00682 + "' : " + e.getMessage());
00683 }
00684 cont.setPermissionManager(permissionManager);
00685 }
00686
00687
00688
00689
00690 cont.setThreadPoolSize(threadPoolSize);
00691 cont.setMaxThreadPoolSize(maxthreadPoolSize);
00692
00693 String domainName = getDomainName();
00694 String serverName = getJonasServerName();
00695 String j2eeAppName = EarServiceImpl.buildJ2eeApplicationName(earFileName);
00696 Registry oRegistry = JonasMBeanTools.getRegistry();
00697 EJBModule ejbModuleMBean = null;
00698 ObjectName onEjbModule = null;
00699
00700
00701 Map mbeansToRegister = new HashMap();
00702
00703
00704 if (mbeanServer != null) {
00705
00706
00707
00708 onEjbModule = J2eeObjectName.getEJBModule(domainName, serverName, j2eeAppName, cname);
00709
00710 ejbModuleMBean = new EJBModule(onEjbModule.toString(), cont, fileName, cname);
00711 ejbModuleMBean.setDeploymentDescriptor(dd.getXmlContent());
00712 ejbModuleMBean.setJonasDeploymentDescriptor(dd.getJOnASXmlContent());
00713
00714
00715 ManagedBean oManaged = oRegistry.findManagedBean("EJBModule");
00716 ModelMBean oMBean = oManaged.createMBean(ejbModuleMBean);
00717 mbeansToRegister.put(oMBean, onEjbModule);
00718 }
00719
00720
00721 BeanDesc[] beans = dd.getBeanDesc();
00722 for (int i = 0; i < beans.length; i++) {
00723 BeanFactory bf = cont.addBean(beans[i]);
00724 beans[i].setBeanFactory(bf);
00725 if (mbeanServer != null) {
00726 try {
00727
00728 String beanName = beans[i].getEjbName();
00729 if ((beans[i] instanceof EntityBmpDesc) || (beans[i] instanceof EntityCmpDesc)) {
00730 JEntityFactory jef = (JEntityFactory) bf;
00731 ObjectName onEntityBean = J2eeObjectName.getEntityBean(domainName, cname, serverName,
00732 j2eeAppName, beanName);
00733 EntityBean entityBeanMBean;
00734
00735 if (beans[i] instanceof EntityBmpDesc) {
00736 entityBeanMBean = new EntityBean(onEntityBean.toString(), jef, BMP);
00737 } else {
00738 entityBeanMBean = new EntityBean(onEntityBean.toString(), jef, CMP);
00739 }
00740 ManagedBean oManaged = oRegistry.findManagedBean("EntityBean");
00741 ModelMBean oMBean = oManaged.createMBean(entityBeanMBean);
00742 mbeansToRegister.put(oMBean, onEntityBean);
00743
00744 ejbModuleMBean.addEjb(onEntityBean.toString());
00745 } else if (beans[i] instanceof SessionStatefulDesc) {
00746 JSessionFactory jsf = (JSessionFactory) bf;
00747 ObjectName onStatefulSessionBean = J2eeObjectName.getStatefulSessionBean(domainName, cname,
00748 serverName, j2eeAppName, beanName);
00749 StatefulSessionBean sessionBeanMBean = new StatefulSessionBean(
00750 onStatefulSessionBean.toString(), jsf);
00751 ManagedBean oManaged = oRegistry.findManagedBean("StatefulSessionBean");
00752 ModelMBean oMBean = oManaged.createMBean(sessionBeanMBean);
00753 mbeansToRegister.put(oMBean, onStatefulSessionBean);
00754
00755 ejbModuleMBean.addEjb(onStatefulSessionBean.toString());
00756 } else if (beans[i] instanceof SessionStatelessDesc) {
00757 JStatelessFactory jsf = (JStatelessFactory) bf;
00758 ObjectName onStatelessSessionBean = J2eeObjectName.getStatelessSessionBean(domainName, cname,
00759 serverName, j2eeAppName, beanName);
00760 StatelessSessionBean sessionBeanMBean = new StatelessSessionBean(onStatelessSessionBean
00761 .toString(), jsf);
00762 ManagedBean oManaged = oRegistry.findManagedBean("StatelessSessionBean");
00763 ModelMBean oMBean = oManaged.createMBean(sessionBeanMBean);
00764 mbeansToRegister.put(oMBean, onStatelessSessionBean);
00765
00766 ejbModuleMBean.addEjb(onStatelessSessionBean.toString());
00767 } else {
00768 ObjectName onMessageDrivenBean = J2eeObjectName.getMessageDrivenBean(domainName, cname,
00769 serverName, j2eeAppName, beanName);
00770 MessageDrivenBean messageDrivenBeanMBean = null;
00771 if (bf instanceof JMdbFactory) {
00772 JMdbFactory jmf = (JMdbFactory) bf;
00773 messageDrivenBeanMBean = new MessageDrivenBean(onMessageDrivenBean.toString(), jmf);
00774 } else {
00775 JMdbEndpointFactory jmf = (JMdbEndpointFactory) bf;
00776 messageDrivenBeanMBean = new MessageDrivenBean(onMessageDrivenBean.toString(), jmf);
00777 }
00778 ManagedBean oManaged = oRegistry.findManagedBean("MessageDrivenBean");
00779 ModelMBean oMBean = oManaged.createMBean(messageDrivenBeanMBean);
00780 mbeansToRegister.put(oMBean, onMessageDrivenBean);
00781
00782 ejbModuleMBean.addEjb(onMessageDrivenBean.toString());
00783 }
00784 } catch (Exception e) {
00785 servlog.log(BasicLevel.ERROR, "Cannot create container: " + e);
00786 }
00787 }
00788 }
00789
00790
00791 for (int i = 0; i < beans.length; i++) {
00792 BeanFactory bf = beans[i].getBeanFactory();
00793 bf.initInstancePool();
00794 }
00795
00796
00797
00798 containers.addElement(cont);
00799 for (Iterator it = mbeansToRegister.keySet().iterator(); it.hasNext();) {
00800 ModelMBean mbean = (ModelMBean) it.next();
00801 ObjectName oName = (ObjectName) mbeansToRegister.get(mbean);
00802 mbeanServer.registerMBean(mbean, oName);
00803
00804 }
00805
00806 servlog.log(BasicLevel.DEBUG, "created container for " + fileName);
00807
00808 return onEjbModule.toString();
00809 }
00810
00816 public Container getContainer(String fileName) {
00817 servlog.log(BasicLevel.DEBUG, fileName);
00818
00819
00820
00821
00822 String ejbjarFileName = EJBJARS_DIR + File.separator + fileName;
00823 File f = null;
00824 String pathEjbJarDirFile;
00825 String pathFile = "";
00826 try {
00827 f = new File(ejbjarFileName).getCanonicalFile();
00828
00829
00830 pathEjbJarDirFile = f.toURL().getPath();
00831 } catch (Exception e) {
00832
00833
00834 pathEjbJarDirFile = "";
00835 }
00836 try {
00837 f = new File(fileName).getCanonicalFile();
00838
00839
00840 pathFile = f.toURL().getPath();
00841 } catch (Exception e) {
00842 String err = "Error while trying to get canonical file '" + fileName + "'";
00843 servlog.log(BasicLevel.ERROR, err);
00844 return null;
00845 }
00846
00847
00848 Enumeration lc = containers.elements();
00849 while (lc.hasMoreElements()) {
00850 Container cont = (JContainer) lc.nextElement();
00851 String contName = cont.getExternalFileName();
00852
00853
00854 if (contName.equals(pathFile)) {
00855 return cont;
00856 } else if (contName.equals(pathEjbJarDirFile)) {
00857
00858 return cont;
00859 } else {
00860
00861 List lAlDirs = getAutoloadDirectories();
00862 for (int i = 0; i < lAlDirs.size(); i++) {
00863 String alDir = (String) lAlDirs.get(i);
00864
00865 String alFileName = alDir + File.separator + fileName;
00866 try {
00867 f = new File(alFileName).getCanonicalFile();
00868
00869
00870 alFileName = f.toURL().getPath();
00871 } catch (Exception e) {
00872
00873
00874 alFileName = alDir + File.separator + fileName;
00875 }
00876 servlog.log(BasicLevel.DEBUG, "alFileName= " + alFileName);
00877
00878 if (contName.equals(alFileName)) {
00879 return cont;
00880 }
00881 }
00882 }
00883 }
00884
00885
00886 return null;
00887 }
00888
00892 public void removeContainer(Container cont) {
00893 servlog.log(BasicLevel.DEBUG, cont.getName());
00894
00895 if (mbeanServer != null) {
00896 String moduleName = cont.getName();
00897 String earFileName = cont.getEarFileName();
00898 String earName = null;
00899 if (earFileName != null) {
00900 earName = EarServiceImpl.buildJ2eeApplicationName(earFileName);
00901 }
00902 String domainName = getDomainName();
00903 String serverName = getJonasServerName();
00904 ObjectName onEjbModule = J2eeObjectName.getEJBModule(domainName, serverName, earName, moduleName);
00905
00906
00907 ObjectName onEjb = null;
00908 try {
00909 String[] onEjbs = (String[]) mbeanServer.getAttribute(onEjbModule, "ejbs");
00910 for (int i = 0; i < onEjbs.length; i++) {
00911 onEjb = new ObjectName(onEjbs[i]);
00912 mbeanServer.unregisterMBean(onEjb);
00913 }
00914 } catch (Exception e) {
00915 servlog.log(BasicLevel.ERROR, "Problem when trying to unregsiter MBean " + onEjb.toString() + ": " + e);
00916 }
00917
00918
00919 try {
00920 mbeanServer.unregisterMBean(onEjbModule);
00921 if (servlog.isLoggable(BasicLevel.DEBUG)) {
00922 servlog.log(BasicLevel.DEBUG, "unregistered MBean: " + onEjbModule.toString());
00923 }
00924 } catch (Exception e) {
00925 servlog.log(BasicLevel.ERROR, "Problem when trying to unregsiter MBean " + onEjbModule.toString()
00926 + ": " + e);
00927 }
00928 }
00929 cont.remove();
00930 containers.removeElement(cont);
00931
00932
00933 Runtime.getRuntime().gc();
00934
00935 }
00936
00940 public Container[] listContainers() {
00941 servlog.log(BasicLevel.DEBUG, "size= " + containers.size());
00942
00943 Container[] ret = new Container[containers.size()];
00944 containers.copyInto(ret);
00945 return ret;
00946 }
00947
00952 public void syncAllEntities(boolean passivate) {
00953 servlog.log(BasicLevel.DEBUG, "");
00954
00955 Enumeration lc = containers.elements();
00956 while (lc.hasMoreElements()) {
00957 JContainer cont = (JContainer) lc.nextElement();
00958 cont.syncAll(passivate);
00959 }
00960 }
00961
00968 public Integer getCurrentNumberOfContainer() {
00969 return new Integer(containers.size());
00970 }
00971
00976 public Integer getTotalCurrentNumberOfBeanType() {
00977 int count = 0;
00978 Enumeration lc = containers.elements();
00979 while (lc.hasMoreElements()) {
00980 JContainer cont = (JContainer) lc.nextElement();
00981 count += cont.getBeanNb();
00982 }
00983 return new Integer(count);
00984 }
00985
00990 public Integer getTotalCurrentNumberOfBMPType() {
00991 int count = 0;
00992 Enumeration lc = containers.elements();
00993 while (lc.hasMoreElements()) {
00994 JContainer cont = (JContainer) lc.nextElement();
00995 count += cont.getEntityBMPNb();
00996 }
00997 return new Integer(count);
00998 }
00999
01004 public Integer getTotalCurrentNumberOfCMPType() {
01005 int count = 0;
01006 Enumeration lc = containers.elements();
01007 while (lc.hasMoreElements()) {
01008 JContainer cont = (JContainer) lc.nextElement();
01009 count += cont.getEntityCMPNb();
01010 }
01011 return new Integer(count);
01012 }
01013
01018 public Integer getTotalCurrentNumberOfSBFType() {
01019 int count = 0;
01020 Enumeration lc = containers.elements();
01021 while (lc.hasMoreElements()) {
01022 JContainer cont = (JContainer) lc.nextElement();
01023 count += cont.getStatefulSessionNb();
01024 }
01025 return new Integer(count);
01026 }
01027
01032 public Integer getTotalCurrentNumberOfSBLType() {
01033 int count = 0;
01034 Enumeration lc = containers.elements();
01035 while (lc.hasMoreElements()) {
01036 JContainer cont = (JContainer) lc.nextElement();
01037 count += cont.getStatelessSessionNb();
01038 }
01039 return new Integer(count);
01040 }
01041
01046 public Integer getTotalCurrentNumberOfMDBType() {
01047 int count = 0;
01048 Enumeration lc = containers.elements();
01049 while (lc.hasMoreElements()) {
01050 JContainer cont = (JContainer) lc.nextElement();
01051 count += cont.getMessageDrivenNb();
01052 }
01053 return new Integer(count);
01054 }
01055
01056 public void removeContainer(String fileName) throws Exception {
01057 removeContainerMBean(fileName);
01058 }
01059
01064 public void removeContainerMBean(String fileName) throws Exception {
01065 Container cont = null;
01066 try {
01067 cont = getContainer(fileName);
01068 } catch (Exception e) {
01069 String err = "Error while trying to find file '" + fileName + "'";
01070 servlog.log(BasicLevel.ERROR, err);
01071 throw new Exception(err);
01072 }
01073
01074 if (cont != null) {
01075 removeContainer(cont, false);
01076 } else {
01077 String err = "Cannot remove the non-existant container '" + fileName + "'";
01078 servlog.log(BasicLevel.ERROR, err);
01079 throw new Exception(err);
01080 }
01081 }
01082
01083 public String createContainer(String file) throws Exception {
01084 return createContainerMBean(file);
01085 }
01086
01094 public String createContainerMBean(String file) throws Exception {
01095 String ejbModuleObjectName = null;
01096 try {
01097 Context contctx = new CompNamingContext(file);
01098 contctx.rebind("filename", file);
01099 ejbModuleObjectName = createContainer(contctx);
01100 } catch (Exception e) {
01101 throw new Exception("Cannot create Container: " + e.toString());
01102 }
01103 return ejbModuleObjectName;
01104 }
01105
01112 public Boolean isJarDeployed(String fileName) {
01113 return new Boolean(isJarLoaded(fileName));
01114 }
01115
01122 public boolean isJarDeployedByWorkName(String workFileName) {
01123 servlog.log(BasicLevel.DEBUG, workFileName);
01124
01125
01126 Enumeration lc = containers.elements();
01127 while (lc.hasMoreElements()) {
01128 Container cont = (JContainer) lc.nextElement();
01129
01130
01131
01132 String contName = new File(cont.getFileName()).getName();
01133 if (contName.equals(workFileName)) {
01134 return true;
01135 }
01136 }
01137
01138
01139 return false;
01140 }
01141
01142
01143 static final int DATASOURCE = 1;
01144
01145 static final int JMS_DESTINATION = 2;
01146
01147 static final int JMS_FACTORY = 3;
01148
01149 static final int MAIL_FACTORY = 4;
01150
01158 Set getDependence(String name, int type) {
01159 Set resultObjectName = null;
01160 Set resultProperties = new HashSet();
01161 Enumeration lc = containers.elements();
01162 try {
01163 Context ctx = new InitialContext();
01164 ctx.lookup(name);
01165 ctx.close();
01166
01167 while (lc.hasMoreElements()) {
01168 JContainer cont = (JContainer) lc.nextElement();
01169 Set depProps = null;
01170
01171 switch (type) {
01172
01173 case DATASOURCE:
01174 depProps = cont.getDataSourceDependence(name);
01175 break;
01176
01177 case JMS_DESTINATION:
01178 depProps = cont.getJmsDestinationDependence(name);
01179 break;
01180
01181 case JMS_FACTORY:
01182 depProps = cont.getJmsConnectionFactoryDependence(name);
01183 break;
01184
01185 case MAIL_FACTORY:
01186 depProps = cont.getMailFactoryDependence(name);
01187 break;
01188
01189 }
01190 resultProperties.addAll(depProps);
01191
01192 try {
01193 resultObjectName = convertToObjectNames(resultProperties);
01194 } catch (Exception e) {
01195 servlog.log(BasicLevel.ERROR, "EjbServiceImpl: Object Name Error:" + e);
01196 }
01197 }
01198 } catch (NamingException ne) {
01199
01200 resultObjectName = new HashSet();
01201 }
01202 return resultObjectName;
01203 }
01204
01210 private Set convertToObjectNames(Set resultProperties) {
01211 Set resultObjectName = new HashSet();
01212 Iterator it = resultProperties.iterator();
01213 String domainName = getDomainName();
01214 String serverName = getJonasServerName();
01215 Properties item = null;
01216 String ejbType = null;
01217 String ejbName = null;
01218 String fileName = null;
01219 String earFileName = null;
01220 String moduleName = null;
01221 String j2eeAppName = null;
01222 while (it.hasNext()) {
01223 item = (Properties) it.next();
01224 ObjectName ejbObjectName = null;
01225
01226
01227 ejbType = item.getProperty("type");
01228 ejbName = item.getProperty("name");
01229 fileName = item.getProperty("fname");
01230 earFileName = item.getProperty("earFileName");
01231
01232 moduleName = item.getProperty("cname");
01233 if (earFileName != null) {
01234 j2eeAppName = EarServiceImpl.buildJ2eeApplicationName(earFileName);
01235 }
01236
01237 if (ejbType.equals("ejbbmp") || ejbType.equals("ejbcmp")) {
01238 ejbObjectName = J2eeObjectName.getEntityBean(domainName, moduleName, serverName, j2eeAppName, ejbName);
01239 } else if (ejbType.equals("ejbsbf")) {
01240 ejbObjectName = J2eeObjectName.getStatefulSessionBean(domainName, moduleName, serverName, j2eeAppName,
01241 ejbName);
01242 } else if (ejbType.equals("ejbsbl")) {
01243 ejbObjectName = J2eeObjectName.getStatelessSessionBean(domainName, moduleName, serverName, j2eeAppName,
01244 ejbName);
01245 } else if (ejbType.equals("ejbmdb")) {
01246 ejbObjectName = J2eeObjectName.getMessageDrivenBean(domainName, moduleName, serverName, j2eeAppName,
01247 ejbName);
01248 }
01249 if (ejbObjectName != null) {
01250 resultObjectName.add(ejbObjectName);
01251 }
01252 }
01253 return resultObjectName;
01254 }
01255
01260 public Set getDataSourceDependence(String dsName) {
01261 return getDependence(dsName, DATASOURCE);
01262 }
01263
01268 public Set getJmsDestinationDependence(String destName) {
01269 return getDependence(destName, JMS_DESTINATION);
01270 }
01271
01276 public Set getJmsConnectionFactoryDependence(String cfName) {
01277 return getDependence(cfName, JMS_FACTORY);
01278 }
01279
01284 public Set getMailFactoryDependence(String mfName) {
01285 return getDependence(mfName, MAIL_FACTORY);
01286 }
01287
01296 public List getInstalledJars() throws Exception {
01297
01298 ArrayList al = JModule.getInstalledContainersInDir(EJBJARS_DIR, JModule.EJBJAR_EXTENSION,
01299 JModule.EJBJAR_CHILD_DIR, JModule.EJBJAR_CONFIRM_FILE);
01300
01301 for (int i = 0; i < autoloadDirectories.size(); i++) {
01302 al.addAll(JModule.getInstalledContainersInDir(autoloadDirectories.get(i).toString(),
01303 JModule.EJBJAR_EXTENSION, JModule.EJBJAR_CHILD_DIR, JModule.EJBJAR_CONFIRM_FILE));
01304 }
01305 return al;
01306 }
01307
01322 public void deployJars(Context ctx) throws ServiceException {
01323
01324
01325
01326
01327
01328
01329
01330 URL earRootUrl = null;
01331 URL earUrl = null;
01332 ClassLoader earClassLoader = null;
01333 ClassLoader ejbClassLoader = null;
01334 URL[] jarURLs = null;
01335 String[] roleNames = null;
01336 try {
01337 earRootUrl = (URL) ctx.lookup("earRootUrl");
01338 earUrl = (URL) ctx.lookup("earUrl");
01339 earClassLoader = (ClassLoader) ctx.lookup("earClassLoader");
01340 ejbClassLoader = (ClassLoader) ctx.lookup("ejbClassLoader");
01341 jarURLs = (URL[]) ctx.lookup("jarURLs");
01342 roleNames = (String[]) ctx.lookup("roleNames");
01343 } catch (NamingException e) {
01344 String err = "Error while getting parameter from context param :" + e.getMessage();
01345 servlog.log(BasicLevel.ERROR, err);
01346 throw new ServiceException(err, e);
01347 }
01348
01349
01350 for (int i = 0; i < jarURLs.length; i++) {
01351
01352
01353 String fileName = jarURLs[i].getFile();
01354 servlog.log(BasicLevel.DEBUG, "Deploy '" + fileName + "' for the ear service.");
01355
01356
01357
01358 Context contctx = null;
01359 try {
01360 contctx = new CompNamingContext(fileName);
01361 contctx.rebind("filename", fileName);
01362 contctx.rebind("earClassLoader", earClassLoader);
01363 contctx.rebind("ejbClassLoader", ejbClassLoader);
01364 contctx.rebind("earRoot", earRootUrl.getFile());
01365 contctx.rebind("earFileName", earUrl.getFile());
01366 contctx.rebind("roleNames", roleNames);
01367 createContainer(contctx);
01368 } catch (Exception e) {
01369
01370
01371 servlog.log(BasicLevel.ERROR, "Error when deploying '" + fileName + "'");
01372 servlog.log(BasicLevel.ERROR, e.getMessage());
01373 servlog.log(BasicLevel.ERROR, "Undeploy ejb-jar of the ear application");
01374
01375 for (int j = 0; j <= i; j++) {
01376 Container cont = getContainer(jarURLs[j].getFile());
01377 if (cont != null) {
01378 removeContainer(cont, true);
01379 } else {
01380 servlog.log(BasicLevel.ERROR, "Cannot remove the non-existant container '" + fileName + "'");
01381 }
01382 }
01383 throw new ServiceException("Error during the deployment", e);
01384 }
01385 }
01386
01387 }
01388
01394 public void unDeployJars(URL[] urls) {
01395 for (int i = 0; i < urls.length; i++) {
01396 String fileName = urls[i].getFile();
01397 Container cont = getContainer(urls[i].getFile());
01398 if (cont != null) {
01399 removeContainer(cont, true);
01400 } else {
01401 servlog.log(BasicLevel.ERROR, "Cannot remove the non-existant container '" + fileName + "'");
01402 }
01403 }
01404 }
01405
01412 public void removeContainer(Container cont, boolean isEarCase) {
01413 if (isEarCase == (cont.getEarFileName() != null)) {
01414 removeContainer(cont);
01415 } else {
01416 String err = "Cannot remove container '" + cont.getName()
01417 + "' it is in an ear application. You must undeploy the ear associated.";
01418 servlog.log(BasicLevel.ERROR, err);
01419 }
01420 }
01421
01429 public void removeCache(ClassLoader earClassLoader) {
01430 EjbManagerWrapper.removeCache(earClassLoader);
01431 }
01432
01440 private void addEjbjars(String dirPath) {
01441 boolean found = false;
01442
01443
01444
01445 File dir = new File(dirPath);
01446 found = dir.isDirectory();
01447
01448 if (!found) {
01449
01450 dir = new File(EJBJARS_DIR + File.separator + dirPath);
01451 found = dir.isDirectory();
01452 }
01453
01454 if (found) {
01455 addEjbjarsFrom(dir);
01456 } else {
01457 String err = "Warning: Cannot load dir: '" + dirPath + "' ";
01458 err += "is not a directory or directory doesn't exist";
01459 servlog.log(BasicLevel.WARN, err);
01460 }
01461 }
01462
01468 private void addEjbjarsFrom(File dir) throws ServiceException {
01469
01470 try {
01471 if (dir.isDirectory()) {
01472 File[] files = dir.listFiles();
01473 for (int i = 0; i < files.length; i++) {
01474 if (files[i].getPath().toLowerCase().endsWith(".jar")) {
01475 ejbNames.add(files[i].getCanonicalPath());
01476 } else if (files[i].isDirectory()) {
01477 addEjbjarsFrom(files[i]);
01478 }
01479 }
01480 } else {
01481 String err = "Cannot load dir: '" + dir.getPath();
01482 err += "' is not a directory";
01483 servlog.log(BasicLevel.ERROR, err);
01484 throw new ServiceException(err);
01485 }
01486 } catch (IOException e) {
01487 String err = "Invalid file name '" + dir.getPath();
01488 servlog.log(BasicLevel.ERROR, err);
01489 throw new ServiceException(err, e);
01490 }
01491 }
01492
01498 public boolean isJarLoaded(String fileName) {
01499 return (getContainer(fileName) != null);
01500 }
01501
01506 public List getDeployedJars() {
01507 ArrayList al = new ArrayList();
01508 Container oContainer;
01509 for (Enumeration jars = containers.elements(); jars.hasMoreElements();) {
01510 oContainer = (Container) jars.nextElement();
01511 try {
01512 al.add((new File(oContainer.getExternalFileName())).toURL().getPath());
01513 } catch (Exception e) {
01514
01515 }
01516 }
01517 return al;
01518 }
01519
01525 public List getDeployableJars() throws Exception {
01526 List al = getInstalledJars();
01527 al.removeAll(getDeployedJars());
01528 return al;
01529 }
01530
01535 public List getAutoloadDirectories() {
01536 ArrayList al = new ArrayList();
01537 for (int i = 0; i < autoloadDirectories.size(); i++) {
01538 try {
01539 al.add((new File(autoloadDirectories.get(i).toString())).toURL().getPath());
01540 } catch (Exception e) {
01541
01542 }
01543 }
01544 return al;
01545 }
01546
01551 public String getEjbjarsDirectory() {
01552 String sRet;
01553 try {
01554 sRet = (new File(EJBJARS_DIR)).toURL().getPath();
01555 } catch (Exception e) {
01556 sRet = null;
01557 }
01558 return sRet;
01559 }
01560
01566 public static String buildEJBModuleName(URL pUrl) {
01567 String sName;
01568 try {
01569 sName = new File(pUrl.getFile()).getName();
01570 if ("file".equals(pUrl.getProtocol())) {
01571 sName = buildEJBModuleName(sName);
01572 }
01573 } catch (NullPointerException e) {
01574 sName = null;
01575 }
01576 return sName;
01577 }
01578
01584 public static String buildEJBModuleName(String pFilename) {
01585 String sName;
01586 try {
01587 sName = new File(pFilename).getName();
01588 int iPos = sName.lastIndexOf('.');
01589 if (iPos > -1) {
01590 sName = sName.substring(0, iPos);
01591 }
01592 } catch (NullPointerException e) {
01593 sName = null;
01594 }
01595 return sName;
01596 }
01597 }