FileDocCategorySizeDatePackage
ContainerFactoryImpl.javaAPI DocGlassfish v2 API22881Fri May 04 22:32:58 BST 2007com.sun.ejb.containers

ContainerFactoryImpl

public final class ContainerFactoryImpl extends Object implements ContainerFactory
A factory for containers. Called from JarManagerImpl during deployment of EJB JARs and everytime the J2EEServer starts up. There is exactly one instance of ContainerFactoryImpl per JVM.
author
Darpan Dinker

Fields Summary
private boolean
debugMonitoring
private long
debugMonitoringPeriodMS
private static final Logger
_logger
private static final boolean
debug
public static final byte
HOME_KEY
public static final byte[]
homeInstanceKey
private EJBTimerService
ejbTimerService
private PMTransactionManagerImpl
pmtm
com.sun.ejb.containers.util.LongHashMap
containers
private ThreadLocal
threadLocalContext
private static com.sun.ejb.containers.util.PoolCacheTimer
_timer
private com.sun.ejb.containers.util.PoolCacheTimer
_localTimer
private static com.sun.enterprise.util.LocalStringManagerImpl
localStrings
private static com.sun.ejb.spi.container.ContainerService
_containerService
Constructors Summary
public ContainerFactoryImpl()


     
    

        // Create the PMTransactionManagerImpl, this is looked up
        // thru JNDI by the PM at the name "java:pm/TransactionManager".
        pmtm = new PMTransactionManagerImpl();

        _localTimer = _timer;

        getDebugMonitoringDetails();  
        
        if(debugMonitoring) {
            _timer.schedule(new DebugMonitor(), 0L, debugMonitoringPeriodMS);
        }

        _containerService = new ContainerServiceImpl();
        _containerService.initializeService();

        IOUtils.setJ2EEObjectStreamFactory(
            _containerService.getJ2EEObjectStreamFactory());
        
    
Methods Summary
public ContainercreateContainer(EjbDescriptor ejbDescriptor, java.lang.ClassLoader loader, com.sun.enterprise.SecurityManager sm, com.sun.enterprise.config.ConfigContext dynamicConfigContext)

        BaseContainer container = null;
        boolean hasHome = true;
        String commitOption = null;
        String appid = ejbDescriptor.getApplication().getRegistrationName();
        String archiveuri = ejbDescriptor.getEjbBundleDescriptor().
            getModuleDescriptor().getArchiveUri();
            
        String modulename = 
            com.sun.enterprise.util.io.FileUtils.makeFriendlyFilename(archiveuri);
        String ejbname = ejbDescriptor.getName();

        IASEjbExtraDescriptors iased = null;
        //Server svr = null;
        Config cfg = null;
        EjbContainer ejbContainerDesc = null;

        try {
            // instantiate container class
            if (ejbDescriptor instanceof EjbSessionDescriptor) {
                EjbSessionDescriptor sd = (EjbSessionDescriptor)ejbDescriptor;
                if ( sd.isStateless() ) {
                    container = new StatelessSessionContainer(ejbDescriptor, loader);
                } else {
                    //container = new StatefulSessionContainer(ejbDescriptor, loader);
		    BaseContainerBuilder builder =
			new StatefulContainerBuilder();
		    builder.buildContainer(ejbDescriptor, loader,
			dynamicConfigContext);
		    container = builder.getContainer();
		    //containers.put(ejbDescriptor.getUniqueId(), container);
		    //builder.completeInitialization(sm);
                }
            } else if ( ejbDescriptor instanceof EjbMessageBeanDescriptor ) {
                container = new MessageBeanContainer(ejbDescriptor, loader);
		// Message-driven beans don't have a home or remote interface.
                hasHome = false;
            } else {
                if (((EjbEntityDescriptor)ejbDescriptor).getIASEjbExtraDescriptors()
                    .isIsReadOnlyBean()) { 

                    EjbEntityDescriptor robDesc = (EjbEntityDescriptor) ejbDescriptor;                    
                    container = new ReadOnlyBeanContainer (ejbDescriptor, loader);
                } else 
                    if ((ejbDescriptor.getLocalHomeClassName() != null) &&
                        (ejbDescriptor.getLocalHomeClassName()
                         .equals("com.sun.ejb.containers.TimerLocalHome"))) {
                        container = new TimerBeanContainer(ejbDescriptor, loader);
                    } else {
                        iased = ((EjbEntityDescriptor)ejbDescriptor).
                            getIASEjbExtraDescriptors();
                        if (iased != null) {
                            commitOption = iased.getCommitOption();    	
                        }
                        if (commitOption == null) {
                            try {
                                ServerContext sc = 
                                    ApplicationServer.getServerContext();

                                cfg = ServerBeansFactory.getConfigBean
                                    (sc.getConfigContext());
 
                            }  catch (ConfigException ex) {
                                _logger.log(Level.WARNING, 
                                            "ejb.createContainer_exception", ex);
                            }

                            ejbContainerDesc = cfg.getEjbContainer();
 
                            commitOption = ejbContainerDesc.getCommitOption();  
                        }
                        if (commitOption.equals("A")) {
                            _logger.log(Level.WARNING, 
                                        "ejb.commit_option_A_not_supported",
                                        new Object []{ejbDescriptor.getName()}
                                        );
                            container = 
                                new EntityContainer(ejbDescriptor, loader);
                        } else if (commitOption.equals("C")) {
                            _logger.log(Level.FINE, "Using commit option C for: " 
                                        + ejbDescriptor.getName());
                            container = new CommitCEntityContainer(ejbDescriptor,
                                                                   loader);
                        } else {
                            _logger.log(Level.FINE,"Using commit option B for: " + 
                                        ejbDescriptor.getName());
                            container = new EntityContainer(ejbDescriptor, loader);
                        }
                    }
            }

            containers.put(ejbDescriptor.getUniqueId(), container);
		
            container.setSecurityManager(sm);
    
            // Initialize home after putting the container into containers,
            // so that any calls from ProtocolManager during home initialization
            // (e.g. is_a during PRO.narrow) will work.
            if ( hasHome ) {
                container.initializeHome();
            }

            container.setDebugMonitorFlag(debugMonitoring);

            return container;
        } catch ( InvalidNameException ex ) {
            _logger.log(Level.SEVERE,"ejb.create_container_exception", ex.toString());
            _logger.log(Level.SEVERE,"Invalid jndiName for" + "appId=" + appid +
                        "; moduleName=" + modulename + "; ejbName=" + ejbname); 
            _logger.log(Level.SEVERE,"jndiName=" +  ejbDescriptor.getJndiName());
            
            // removes the ejb from containers table
            try {
                removeContainer(ejbDescriptor.getUniqueId());
            } catch (Exception e) { 
                _logger.log(Level.FINE, "", e);            
            }
            
            throw ex;
        } catch (UnsupportedOperationException unSupEx) {
            throw unSupEx;
        } catch ( Exception ex ) {
            _logger.log(Level.SEVERE,"ejb.create_container_exception", ex.toString());
            _logger.log(Level.SEVERE,"appId=" + appid + " moduleName=" + modulename + 
                        " ejbName=" + ejbname);
            if (debug) {
                _logger.log(Level.SEVERE,  ex.getMessage(), ex);
            }
            
            // removes the ejb from containers table
            try {
                removeContainer(ejbDescriptor.getUniqueId());
            } catch (Exception e) { 
                _logger.log(Level.FINE, "", e);            
            }
            
            throw ex;
        }
    
java.util.VectorgetBeans(javax.transaction.Transaction tx)


        TxData txData = (TxData) ((J2EETransaction) tx).getContainerData();
            
        if ( txData == null ) {
            txData = new TxData();
            ((J2EETransaction)tx).setContainerData(txData);
        }

        if( txData.beans == null ) {
            txData.beans = new Vector();
        }
        
        return txData.beans;

    
public ContainergetContainer(long ejbId)
Get the container instance corresponding to the given EJB id. Called from the POAProtocolMgr when an invocation arrives for the home/remote object (and other callers).

        return (Container)containers.get(ejbId);
    
public static com.sun.ejb.spi.container.ContainerServicegetContainerService()

        return _containerService;
    
ContainerSynchronizationgetContainerSync(javax.transaction.Transaction tx)
Get/create a ContainerSynchronization object for the given tx. Called only from BaseContainer.

        TxData txData = (TxData) ((J2EETransaction) tx).getContainerData();
            
        if ( txData == null ) {
            txData = new TxData();
            ((J2EETransaction)tx).setContainerData(txData);
        }

        if( txData.sync == null ) {
            txData.sync = new ContainerSynchronization(tx, this);
            tx.registerSynchronization(txData.sync);
        }

        return txData.sync;
    
private voidgetDebugMonitoringDetails()

 
        try{
            Properties props = System.getProperties();
            String str=props.getProperty("MONITOR_EJB_CONTAINER");
            if( null != str) {
		str = str.toLowerCase();
                debugMonitoring = Boolean.valueOf(str).booleanValue();
		String period =
		    props.getProperty("MONITOR_EJB_TIME_PERIOD_SECONDS");
		if (period != null) {
                debugMonitoringPeriodMS =
		    (new Long(period).longValue())* 1000;
		}
            }
        } catch(Exception e) {
            _logger.log(Level.INFO,
                "ContainerFactoryImpl.getDebugMonitoringDetails(), " +
                " Exception when trying to " + 
                "get the System properties - ", e);
        }
    
public java.lang.ObjectgetEJBContextObject(java.lang.String contextType)


        InvocationManager invMgr = Switch.getSwitch().getInvocationManager();

        ComponentInvocation currentInv = invMgr.getCurrentInvocation();

        if(currentInv == null) {
            throw new IllegalStateException("no current invocation");
        } else if (currentInv.getInvocationType() != 
                   ComponentInvocation.EJB_INVOCATION) {
            throw new IllegalStateException
                ("Illegal invocation type for EJB Context : " 
                 + currentInv.getInvocationType());
        }
        
        Object returnObject = currentInv.context;

        if( contextType.equals("javax.ejb.TimerService") ) {
            if( ejbTimerService == null ) {
                throw new IllegalStateException("EJB Timer Service not " +
                                                "available");
            }
            returnObject = new EJBTimerServiceWrapper
                (ejbTimerService, (EJBContextImpl) currentInv.context);
        }

        return returnObject;
    
EJBTimerServicegetEJBTimerService()

        return ejbTimerService;
    
public EjbDescriptorgetEjbDescriptor(long ejbId)
Return the EjbDescriptor for the given ejbId. Called from the ProtocolManager.

        Container c = (Container)containers.get(ejbId);

        if ( c == null ) {
            return null;
        }
        return c.getEjbDescriptor();
    
public static java.util.TimergetTimer()

        return _timer;
    
public javax.transaction.TransactionManagergetTransactionMgr()

        return pmtm;
    
public voidinitEJBTimerService()

        ejbTimerService = null;
    
public java.util.EnumerationlistContainers()
List all container instances in this JVM.

        return containers.elements();
    
public javax.persistence.EntityManagerlookupExtendedEntityManager(javax.persistence.EntityManagerFactory factory)


        Switch theSwitch = Switch.getSwitch();

        InvocationManager invMgr = theSwitch.getInvocationManager();
        ComponentInvocation inv = invMgr.getCurrentInvocation();

        EntityManager em = null;
        
        if( (inv != null) && 
            (inv.getInvocationType() == ComponentInvocation.EJB_INVOCATION )) {

            EjbDescriptor ejbDesc = (EjbDescriptor)
                theSwitch.getDescriptorFor(inv.getContainerContext());

            if( (ejbDesc instanceof EjbSessionDescriptor) &&
                ( ((EjbSessionDescriptor)ejbDesc).isStateful() ) ) {
                em = ((SessionContextImpl)inv.context).
                    getExtendedEntityManager(factory);
            }
        }

        return em;
    
public voidremoveContainer(long ejbId)
Remove the container instance corresponding to the given EJB id.

        containers.remove(ejbId);
    
voidremoveContainerSync(javax.transaction.Transaction tx)

    
public voidrestoreEJBTimers()

        if( ejbTimerService != null ) {
            ejbTimerService.restoreTimers();
        }
    
public voidsetEJBTimerService(EJBTimerService ejbTimerService)
Set EJB Timer Service. Can be null if timer service is being disabled.

        this.ejbTimerService = ejbTimerService;	

        DistributedEJBServiceFactory.setDistributedEJBTimerService( 
            (DistributedEJBTimerService)ejbTimerService );

        if( null != ejbTimerService ) {
            //Register the Admin event listener to satisfy requests from the 
            //admin cli. The AdminEJBTimerListerner is created as a singleton
            AdminEJBTimerEventListenerImpl.getEjbTimerEventListener();
        }
    
public voidshutdownEJBTimerService()

        if( ejbTimerService != null ) {
            ejbTimerService.shutdown();
        }