1 19 20 package org.apache.cayenne.jpa; 21 22 import java.util.Collections ; 23 import java.util.Map ; 24 25 import javax.persistence.EntityManager; 26 import javax.persistence.EntityManagerFactory; 27 import javax.persistence.spi.PersistenceUnitInfo; 28 29 import org.apache.cayenne.ObjectContext; 30 import org.apache.cayenne.access.DataContext; 31 import org.apache.cayenne.access.DataDomain; 32 import org.apache.cayenne.access.ObjectStore; 33 import org.apache.cayenne.intercept.DataChannelCallbackInterceptor; 34 import org.apache.cayenne.intercept.ObjectContextCallbackInterceptor; 35 36 41 public class ResourceLocalEntityManagerFactory implements EntityManagerFactory { 42 43 protected DataDomain domain; 44 protected boolean open; 45 protected PersistenceUnitInfo unitInfo; 46 protected Provider provider; 47 48 51 ResourceLocalEntityManagerFactory(PersistenceUnitInfo unitInfo) { 52 this(null, null, unitInfo); 53 } 54 55 58 public ResourceLocalEntityManagerFactory(Provider provider, DataDomain domain, 59 PersistenceUnitInfo unitInfo) { 60 this.unitInfo = unitInfo; 61 this.open = true; 62 this.domain = domain; 63 this.provider = provider; 64 } 65 66 69 protected PersistenceUnitInfo getPersistenceUnitInfo() { 70 return unitInfo; 71 } 72 73 77 public boolean isOpen() { 78 return open; 79 } 80 81 86 public void close() { 87 checkClosed(); 88 this.open = false; 89 } 90 91 98 public EntityManager createEntityManager() { 99 return createEntityManager(Collections.EMPTY_MAP); 100 } 101 102 111 public EntityManager createEntityManager(Map map) { 112 checkClosed(); 113 CayenneEntityManager em = new ResourceLocalEntityManager(createObjectContext(), this); 114 return new TypeCheckingEntityManager(em); 115 } 116 117 122 protected ObjectContext createObjectContext() { 123 DataChannelCallbackInterceptor postInterceptor = new DataChannelCallbackInterceptor(); 124 postInterceptor.setChannel(domain); 125 ObjectStore objectStore = new ObjectStore(domain.getSharedSnapshotCache()); 126 127 ObjectContextCallbackInterceptor preInterceptor = new ObjectContextCallbackInterceptor(); 128 preInterceptor.setContext(new DataContext(postInterceptor, objectStore)); 129 130 return preInterceptor; 131 } 132 133 136 void checkClosed() throws IllegalStateException { 137 if (!isOpen()) { 138 throw new IllegalStateException ( 139 "An attempt to access closed EntityManagerFactory."); 140 } 141 } 142 143 146 public Provider getProvider() { 147 return provider; 148 } 149 150 153 PersistenceUnitInfo getUnitInfo() { 154 return unitInfo; 155 } 156 } 157 | Popular Tags |