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

PersistenceContextHandler

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

Fields Summary
private static final Logger
log
Constructors Summary
Methods Summary
public voidhandleClassAnnotations(java.lang.Class clazz, InjectionContainer container)

      PersistenceContexts resources = container.getAnnotation(PersistenceContexts.class, clazz);
      if (resources != null)
      {
         for (PersistenceContext ref : resources.value())
         {
            loadPersistenceContextClassAnnotation(ref, container, clazz);
         }
      }
      PersistenceContext pc = container.getAnnotation(PersistenceContext.class, clazz);

      if (pc != null)
      {
         loadPersistenceContextClassAnnotation(pc, container, clazz);
      }

   
public voidhandleFieldAnnotations(java.lang.reflect.Field field, InjectionContainer container, java.util.Map injectors)

      PersistenceContext ref = field.getAnnotation(PersistenceContext.class);
      if (ref == null) return;

      String encName = ref.name();
      if (encName == null || encName.equals(""))
      {
         encName = InjectionUtil.getEncName(field);
      }
      else
      {
         encName = "env/" + ref.name();
      }
      if (!container.getEncInjectors().containsKey(encName))
      {
         try
         {
            if (!field.isAnnotationPresent(IgnoreDependency.class)) PersistenceUnitHandler.addPUDependency(ref.unitName(), container);
         }
         catch (NameNotFoundException e)
         {
            throw new RuntimeException("Illegal @PersistenceUnit on " + field + " :" + e.getMessage());
         }
         String error = "@PersistenceContext(name='" + encName
                 + "',unitName='" + ref.unitName() + "') on EJB: "
                 + container.getIdentifier() + " failed to inject on field "
                 + field.toString();
         container.getEncInjectors().put(encName, new PcEncInjector(encName, ref.unitName(), ref.type(), field.getType(), error));
      }
      injectors.put(field, new JndiFieldInjector(field,
              encName, container.getEnc()));
   
public voidhandleMethodAnnotations(java.lang.reflect.Method method, InjectionContainer container, java.util.Map injectors)

      PersistenceContext ref = method.getAnnotation(PersistenceContext.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/" + ref.name();
      }
      if (!container.getEncInjectors().containsKey(encName))
      {
         try
         {
            if (!method.isAnnotationPresent(IgnoreDependency.class)) PersistenceUnitHandler.addPUDependency(ref.unitName(), container);
         }
         catch (NameNotFoundException e)
         {
            throw new RuntimeException("Illegal @PersistenceUnit on " + method + " :" + e.getMessage());
         }
         String error = "@PersistenceContext(name='" + encName
                 + "',unitName='" + ref.unitName() + "') on EJB: "
                 + container.getIdentifier() + " failed to inject on method "
                 + method.toString();
         container.getEncInjectors().put(encName, new PcEncInjector(encName, ref.unitName(), ref.type(), method.getParameterTypes()[0], error));
      }
      injectors.put(method, new JndiMethodInjector(method,
              encName, container.getEnc()));
   
private static voidloadPersistenceContextClassAnnotation(javax.persistence.PersistenceContext 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 @PersistenceContext");
      }
      encName = "env/" + ref.name();
      if (container.getEncInjectors().containsKey(encName)) return;

      String error = "Unable to load class-level @PersistenceContext("
              + ref.unitName() + ") on " + container.getIdentifier();
      container.getEncInjectors().put(encName, new PcEncInjector(encName, ref.unitName(), ref.type(), null, error));
      try
      {
         PersistenceUnitHandler.addPUDependency(ref.unitName(), container);
      }
      catch (NameNotFoundException e)
      {
         throw new RuntimeException("Illegal @PersistenceUnit on " + clazz.getName() + " of unitname " + ref.unitName() + " :" + e.getMessage());
      }
   
public voidloadXml(org.jboss.metamodel.descriptor.EnvironmentRefGroup xml, InjectionContainer container)


        
   
      if (xml == null) return;
      if (xml.getPersistenceContextRefs() == null) return;
      for (PersistenceContextRef ref : xml.getPersistenceContextRefs())
      {
         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))
            continue;
         // add it to list of
         String error = "unable to load <persistence-context-ref> for unitName: "
                 + ref.getUnitName() + " <ref-name>: " + ref.getRefName();
         PersistenceContextType type = ref.getPersistenceContextType();
         String unitName = ref.getUnitName();
         container.getEncInjectors().put(encName, new PcEncInjector(encName, unitName, type, injectionType, error));
         try
         {
            PersistenceUnitHandler.addPUDependency(ref.getUnitName(), container);
         }
         catch (NameNotFoundException e)
         {
            throw new RuntimeException("Illegal <persistence-context-ref> of " + ref.getRefName() + " :" + e.getMessage());
         }
      }