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);
}
}