FileDocCategorySizeDatePackage
Ejb3Deployment.javaAPI DocJBoss 4.2.122326Fri Jul 13 20:53:58 BST 2007org.jboss.ejb3

Ejb3Deployment

public abstract class Ejb3Deployment extends Object
An EjbModule represents a collection of beans that are deployed as a unit.
author
Bill Burke
version
$Revision: 63291 $

Fields Summary
private static final Logger
log
public static final String
ACTUAL_ENTITY_MANAGER_FACTORY_CONTEXT
public static final String
MANAGED_ENTITY_FACTORY_CONTEXT
protected DeploymentUnit
unit
protected LinkedHashMap
ejbContainers
protected boolean
hasEntities
protected List
explicitEntityClasses
protected List
persistenceUnitDeployments
protected String
defaultSLSBDomain
protected String
defaultSFSBDomain
protected String
defaultMDBDomain
protected String
defaultConsumerDomain
protected String
defaultServiceDomain
protected InitialContext
initialContext
protected KernelAbstraction
kernelAbstraction
protected MBeanServer
mbeanServer
protected DeploymentScope
deploymentScope
protected org.jboss.ejb3.enc.EjbModuleEjbResolver
ejbRefResolver
protected org.jboss.ejb3.enc.EjbModulePersistenceUnitResolver
persistenceUnitResolver
javax.security.jacc.PolicyConfiguration
pc
Constructors Summary
public Ejb3Deployment(DeploymentUnit unit, DeploymentScope deploymentScope)


       
   
      this.unit = unit;
      this.deploymentScope = deploymentScope;
      try
      {
         initialContext = EJB3Util.getInitialContext(unit.getJndiProperties());
      }
      catch (NamingException e)
      {
         throw new RuntimeException(e);
      }
      ejbRefResolver = new EjbModuleEjbResolver(deploymentScope, unit.getShortName(), ejbContainers, this);
      persistenceUnitResolver = new EjbModulePersistenceUnitResolver(persistenceUnitDeployments, deploymentScope, ejbContainers);
   
Methods Summary
public voidcreate()
Create all EJB containers and Persistence Units The only things that should be initialized is metadata that does not need access to any other deployment. This is because we want the entire EAR to be initialized so that we do not have to guess on dependencies MBean names. This is because of the silly scoping rules for persistence units and EJBs.

throws
Exception

      try
      {
         long start = System.currentTimeMillis();
   
         pc = createPolicyConfiguration();
   
         deploy();
   
         initializePersistenceUnits();
   
         log.debug("EJB3 deployment time took: " + (System.currentTimeMillis() - start));
      }
      catch(Exception e)
      {
         try
         {
            destroy();
         }
         catch(Exception ignored)
         {
            // ignore
         }
         throw e;
      }
   
public abstract DependencyPolicycreateDependencyPolicy()

protected abstract javax.security.jacc.PolicyConfigurationcreatePolicyConfiguration()

protected voiddeploy()

      Ejb3HandlerFactory factory = Ejb3HandlerFactory.getInstance(this);
      if (unit.getUrl() != null) deployUrl(factory);
      
      if (unit.getClasses() != null)
      {
         for (Class explicit : unit.getClasses())
         {
            if (explicit.isAnnotationPresent(Entity.class))
            {
               continue;
            }
            String name = explicit.getName().replace('.", '/") + ".class";
            InputStream stream = explicit.getClassLoader().getResourceAsStream(name);
            deployElement(stream, factory, initialContext);
         }
      }
 
      deployBeansFromLib(initialContext);
   
protected voiddeployBeansFromLib(javax.naming.InitialContext ctx)

      EjbJarDD dd = EjbJarDDObjectFactory.parse(getDeploymentUnit().getEjbJarXml());
      dd = JBossDDObjectFactory.parse(this.getDeploymentUnit().getJbossXml(), dd);
      
      if (dd != null)
      {
         Ejb3DescriptorHandler handler = new Ejb3DescriptorHandler(this, dd);
         handler.setCtxProperties(unit.getJndiProperties());
         
         Map<String, Container> localContainers = new HashMap<String, Container>();
         Iterator<Container> containerIterator = ejbContainers.values().iterator();
         while (containerIterator.hasNext())
         {
            Container container = containerIterator.next();
            localContainers.put(container.getEjbName(), container);
         }
          
         List<Container> containers = handler.getContainers(this, localContainers);
         for (Container con : containers)
         {
            // EJBContainer has finished with all metadata initialization from XML files and such.
            // this is really a hook to do some processing after XML has been set up and before
            // and processing of dependencies and such.
            ((EJBContainer) con).instantiated();
            this.ejbContainers.put(con.getObjectName(), con);
            Ejb3Registry.register(con);
         }
      }
   
protected voiddeployElement(java.io.InputStream stream, Ejb3HandlerFactory factory, javax.naming.InitialContext ctx)

      DataInputStream dstream = new DataInputStream(new BufferedInputStream(stream));
      ClassFile cf = null;
      try
      {
         cf = new ClassFile(dstream);
      }
      finally
      {
         dstream.close();
         stream.close();
      }

      deployElement(factory, cf, ctx);

   
protected voiddeployElement(Ejb3HandlerFactory factory, javassist.bytecode.ClassFile cf, javax.naming.InitialContext ctx)

      Ejb3Handler handler = factory.createHandler(cf);
      handler.setCtxProperties(unit.getJndiProperties());
      
      if (handler.isEjb() || handler.isJBossBeanType())
      {
         List<Container> containers = handler.getContainers(this);
         for (Container con : containers)
         {
            // EJBContainer has finished with all metadata initialization from XML files and such.
            // this is really a hook to do some processing after XML has been set up and before
            // and processing of dependencies and such.
            ((EJBContainer) con).instantiated();
            ejbContainers.put(con.getObjectName(), con);
            Ejb3Registry.register(con);
         }
      }
   
protected voiddeployUrl(Ejb3HandlerFactory factory)

//      InitialContext ctx = initialContext;
//      // need to look into every entry in the archive to see if anybody has tags
//      // defined.
//      List<VirtualFile> classes = unit.getResources(new org.jboss.ejb3.ClassFileFilter());
//      for (VirtualFile classFile : classes)
//      {
//         InputStream stream = classFile.openStream();
//         deployElement(stream, factory, ctx);
//      }
        Iterator<?> it = ArchiveBrowser.getBrowser(unit.getUrl(), new ClassFileFilter());

        InitialContext ctx = initialContext;
        // need to look into every entry in the archive to see if anybody has tags
        // defined.
        while (it.hasNext())
        {
           InputStream stream = (InputStream) it.next();
           deployElement(stream, factory, ctx);
        }
   
public voiddestroy()

      destroyPersistenceUnits();
      
      undeploy();
      
      PolicyConfigurationFactory pcFactory = PolicyConfigurationFactory.getPolicyConfigurationFactory();
      PolicyConfiguration pc = pcFactory.getPolicyConfiguration(getJaccContextId(), true);
      pc.delete();
   
private voiddestroyPersistenceUnits()

      if (persistenceUnitDeployments == null) return;

      for (PersistenceUnitDeployment entityDeployment : persistenceUnitDeployments)
      {
         if (entityDeployment != null)
         {
            PersistenceUnitRegistry.unregister(entityDeployment);
         }
      }
   
public ContainergetContainer(javax.management.ObjectName name)

      return (Container) ejbContainers.get(name);
   
public java.lang.StringgetDefaultConsumerDomain()

      return defaultConsumerDomain;
   
public java.lang.StringgetDefaultMDBDomain()

      return defaultMDBDomain;
   
public java.lang.StringgetDefaultSFSBDomain()

      return defaultSFSBDomain;
   
public java.lang.StringgetDefaultSLSBDomain()

      return defaultSLSBDomain;
   
public java.lang.StringgetDefaultServiceDomain()

      return defaultServiceDomain;
   
public DeploymentUnitgetDeploymentUnit()

      return unit;
   
public DeploymentScopegetEar()

      return deploymentScope;
   
public EJBContainergetEjbContainer(java.lang.String ejbLink, java.lang.Class businessIntf)

      return ejbRefResolver.getEjbContainer(ejbLink, businessIntf);
   
public EJBContainergetEjbContainer(org.jboss.ejb3.Ejb3Deployment deployment, java.lang.Class businessIntf)

      return ejbRefResolver.getEjbContainer(deployment, businessIntf);
   
public EJBContainergetEjbContainer(java.lang.Class businessIntf)

      return ejbRefResolver.getEjbContainer(businessIntf);
   
public java.util.MapgetEjbContainers()

      return ejbContainers;
   
public java.lang.StringgetEjbJndiName(java.lang.String ejbLink, java.lang.Class businessIntf)

      return ejbRefResolver.getEjbJndiName(ejbLink, businessIntf);
   
public java.lang.StringgetEjbJndiName(java.lang.Class businessIntf)

      return ejbRefResolver.getEjbJndiName(businessIntf);
   
protected java.lang.StringgetJaccContextId()

      return unit.getShortName();
   
public KernelAbstractiongetKernelAbstraction()

      return kernelAbstraction;
   
public javax.management.MBeanServergetMbeanServer()

      return mbeanServer;
   
public org.jboss.ejb3.entity.PersistenceUnitDeploymentgetPersistenceUnitDeployment(java.lang.String unitName)

      return persistenceUnitResolver.getPersistenceUnitDeployment(unitName);
   
public org.jboss.ejb3.entity.PersistenceUnitDeploymentgetPersistenceUnitDeploymentInternal(java.lang.String unitName)

      return persistenceUnitResolver.getPersistenceUnitDeploymentInternal(unitName);
   
public java.util.ListgetPersistenceUnitDeployments()

      return persistenceUnitDeployments;
   
public java.lang.StringgetScopeKernelName()
Returns a partial MBean attribute name of the form ",ear=foo.ear,jar=foo.jar"

return

      String scopedKernelName = "";
      if (deploymentScope != null) scopedKernelName += ",ear=" + deploymentScope.getShortName();
      scopedKernelName += ",jar=" + unit.getShortName();
      return scopedKernelName;
   
protected voidinitializePersistenceUnits()

      URL persistenceXmlUrl = null;
      persistenceXmlUrl = unit.getPersistenceXml();
 
      hasEntities = persistenceXmlUrl != null;

      if (!hasEntities) return;

      if (unit.getClasses() != null)
      {
         for (Class explicit : unit.getClasses())
         {
            if (explicit.isAnnotationPresent(Entity.class))
            {
               explicitEntityClasses.add(explicit.getName());
            }
         }
      }

      // scope the unitName if this is an ejb archive
      // todo revert to this: List<PersistenceMetadata> persistenceMetadata = PersistenceXmlLoader.deploy(persistenceXmlUrl, new HashMap(), new EJB3DTDEntityResolver());
      List<PersistenceMetadata> persistenceMetadata = PersistenceXmlLoader.deploy(persistenceXmlUrl, new HashMap(), new EJB3DTDEntityResolver(), PersistenceUnitTransactionType.JTA);
      replaceSystemProperties(persistenceMetadata);
      for (PersistenceMetadata metadata : persistenceMetadata)
      {
         String earShortName = deploymentScope == null ? null : deploymentScope.getShortName();
         boolean isScoped = ejbContainers.size() > 0;
         
         // Ensure 2nd level cache entries are segregated from other deployments
         String cache_prefix = metadata.getProps().getProperty(SecondLevelCacheUtil.HIBERNATE_CACHE_REGION_PREFIX);
         if (cache_prefix == null)
         {
            // Create a region_prefix for the 2nd level cache to ensure
            // deployments are segregated
            String jarName = isScoped ? unit.getShortName() : null;
            cache_prefix = SecondLevelCacheUtil.createCacheRegionPrefix(earShortName, jarName, metadata.getName());
            metadata.getProps().setProperty(SecondLevelCacheUtil.HIBERNATE_CACHE_REGION_PREFIX, cache_prefix);
         }
         PersistenceUnitDeployment deployment = new PersistenceUnitDeployment(initialContext, this, explicitEntityClasses, persistenceXmlUrl, metadata, earShortName, unit.getShortName(), isScoped);
         PersistenceUnitRegistry.register(deployment);
         persistenceUnitDeployments.add(deployment);
      }
   
protected voidprocessEJBContainerMetadata(Container container)

      ObjectName on = container.getObjectName();
      ejbContainers.put(on, container);
      DependencyPolicy policy = createDependencyPolicy();
      container.processMetadata(policy);

   
protected abstract voidputJaccInService(javax.security.jacc.PolicyConfiguration pc, DeploymentUnit unit)

protected voidregisterEJBContainer(Container container)

      ObjectName on = container.getObjectName();
      String name = on.getCanonicalName();
      kernelAbstraction.install(name, container.getDependencyPolicy(), container);
      log.debug("Bound ejb3 container " + name);
   
protected voidreplaceSystemProperties(java.util.List persistenceMetadata)

      for (PersistenceMetadata metadata : persistenceMetadata)
      {
         List<String> classes = metadata.getClasses();
         List<String> replacedClasses = new ArrayList(classes.size());
         for (String clazz : classes)
            replacedClasses.add(StringPropertyReplacer.replaceProperties(clazz));
         metadata.setClasses(replacedClasses);
         
         Set<String> jarFiles = metadata.getJarFiles();
         Set<String> replacedJarFiles = new HashSet();
         for (String jarFile : jarFiles)
            replacedJarFiles.add(StringPropertyReplacer.replaceProperties(jarFile));
         metadata.setJarFiles(replacedJarFiles);
         
         List<String> mappingFiles = metadata.getMappingFiles();
         List<String> replacedMappingFiles = new ArrayList(mappingFiles.size());
         for (String mappingFile : mappingFiles)
            replacedMappingFiles.add(StringPropertyReplacer.replaceProperties(mappingFile));
         metadata.setMappingFiles(replacedMappingFiles);
         
         List<String> packages = metadata.getPackages();
         List<String> replacedPackages = new ArrayList(packages.size());
         for (String pkg : packages)
            replacedPackages.add(StringPropertyReplacer.replaceProperties(pkg));
         metadata.setPackages(replacedPackages);
         
         Properties props = metadata.getProps();
         Properties replacedProps = new Properties();
         if (props != null)
         {
            Iterator keys = props.keySet().iterator();
            while (keys.hasNext())
            {
               Object key = keys.next();
               String value = (String)props.get(key);
               replacedProps.put(key, StringPropertyReplacer.replaceProperties(value));
            }
            metadata.setProps(replacedProps);
         }
         
         if (metadata.getJtaDatasource() != null)
            metadata.setJtaDatasource(StringPropertyReplacer.replaceProperties(metadata.getJtaDatasource()));
         
         if (metadata.getNonJtaDatasource() != null)
            metadata.setNonJtaDatasource(StringPropertyReplacer.replaceProperties(metadata.getNonJtaDatasource()));
         
         if (metadata.getName() != null)
            metadata.setName(StringPropertyReplacer.replaceProperties(metadata.getName()));
 
         if (metadata.getProvider() != null)
            metadata.setProvider(StringPropertyReplacer.replaceProperties(metadata.getProvider()));
               
      }
   
public voidsetDefaultMDBDomain(java.lang.String defaultMDBDomain)
The default AOP domain for message driven beans.

param
defaultMDBDomain

      this.defaultMDBDomain = defaultMDBDomain;
   
public voidsetDefaultSFSBDomain(java.lang.String defaultSFSBDomain)
The default stateful session bean aspect domain

param
defaultSFSBDomain

      this.defaultSFSBDomain = defaultSFSBDomain;
   
public voidsetDefaultSLSBDomain(java.lang.String defaultSLSBDomain)
The default AOP domain for stateless session beans

param
defaultSLSBDomain

      this.defaultSLSBDomain = defaultSLSBDomain;
   
public voidsetDefaultServiceDomain(java.lang.String defaultServiceDomain)
default AOP domain for service beans.

param
defaultServiceDomain

      this.defaultServiceDomain = defaultServiceDomain;
   
public voidsetMbeanServer(javax.management.MBeanServer mbeanServer)

      this.mbeanServer = mbeanServer;
   
public voidstart()

      try
      {
         startPersistenceUnits();

         for (Object o : ejbContainers.values())
         {
            Container con = (Container) o;
            processEJBContainerMetadata(con);
         }

         for (Object o : ejbContainers.values())
         {
            Container con = (Container) o;
            registerEJBContainer(con);
         }

         putJaccInService(pc, unit);
      }
      catch (Exception ex)
      {
         try
         {
            stop();
            destroy();
         }
         catch (Exception ignored)
         {
         }
         throw ex;
      }
   
protected voidstartPersistenceUnits()

      if (persistenceUnitDeployments == null) return;

      for (PersistenceUnitDeployment entityDeployment : persistenceUnitDeployments)
      {
         if (entityDeployment != null)
         {
            DependencyPolicy policy = createDependencyPolicy();
            entityDeployment.addDependencies(policy);
            kernelAbstraction.install(entityDeployment.getKernelName(), policy, entityDeployment);
         }
      }
   
public voidstop()

      for (Object o : ejbContainers.keySet())
      {
         try
         {
            ObjectName on = (ObjectName) o;
            kernelAbstraction.uninstall(on.getCanonicalName());
         }
         catch (Exception e)
         {
            log.debug("error trying to shut down ejb container", e);
         }
      }
      stopPersistenceUnits();
   
protected voidstopPersistenceUnits()

      if (persistenceUnitDeployments == null) return;

      for (PersistenceUnitDeployment entityDeployment : persistenceUnitDeployments)
      {
         try
         {
            if (entityDeployment != null)
            {
               kernelAbstraction.uninstall(entityDeployment.getKernelName());
            }
            //FIXME: entityDeployment.removeDependencies(...)
         }
         catch (Exception e)
         {
            log.debug("error trying to shut down persistence unit", e);
         }
      }

   
private voidundeploy()

      for(Container container : ejbContainers.values())
      {
         Ejb3Registry.unregister(container);
      }