1 22 package org.jboss.injection; 23 24 import org.jboss.metamodel.descriptor.PersistenceContextRef; 25 import org.jboss.logging.Logger; 26 import org.jboss.annotation.IgnoreDependency; 27 import org.jboss.metamodel.descriptor.EnvironmentRefGroup; 28 29 import javax.naming.NameNotFoundException ; 30 import javax.persistence.PersistenceContext; 31 import javax.persistence.PersistenceContextType; 32 import javax.persistence.PersistenceContexts; 33 import java.lang.reflect.AccessibleObject ; 34 import java.lang.reflect.Field ; 35 import java.lang.reflect.Method ; 36 import java.util.Map ; 37 38 45 public class PersistenceContextHandler implements InjectionHandler 46 { 47 private static final Logger log = Logger 48 .getLogger(PersistenceContextHandler.class); 49 50 public void loadXml(EnvironmentRefGroup xml, InjectionContainer container) 51 { 52 if (xml == null) return; 53 if (xml.getPersistenceContextRefs() == null) return; 54 for (PersistenceContextRef ref : xml.getPersistenceContextRefs()) 55 { 56 String encName = "env/" + ref.getRefName(); 57 Class injectionType = InjectionUtil.injectionTarget(encName, ref, container, container.getEncInjections()); 60 61 if (container.getEncInjectors().containsKey(encName)) 62 continue; 63 String error = "unable to load <persistence-context-ref> for unitName: " 65 + ref.getUnitName() + " <ref-name>: " + ref.getRefName(); 66 PersistenceContextType type = ref.getPersistenceContextType(); 67 String unitName = ref.getUnitName(); 68 container.getEncInjectors().put(encName, new PcEncInjector(encName, unitName, type, injectionType, error)); 69 try 70 { 71 PersistenceUnitHandler.addPUDependency(ref.getUnitName(), container); 72 } 73 catch (NameNotFoundException e) 74 { 75 throw new RuntimeException ("Illegal <persistence-context-ref> of " + ref.getRefName() + " :" + e.getMessage()); 76 } 77 } 78 } 79 80 public void handleClassAnnotations(Class clazz, InjectionContainer container) 81 { 82 PersistenceContexts resources = container.getAnnotation(PersistenceContexts.class, clazz); 83 if (resources != null) 84 { 85 for (PersistenceContext ref : resources.value()) 86 { 87 loadPersistenceContextClassAnnotation(ref, container, clazz); 88 } 89 } 90 PersistenceContext pc = container.getAnnotation(PersistenceContext.class, clazz); 91 92 if (pc != null) 93 { 94 loadPersistenceContextClassAnnotation(pc, container, clazz); 95 } 96 97 } 98 99 private static void loadPersistenceContextClassAnnotation( 100 PersistenceContext ref, InjectionContainer container, Class clazz) 101 { 102 String encName = ref.name(); 103 if (encName == null || encName.equals("")) 104 { 105 throw new RuntimeException ( 106 "JBoss requires name() for class level @PersistenceContext"); 107 } 108 encName = "env/" + ref.name(); 109 if (container.getEncInjectors().containsKey(encName)) return; 110 111 String error = "Unable to load class-level @PersistenceContext(" 112 + ref.unitName() + ") on " + container.getIdentifier(); 113 container.getEncInjectors().put(encName, new PcEncInjector(encName, ref.unitName(), ref.type(), null, error)); 114 try 115 { 116 PersistenceUnitHandler.addPUDependency(ref.unitName(), container); 117 } 118 catch (NameNotFoundException e) 119 { 120 throw new RuntimeException ("Illegal @PersistenceUnit on " + clazz.getName() + " of unitname " + ref.unitName() + " :" + e.getMessage()); 121 } 122 } 123 124 public void handleMethodAnnotations(Method method, InjectionContainer container, Map <AccessibleObject , Injector> injectors) 125 { 126 PersistenceContext ref = method.getAnnotation(PersistenceContext.class); 127 if (ref == null) return; 128 if (!method.getName().startsWith("set")) 129 throw new RuntimeException ("@PersistenceUnit can only be used with a set method: " + method); 130 131 String encName = ref.name(); 132 if (encName == null || encName.equals("")) 133 { 134 encName = InjectionUtil.getEncName(method); 135 } 136 else 137 { 138 encName = "env/" + ref.name(); 139 } 140 if (!container.getEncInjectors().containsKey(encName)) 141 { 142 try 143 { 144 if (!method.isAnnotationPresent(IgnoreDependency.class)) PersistenceUnitHandler.addPUDependency(ref.unitName(), container); 145 } 146 catch (NameNotFoundException e) 147 { 148 throw new RuntimeException ("Illegal @PersistenceUnit on " + method + " :" + e.getMessage()); 149 } 150 String error = "@PersistenceContext(name='" + encName 151 + "',unitName='" + ref.unitName() + "') on EJB: " 152 + container.getIdentifier() + " failed to inject on method " 153 + method.toString(); 154 container.getEncInjectors().put(encName, new PcEncInjector(encName, ref.unitName(), ref.type(), method.getParameterTypes()[0], error)); 155 } 156 injectors.put(method, new JndiMethodInjector(method, 157 encName, container.getEnc())); 158 } 159 160 public void handleFieldAnnotations(Field field, InjectionContainer container, Map <AccessibleObject , Injector> injectors) 161 { 162 PersistenceContext ref = field.getAnnotation(PersistenceContext.class); 163 if (ref == null) return; 164 165 String encName = ref.name(); 166 if (encName == null || encName.equals("")) 167 { 168 encName = InjectionUtil.getEncName(field); 169 } 170 else 171 { 172 encName = "env/" + ref.name(); 173 } 174 if (!container.getEncInjectors().containsKey(encName)) 175 { 176 try 177 { 178 if (!field.isAnnotationPresent(IgnoreDependency.class)) PersistenceUnitHandler.addPUDependency(ref.unitName(), container); 179 } 180 catch (NameNotFoundException e) 181 { 182 throw new RuntimeException ("Illegal @PersistenceUnit on " + field + " :" + e.getMessage()); 183 } 184 String error = "@PersistenceContext(name='" + encName 185 + "',unitName='" + ref.unitName() + "') on EJB: " 186 + container.getIdentifier() + " failed to inject on field " 187 + field.toString(); 188 container.getEncInjectors().put(encName, new PcEncInjector(encName, ref.unitName(), ref.type(), field.getType(), error)); 189 } 190 injectors.put(field, new JndiFieldInjector(field, 191 encName, container.getEnc())); 192 } 193 } 194 | Popular Tags |