KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ejb > container > JContainer


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: JContainer.java,v 1.81 2005/07/13 06:29:45 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_ejb.container;
27
28 import java.io.File JavaDoc;
29 import java.lang.reflect.Method JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.HashSet JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.Properties JavaDoc;
36 import java.util.Set JavaDoc;
37
38 import javax.ejb.AccessLocalException JavaDoc;
39 import javax.ejb.EJBException JavaDoc;
40 import javax.naming.Context JavaDoc;
41 import javax.naming.InitialContext JavaDoc;
42 import javax.naming.LinkRef JavaDoc;
43 import javax.naming.NamingException JavaDoc;
44 import javax.naming.NameNotFoundException JavaDoc;
45 import javax.naming.Reference JavaDoc;
46 import javax.naming.StringRefAddr JavaDoc;
47 import javax.resource.spi.ActivationSpec JavaDoc;
48 import javax.resource.spi.work.WorkManager JavaDoc;
49 import javax.security.jacc.PolicyContext JavaDoc;
50
51 import org.objectweb.jonas.common.JProp;
52 import org.objectweb.jonas.resource.Rar;
53 import org.objectweb.jonas.ws.JServiceFactory;
54 import org.objectweb.jonas.ws.JServiceFactoryFinder;
55 import org.objectweb.jonas_ejb.container.jorm.RdbFactory;
56 import org.objectweb.jonas_ejb.deployment.api.BeanDesc;
57 import org.objectweb.jonas_ejb.deployment.api.EntityBmpDesc;
58 import org.objectweb.jonas_ejb.deployment.api.EntityCmpDesc;
59 import org.objectweb.jonas_ejb.deployment.api.EntityDesc;
60 import org.objectweb.jonas_ejb.deployment.api.EntityJdbcCmp1Desc;
61 import org.objectweb.jonas_ejb.deployment.api.EntityJdbcCmp2Desc;
62 import org.objectweb.jonas_ejb.deployment.api.MessageDrivenDesc;
63 import org.objectweb.jonas_ejb.deployment.api.SessionStatefulDesc;
64 import org.objectweb.jonas_ejb.deployment.api.SessionStatelessDesc;
65 import org.objectweb.jonas_ejb.lib.EJBInvocation;
66
67 import org.objectweb.jonas_lib.deployment.api.EjbLocalRefDesc;
68 import org.objectweb.jonas_lib.deployment.api.EjbRefDesc;
69 import org.objectweb.jonas_lib.deployment.api.EnvEntryDesc;
70 import org.objectweb.jonas_lib.deployment.api.MessageDestinationRefDesc;
71 import org.objectweb.jonas_lib.deployment.api.ResourceEnvRefDesc;
72 import org.objectweb.jonas_lib.deployment.api.ResourceRefDesc;
73 import org.objectweb.jonas_lib.naming.ContainerNaming;
74 import org.objectweb.jonas_lib.security.PermissionManagerException;
75
76 import org.objectweb.jonas_jms.api.JmsManager;
77
78 import org.objectweb.jonas_ws.deployment.api.ServiceRefDesc;
79
80 import org.objectweb.transaction.jta.TransactionManager;
81
82 import org.objectweb.carol.rmi.exception.NamingExceptionHelper;
83
84 import org.objectweb.util.monolog.api.BasicLevel;
85
86 /**
87  * This class represents an EJB container. A container is where an enterprise
88  * Bean object lives. All beans from a same ejb-jar file are installed in a
89  * single container. For each EJB installed, the container provides a factory
90  * and makes it available in the JNDI name space. The JContainer basically
91  * manages a set of BeanFactory objects.
92  * @author Philippe Coq
93  * @author Jeff Mesnil (Security)
94  * @author Christophe Ney (Making easier Enhydra integration)
95  * @author Philippe Durieux (New architecture for local interfaces)
96  * @author Florent Benoit (Ear service, ejb-link, JACC security)
97  * @author Ludovic Bert (Ear service, ejb-link)
98  * @author Benjamin Bonnet (max size for thread pool)
99  */

100
101 public class JContainer implements Container {
102
103     /**
104      * This class is the default factory class name used for the Entity bean.
105      */

106     public static final String JavaDoc DEFAULT_FACTORY_CLASS_NAME = "org.objectweb.jonas_ejb.container.JEntityFactory";
107
108     /**
109      * Work Manager, used as a pool of threads.
110      */

111     private static WorkManager JavaDoc workManager = null;
112
113     /**
114      * List of beans (key=EJBName / Value=BeanFactory)
115      */

116     private HashMap JavaDoc beanList = new HashMap JavaDoc();
117
118     /**
119      * Name of the ear application containing this container.
120      */

121     private String JavaDoc earFileName = null;
122
123     /**
124      * Container file name x.jar or x.xml
125      */

126     private String JavaDoc fileName;
127
128     /**
129      * External file name
130      */

131     private String JavaDoc externalFileName;
132
133     /**
134      * Jms manager
135      */

136     private JmsManager jms = null;
137
138     /**
139      * Classloader used by this container
140      */

141     private ClassLoader JavaDoc loader = null;
142
143     /**
144      * Container name
145      */

146     private String JavaDoc myname;
147
148     /**
149      * Naming object used to do lookup/bind in environment
150      */

151     private ContainerNaming naming = null;
152
153     /**
154      * Permission manager of this container. JACC
155      */

156     private PermissionManager permissionManager = null;
157
158     /**
159      * Factory used to make Principal objects
160      */

161     private PrincipalFactory principalFactory = null;
162
163     /**
164      * Enable / disable security
165      */

166     private boolean securityFlag = true;
167
168     /**
169      * Swapper object
170      */

171     private Swapper swapper;
172
173     /**
174      * Transaction manager
175      */

176     private TransactionManager tm = null;
177
178     private String JavaDoc tmpDirName = null;
179     
180     /**
181      * This is the default name for JORAM that will be used when trying to configure an MDB
182      * when the JMS service is not running.
183      */

184     private static final String JavaDoc DEFAULT_ACTIVATION_SPEC_NAME = "joramActivationSpec";
185
186     /**
187      * constructor
188      * @param name name of the container.
189      * @param extFileName external file name
190      * @param file file name (.jar or .xml)
191      * @param ld the class loader to be used
192      */

193     public JContainer(String JavaDoc name, String JavaDoc extFileName, String JavaDoc file, ClassLoader JavaDoc ld) {
194         myname = name;
195         externalFileName = extFileName;
196         fileName = file;
197         loader = ld;
198         swapper = new Swapper(this);
199         swapper.start();
200         if (TraceEjb.isDebugIc()) {
201             TraceEjb.interp.log(BasicLevel.DEBUG, "New Container extFN= " + externalFileName + " filename=" + fileName);
202         }
203         tmpDirName = JProp.getJonasBase() + File.separator + "tmp";
204         File JavaDoc d = new File JavaDoc(tmpDirName);
205         d.mkdir();
206     }
207
208     public String JavaDoc getTmpDirName() {
209         return tmpDirName;
210     }
211     
212     // -------------------------------------------------------------------
213
// Methods used by JOnAS Server (but not part of JContainer interface)
214
// -------------------------------------------------------------------
215

216     /**
217      * Adds beans in container. This method is not part of JContainer interface,
218      * although it is used by JOnAS Server, because we don't want to get all
219      * BeanDesc classes (jonas_ejb.deployment.api) LATER: Replace this by
220      * setDeploymentDesc ?
221      * @param dd The Bean Deployment Descriptor
222      * @return The bean factory created for this bean.
223      * @throws EJBException
224      */

225     public synchronized BeanFactory addBean(BeanDesc dd) {
226         BeanFactory bf = null;
227         String JavaDoc beanName = dd.getEjbName();
228         if (dd instanceof SessionStatefulDesc) {
229             if (TraceEjb.isDebugIc()) {
230                 TraceEjb.interp.log(BasicLevel.DEBUG, "add SessionStatefulBean " + beanName);
231             }
232             bf = new JStatefulFactory((SessionStatefulDesc) dd, this);
233         } else if (dd instanceof SessionStatelessDesc) {
234             if (TraceEjb.isDebugIc()) {
235                 TraceEjb.interp.log(BasicLevel.DEBUG, "add SessionStatelessBean " + beanName);
236             }
237             bf = new JStatelessFactory((SessionStatelessDesc) dd, this);
238         } else if (dd instanceof MessageDrivenDesc) {
239             if (TraceEjb.isDebugIc()) {
240                 TraceEjb.interp.log(BasicLevel.DEBUG, "add MessageDrivenBean " + beanName);
241             }
242             Object JavaDoc obj = null;
243             InitialContext JavaDoc ictx = null;
244             String JavaDoc destJName = ((MessageDrivenDesc) dd).getDestinationJndiName();
245             try {
246                 ictx = naming.getInitialContext();
247                 obj = ictx.lookup(destJName);
248             } catch (NamingException JavaDoc ex) {
249                 // Test also the root cause as with iiop, we got a NamingException with a NameNotFoundException as root cause
250
if (ex instanceof NameNotFoundException JavaDoc || ex.getCause() instanceof NameNotFoundException JavaDoc) {
251                     if (TraceEjb.isDebugIc()) {
252                         TraceEjb.interp.log(BasicLevel.DEBUG, "Can not find destination JNDI name " + destJName, ex);
253                     }
254                 } else {
255                     throw new EJBException JavaDoc(ex);
256                 }
257             }
258             if (obj != null && obj instanceof ActivationSpec JavaDoc) {
259                 bf = new JMdbEndpointFactory((MessageDrivenDesc) dd, this, (ActivationSpec JavaDoc) obj);
260             } else if (getJmsManager() == null) {
261                 // This is the case where we need to attach the MDB to an RAR, but it is either configured
262
// as an EJB 2.0 MDB or incorrectly for a 2.1 MDB.
263
if (((MessageDrivenDesc) dd).getDestination() != null) {
264                     if (TraceEjb.isDebugIc()) {
265                         TraceEjb.interp.log(BasicLevel.DEBUG, "JMS service not started and specified ActivationSpec(" + destJName + ") not deployed");
266                     }
267                     throw new EJBException JavaDoc("JMS service not started and specified ActivationSpec(" + destJName + ") not deployed");
268                 }
269
270                 // Get the default ActivationSpec name and attempt to configure it
271
String JavaDoc dest = Rar.getDefaultAS(); //default name
272
if (dest == null) {
273                     dest = DEFAULT_ACTIVATION_SPEC_NAME; //default name
274
}
275
276                 try {
277                     obj = ictx.lookup(dest);
278                 } catch (Exception JavaDoc ex) {
279                     if (TraceEjb.isDebugIc()) {
280                         TraceEjb.interp.log(BasicLevel.DEBUG, "JMS service not started and default ActivationSpec(" + dest + ") not deployed");
281                     }
282                     throw new EJBException JavaDoc("JMS service not started and default ActivationSpec(" + dest + ") not deployed", ex);
283                 }
284                 if (obj != null && obj instanceof ActivationSpec JavaDoc) {
285                     bf = new JMdbEndpointFactory((MessageDrivenDesc) dd, dest, this, (ActivationSpec JavaDoc) obj);
286                 } else {
287                     if (TraceEjb.isDebugIc()) {
288                         TraceEjb.interp.log(BasicLevel.DEBUG, "Invalid destination: No ActivationSpec deployed matching " + dest);
289                     }
290                     throw new EJBException JavaDoc("Invalid destination: No ActivationSpec deployed matching " + dest);
291                 }
292             } else {
293                 // This is the case where a 2.1 MDB is trying to connect to an RAR that isn't deployed
294
if (((MessageDrivenDesc) dd).getDestination() != null) {
295                     if (TraceEjb.isDebugIc()) {
296                         TraceEjb.interp.log(BasicLevel.DEBUG, "JMS service started and specified ActivationSpec(" + destJName + ") not deployed");
297                     }
298                     throw new EJBException JavaDoc("JMS service started and specified ActivationSpec(" + destJName + ") not deployed");
299                 }
300                 bf = new JMdbFactory((MessageDrivenDesc) dd, this);
301             }
302         } else if (dd instanceof EntityJdbcCmp2Desc) {
303             EntityJdbcCmp2Desc ecd = (EntityJdbcCmp2Desc) dd;
304             // TODO We only manage "Jdbc" CMP2 entity beans today.
305
if (TraceEjb.isDebugIc()) {
306                 TraceEjb.interp.log(BasicLevel.DEBUG, "add CMP2 EntityBean " + beanName);
307             }
308             // Instanciate the Factory depending on the mapper
309
String JavaDoc cn = null;
310             try {
311                 String JavaDoc dsn = ecd.getDatasourceJndiName();
312                 // Pb: We need the mapper to instanciate the Factory.
313
// The mapper is in the Datasource. No way to get it in a
314
// standard way
315
// since Datasource interface is very simple (getConnection()
316
// only)
317
//
318
// This will now try to call a getMapperName method for the
319
// datasource,
320
// if it doesn't exist then get the mapper.properties file and
321
// look for the
322
// jndiname. This allows flexibility for additional data source
323
// support,
324
// but still is not perfect.
325

326                 InitialContext JavaDoc ictx = naming.getInitialContext();
327                 String JavaDoc mapperName = null;
328                 // Attempt to call getMapperName method and if it doesn't exist
329
// then
330
// read the mapper.properties file
331
try {
332                     Object JavaDoc cls = ictx.lookup(dsn);
333                     Method JavaDoc meth = cls.getClass().getMethod("getMapperName", (Class JavaDoc[]) null);
334                     mapperName = (String JavaDoc) meth.invoke(cls, (Object JavaDoc[]) null);
335                 } catch (Exception JavaDoc e1) {
336                     if (TraceEjb.isDebugIc()) {
337                         TraceEjb.interp.log(BasicLevel.DEBUG, "Cannot read mapper name", e1);
338                     }
339                 }
340                 if (mapperName == null || mapperName.trim().length() == 0) {
341                     // Method doesn't exist so go read mapper.properties and
342
// look for jndiname mapping
343
try {
344                         JProp dsProps = JProp.getInstance("mapper");
345                         mapperName = dsProps.getValue(dsn, "");
346                     } catch (Exception JavaDoc e2) {
347                         throw new EJBException JavaDoc("Unable to retrieve mapperName for " + dsn, e2);
348                     }
349                     if (mapperName == null) {
350                         throw new EJBException JavaDoc("Unable to retrieve mapperName for " + dsn + ". mappername is null.");
351                     }
352                 }
353
354                 cn = ecd.getFactoryClassName();
355                 bf = (JEntityFactory) loader.loadClass(cn).newInstance();
356                 ((RdbFactory) bf).init(ecd, this, mapperName);
357                 setSwapTime(((EntityDesc) ecd).getPassivationTimeout());
358                 TraceEjb.interp.log(BasicLevel.INFO, beanName + " is loaded and using " + mapperName);
359             } catch (Exception JavaDoc e) {
360                 TraceEjb.interp.log(BasicLevel.ERROR, "Impossible to instanciate the entity factory: " + cn, e);
361                 throw new EJBException JavaDoc("Impossible to instanciate the entity factory: " + cn, e);
362             }
363         } else if (dd instanceof EntityDesc) {
364             if (TraceEjb.isDebugIc()) {
365                 TraceEjb.interp.log(BasicLevel.DEBUG, "add EntityBean " + beanName);
366             }
367             // Instanciate the default factory
368
String JavaDoc cn = null;
369             try {
370                 cn = DEFAULT_FACTORY_CLASS_NAME;
371                 bf = (JEntityFactory) loader.loadClass(cn).newInstance();
372             } catch (Exception JavaDoc e) {
373                 throw new EJBException JavaDoc("Impossible to instanciate the specified entity factory: " + cn, e);
374             }
375             ((JEntityFactory) bf).init((EntityDesc) dd, this);
376             setSwapTime(((EntityDesc) dd).getPassivationTimeout());
377         } else {
378             throw new EJBException JavaDoc("Bad Descriptor Type for " + beanName);
379         }
380         beanList.put(beanName, bf);
381         TraceEjb.interp.log(BasicLevel.INFO, beanName + " available");
382         return bf;
383     }
384
385     /**
386      * Used by the above getXXXDependence() methods.
387      * @param beanFactories the bean factories managed by this container.
388      * @param rName jndi name of a resource (datasource, JMS destination or
389      * connection factory, mail factory).
390      * @param isResRef true for resource-ref, false for resource-env-ref.
391      * @return set of Properties, where each Properties provides the bean name,
392      * the container's file-name and the type of the bean which uses the
393      * resource.
394      */

395     private Set JavaDoc beansDependence(Enumeration JavaDoc beanFactories, String JavaDoc rName, boolean isResRef) {
396         HashSet JavaDoc result = new HashSet JavaDoc();
397         BeanFactory bf = null;
398         while (beanFactories.hasMoreElements()) {
399             // for each bean,
400
bf = (BeanFactory) beanFactories.nextElement();
401             BeanDesc ejbDesc = bf.getDeploymentDescriptor();
402
403             boolean isDependent = false; // suppose its not dependent
404

405             // get its type from the DeploymentDescriptor
406
String JavaDoc ejbType = null;
407             if (bf instanceof JEntityFactory) {
408                 if (ejbDesc instanceof EntityBmpDesc) {
409                     ejbType = "ejbbmp";
410                 }
411                 if (ejbDesc instanceof EntityCmpDesc) {
412                     ejbType = "ejbcmp";
413                 }
414             } else if (bf instanceof JStatefulFactory) {
415                 ejbType = "ejbsbf";
416             } else if (bf instanceof JStatelessFactory) {
417                 ejbType = "ejbsbl";
418             } else if (bf instanceof JMdbFactory || bf instanceof JMdbEndpointFactory) {
419                 ejbType = "ejbmdb";
420             }
421
422             if (isResRef) {
423                 // if this is a cmp bean, directly compare rName with the data
424
// source JNDI name
425
// from its deployment descriptor
426
if (ejbType.equals("ejbcmp")) {
427                     String JavaDoc jndiName = null;
428                     if (ejbDesc instanceof EntityJdbcCmp1Desc) {
429                         jndiName = ((EntityJdbcCmp1Desc) ejbDesc).getDatasourceJndiName();
430                     } else if (ejbDesc instanceof EntityJdbcCmp2Desc) {
431                         jndiName = ((EntityJdbcCmp2Desc) ejbDesc).getDatasourceJndiName();
432                     }
433                     isDependent = rName.equals(jndiName);
434                 }
435                 // get the description of the bean's resource-refs
436
ResourceRefDesc[] rrDesc = ejbDesc.getResourceRefDesc();
437                 for (int i = 0; i < rrDesc.length; i++) {
438                     // for each resource-ref compare its name with rName
439
if (rrDesc[i].getJndiName().equals(rName)) {
440                         isDependent = true;
441                         break;
442                     }
443                 }
444             } else {
445                 if (ejbType.equals("ejbmdb")) {
446                     if (rName.equals(((MessageDrivenDesc) ejbDesc).getDestinationJndiName())) {
447                         isDependent = true;
448                     }
449                 }
450                 // get the description of the bean's resource-env-refs
451
ResourceEnvRefDesc[] rerDesc = ejbDesc.getResourceEnvRefDesc();
452                 for (int i = 0; i < rerDesc.length; i++) {
453                     // for each resource-env-ref compare its name with rName
454
if (rerDesc[i].getJndiName().equals(rName)) {
455                         isDependent = true;
456                         break;
457                     }
458                 }
459             }
460
461             if (isDependent) {
462                 Properties JavaDoc toAdd = new Properties JavaDoc();
463                 toAdd.setProperty("type", ejbType);
464                 toAdd.setProperty("fname", getFileName());
465                 toAdd.setProperty("name", ejbDesc.getEjbName());
466                 toAdd.setProperty("cname", getName());
467                 String JavaDoc earFileName = getEarFileName();
468                 if (earFileName != null) {
469                     toAdd.setProperty("earFileName", earFileName);
470                 }
471                 result.add(toAdd);
472             }
473
474         }
475         return result;
476     }
477
478     /**
479      * Check Security. No control for Message Driven Beans
480      * @param ejbName name of the EJB of which do control
481      * @param ejbInv object containing security signature of the method, args of
482      * method, etc
483      * @param inRunAs bean calling this method is running in run-as mode or not ?
484      */

485     protected void checkSecurity(String JavaDoc ejbName, EJBInvocation ejbInv, boolean inRunAs) {
486         String JavaDoc oldContextId = PolicyContext.getContextID();
487
488         boolean accessIsOk = false;
489         try {
490             // Set contextID to PermissionManager contextID (EJB ContextID)
491
if (permissionManager != null) {
492                 accessIsOk = permissionManager.checkSecurity(ejbName, ejbInv, inRunAs);
493             }
494         } catch (Exception JavaDoc e) {
495             TraceEjb.security.log(BasicLevel.ERROR, "Error while checking security", e);
496         } finally {
497             PolicyContext.setContextID(oldContextId);
498         }
499         if (!accessIsOk) {
500             StringBuffer JavaDoc errMsg = new StringBuffer JavaDoc("Access Denied on bean '");
501             errMsg.append(ejbName);
502             errMsg.append("' with run-as = '");
503             errMsg.append(inRunAs);
504             errMsg.append("'. ");
505             if (ejbInv != null && ejbInv.methodPermissionSignature != null) {
506                 errMsg.append(" Method signature = '");
507                 errMsg.append(ejbInv.methodPermissionSignature);
508                 errMsg.append("'.");
509             }
510             throw new AccessLocalException JavaDoc(errMsg.toString());
511         }
512
513     }
514
515     /**
516      * Get the bean factory for the given bean.
517      * @param ejbName the name of the bean
518      * @return the bean factory for this bean
519      */

520     public BeanFactory getBeanFactory(String JavaDoc ejbName) {
521         return (BeanFactory) beanList.get(ejbName);
522     }
523
524     /**
525      * @return total Number of Beans
526      */

527     public int getBeanNb() {
528         return beanList.size();
529     }
530
531     /**
532      * @return the classloader used for this Container
533      */

534     public ClassLoader JavaDoc getClassLoader() {
535         if (loader == null) {
536             TraceEjb.logger.log(BasicLevel.ERROR, "container has been removed");
537             //return Thread.currentThread().getContextClassLoader();
538
return null;
539         } else {
540             return loader;
541         }
542     }
543
544     /**
545      * used internally by all the EJB Container classes.
546      * @return the ContainerNaming object
547      */

548     public ContainerNaming getContainerNaming() {
549         return naming;
550     }
551
552     /**
553      * Management method used by the EJBServiceImpl MBean. Determine which are
554      * the ejbs using a given data source.
555      * @param dsName JNDI name of the data source
556      * @return a set of Properties describing the beans that use the data source
557      */

558     public Set JavaDoc getDataSourceDependence(String JavaDoc dsName) {
559         // the true param corresponds to a resource-ref
560
return beansDependence(Collections.enumeration(beanList.values()), dsName, true);
561     }
562
563     /**
564      * get the name of the ear application containing this container.
565      * @return the name of the ear application containing this container.
566      */

567     public String JavaDoc getEarFileName() {
568         return earFileName;
569     }
570
571     /**
572      * @return int Number of BMP type currently in this container
573      */

574     public int getEntityBMPNb() {
575         BeanFactory bf;
576         int total = 0;
577         Iterator JavaDoc it = beanList.values().iterator();
578         while (it.hasNext()) {
579             bf = (BeanFactory) it.next();
580             if (bf.getDeploymentDescriptor() instanceof EntityBmpDesc) {
581                 total++;
582             }
583         }
584         return total;
585     }
586
587     /**
588      * @return int Number of CMP type currently in this container
589      */

590     public int getEntityCMPNb() {
591         BeanFactory bf;
592         int total = 0;
593         Iterator JavaDoc it = beanList.values().iterator();
594         while (it.hasNext()) {
595             bf = (BeanFactory) it.next();
596             if (bf.getDeploymentDescriptor() instanceof EntityCmpDesc) {
597                 total++;
598             }
599         }
600         return total;
601     }
602
603     /**
604      * @return the file name of the container (.xml or .jar)
605      */

606     public String JavaDoc getFileName() {
607         return fileName;
608     }
609
610     /**
611      * @return the external file name of the container
612      */

613     public String JavaDoc getExternalFileName() {
614         return externalFileName;
615     }
616
617     /**
618      * Management method used by the EJBServiceImpl MBean. Determine which are
619      * the ejbs using a JMS Connection Factory.
620      * @param cfName JNDI name of a JMS Connection Factory.
621      * @return a set of Properties describing the beans that use the JMS
622      * Connection Factory.
623      */

624     public Set JavaDoc getJmsConnectionFactoryDependence(String JavaDoc cfName) {
625         // the true param corresponds to a resource-ref
626
return beansDependence(Collections.enumeration(beanList.values()), cfName, true);
627     }
628
629     /**
630      * Management method used by the EJBServiceImpl MBean. Determine which are
631      * the beans using a JMS destination.
632      * @param destName JNDI name of a JMS destination
633      * @return a set of Properties describing the ejbs that use the JMS
634      * destination.
635      */

636     public Set JavaDoc getJmsDestinationDependence(String JavaDoc destName) {
637         // the true param corresponds to a resource-ref
638
return beansDependence(Collections.enumeration(beanList.values()), destName, false);
639     }
640
641     /**
642      * used internally by all the EJB Container classes.
643      * @return the JmsManager object
644      */

645     public JmsManager getJmsManager() {
646         return jms;
647     }
648
649     /**
650      * Management method used by the EJBServiceImpl MBean. Determine which are
651      * the ejbs using a Mail Factory.
652      * @param mfName JNDI name of a Mail Factory.
653      * @return a set of Properties describing the beans that use the given Mail
654      * Factory.
655      */

656     public Set JavaDoc getMailFactoryDependence(String JavaDoc mfName) {
657         // the true param corresponds to a resource-ref
658
return beansDependence(Collections.enumeration(beanList.values()), mfName, true);
659     }
660
661     /**
662      * @return int Number of MDB type currently in this container
663      */

664     public int getMessageDrivenNb() {
665         BeanFactory bf;
666         int total = 0;
667         Iterator JavaDoc it = beanList.values().iterator();
668         while (it.hasNext()) {
669             bf = (BeanFactory) it.next();
670             if (bf.getDeploymentDescriptor() instanceof MessageDrivenDesc) {
671                 total++;
672             }
673         }
674         return total;
675     }
676
677     /**
678      * @return name of this Container
679      */

680     public String JavaDoc getName() {
681         return myname;
682     }
683
684     /**
685      * Gets the permission manager
686      * @return the permission manager
687      */

688     public PermissionManager getPermissionManager() {
689         return permissionManager;
690     }
691
692     /**
693      * @return the PrincipalFactory of the Container
694      */

695     public PrincipalFactory getPrincipalFactory() {
696         return principalFactory;
697     }
698
699     /**
700      * @return int Number of SBF type currently in this container
701      */

702     public int getStatefulSessionNb() {
703         BeanFactory bf;
704         int total = 0;
705         Iterator JavaDoc it = beanList.values().iterator();
706         while (it.hasNext()) {
707             bf = (BeanFactory) it.next();
708             if (bf.getDeploymentDescriptor() instanceof SessionStatefulDesc) {
709                 total++;
710             }
711         }
712         return total;
713     }
714
715     /**
716      * @return int Number of SBL type currently in this container
717      */

718     public int getStatelessSessionNb() {
719         BeanFactory bf;
720         int total = 0;
721         Iterator JavaDoc it = beanList.values().iterator();
722         while (it.hasNext()) {
723             bf = (BeanFactory) it.next();
724             if (bf.getDeploymentDescriptor() instanceof SessionStatelessDesc) {
725                 total++;
726             }
727         }
728         return total;
729     }
730
731     /**
732      * @return passivation time out for this container in seconds
733      */

734     public int getSwapTime() {
735         return swapper.getSwapperTimeout();
736     }
737
738     /**
739      * return the Transaction Manager used internally by all the EJB Container
740      * classes.
741      * @return the Transaction Manager
742      */

743     public TransactionManager getTransactionManager() {
744         return tm;
745     }
746
747     /**
748      * Return true if only if this ejbjar is in an ear file.
749      * @return true if only if this ejbjar is in an ear file.
750      */

751     public boolean isInEarCase() {
752         return (earFileName != null);
753     }
754
755     // ---------------------------------------------------------------
756
// Container Implementation
757
// ---------------------------------------------------------------
758

759     /**
760      * @return List of beans hosted in this Container
761      */

762     public String JavaDoc[] listBeanNames() {
763         return (String JavaDoc[]) beanList.keySet().toArray(new String JavaDoc[0]);
764     }
765
766     // -------------------------------------------------------------------
767
// Other public methods, used internally in this package.
768
// -------------------------------------------------------------------
769

770     /**
771      * register a BeanFactory
772      * @param bf The Bean Factory to be registered
773      */

774     public void registerBF(BeanFactory bf) {
775         swapper.addBeanFactory(bf);
776     }
777
778     /**
779      * register a BeanFactory for Sync
780      * @param bf The Bean Factory to be registered
781      */

782     public void registerBFS(BeanFactory bf) {
783         swapper.addBeanFactorySync(bf);
784     }
785
786     /**
787      * Remove the JOnAS container and unregister all beans.
788      */

789     public synchronized void remove() {
790         if (TraceEjb.isDebugIc()) {
791             TraceEjb.interp.log(BasicLevel.DEBUG, myname);
792         }
793
794         // Stop the swapper.
795
swapper.stopIt();
796
797         // synchronize all entity beans before removing them.
798
// must be done here, not in the swapper.
799
syncAll(false);
800
801         // remove all factories
802
Iterator JavaDoc it = beanList.values().iterator();
803         while (it.hasNext()) {
804             BeanFactory bf = (BeanFactory) it.next();
805             try {
806                 bf.stop();
807                 TraceEjb.interp.log(BasicLevel.INFO, myname + ": " + bf.getEJBName() + " no longer available");
808             } catch (Exception JavaDoc e) {
809                 TraceEjb.logger.log(BasicLevel.ERROR, myname, e);
810             }
811         }
812         beanList.clear();
813
814         // to unload all classes.
815
loader = null;
816
817         // Remove permission manager
818
try {
819             if (permissionManager != null) {
820                 permissionManager.delete();
821             }
822         } catch (PermissionManagerException pme) {
823             TraceEjb.logger.log(BasicLevel.ERROR, myname, pme);
824         }
825         permissionManager = null;
826
827         // Run the garbage collector
828
Runtime.getRuntime().gc();
829     }
830
831     /**
832      * Set the bean environment
833      * @param ctx Context for this bean
834      * @param dd Bean Deployment Descriptor
835      * @throws NamingException if could not rebind objects
836      */

837     public void setBeanEnvironment(Context JavaDoc ctx, BeanDesc dd) throws NamingException JavaDoc {
838
839         // Set bean context
840
// Build comp/env
841
Context JavaDoc ctxold = naming.setComponentContext(ctx);
842         Context JavaDoc envCtx = ctx.createSubcontext("comp/env");
843
844         // Bean Environment
845
EnvEntryDesc[] envt = dd.getEnvEntryDesc();
846         for (int i = 0; i < envt.length; i++) {
847             // get information in descriptor
848
String JavaDoc name = envt[i].getName();
849             Object JavaDoc obj = envt[i].getValue();
850             // register object in JNDI
851
if (TraceEjb.isDebugIc()) {
852                 TraceEjb.interp.log(BasicLevel.DEBUG, myname + ": Binding object " + name + " -> " + obj);
853             }
854             envCtx.rebind(name, obj);
855         }
856
857         // Resource References
858
ResourceRefDesc[] resref = dd.getResourceRefDesc();
859         for (int i = 0; i < resref.length; i++) {
860             // get information in descriptor
861
String JavaDoc name = resref[i].getName();
862             String JavaDoc resname = resref[i].getJndiName();
863             String JavaDoc type = resref[i].getTypeName();
864             // build the LinkRef that will be registered:
865
// FactoryClassName = null, size = 1, refAddr = resname.
866

867             // register object in JNDI
868
if (TraceEjb.isDebugIc()) {
869                 TraceEjb.interp.log(BasicLevel.DEBUG, myname + ": Linking resource " + name + " -> " + resname);
870             }
871
872             if (type.equalsIgnoreCase("java.net.URL")) {
873                 // Specify the factory to use with the right URL
874
Reference JavaDoc ref = new Reference JavaDoc("java.net.URL", "org.objectweb.jonas_lib.naming.factory.URLFactory", null);
875                 StringRefAddr JavaDoc refAddr = new StringRefAddr JavaDoc("url", resname);
876                 ref.add(refAddr);
877                 envCtx.rebind(name, ref);
878             } else {
879                 LinkRef JavaDoc lref = new LinkRef JavaDoc(resname);
880                 envCtx.rebind(name, lref);
881             }
882         }
883
884         // Resource Environment References
885
ResourceEnvRefDesc[] resEnvref = dd.getResourceEnvRefDesc();
886         for (int i = 0; i < resEnvref.length; i++) {
887             // get information in descriptor
888
String JavaDoc name = resEnvref[i].getName();
889             String JavaDoc resname = resEnvref[i].getJndiName();
890             LinkRef JavaDoc lref = new LinkRef JavaDoc(resname);
891
892             if (TraceEjb.isDebugIc()) {
893                 TraceEjb.interp.log(BasicLevel.DEBUG, myname + ": Linking resource environment " + name + " -> "
894                         + resname);
895             }
896             envCtx.rebind(name, lref);
897         }
898
899         // EJB References
900
EjbRefDesc[] ejbref = dd.getEjbRefDesc();
901         for (int i = 0; i < ejbref.length; i++) {
902             // get information in descriptor
903
String JavaDoc name = ejbref[i].getEjbRefName();
904             String JavaDoc ejbname = null;
905             ejbname = ejbref[i].getJndiName();
906             LinkRef JavaDoc lref = new LinkRef JavaDoc(ejbname);
907
908             if (TraceEjb.isDebugIc()) {
909                 TraceEjb.interp.log(BasicLevel.DEBUG, myname + ": Linking ejb " + name + " -> " + ejbname);
910             }
911             envCtx.rebind(name, lref);
912         }
913
914         // EJB Local Refs
915
// We use here ejb-link tag. This should be used also for ejb-ref when
916
// we are able to manage references to another jar file.
917
EjbLocalRefDesc[] ejblocalref = dd.getEjbLocalRefDesc();
918         for (int i = 0; i < ejblocalref.length; i++) {
919             String JavaDoc name = ejblocalref[i].getEjbRefName();
920             String JavaDoc ejbname = ejblocalref[i].getJndiLocalName();
921             LinkRef JavaDoc lref = new LinkRef JavaDoc(ejbname);
922             if (TraceEjb.isDebugIc()) {
923                 TraceEjb.interp.log(BasicLevel.DEBUG, myname + ": Linking ejb " + name + " -> " + ejbname);
924             }
925             envCtx.rebind(name, lref);
926         }
927
928         // ServiceRef
929
ServiceRefDesc[] serviceRefs = dd.getServiceRefDesc();
930         if (serviceRefs.length != 0) {
931
932             // get the JServiceFactory
933
JServiceFactory factory = null;
934
935             for (int i = 0; i < serviceRefs.length; i++) {
936
937                 if (factory == null) {
938                     factory = JServiceFactoryFinder.getJOnASServiceFactory();
939                 }
940                 // Create the Service from the ServiceRef description
941
String JavaDoc refname = serviceRefs[i].getServiceRefName();
942
943                 // create a full Reference
944
Reference JavaDoc ref;
945                 try {
946                     ref = factory.getServiceReference(serviceRefs[i], loader);
947                 } catch (Exception JavaDoc e1) {
948                     throw NamingExceptionHelper.create("Cannot get service reference", e1);
949                 }
950                 envCtx.rebind(refname, ref);
951                 if (TraceEjb.isDebugIc()) {
952                     TraceEjb.interp.log(BasicLevel.DEBUG, "Adding service-ref 'java:comp/env/" + refname + "'");
953                 }
954             }
955         }
956
957         // MessageDestination References
958
MessageDestinationRefDesc[] mdref = dd.getMessageDestinationRefDesc();
959         for (int i = 0; i < mdref.length; i++) {
960             // get information in descriptor
961
String JavaDoc name = mdref[i].getMessageDestinationRefName();
962             String JavaDoc mdname = null;
963             mdname = mdref[i].getJndiName();
964             LinkRef JavaDoc lref = new LinkRef JavaDoc(mdname);
965
966             if (TraceEjb.isDebugIc()) {
967                 TraceEjb.interp.log(BasicLevel.DEBUG, myname + ": Linking message-destination " + name + " -> " + mdname);
968             }
969             envCtx.rebind(name, lref);
970         }
971
972         // Reset bean context
973
naming.setComponentContext(ctxold);
974     }
975
976     /**
977      * set the ContainerNaming object Called by the EJB Server when starting the
978      * service.
979      * @param naming the ContainerNaming object
980      */

981     public void setContainerNaming(ContainerNaming naming) {
982         this.naming = naming;
983     }
984
985     /**
986      * set the name of the ear application containing this container.
987      * @param fileName the name of the ear application containing this
988      * container.
989      */

990     public void setEarFileName(String JavaDoc fileName) {
991         earFileName = fileName;
992     }
993
994     /**
995      * set the JmsManager object Called by the EJB Server when starting ths
996      * service.
997      * @param jms the JmsManager
998      */

999     public void setJmsManager(JmsManager jms) {
1000        this.jms = jms;
1001    }
1002
1003
1004    /**
1005     * Set the permission manager object
1006     * @param permissionManager permission manager object
1007     */

1008    public void setPermissionManager(PermissionManager permissionManager) {
1009        this.permissionManager = permissionManager;
1010    }
1011
1012    /**
1013     * Set the PrincipalFactory. This factory can be JOnAS Server dependant. The
1014     * Container makes no assumption on how to get the Principal.
1015     * @param pf the PrincipalFactory
1016     */

1017    public void setPrincipalFactory(PrincipalFactory pf) {
1018        principalFactory = pf;
1019    }
1020
1021    /**
1022     * Set the security flag to enable or disable security
1023     * @param b true or false to enable/disable security
1024     */

1025    public void setSecurity(boolean b) {
1026        securityFlag = b;
1027    }
1028
1029    /**
1030     * Take into account the swapping time for the bean.
1031     * @param t time in seconds (t = 0 no time out)
1032     */

1033    public void setSwapTime(int t) {
1034        if (t > 0) {
1035            if (TraceEjb.isDebugSwapper()) {
1036                TraceEjb.swapper.log(BasicLevel.DEBUG, myname + " sec=" + t);
1037            }
1038            swapper.setSwapperTimeout(t);
1039        }
1040    }
1041
1042    /**
1043     * set the Work Manager. Called by the EJB Server when starting the
1044     * service.
1045     * @param wm the Work Manager.
1046     */

1047    public void setWorkManager(WorkManager JavaDoc wm) {
1048        workManager = wm;
1049    }
1050
1051    /**
1052     * @return the WorkManager unique instance
1053     */

1054    public WorkManager JavaDoc getWorkManager() {
1055        return workManager;
1056    }
1057
1058    /**
1059     * set the Transaction Manager. Called by the EJB Server when starting the
1060     * service.
1061     * @param tm the Transaction Manager.
1062     */

1063    public void setTransactionManager(TransactionManager tm) {
1064        this.tm = tm;
1065    }
1066
1067    /**
1068     * Try to passivate all entity bean instances
1069     * @param passivate true if bean instances will be released after having
1070     * been written on storage.
1071     */

1072    public synchronized void syncAll(boolean passivate) {
1073        if (TraceEjb.isDebugIc()) {
1074            TraceEjb.interp.log(BasicLevel.DEBUG, myname);
1075        }
1076        BeanFactory bf = null;
1077        Iterator JavaDoc it = beanList.values().iterator();
1078        while (it.hasNext()) {
1079            bf = (BeanFactory) it.next();
1080            if (passivate) {
1081                bf.reduceCache();
1082            } else {
1083                bf.sync();
1084            }
1085        }
1086    }
1087
1088    /**
1089     * Gets the context ID of this container (for jacc)
1090     * @return contextID used for JACC
1091     */

1092    public String JavaDoc getContextId() {
1093        return externalFileName;
1094    }
1095}
1096
Popular Tags