FileDocCategorySizeDatePackage
PersistenceUnitDeployment.javaAPI DocJBoss 4.2.110218Fri Jul 13 20:53:58 BST 2007org.jboss.ejb3.entity

PersistenceUnitDeployment

public class PersistenceUnitDeployment extends Object
Comment
author
Bill Burke
version
$Revision: 61833 $

Fields Summary
private static final Logger
log
protected InitialContext
initialContext
protected org.jboss.ejb3.DeploymentUnit
di
protected List
explicitEntityClasses
protected ManagedEntityManagerFactory
managedFactory
protected EntityManagerFactory
actualFactory
protected URL
persistenceXmlUrl
protected org.hibernate.ejb.packaging.PersistenceMetadata
xml
protected String
kernelName
protected org.jboss.ejb3.Ejb3Deployment
deployment
protected boolean
scoped
Constructors Summary
public PersistenceUnitDeployment(InitialContext initialContext, org.jboss.ejb3.Ejb3Deployment deployment, List explicitEntityClasses, URL persistenceXmlUrl, org.hibernate.ejb.packaging.PersistenceMetadata metadata, String ear, String jar, boolean isScoped)


                   
   
      this.scoped = isScoped;
      this.deployment = deployment;
      this.initialContext = initialContext;
      this.di = deployment.getDeploymentUnit();
      this.explicitEntityClasses = explicitEntityClasses;
      this.persistenceXmlUrl = persistenceXmlUrl;
      xml = metadata;
      kernelName = "persistence.units:";
      String name = getEntityManagerName();
      if (name == null || name.equals(""))
         throw new RuntimeException("Empty string is not allowed for a persistence unit.  Fix your persistence.xml file");
      if (ear != null)
      {
         kernelName += "ear=" + ear;
         if (!ear.endsWith(".ear")) kernelName += ".ear";
         kernelName += ",";
      }
      if (isScoped)
      {
         kernelName += "jar=" + jar;
         if (!jar.endsWith(".jar")) kernelName += ".jar";
         kernelName += ",";
      }
      kernelName += "unitName=" + name;
   
Methods Summary
public voidaddDependencies(org.jboss.ejb3.DependencyPolicy policy)

      Properties props = getXml().getProps();
      if (!props.containsKey("jboss.no.implicit.datasource.dependency"))
      {
         if (getXml().getJtaDatasource() != null)
         {
            String ds = getXml().getJtaDatasource();
            policy.addDatasource(ds);
         }
         if (getXml().getNonJtaDatasource() != null)
         {
            String ds = getXml().getNonJtaDatasource();
            policy.addDatasource(ds);
         }
      }
      for (Object prop : props.keySet())
      {
         String property = (String)prop;
         if (property.startsWith("jboss.depends"))
         {
            policy.addDependency(props.getProperty(property));
         }
      }

   
public javax.persistence.EntityManagerFactorygetActualFactory()

      return actualFactory;
   
public static java.lang.StringgetDefaultKernelName(java.lang.String unitName)

      int hashIndex = unitName.indexOf('#");
      if (hashIndex != -1)
      {
         String relativePath = unitName.substring(3, hashIndex);
         String name = unitName.substring(hashIndex + 1);
         return "persistence.units:jar=" + relativePath + "," + "unitName=" + name;
      }
      return "persistence.units:unitName=" + unitName;
   
public org.jboss.ejb3.Ejb3DeploymentgetDeployment()

      return deployment;
   
public java.lang.StringgetEntityManagerName()

      return xml.getName() == null ? "" : xml.getName();
   
protected java.lang.StringgetJaccContextId()

      return di.getShortName();
   
public java.lang.StringgetKernelName()

      return kernelName;
   
public ManagedEntityManagerFactorygetManagedFactory()

      if(managedFactory == null)
         log.warn("managed factory is null, persistence unit " + kernelName + " has not yet been started");
      return managedFactory;
   
public org.hibernate.ejb.packaging.PersistenceMetadatagetXml()

      return xml;
   
public booleanisScoped()

      return scoped;
   
public voidstart()

      log.info("Starting persistence unit " + kernelName);
      
      Properties props = new Properties();
      props.putAll(di.getDefaultPersistenceProperties());
      props.put(HibernatePersistence.JACC_CONTEXT_ID, getJaccContextId());

      PersistenceUnitInfoImpl pi = new PersistenceUnitInfoImpl();
      pi.setClassLoader(di.getClassLoader());

      ArrayList<URL> jarFiles = new ArrayList<URL>();
      pi.setJarFiles(jarFiles);
      pi.setPersistenceProviderClassName(HibernatePersistence.class.getName());
      log.debug("Found persistence.xml file in EJB3 jar");
      props.putAll(xml.getProps());
      pi.setManagedClassnames(xml.getClasses());
      pi.setPersistenceUnitName(xml.getName());
      pi.setMappingFileNames(xml.getMappingFiles());
      pi.setExcludeUnlistedClasses(xml.getExcludeUnlistedClasses());
      pi.setPersistenceUnitRootUrl(di.getUrl());
//      PersistenceUnitTransactionType transactionType = PersistenceUnitTransactionType.JTA;
//      if ("RESOURCE_LOCAL".equals(xml.getTransactionType()))
//         transactionType = PersistenceUnitTransactionType.RESOURCE_LOCAL;
      PersistenceUnitTransactionType transactionType = xml.getTransactionType();
      pi.setTransactionType(transactionType);

      for (String jar : xml.getJarFiles())
      {
         jarFiles.add(deployment.getDeploymentUnit().getRelativeURL(jar));
      }


      if (xml.getProvider() != null) pi.setPersistenceProviderClassName(xml.getProvider());
      if (explicitEntityClasses.size() > 0)
      {
         List<String> classes = pi.getManagedClassNames();
         if (classes == null) classes = explicitEntityClasses;
         else classes.addAll(explicitEntityClasses);
         pi.setManagedClassnames(classes);
      }
      if (xml.getJtaDatasource() != null)
      {
         pi.setJtaDataSource((javax.sql.DataSource) initialContext.lookup(xml.getJtaDatasource()));
      }
      else if (transactionType == PersistenceUnitTransactionType.JTA)
      {
         throw new RuntimeException("You have not defined a jta-data-source for a JTA enabled persistence context named: " + xml.getName());
      }
      if (xml.getNonJtaDatasource() != null)
      {
         pi.setNonJtaDataSource((javax.sql.DataSource) initialContext.lookup(xml.getNonJtaDatasource()));
      }
      else if (transactionType == PersistenceUnitTransactionType.RESOURCE_LOCAL)
      {
         throw new RuntimeException("You have not defined a non-jta-data-source for a RESOURCE_LOCAL enabled persistence context named: " + xml.getName());
      }
      pi.setProperties(props);

      if (pi.getPersistenceUnitName() == null)
      {
         throw new RuntimeException("you must specify a name in persistence.xml");
      }

      Class providerClass = Thread.currentThread().getContextClassLoader().loadClass(pi.getPersistenceProviderClassName());

      // EJBTHREE-893
      if(!pi.getProperties().containsKey("hibernate.session_factory_name"))
      {
         pi.getProperties().put("hibernate.session_factory_name", kernelName);
      }
      
      PersistenceProvider pp = (PersistenceProvider) providerClass.newInstance();
      actualFactory = pp.createContainerEntityManagerFactory(pi, null);

      managedFactory = new ManagedEntityManagerFactory(actualFactory, kernelName);

      String entityManagerJndiName = (String) props.get("jboss.entity.manager.jndi.name");
      if (entityManagerJndiName != null)
      {
         EntityManager injectedManager = new TransactionScopedEntityManager(managedFactory);
         NonSerializableFactory.rebind(initialContext, entityManagerJndiName, injectedManager);
      }
      String entityManagerFactoryJndiName = (String) props.get("jboss.entity.manager.factory.jndi.name");
      if (entityManagerFactoryJndiName != null)
      {
         EntityManagerFactory injectedFactory = new InjectedEntityManagerFactory(managedFactory);
         NonSerializableFactory.rebind(initialContext, entityManagerFactoryJndiName, injectedFactory);
      }
   
public voidstop()

      log.info("Stopping persistence unit " + kernelName);
      
      String entityManagerJndiName = (String) xml.getProps().get("jboss.entity.manager.jndi.name");
      if (entityManagerJndiName != null)
      {
         NonSerializableFactory.unbind(initialContext, entityManagerJndiName);
      }
      String entityManagerFactoryJndiName = (String) xml.getProps().get("jboss.entity.manager.factory.jndi.name");
      if (entityManagerFactoryJndiName != null)
      {
         NonSerializableFactory.unbind(initialContext, entityManagerFactoryJndiName);
      }
      managedFactory.destroy();