1 7 package org.jboss.ejb3.injection; 8 9 import javax.persistence.EntityManager; 10 import javax.persistence.PersistenceContextType; 11 import org.jboss.ejb3.BeanContext; 12 import org.jboss.ejb3.Container; 13 import org.jboss.ejb3.entity.ExtendedPersistenceContext; 14 import org.jboss.ejb3.entity.InjectedEntityManager; 15 import org.jboss.ejb3.entity.ManagedEntityManagerFactory; 16 import org.jboss.ejb3.stateful.StatefulContainer; 17 import org.jboss.logging.Logger; 18 19 25 public abstract class EntityManagerInjector implements Injector 26 { 27 protected static final Logger log = Logger.getLogger(EntityManagerFieldInjector.class); 28 protected Container container; 29 protected ManagedEntityManagerFactory factory; 30 protected PersistenceContextType type; 31 32 33 protected EntityManagerInjector(Container container, ManagedEntityManagerFactory factory, PersistenceContextType type) 34 { 35 this.container = container; 36 this.factory = factory; 37 this.type = type; 38 } 39 40 protected EntityManager getEntityManager(BeanContext ctx, boolean isTransient) 41 { 42 EntityManager entityManager; 43 if (type == PersistenceContextType.EXTENDED) 44 { 45 if (!(container instanceof StatefulContainer)) throw new IllegalStateException ("EXTENDED persistence contexts can only be used within a Stateful Session bean"); 46 entityManager = (EntityManager)ctx.getMetaData().getMetaData(ManagedEntityManagerFactory.LONG_LIVED_SESSION, factory.getJndiName()); 47 if (entityManager == null) 48 { 49 entityManager = new ExtendedPersistenceContext(isTransient, factory); 50 log.info("***Injecting an extended EntityManager: " + factory.getJndiName()); 51 ctx.getMetaData().addMetaData(ManagedEntityManagerFactory.LONG_LIVED_SESSION, factory.getJndiName(), entityManager); 52 } 53 } 54 else 55 { 56 entityManager = new InjectedEntityManager(factory); 57 } 58 return entityManager; 59 } 60 } 61 | Popular Tags |