1 22 package org.jboss.ejb3.entity; 23 24 import java.io.Externalizable ; 25 import java.io.IOException ; 26 import java.io.ObjectInput ; 27 import java.io.ObjectOutput ; 28 import java.util.Map ; 29 import javax.persistence.EntityManager; 30 import javax.persistence.EntityManagerFactory; 31 import javax.persistence.PersistenceContextType; 32 import org.jboss.ejb3.PersistenceUnitRegistry; 33 34 40 public class InjectedEntityManagerFactory implements EntityManagerFactory, Externalizable 41 { 42 private transient EntityManagerFactory delegate; 43 private transient ManagedEntityManagerFactory managedFactory; 44 private String kernelName; 45 46 public InjectedEntityManagerFactory() {} 47 48 public InjectedEntityManagerFactory(ManagedEntityManagerFactory managedFactory) 49 { 50 this.delegate = managedFactory.getEntityManagerFactory(); 51 this.managedFactory = managedFactory; 52 } 53 54 55 public EntityManagerFactory getDelegate() 56 { 57 return delegate; 58 } 59 60 public void writeExternal(ObjectOutput out) throws IOException 61 { 62 out.writeUTF(managedFactory.getKernelName()); 63 } 64 65 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException 66 { 67 String kernelName = in.readUTF(); 68 PersistenceUnitDeployment deployment = PersistenceUnitRegistry.getPersistenceUnit(kernelName); 69 if (deployment == null) throw new IOException ("Unable to find persistence unit in registry: " + kernelName); 70 managedFactory = deployment.getManagedFactory(); 71 delegate = managedFactory.getEntityManagerFactory(); 72 } 73 74 public EntityManager createEntityManager() 75 { 76 return getDelegate().createEntityManager(); 77 } 78 79 public EntityManager createEntityManager(Map map) 80 { 81 return delegate.createEntityManager(map); 82 } 83 84 85 public EntityManager getEntityManager() 86 { 87 return new TransactionScopedEntityManager(managedFactory); 88 } 89 90 public void close() 91 { 92 throw new IllegalStateException ("It is illegal to close an injected EntityManagerFactory"); 93 } 94 95 public boolean isOpen() 96 { 97 return getDelegate().isOpen(); 98 } 99 } 100 | Popular Tags |