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

PcEncInjector

public class PcEncInjector extends Object implements EncInjector
Comment
author
Bill Burke
version
$Revision: 61629 $

Fields Summary
private static final Class[]
SESS_PROXY_INTERFACES
private String
encName
private String
unitName
private PersistenceContextType
type
private Class
injectionType
private String
error
Constructors Summary
public PcEncInjector(String encName, String unitName, PersistenceContextType type, Class injectionType, String error)


             
   
      this.encName = encName;
      this.unitName = unitName;
      this.type = type;
      this.injectionType = injectionType;
      this.error = error;
   
Methods Summary
public voidinject(InjectionContainer container)

      String error1 = error;
      ManagedEntityManagerFactory factory = null;
      try
      {
         factory = PersistenceUnitHandler.getManagedEntityManagerFactory(
                 container, unitName);
      }
      catch (NameNotFoundException e)
      {
         error1 += " " + e.getMessage();
      }
      if (factory == null)
      {
         throw new RuntimeException(error1);
      }
      if (type == PersistenceContextType.EXTENDED)
      {
         if (!(container instanceof StatefulContainer))
            throw new RuntimeException("It is illegal to inject an EXTENDED PC into something other than a SFSB");
         container.getInjectors().add(0, new ExtendedPersistenceContextInjector(factory));
         Object extendedPc;
         if (injectionType == null
                 || injectionType.getName().equals(EntityManager.class.getName()))
         {
            extendedPc = new ExtendedEntityManager(factory.getKernelName());
         }
         else
         {
            ExtendedSessionInvocationHandler handler = new ExtendedSessionInvocationHandler(factory.getKernelName());
            extendedPc = Proxy.newProxyInstance(
                  Session.class.getClassLoader(), //use the Hibernate classloader so the proxy has the same scope as Hibernate
                  SESS_PROXY_INTERFACES,
                  handler
            );
         }
         try
         {
            Util.rebind(container.getEnc(), encName, extendedPc);
         }
         catch (NamingException e)
         {
            throw new RuntimeException(error1, e);
         }
      }
      else
      {
         Object entityManager;
         if (injectionType == null
                 || EntityManager.class.isAssignableFrom(injectionType))
         {
            entityManager = new TransactionScopedEntityManager(factory);
         }
         else
         {
            TransactionScopedSessionInvocationHandler handler = new TransactionScopedSessionInvocationHandler(factory);
            entityManager = Proxy.newProxyInstance(
                  Session.class.getClassLoader(), //use the Hibernate classloader so the proxy has the same scope as Hibernate
                  SESS_PROXY_INTERFACES,
                  handler
            );
         }
         try
         {
            Util.rebind(container.getEnc(), encName, entityManager);
         }
         catch (NamingException e)
         {
            throw new RuntimeException(error1, e);
         }
      }