Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2006, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 package org.jboss.injection;
 
 import java.util.Map;
 
 
Searches bean class for all

Author(s):
Bill Burke
Version:
$Revision: 82749 $
Inject:
and create Injectors
 
 public class PersistenceUnitHandler<X extends RemoteEnvironmentimplements InjectionHandler<X>
 {
    private static final Logger log = Logger.getLogger(PersistenceUnitHandler.class);
   
   
Contracted error message value, form may be checked from Integration Tests
 
    public static final String ERROR_MESSAGE_FAILED_TO_RESOVLE_PU = " failed to resolve persistence unit ";
 
    public void loadXml(X xmlInjectionContainer container)
    {
       if (xml == nullreturn;
       if (xml.getPersistenceUnitRefs() == nullreturn;
 
       {
          String encName = "env/" + ref.getPersistenceUnitRefName();
          // we add injection target no matter what.  enc injection might be overridden but
          // XML injection cannot be overriden
          Class<?> injectionType = InjectionUtil.injectionTarget(encNamerefcontainercontainer.getEncInjections());
          if (container.getEncInjectors().containsKey(encName))
             return;
          container.getEncInjectors().put(encNamenew PuEncInjector(encNameinjectionTyperef.getPersistenceUnitName(), "<persistence-unit-ref>"));
          try
          {
             addPUDependency(ref.getPersistenceUnitName(), container);
          }
          catch (NameNotFoundException e)
          {
             throw new RuntimeException("Illegal <persistence-unit-ref> of " + ref.getPersistenceUnitRefName() + " :" + e.getMessage());
          }
       }
    }
 
 
    public void handleClassAnnotations(Class<?> clazzInjectionContainer container)
    {
       PersistenceUnits resources = container.getAnnotation(
               PersistenceUnits.classclazz);
       if (resources != null)
       {
          for (PersistenceUnit ref : resources.value())
          {
             handleClassAnnotation(refcontainerclazz);
          }
       }
       PersistenceUnit pu = container.getAnnotation(PersistenceUnit.classclazz);
       if (pu != null)
       {
          handleClassAnnotation(pucontainerclazz);
      }
   }
   private static void handleClassAnnotation(PersistenceUnit refInjectionContainer containerClass<?> 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(encNamenew PuEncInjector(encNamenullref.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 static void addPUDependency(String unitNameInjectionContainer containerthrows NameNotFoundException
   {
      if(container instanceof ExtendedInjectionContainer)
      {
         ExtendedInjectionContainer eic = (ExtendedInjectionContainercontainer;
         try
         {
            String dependency = eic.resolvePersistenceUnitSupplier(unitName);
            container.getDependencyPolicy().addDependency(dependency);
         }
         catch(IllegalArgumentException e)
         {
            throw new IllegalArgumentException("Container " + container +  + unitNamee);
         }
         return;
      }
      throw new UnsupportedOperationException("Container " + container + " does not implement ExtendedInjectionContainer, can't resolve persistence unit " + unitName);
   }
           throws NameNotFoundException
   {
      if(container instanceof ExtendedInjectionContainer)
      {
         ExtendedInjectionContainer eic = (ExtendedInjectionContainercontainer;
         String beanName = eic.resolvePersistenceUnitSupplier(unitName);
         return ((org.jboss.jpa.deployment.PersistenceUnitDeployment) PersistenceUnitRegistry.getPersistenceUnit(beanName)).getManagedFactory();
      }
      throw new UnsupportedOperationException("Container " + container + " does not implement ExtendedInjectionContainer, can't resolve persistence unit " + unitName);
   }
   {
      return getEntityManagerFactory(ref.unitName(), container);
   }
   public static Object getFactory(Class<?> typeString unitNameInjectionContainer containerthrows NameNotFoundException
   {
      if (type != null && type.getName().equals(SessionFactory.class.getName()))
         return getSessionFactory(unitNamecontainer);
      return getEntityManagerFactory(unitNamecontainer);
   }
   {
      if(container instanceof ExtendedInjectionContainer)
      {
         ExtendedInjectionContainer eic = (ExtendedInjectionContainercontainer;
         String beanName = eic.resolvePersistenceUnitSupplier(unitName);
         ManagedEntityManagerFactory managedFactory = ((org.jboss.jpa.deployment.PersistenceUnitDeployment) PersistenceUnitRegistry.getPersistenceUnit(beanName)).getManagedFactory();
         return new InjectedEntityManagerFactory(managedFactory);
      }
      throw new UnsupportedOperationException("Container " + container + " does not implement ExtendedInjectionContainer, can't resolve persistence unit " + unitName);
   }
   private static SessionFactory getSessionFactory(String refInjectionContainer containerthrows NameNotFoundException
   {
      if(container instanceof ExtendedInjectionContainer)
      {
         ExtendedInjectionContainer eic = (ExtendedInjectionContainercontainer;
         String beanName = eic.resolvePersistenceUnitSupplier(ref);
         ManagedEntityManagerFactory managedFactory = ((org.jboss.jpa.deployment.PersistenceUnitDeployment) PersistenceUnitRegistry.getPersistenceUnit(beanName)).getManagedFactory();
         return new InjectedSessionFactory(managedFactory);
      }
      throw new UnsupportedOperationException("Container " + container + " does not implement ExtendedInjectionContainer, can't resolve persistence unit " + ref);
   }
   public void handleMethodAnnotations(Method methodInjectionContainer containerMap<AccessibleObjectInjectorinjectors)
   {
      PersistenceUnit ref = method.getAnnotation(PersistenceUnit.class);
      if (ref == nullreturn;
      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(encNamenew PuEncInjector(encNamemethod.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(methodnew JndiMethodInjector(methodencNamecontainer.getEnc()));
   }
   public void handleFieldAnnotations(Field fieldInjectionContainer containerMap<AccessibleObjectInjectorinjectors)
   {
      PersistenceUnit ref = field.getAnnotation(PersistenceUnit.class);
      if (ref == nullreturn;
      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(encNamenew PuEncInjector(encNamefield.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(fieldnew JndiFieldInjector(fieldencNamecontainer.getEnc()));
   }
New to GrepCode? Check out our FAQ X