FileDocCategorySizeDatePackage
PersistenceUnitHandler.javaAPI DocJBoss 4.2.19889Fri Jul 13 20:53:46 BST 2007org.jboss.injection

PersistenceUnitHandler

public class PersistenceUnitHandler extends Object implements InjectionHandler
Searches bean class for all @Inject and create Injectors
author
Bill Burke
version
$Revision: 60233 $

Fields Summary
private static final Logger
log
Constructors Summary
Methods Summary
public static voidaddPUDependency(java.lang.String unitName, InjectionContainer container)

      PersistenceUnitDeployment deployment = null;
      // look in EAR first
      deployment = container.getPersistenceUnitDeployment(unitName);
      if (deployment != null)
      {
         container.getDependencyPolicy().addDependency(deployment.getKernelName());
         log.debug("***** adding PU dependency from located persistence unit: " + deployment.getKernelName());
         return;
      }
      // probably not deployed yet.
      // todo not sure if we should do this in JBoss 5
      log.debug("******* could not find PU dependency so adding a default: " + PersistenceUnitDeployment.getDefaultKernelName(unitName));
      container.getDependencyPolicy().addDependency(PersistenceUnitDeployment.getDefaultKernelName(unitName));
   
public static javax.persistence.EntityManagerFactorygetEntityManagerFactory(javax.persistence.PersistenceUnit ref, InjectionContainer container)

      return getEntityManagerFactory(ref.unitName(), container);
   
public static javax.persistence.EntityManagerFactorygetEntityManagerFactory(java.lang.String unitName, InjectionContainer container)

      ManagedEntityManagerFactory managedFactory;
      PersistenceUnitDeployment deployment = container.getPersistenceUnitDeployment(unitName);
      if (deployment != null)
      {
         managedFactory = deployment.getManagedFactory();
      }
      else
      {
         return null;
      }
      return new InjectedEntityManagerFactory(managedFactory);
   
public static java.lang.ObjectgetFactory(java.lang.Class type, java.lang.String unitName, InjectionContainer container)

      if (type != null && type.getName().equals(SessionFactory.class.getName()))
         return getSessionFactory(unitName, container);
      return getEntityManagerFactory(unitName, container);
   
public static org.jboss.ejb3.entity.ManagedEntityManagerFactorygetManagedEntityManagerFactory(InjectionContainer container, java.lang.String unitName)

      ManagedEntityManagerFactory factory;
      PersistenceUnitDeployment deployment = container.getPersistenceUnitDeployment(unitName);
      if (deployment != null)
      {
         factory = deployment.getManagedFactory();
      }
      else
      {
         throw new NameNotFoundException("Unable to find persistence unit: " + unitName + " for deployment: " + container.getIdentifier());
      }
      return factory;
   
private static org.hibernate.SessionFactorygetSessionFactory(java.lang.String ref, InjectionContainer container)

      ManagedEntityManagerFactory managedFactory;
      PersistenceUnitDeployment deployment = container.getPersistenceUnitDeployment(ref);
      if (deployment != null)
      {
         managedFactory = deployment.getManagedFactory();
      }
      else
      {
         return null;
      }
      return new InjectedSessionFactory(managedFactory);
   
private static voidhandleClassAnnotation(javax.persistence.PersistenceUnit ref, InjectionContainer container, java.lang.Class clazz)

      String encName = ref.name();
      if (encName == null || encName.equals(""))
      {
         throw new RuntimeException("JBoss requires name() for class level @PersistenceUnit");
      }
      encName = "env/" + encName;
      if (container.getEncInjectors().containsKey(encName)) return;
      container.getEncInjectors().put(encName, new PuEncInjector(encName, null, ref.unitName(), "@PersistenceUnit"));
      try
      {
         addPUDependency(ref.unitName(), container);
      }
      catch (NameNotFoundException e)
      {
         throw new RuntimeException("Illegal @PersistenceUnit on " + clazz.getName() + " of unitname " + ref.unitName() + " :" + e.getMessage());
      }
   
public voidhandleClassAnnotations(java.lang.Class clazz, InjectionContainer container)

      PersistenceUnits resources = container.getAnnotation(
              PersistenceUnits.class, clazz);
      if (resources != null)
      {
         for (PersistenceUnit ref : resources.value())
         {
            handleClassAnnotation(ref, container, clazz);
         }
      }
      PersistenceUnit pu = container.getAnnotation(PersistenceUnit.class, clazz);
      if (pu != null)
      {
         handleClassAnnotation(pu, container, clazz);
      }
   
public voidhandleFieldAnnotations(java.lang.reflect.Field field, InjectionContainer container, java.util.Map injectors)

      PersistenceUnit ref = field.getAnnotation(PersistenceUnit.class);
      if (ref == null) return;
      String encName = ref.name();
      if (encName == null || encName.equals(""))
      {
         encName = InjectionUtil.getEncName(field);
      }
      else
      {
         encName = "env/" + encName;
      }
      if (!container.getEncInjectors().containsKey(encName))
      {
         container.getEncInjectors().put(encName, new PuEncInjector(encName, field.getType(), ref.unitName(), "@PersistenceUnit"));
         try
         {
            if (!field.isAnnotationPresent(IgnoreDependency.class)) addPUDependency(ref.unitName(), container);
         }
         catch (NameNotFoundException e)
         {
            throw new RuntimeException("Illegal @PersistenceUnit on " + field + " :" + e.getMessage());
         }
      }

      injectors.put(field, new JndiFieldInjector(field, encName, container.getEnc()));
   
public voidhandleMethodAnnotations(java.lang.reflect.Method method, InjectionContainer container, java.util.Map injectors)

      PersistenceUnit ref = method.getAnnotation(PersistenceUnit.class);
      if (ref == null) return;
      if (!method.getName().startsWith("set"))
         throw new RuntimeException("@PersistenceUnit can only be used with a set method: " + method);
      String encName = ref.name();
      if (encName == null || encName.equals(""))
      {
         encName = InjectionUtil.getEncName(method);
      }
      else
      {
         encName = "env/" + encName;
      }
      if (!container.getEncInjectors().containsKey(encName))
      {
         container.getEncInjectors().put(encName, new PuEncInjector(encName, method.getParameterTypes()[0], ref.unitName(), "@PersistenceUnit"));
         try
         {
            if (!method.isAnnotationPresent(IgnoreDependency.class)) addPUDependency(ref.unitName(), container);
         }
         catch (NameNotFoundException e)
         {
            throw new RuntimeException("Illegal @PersistenceUnit on " + method + " :" + e.getMessage());
         }
      }

      injectors.put(method, new JndiMethodInjector(method, encName, container.getEnc()));
   
public voidloadXml(org.jboss.metamodel.descriptor.EnvironmentRefGroup xml, InjectionContainer container)


        
   
      if (xml == null) return;
      if (xml.getPersistenceUnitRefs() == null) return;

      for (PersistenceUnitRef ref : xml.getPersistenceUnitRefs())
      {
         String encName = "env/" + ref.getRefName();
         // we add injection target no matter what.  enc injection might be overridden but
         // XML injection cannot be overriden
         Class injectionType = InjectionUtil.injectionTarget(encName, ref, container, container.getEncInjections());
         if (container.getEncInjectors().containsKey(encName))
            return;
         container.getEncInjectors().put(encName, new PuEncInjector(encName, injectionType, ref.getUnitName(), "<persistence-unit-ref>"));
         try
         {
            addPUDependency(ref.getUnitName(), container);
         }
         catch (NameNotFoundException e)
         {
            throw new RuntimeException("Illegal <persistence-unit-ref> of " + ref.getRefName() + " :" + e.getMessage());
         }
      }