1 22 package org.jboss.injection; 23 24 import java.lang.reflect.AccessibleObject ; 25 import java.lang.reflect.Field ; 26 import java.lang.reflect.Method ; 27 import java.util.Map ; 28 import javax.naming.NameNotFoundException ; 29 import javax.persistence.EntityManagerFactory; 30 import javax.persistence.PersistenceUnit; 31 import javax.persistence.PersistenceUnits; 32 33 import org.hibernate.SessionFactory; 34 import org.jboss.annotation.IgnoreDependency; 35 import org.jboss.ejb3.entity.InjectedEntityManagerFactory; 36 import org.jboss.ejb3.entity.InjectedSessionFactory; 37 import org.jboss.ejb3.entity.ManagedEntityManagerFactory; 38 import org.jboss.ejb3.entity.PersistenceUnitDeployment; 39 import org.jboss.logging.Logger; 40 import org.jboss.metamodel.descriptor.EnvironmentRefGroup; 41 import org.jboss.metamodel.descriptor.PersistenceUnitRef; 42 43 49 public class PersistenceUnitHandler implements InjectionHandler 50 { 51 private static final Logger log = Logger.getLogger(PersistenceUnitHandler.class); 52 53 public void loadXml(EnvironmentRefGroup xml, InjectionContainer container) 54 { 55 if (xml == null) return; 56 if (xml.getPersistenceUnitRefs() == null) return; 57 58 for (PersistenceUnitRef ref : xml.getPersistenceUnitRefs()) 59 { 60 String encName = "env/" + ref.getRefName(); 61 Class injectionType = InjectionUtil.injectionTarget(encName, ref, container, container.getEncInjections()); 64 if (container.getEncInjectors().containsKey(encName)) 65 return; 66 container.getEncInjectors().put(encName, new PuEncInjector(encName, injectionType, ref.getUnitName(), "<persistence-unit-ref>")); 67 try 68 { 69 addPUDependency(ref.getUnitName(), container); 70 } 71 catch (NameNotFoundException e) 72 { 73 throw new RuntimeException ("Illegal <persistence-unit-ref> of " + ref.getRefName() + " :" + e.getMessage()); 74 } 75 } 76 } 77 78 79 public void handleClassAnnotations(Class clazz, InjectionContainer container) 80 { 81 PersistenceUnits resources = container.getAnnotation( 82 PersistenceUnits.class, clazz); 83 if (resources != null) 84 { 85 for (PersistenceUnit ref : resources.value()) 86 { 87 handleClassAnnotation(ref, container, clazz); 88 } 89 } 90 PersistenceUnit pu = container.getAnnotation(PersistenceUnit.class, clazz); 91 if (pu != null) 92 { 93 handleClassAnnotation(pu, container, clazz); 94 } 95 } 96 97 private static void handleClassAnnotation(PersistenceUnit ref, InjectionContainer container, Class clazz) 98 { 99 String encName = ref.name(); 100 if (encName == null || encName.equals("")) 101 { 102 throw new RuntimeException ("JBoss requires name() for class level @PersistenceUnit"); 103 } 104 encName = "env/" + encName; 105 if (container.getEncInjectors().containsKey(encName)) return; 106 container.getEncInjectors().put(encName, new PuEncInjector(encName, null, ref.unitName(), "@PersistenceUnit")); 107 try 108 { 109 addPUDependency(ref.unitName(), container); 110 } 111 catch (NameNotFoundException e) 112 { 113 throw new RuntimeException ("Illegal @PersistenceUnit on " + clazz.getName() + " of unitname " + ref.unitName() + " :" + e.getMessage()); 114 } 115 } 116 117 public static void addPUDependency(String unitName, InjectionContainer container) throws NameNotFoundException 118 { 119 PersistenceUnitDeployment deployment = null; 120 deployment = container.getPersistenceUnitDeployment(unitName); 122 if (deployment != null) 123 { 124 container.getDependencyPolicy().addDependency(deployment.getKernelName()); 125 log.debug("***** adding PU dependency from located persistence unit: " + deployment.getKernelName()); 126 return; 127 } 128 log.debug("******* could not find PU dependency so adding a default: " + PersistenceUnitDeployment.getDefaultKernelName(unitName)); 131 container.getDependencyPolicy().addDependency(PersistenceUnitDeployment.getDefaultKernelName(unitName)); 132 } 133 134 public static ManagedEntityManagerFactory getManagedEntityManagerFactory(InjectionContainer container, String unitName) 135 throws NameNotFoundException 136 { 137 ManagedEntityManagerFactory factory; 138 PersistenceUnitDeployment deployment = container.getPersistenceUnitDeployment(unitName); 139 if (deployment != null) 140 { 141 factory = deployment.getManagedFactory(); 142 } 143 else 144 { 145 throw new NameNotFoundException ("Unable to find persistence unit: " + unitName + " for deployment: " + container.getIdentifier()); 146 } 147 return factory; 148 } 149 150 151 public static EntityManagerFactory getEntityManagerFactory(PersistenceUnit ref, InjectionContainer container) throws NameNotFoundException 152 { 153 return getEntityManagerFactory(ref.unitName(), container); 154 } 155 156 public static Object getFactory(Class type, String unitName, InjectionContainer container) throws NameNotFoundException 157 { 158 if (type != null && type.getName().equals(SessionFactory.class.getName())) 159 return getSessionFactory(unitName, container); 160 return getEntityManagerFactory(unitName, container); 161 } 162 163 public static EntityManagerFactory getEntityManagerFactory(String unitName, InjectionContainer container) throws NameNotFoundException 164 { 165 ManagedEntityManagerFactory managedFactory; 166 PersistenceUnitDeployment deployment = container.getPersistenceUnitDeployment(unitName); 167 if (deployment != null) 168 { 169 managedFactory = deployment.getManagedFactory(); 170 } 171 else 172 { 173 return null; 174 } 175 return new InjectedEntityManagerFactory(managedFactory); 176 } 177 178 179 private static SessionFactory getSessionFactory(String ref, InjectionContainer container) throws NameNotFoundException 180 { 181 ManagedEntityManagerFactory managedFactory; 182 PersistenceUnitDeployment deployment = container.getPersistenceUnitDeployment(ref); 183 if (deployment != null) 184 { 185 managedFactory = deployment.getManagedFactory(); 186 } 187 else 188 { 189 return null; 190 } 191 return new InjectedSessionFactory(managedFactory); 192 } 193 194 public void handleMethodAnnotations(Method method, InjectionContainer container, Map <AccessibleObject , Injector> injectors) 195 { 196 PersistenceUnit ref = method.getAnnotation(PersistenceUnit.class); 197 if (ref == null) return; 198 if (!method.getName().startsWith("set")) 199 throw new RuntimeException ("@PersistenceUnit can only be used with a set method: " + method); 200 String encName = ref.name(); 201 if (encName == null || encName.equals("")) 202 { 203 encName = InjectionUtil.getEncName(method); 204 } 205 else 206 { 207 encName = "env/" + encName; 208 } 209 if (!container.getEncInjectors().containsKey(encName)) 210 { 211 container.getEncInjectors().put(encName, new PuEncInjector(encName, method.getParameterTypes()[0], ref.unitName(), "@PersistenceUnit")); 212 try 213 { 214 if (!method.isAnnotationPresent(IgnoreDependency.class)) addPUDependency(ref.unitName(), container); 215 } 216 catch (NameNotFoundException e) 217 { 218 throw new RuntimeException ("Illegal @PersistenceUnit on " + method + " :" + e.getMessage()); 219 } 220 } 221 222 injectors.put(method, new JndiMethodInjector(method, encName, container.getEnc())); 223 } 224 225 public void handleFieldAnnotations(Field field, InjectionContainer container, Map <AccessibleObject , Injector> injectors) 226 { 227 PersistenceUnit ref = field.getAnnotation(PersistenceUnit.class); 228 if (ref == null) return; 229 String encName = ref.name(); 230 if (encName == null || encName.equals("")) 231 { 232 encName = InjectionUtil.getEncName(field); 233 } 234 else 235 { 236 encName = "env/" + encName; 237 } 238 if (!container.getEncInjectors().containsKey(encName)) 239 { 240 container.getEncInjectors().put(encName, new PuEncInjector(encName, field.getType(), ref.unitName(), "@PersistenceUnit")); 241 try 242 { 243 if (!field.isAnnotationPresent(IgnoreDependency.class)) addPUDependency(ref.unitName(), container); 244 } 245 catch (NameNotFoundException e) 246 { 247 throw new RuntimeException ("Illegal @PersistenceUnit on " + field + " :" + e.getMessage()); 248 } 249 } 250 251 injectors.put(field, new JndiFieldInjector(field, encName, container.getEnc())); 252 } 253 } 254 | Popular Tags |