KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > container > JContainer3


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@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: JContainer3.java 1121 2006-09-27 08:51:06Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.container;
27
28 import static org.objectweb.easybeans.container.mdb.MDBMessageEndPointFactory.DEFAULT_ACTIVATION_SPEC_NAME;
29
30 import java.net.URL JavaDoc;
31 import java.util.Collection JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.List JavaDoc;
34 import java.util.Map JavaDoc;
35
36 import javax.naming.Context JavaDoc;
37 import javax.naming.InitialContext JavaDoc;
38 import javax.naming.NamingException JavaDoc;
39 import javax.resource.ResourceException JavaDoc;
40 import javax.resource.spi.ActivationSpec JavaDoc;
41 import javax.resource.spi.ResourceAdapter JavaDoc;
42
43 import org.objectweb.easybeans.api.EZBArchive;
44 import org.objectweb.easybeans.api.EZBArchiveException;
45 import org.objectweb.easybeans.api.EZBContainer;
46 import org.objectweb.easybeans.api.EZBContainerCallbackInfo;
47 import org.objectweb.easybeans.api.EZBContainerConfig;
48 import org.objectweb.easybeans.api.EZBContainerException;
49 import org.objectweb.easybeans.api.EZBContainerLifeCycleCallback;
50 import org.objectweb.easybeans.api.EZBPermissionManager;
51 import org.objectweb.easybeans.api.EZBServer;
52 import org.objectweb.easybeans.api.Factory;
53 import org.objectweb.easybeans.api.FactoryException;
54 import org.objectweb.easybeans.api.PermissionManagerException;
55 import org.objectweb.easybeans.api.bean.info.IBeanInfo;
56 import org.objectweb.easybeans.api.bean.info.IEJBJarInfo;
57 import org.objectweb.easybeans.container.info.EJBJarInfo;
58 import org.objectweb.easybeans.container.info.MessageDrivenInfo;
59 import org.objectweb.easybeans.container.info.SessionBeanInfo;
60 import org.objectweb.easybeans.container.info.security.SecurityInfoHelper;
61 import org.objectweb.easybeans.container.mdb.MDBMessageEndPointFactory;
62 import org.objectweb.easybeans.container.mdb.MDBResourceAdapterHelper;
63 import org.objectweb.easybeans.container.session.SessionFactory;
64 import org.objectweb.easybeans.container.session.stateful.StatefulSessionFactory;
65 import org.objectweb.easybeans.container.session.stateless.StatelessSessionFactory;
66 import org.objectweb.easybeans.deployment.Deployment;
67 import org.objectweb.easybeans.deployment.annotations.exceptions.AnalyzerException;
68 import org.objectweb.easybeans.deployment.annotations.exceptions.ResolverException;
69 import org.objectweb.easybeans.deployment.annotations.impl.JLocal;
70 import org.objectweb.easybeans.deployment.annotations.impl.JRemote;
71 import org.objectweb.easybeans.deployment.annotations.metadata.ClassAnnotationMetadata;
72 import org.objectweb.easybeans.deployment.annotations.metadata.EjbJarAnnotationMetadata;
73 import org.objectweb.easybeans.deployment.helper.JavaContextHelper;
74 import org.objectweb.easybeans.deployment.helper.JavaContextHelperException;
75 import org.objectweb.easybeans.deployment.resolver.JNDIResolver;
76 import org.objectweb.easybeans.deployment.xml.EJB3DeploymentDescException;
77 import org.objectweb.easybeans.enhancer.Enhancer;
78 import org.objectweb.easybeans.enhancer.EnhancerException;
79 import org.objectweb.easybeans.jmx.MBeansException;
80 import org.objectweb.easybeans.jmx.MBeansHelper;
81 import org.objectweb.easybeans.loader.EasyBeansClassLoader;
82 import org.objectweb.easybeans.log.JLog;
83 import org.objectweb.easybeans.log.JLogFactory;
84 import org.objectweb.easybeans.persistence.PersistenceUnitManager;
85 import org.objectweb.easybeans.persistence.api.EZBPersistenceUnitManager;
86 import org.objectweb.easybeans.persistence.xml.PersistenceXmlFileAnalyzer;
87 import org.objectweb.easybeans.persistence.xml.PersistenceXmlFileAnalyzerException;
88 import org.objectweb.easybeans.rpc.LocalCallRef;
89 import org.objectweb.easybeans.rpc.RemoteCallRef;
90 import org.objectweb.easybeans.security.permissions.PermissionManager;
91 import org.objectweb.easybeans.server.Embedded;
92
93 /**
94  * Defines an EJB3 container.
95  * @author Florent Benoit
96  */

97 public class JContainer3 implements EZBContainer {
98
99     /**
100      * Logger.
101      */

102     private static JLog logger = JLogFactory.getLog(JContainer3.class);
103
104     /**
105      * Id of this container.
106      */

107     private String JavaDoc id = null;
108
109     /**
110      * Classloader use to manage this archive.
111      */

112     private ClassLoader JavaDoc classLoader = null;
113
114     /**
115      * Deployment for the managed archive.
116      */

117     private Deployment deployment = null;
118
119     /**
120      * Container available.
121      */

122     private boolean available = false;
123
124     /**
125      * Map of managed ejb3 factories.
126      */

127     private Map JavaDoc<String JavaDoc, Factory> factories = null;
128
129     /**
130      * Persistence manager object which manages all persistence-unit associated
131      * to this container.
132      */

133     private EZBPersistenceUnitManager persistenceUnitManager = null;
134
135     /**
136      * JContainer Configuration.
137      */

138     private EZBContainerConfig configuration = null;
139
140     /**
141      * PermissionManager for the security permissions.
142      */

143     private EZBPermissionManager permissionManager = null;
144
145     /**
146      * Info on an ejb-jar file.
147      */

148     private IEJBJarInfo ejbJarInfo = null;
149
150     /**
151      * Build a new container on the given archive.
152      * @param config The JContainer configuration storing the archive (jar file
153      * or exploded).
154      */

155     public JContainer3(final EZBContainerConfig config) {
156         setContainerConfig(config);
157     }
158
159     /**
160      * Default constructor. Must be used in conjonction with setContainerConfig().
161      */

162     protected JContainer3() {}
163
164     /**
165      * Configure ths JConatiner. Must be called before start().
166      * @param config ContainerConfiguration instance.
167      */

168     protected void setContainerConfig(final EZBContainerConfig config) {
169         if (this.available) {
170             throw new IllegalStateException JavaDoc("Cannot change the EZBContainer configuration after start().");
171         }
172         this.configuration = config;
173         this.id = String.valueOf(System.identityHashCode(this));
174         this.deployment = new Deployment(getArchive());
175         this.factories = new HashMap JavaDoc<String JavaDoc, Factory>();
176     }
177     /**
178      * Gets the id of this container.
179      * @return string id.
180      */

181     public String JavaDoc getId() {
182         return id;
183     }
184
185     /**
186      * Start this container.
187      * @throws EZBContainerException if starting fails.
188      */

189     public void start() throws EZBContainerException {
190
191         // Analyze files
192
long tStart = System.currentTimeMillis();
193         try {
194             deployment.analyze();
195         } catch (AnalyzerException e) {
196             throw new EZBContainerException("Cannot analyze archive '" + getArchive().getName() + "'.", e);
197         } catch (ResolverException e) {
198             throw new EZBContainerException("Cannot resolve some annotations in the archive '" + getName()
199                     + "'.", e);
200         } catch (EJB3DeploymentDescException e) {
201             throw new EZBContainerException("Cannot parse deployment descriptor in the archive '" + getName()
202                     + "'.", e);
203         }
204         if (logger.isInfoEnabled()) {
205             logger.info("Analyze elapsed during : " + (System.currentTimeMillis() - tStart) + " ms");
206         }
207
208         // Run enhancer on classes found
209
URL JavaDoc url = null;
210         try {
211             url = getArchive().getURL();
212         } catch (EZBArchiveException e) {
213             throw new EZBContainerException("Cannot get URL on the archive '" + getName() + "'.", e);
214         }
215
216         // Build JNDI resolver
217
JNDIResolver jndiResolver = new JNDIResolver();
218         jndiResolver.addDeployment(deployment);
219
220         // add resolver in the map
221
Map JavaDoc<String JavaDoc, Object JavaDoc> enhancerMap = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
222         enhancerMap.put(JNDIResolver.NAME, jndiResolver);
223
224         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
225         // Define classloader if it was not yet defined (ear case -->
226
// classloader already set)
227
if (classLoader == null) {
228             classLoader = new EasyBeansClassLoader(new URL JavaDoc[] {url}, old);
229         }
230         try {
231             Thread.currentThread().setContextClassLoader(classLoader);
232             Enhancer enhancer = new Enhancer(classLoader, deployment.getAnnotationDeploymentAnalyzer()
233                     .getEjbJarAnnotationMetadata(), enhancerMap);
234
235             long tStartEnhancing = System.currentTimeMillis();
236             try {
237                 enhancer.enhance();
238             } catch (EnhancerException e) {
239                 throw new EZBContainerException("Cannot run enhancer on archive '" + getName() + "'.", e);
240             }
241             if (logger.isInfoEnabled()) {
242                 logger.info("Enhancement elapsed during : " + (System.currentTimeMillis() - tStartEnhancing) + " ms");
243             }
244
245             // Check if there is META-INF/persistence.xml file
246
PersistenceUnitManager analyzedPersistenceUnitManager = null;
247             try {
248                 analyzedPersistenceUnitManager = PersistenceXmlFileAnalyzer.analyzePersistenceXmlFile(getArchive(),
249                         getClassLoader());
250             } catch (PersistenceXmlFileAnalyzerException e) {
251                 throw new EZBContainerException("Cannot analyze the persistence.xml file in the archive", e);
252             }
253
254             // No previous manager
255
if (this.persistenceUnitManager == null) {
256                 this.persistenceUnitManager = analyzedPersistenceUnitManager;
257             } else {
258                 // merge old and new.
259
if (analyzedPersistenceUnitManager != null) {
260                     analyzedPersistenceUnitManager.merge(persistenceUnitManager);
261                     // update persistence manager with the merged one.
262
this.persistenceUnitManager = analyzedPersistenceUnitManager;
263                 }
264             }
265
266             // Create Beans Factories
267
createBeanFactories();
268
269             // cleanup
270
deployment = new Deployment(getArchive());
271             enhancer = null;
272         } finally {
273             Thread.currentThread().setContextClassLoader(old);
274         }
275         if (logger.isInfoEnabled()) {
276             logger.info("Container started in : " + (System.currentTimeMillis() - tStart) + " ms");
277         }
278         available = true;
279
280         // Send notification to callbacks
281
if (getCallbacksLifeCycle().size() > 0) {
282             EZBContainerCallbackInfo info = getContainer3CallbackInfo();
283             for (EZBContainerLifeCycleCallback callback : getCallbacksLifeCycle()) {
284                 try {
285                     callback.start(info);
286                 } catch (Throwable JavaDoc t) {
287                     // Protection from malicious code
288
logger.error("{0}.start() failed", callback.getClass().getName(), t);
289                 }
290             }
291         }
292
293     }
294
295     /**
296      * Create the factories of the beans (session and MDB).
297      * @throws EZBContainerException if binding fails.
298      */

299     private void createBeanFactories() throws EZBContainerException {
300         ejbJarInfo = new EJBJarInfo();
301         // bind session beans
302
EjbJarAnnotationMetadata ejbMetadata = deployment.getAnnotationDeploymentAnalyzer()
303                 .getEjbJarAnnotationMetadata();
304         if (ejbMetadata != null) {
305             for (ClassAnnotationMetadata bean : ejbMetadata.getClassAnnotationMetadataCollection()) {
306                 Factory factory = null;
307                 if (bean.isSession()) {
308                     factory = createSessionBeanFactory(bean);
309                 } else if (bean.isMdb()) {
310                     factory = createMessageDrivenBeanFactory(bean);
311                 }
312
313                 // Post-Configure the created factories.
314
if (factory != null) {
315
316                     // Adds more runtime info
317
IBeanInfo beanInfo = factory.getBeanInfo();
318
319                     // EJB Name
320
beanInfo.setName(bean.getJCommonBean().getName());
321
322                     // Adds security info.
323
beanInfo.setSecurityInfo(SecurityInfoHelper.getSecurityInfo(bean));
324                     ejbJarInfo.addBeanInfo(beanInfo);
325
326                     // Build java: Context
327
Context JavaDoc javaContext;
328                     try {
329                         javaContext = JavaContextHelper.build(bean, factory);
330                     } catch (JavaContextHelperException e) {
331                         throw new EZBContainerException("Cannot build environment", e);
332                     }
333
334                     // Set java: context
335
factory.setJavaContext(javaContext);
336
337                     // Add Management
338
try {
339                         MBeansHelper.getInstance().registerMBean(factory);
340                     } catch (MBeansException me) {
341                         throw new EZBContainerException("Cannot register the factory MBean", me);
342                     }
343
344
345                     // Init factory
346
try {
347                         factory.init();
348                     } catch (FactoryException e) {
349                         throw new EZBContainerException("Cannot initialize the factory.", e);
350                     }
351
352                     // Add the factory to the managed factories
353
factories.put(factory.getClassName(), factory);
354
355                 }
356             }
357
358             // Permission Manager.
359
try {
360                 permissionManager = new PermissionManager(getArchive().getURL(), ejbJarInfo);
361                 // translate metadata into permission
362
permissionManager.translateMetadata();
363                 // commit
364
permissionManager.commit();
365             } catch (PermissionManagerException e) {
366                 throw new EZBContainerException("Cannot create permission manager", e);
367             } catch (EZBArchiveException e) {
368                 throw new EZBContainerException("Cannot create permission manager", e);
369             }
370
371         }
372     }
373
374     /**
375      * Creates the given message driven bean factory.
376      * @param messageDrivenBean the message driven bean class metadata.
377      * @throws EZBContainerException if the message driven bean cannot be
378      * created.
379      * @return the build factory.
380      */

381     private Factory createMessageDrivenBeanFactory(final ClassAnnotationMetadata messageDrivenBean)
382             throws EZBContainerException {
383         String JavaDoc className = messageDrivenBean.getClassName().replace("/", ".");
384
385         // get Activation spec object
386
ActivationSpec JavaDoc activationSpec = null;
387         try {
388             activationSpec = (ActivationSpec JavaDoc) new InitialContext JavaDoc().lookup(DEFAULT_ACTIVATION_SPEC_NAME);
389         } catch (NamingException JavaDoc e1) {
390             throw new EZBContainerException("Cannot get the activation spec with the name '"
391                     + DEFAULT_ACTIVATION_SPEC_NAME + "'.");
392         }
393
394         // get ResourceAdapter object
395
ResourceAdapter JavaDoc resourceAdapter = null;
396         try {
397             resourceAdapter = MDBResourceAdapterHelper.getResourceAdapter(DEFAULT_ACTIVATION_SPEC_NAME,
398                     (Embedded) getConfiguration().getEZBServer());
399         } catch (ResourceException JavaDoc e) {
400             throw new EZBContainerException("Cannot get the resource adapter for this MDB factory", e);
401         }
402
403         // Associate Resource Adapter with ActivationSpec object (if not set)
404
if (activationSpec.getResourceAdapter() == null) {
405             try {
406                 activationSpec.setResourceAdapter(resourceAdapter);
407             } catch (ResourceException JavaDoc e) {
408                 throw new EZBContainerException("Cannot associate resource adapter with activation spec object", e);
409             }
410         }
411
412         // Create factory
413
MDBMessageEndPointFactory mdbMessageEndPointFactory = null;
414         try {
415             mdbMessageEndPointFactory = new MDBMessageEndPointFactory(className, this, activationSpec, resourceAdapter);
416         } catch (FactoryException e) {
417             throw new EZBContainerException("Cannot build the MDB MessageEndPoint factory", e);
418         }
419
420         // build runtime information
421
MessageDrivenInfo messageDrivenInfo = new MessageDrivenInfo();
422         messageDrivenInfo.setApplicationExceptions(messageDrivenBean.getEjbJarAnnotationMetadata().getApplicationExceptions());
423         messageDrivenInfo.setTransactionManagementType(messageDrivenBean.getTransactionManagementType());
424         messageDrivenInfo.setMessageListenerInterface(messageDrivenBean.getJMessageDriven()
425                 .getMessageListenerInterface());
426         messageDrivenInfo.setActivationConfigProperties(messageDrivenBean.getJMessageDriven()
427                 .getActivationConfigProperties());
428         mdbMessageEndPointFactory.setMessageDrivenInfo(messageDrivenInfo);
429
430
431
432         return mdbMessageEndPointFactory;
433
434     }
435
436     /**
437      * Creates the given session bean and bind it.
438      * @param sessionBean the session bean class metadata.
439      * @throws EZBContainerException if the session bean cannot be created
440      * @return the build factory.
441      */

442     private Factory createSessionBeanFactory(final ClassAnnotationMetadata sessionBean) throws EZBContainerException {
443         String JavaDoc className = sessionBean.getClassName().replace("/", ".");
444
445         SessionFactory sessionFactory = null;
446
447         if (sessionBean.isStateless()) {
448             try {
449                 sessionFactory = new StatelessSessionFactory(className, this);
450             } catch (FactoryException fe) {
451                 throw new EZBContainerException("Cannot build the stateless factory", fe);
452             }
453         } else {
454             try {
455                 sessionFactory = new StatefulSessionFactory(className, this);
456             } catch (FactoryException fe) {
457                 throw new EZBContainerException("Cannot build the stateless factory", fe);
458             }
459         }
460
461         // build runtime information
462
SessionBeanInfo sessionBeanInfo = new SessionBeanInfo();
463         sessionBeanInfo.setTransactionManagementType(sessionBean.getTransactionManagementType());
464         sessionBeanInfo.setApplicationExceptions(sessionBean.getEjbJarAnnotationMetadata().getApplicationExceptions());
465         sessionFactory.setSessionBeanInfo(sessionBeanInfo);
466
467         // get interface of bean
468
JLocal localItfs = sessionBean.getLocalInterfaces();
469         JRemote remoteItfs = sessionBean.getRemoteInterfaces();
470
471         if (localItfs != null) {
472             for (String JavaDoc itf : localItfs.getInterfaces()) {
473                 bindLocalItf(itf, getEmbedded().getID(), getId(), className, sessionBean, "Local");
474             }
475         }
476         if (remoteItfs != null) {
477             for (String JavaDoc itf : remoteItfs.getInterfaces()) {
478                 bindRemoteItf(itf, getId(), className, sessionBean, "Remote");
479             }
480         }
481
482         return sessionFactory;
483     }
484
485     /**
486      * Stop this container.
487      */

488     public void stop() {
489         available = false;
490
491         // stop each factories
492
for (Factory f : factories.values()) {
493             f.stop();
494
495             // Remove MBeans
496
try {
497                 MBeansHelper.getInstance().unregisterMBean(f);
498             } catch (MBeansException me) {
499                 logger.error("Cannot unregister the factory MBean", me);
500             }
501
502         }
503
504         // reset CL
505
classLoader = null;
506
507         // TODO : no unbind for now
508
// need to configure it ?
509

510         // Send notification to callbacks
511
if (getCallbacksLifeCycle().size() > 0) {
512             EZBContainerCallbackInfo info = getContainer3CallbackInfo();
513             for (EZBContainerLifeCycleCallback callback : getCallbacksLifeCycle()) {
514                 try {
515                     callback.stop(info);
516                 } catch (Throwable JavaDoc t) {
517                     // Protection from malicious code
518
logger.error("{0}.stop() failed", callback.getClass().getName(), t);
519                 }
520             }
521         }
522     }
523
524     /**
525      * Gets information on the container that can be given to container
526      * callbacks.
527      * @return information on the managed container.
528      */

529     private EZBContainerCallbackInfo getContainer3CallbackInfo() {
530         EZBContainerCallbackInfo info = new EZBContainerCallbackInfo();
531         info.setArchive(getArchive());
532         info.setFactories(factories);
533         return info;
534     }
535
536     /**
537      * Bind into registry interfaces for a bean making them available from
538      * clients.
539      * @param itf the name of the interface that object will have.
540      * @param embeddedId the ID of the embedded server.
541      * @param containerID the ID of the container.
542      * @param factoryName the name of the factory.
543      * @param bean the bean class associated to given interface.
544      * @param mode Local, Remote, etc
545      * @throws EZBContainerException if interface cannot be loaded or if the
546      * bind fails
547      */

548     private void bindLocalItf(final String JavaDoc itf, final Integer JavaDoc embeddedId, final String JavaDoc containerID, final String JavaDoc factoryName,
549             final ClassAnnotationMetadata bean, final String JavaDoc mode) throws EZBContainerException {
550         String JavaDoc itfClsName = itf.replace("/", ".");
551         Class JavaDoc clz = null;
552         try {
553             clz = classLoader.loadClass(itfClsName);
554         } catch (ClassNotFoundException JavaDoc e) {
555             throw new EZBContainerException(
556                     "Cannot find the class '" + itf + "' in Classloader '" + classLoader + "'.", e);
557         }
558
559         // Build a reference
560
LocalCallRef localCall = new LocalCallRef(itfClsName, embeddedId, containerID, factoryName, bean.isStateful());
561
562         try {
563             String JavaDoc jndiName = null;
564             String JavaDoc mappedName = bean.getJCommonBean().getMappedName();
565             if (mappedName != null && !"".equals(mappedName)) {
566                 jndiName = mappedName;
567             } else {
568                 jndiName = jndiNameEncode(bean.getClassName(), itfClsName, mode);
569             }
570             logger.debug("Binding bean {0} with interface {1} into registry with jndi name {2}", bean.getClassName()
571                     .replace("/", "."), itf.replace("/", "."), jndiName);
572             new InitialContext JavaDoc().rebind(jndiName, localCall);
573         } catch (NamingException JavaDoc e) {
574             throw new EZBContainerException("Cannot bind the object '" + localCall + "' into JNDI with the name '"
575                     + clz.getName() + "'.", e);
576         }
577
578     }
579
580     /**
581      * Bind into registry interfaces for a bean making them available from
582      * clients.
583      * @param itf the name of the interface that object will have.
584      * @param containerID the ID of the container.
585      * @param factoryName the name of the factory.
586      * @param bean the bean class associated to given interface.
587      * @param mode Local, Remote, etc
588      * @throws EZBContainerException if interface cannot be loaded or if the
589      * bind fails
590      */

591     private void bindRemoteItf(final String JavaDoc itf, final String JavaDoc containerID, final String JavaDoc factoryName,
592             final ClassAnnotationMetadata bean, final String JavaDoc mode) throws EZBContainerException {
593         String JavaDoc itfClsName = itf.replace("/", ".");
594         Class JavaDoc clz = null;
595         try {
596             clz = classLoader.loadClass(itfClsName);
597         } catch (ClassNotFoundException JavaDoc e) {
598             throw new EZBContainerException(
599                     "Cannot find the class '" + itf + "' in Classloader '" + classLoader + "'.", e);
600         }
601
602         // Build a reference
603
RemoteCallRef remoteCall = new RemoteCallRef(itfClsName, containerID, factoryName, bean.isStateful());
604
605         try {
606             String JavaDoc jndiName = null;
607             String JavaDoc mappedName = bean.getJCommonBean().getMappedName();
608             if (mappedName != null && !"".equals(mappedName)) {
609                 jndiName = mappedName;
610             } else {
611                 jndiName = jndiNameEncode(bean.getClassName(), itfClsName, mode);
612             }
613             logger.debug("Binding bean {0} with interface {1} into registry with jndi name {2}", bean.getClassName()
614                     .replace("/", "."), itf.replace("/", "."), jndiName);
615             new InitialContext JavaDoc().rebind(jndiName, remoteCall);
616         } catch (NamingException JavaDoc e) {
617             throw new EZBContainerException("Cannot bind the object '" + remoteCall + "' into JNDI with the name '"
618                     + clz.getName() + "'.", e);
619         }
620
621     }
622
623     /**
624      * Gets a factory with its given name.
625      * @param factoryName the factory name.
626      * @return the factory found or null.
627      */

628     public Factory getFactory(final String JavaDoc factoryName) {
629         return factories.get(factoryName);
630     }
631
632     /**
633      * @return Returns a Collection of managed Factories.
634      */

635     public Collection JavaDoc<Factory> getFactories() {
636         return factories.values();
637     }
638
639     /**
640      * Gets the name of this container.
641      * @return the name.
642      */

643     public String JavaDoc getName() {
644         return getArchive().getName();
645     }
646
647     /**
648      * Gets the classloader.
649      * @return classloader of the container
650      */

651     public ClassLoader JavaDoc getClassLoader() {
652         return classLoader;
653     }
654
655     /**
656      * Gets the archive used by this container. It can be a .jar file or a
657      * directory.
658      * @return the archive.
659      */

660     public EZBArchive getArchive() {
661         return configuration.getArchive();
662     }
663
664     /**
665      * Gets the parent EZBServer instance.
666      * @return Returns the Embedded instance.
667      */

668     public EZBServer getEmbedded() {
669         return configuration.getEZBServer();
670     }
671
672     /**
673      * @return Returns the LifeCycleCallback(s) instances as a List.
674      */

675     private List JavaDoc<EZBContainerLifeCycleCallback> getCallbacksLifeCycle() {
676         return configuration.getCallbacks();
677     }
678
679     /**
680      * Check if the container is available or not.
681      * @return true if the container is available.
682      */

683     public boolean isAvailable() {
684         return available;
685     }
686
687     /**
688      * Gets the persistence manager object which manages all persistence-unit
689      * associated to this container.
690      * @return persistence unit manager object
691      */

692     public EZBPersistenceUnitManager getPersistenceUnitManager() {
693         return persistenceUnitManager;
694     }
695
696     /**
697      * Encode a given name by prefixing it with given mode.
698      * @param beanClassName name of the bean class.
699      * @param itfName name of the interface.
700      * @param mode local/remote, etc
701      * @return encoded jndi Name
702      */

703     public static String JavaDoc jndiNameEncode(final String JavaDoc beanClassName, final String JavaDoc itfName, final String JavaDoc mode) {
704         return beanClassName.replace("/", ".") + "_" + itfName.replace("/", ".") + "@" + mode;
705     }
706
707     /**
708      * Sets the classloader.
709      * @param classLoader to be used by the container
710      */

711     public void setClassLoader(final ClassLoader JavaDoc classLoader) {
712         if (this.classLoader != null) {
713             throw new IllegalArgumentException JavaDoc("Cannot replace an existing classloader");
714         }
715         this.classLoader = classLoader;
716     }
717
718     /**
719      * Sets the persistence manager object which manages all persistence-unit
720      * associated to this container.
721      * @param persistenceUnitManager persistence unit manager object to set.
722      */

723     public void setPersistenceUnitManager(final EZBPersistenceUnitManager persistenceUnitManager) {
724         if (this.persistenceUnitManager != null) {
725             throw new IllegalArgumentException JavaDoc("Cannot replace an existing persistenceUnitManager");
726         }
727         this.persistenceUnitManager = persistenceUnitManager;
728     }
729
730     /**
731      * @return Returns the Container Configuration.
732      */

733     public EZBContainerConfig getConfiguration() {
734         return this.configuration;
735     }
736
737
738
739     /**
740      * Gets the permission manager (that manages EJB permissions).
741      * @return permission manager.
742      */

743     public EZBPermissionManager getPermissionManager() {
744         return permissionManager;
745     }
746
747     /**
748      * Sets the permission manager (that manages EJB permissions).
749      * @param ezbPermissionManager the EasyBeans permission manager.
750      */

751     public void setPermissionManager(final EZBPermissionManager ezbPermissionManager) {
752         this.permissionManager = ezbPermissionManager;
753     }
754 }
755
Popular Tags