1 23 package com.sun.enterprise.util; 24 25 import java.io.Serializable ; 26 import java.util.logging.*; 27 import java.util.Set ; 28 import java.util.Map ; 29 30 import javax.naming.Reference ; 31 32 import javax.persistence.*; 33 34 import com.sun.enterprise.deployment.types.EntityManagerFactoryReference; 35 import com.sun.enterprise.deployment.EjbDescriptor; 36 import com.sun.enterprise.deployment.WebBundleDescriptor; 37 import com.sun.enterprise.deployment.ApplicationClientDescriptor; 38 import com.sun.enterprise.deployment.BundleDescriptor; 39 import com.sun.enterprise.deployment.Application; 40 41 import com.sun.enterprise.InvocationManager; 42 import com.sun.enterprise.Switch; 43 import com.sun.enterprise.ComponentInvocation; 44 45 import com.sun.logging.*; 46 47 55 public class EntityManagerFactoryWrapper implements EntityManagerFactory, 56 Serializable { 57 58 static Logger _logger=LogDomains.getLogger(LogDomains.UTIL_LOGGER); 59 60 static private LocalStringManagerImpl localStrings = 61 new LocalStringManagerImpl(EntityManagerFactoryWrapper.class); 62 63 private String unitName; 64 transient private EntityManagerFactory entityManagerFactory; 65 66 public EntityManagerFactoryWrapper(EntityManagerFactoryReference 67 referenceDescriptor) { 68 69 unitName = referenceDescriptor.getUnitName(); 70 71 } 72 73 private EntityManagerFactory getDelegate() { 74 75 if( entityManagerFactory == null ) { 76 77 entityManagerFactory = lookupEntityManagerFactory(unitName); 78 79 if( entityManagerFactory == null ) { 80 throw new IllegalStateException 81 ("Unable to retrieve EntityManagerFactory for unitName " 82 + unitName); 83 } 84 85 } 86 87 return entityManagerFactory; 88 } 89 90 public EntityManager createEntityManager() { 91 return getDelegate().createEntityManager(); 92 } 93 94 public EntityManager createEntityManager(Map map) { 95 return getDelegate().createEntityManager(map); 96 } 97 98 public void close() { 99 getDelegate().close(); 100 } 101 102 public boolean isOpen() { 103 return getDelegate().isOpen(); 104 } 105 106 107 115 static EntityManagerFactory lookupEntityManagerFactory(String emfUnitName) 116 { 117 118 InvocationManager invMgr = Switch.getSwitch().getInvocationManager(); 119 ComponentInvocation inv = invMgr.getCurrentInvocation(); 120 121 EntityManagerFactory emf = null; 122 123 if( inv != null ) { 124 125 Object descriptor = 126 Switch.getSwitch().getDescriptorFor(inv.getContainerContext()); 127 128 emf = lookupEntityManagerFactory(inv.getInvocationType(), 129 emfUnitName, descriptor); 130 } 131 132 return emf; 133 } 134 135 public static EntityManagerFactory lookupEntityManagerFactory(int invType, 136 String emfUnitName, Object descriptor) { 137 138 Application app = null; 139 BundleDescriptor module = null; 140 141 EntityManagerFactory emf = null; 142 143 switch (invType) { 144 145 case ComponentInvocation.EJB_INVOCATION: 146 147 EjbDescriptor ejbDesc = (EjbDescriptor) descriptor; 148 module = ejbDesc.getEjbBundleDescriptor(); 149 app = module.getApplication(); 150 151 break; 152 153 case ComponentInvocation.SERVLET_INVOCATION: 154 155 module = (WebBundleDescriptor) descriptor; 156 app = module.getApplication(); 157 158 break; 159 160 case ComponentInvocation.APP_CLIENT_INVOCATION: 161 162 module = (ApplicationClientDescriptor) descriptor; 163 app = module.getApplication(); 164 165 break; 166 167 default: 168 169 break; 170 } 171 172 if (module != null) { 174 if (emfUnitName != null) { 175 emf = module.getEntityManagerFactory(emfUnitName); 176 } else { 177 Set <EntityManagerFactory> emFactories = module 178 .getEntityManagerFactories(); 179 if (emFactories.size() == 1) { 180 emf = emFactories.iterator().next(); 181 } 182 } 183 } 184 185 if ((app != null) && (emf == null)) { 188 if (emfUnitName != null) { 189 190 emf = app.getEntityManagerFactory(emfUnitName, module); 191 192 } else { 193 Set <EntityManagerFactory> emFactories = app 194 .getEntityManagerFactories(); 195 if (emFactories.size() == 1) { 196 emf = emFactories.iterator().next(); 197 } 198 } 199 } 200 201 return emf; 202 } 203 204 } 205 | Popular Tags |