1 22 package org.jboss.injection; 23 24 import org.jboss.ejb3.entity.ManagedEntityManagerFactory; 25 import org.jboss.ejb3.entity.ExtendedEntityManager; 26 import org.jboss.ejb3.entity.ExtendedHibernateSession; 27 import org.jboss.ejb3.entity.TransactionScopedEntityManager; 28 import org.jboss.ejb3.entity.TransactionScopedHibernateSession; 29 import org.jboss.ejb3.stateful.StatefulContainer; 30 import org.jboss.naming.Util; 31 32 import javax.persistence.PersistenceContextType; 33 import javax.persistence.EntityManager; 34 import javax.naming.NameNotFoundException ; 35 import javax.naming.NamingException ; 36 37 43 public class PcEncInjector implements EncInjector 44 { 45 private String encName; 46 private String unitName; 47 private PersistenceContextType type; 48 private Class injectionType; 49 private String error; 50 51 public PcEncInjector(String encName, String unitName, PersistenceContextType type, Class injectionType, String error) 52 { 53 this.encName = encName; 54 this.unitName = unitName; 55 this.type = type; 56 this.injectionType = injectionType; 57 this.error = error; 58 } 59 60 public void inject(InjectionContainer container) 61 { 62 String error1 = error; 63 ManagedEntityManagerFactory factory = null; 64 try 65 { 66 factory = PersistenceUnitHandler.getManagedEntityManagerFactory( 67 container, unitName); 68 } 69 catch (NameNotFoundException e) 70 { 71 error1 += " " + e.getMessage(); 72 } 73 if (factory == null) 74 { 75 throw new RuntimeException (error1); 76 } 77 if (type == PersistenceContextType.EXTENDED) 78 { 79 if (!(container instanceof StatefulContainer)) 80 throw new RuntimeException ("It is illegal to inject an EXTENDED PC into something other than a SFSB"); 81 container.getInjectors().add(0, new ExtendedPersistenceContextInjector(factory)); 82 Object extendedPc = null; 83 if (injectionType == null 84 || injectionType.getName().equals(EntityManager.class.getName())) 85 { 86 extendedPc = new ExtendedEntityManager(factory.getKernelName()); 87 } 88 else 89 { 90 extendedPc = new ExtendedHibernateSession(factory.getKernelName()); 91 } 92 try 93 { 94 Util.rebind(container.getEnc(), encName, extendedPc); 95 } 96 catch (NamingException e) 97 { 98 throw new RuntimeException (error1, e); 99 } 100 } 101 else 102 { 103 Object entityManager = null; 104 if (injectionType == null 105 || injectionType.getName().equals(EntityManager.class.getName())) 106 { 107 entityManager = new TransactionScopedEntityManager(factory); 108 } 109 else 110 { 111 entityManager = new TransactionScopedHibernateSession(factory); 112 } 113 try 114 { 115 Util.rebind(container.getEnc(), encName, entityManager); 116 } 117 catch (NamingException e) 118 { 119 throw new RuntimeException (error1, e); 120 } 121 } 122 } 123 } 124 | Popular Tags |