1 22 package org.jboss.injection; 23 24 import org.jboss.ejb3.EJBContainer; 25 import org.jboss.logging.Logger; 26 import org.jboss.metamodel.descriptor.InjectionTarget; 27 import org.jboss.metamodel.descriptor.Ref; 28 29 import java.lang.reflect.AccessibleObject ; 30 import java.lang.reflect.Field ; 31 import java.lang.reflect.Method ; 32 import java.lang.reflect.Modifier ; 33 import java.util.Collection ; 34 import java.util.HashMap ; 35 import java.util.HashSet ; 36 import java.util.Map ; 37 import java.util.Set ; 38 39 45 public class InjectionUtil 46 { 47 private static final Logger log = Logger 48 .getLogger(InjectionUtil.class); 49 50 51 60 public static void collapseXmlMethodInjectors(Set <String > visitedMethods, Class clazz, Map <String , Map <AccessibleObject , Injector>> xmlDefinedInjectors, Map <AccessibleObject , Injector> classInjectors) 61 { 62 if (clazz == null || clazz.equals(Object .class)) 63 { 64 return; 65 } 66 Map <AccessibleObject , Injector> xmlInjectors = xmlDefinedInjectors.get(clazz.getName()); 67 if (xmlInjectors != null) 68 { 69 Method [] methods = clazz.getDeclaredMethods(); 70 for (Method method : methods) 71 { 72 if (method.getParameterTypes().length != 1) continue; 73 74 if (!Modifier.isPrivate(method.getModifiers())) 75 { 76 if (visitedMethods.contains(method.getName())) 77 { 78 xmlInjectors.remove(method); continue; 80 } 81 visitedMethods.add(method.getName()); 82 } 83 } 84 classInjectors.putAll(xmlInjectors); 85 } 86 collapseXmlMethodInjectors(visitedMethods, clazz.getSuperclass(), xmlDefinedInjectors, classInjectors); 88 } 89 90 public static void processMethodAnnotations(InjectionContainer container, Collection <InjectionHandler> handlers, Set <String > visitedMethods, Class clazz, Map <AccessibleObject , Injector> classInjectors) 91 { 92 if (clazz == null || clazz.equals(Object .class)) 93 { 94 return; 95 } 96 Method [] methods = clazz.getDeclaredMethods(); 97 for (Method method : methods) 98 { 99 if (method.getParameterTypes().length != 1) continue; 100 101 if (!Modifier.isPrivate(method.getModifiers())) 102 { 103 if (visitedMethods.contains(method.getName())) 104 { 105 continue; 106 } 107 visitedMethods.add(method.getName()); 108 } 109 110 if (handlers != null) 111 { 112 for (InjectionHandler handler : handlers) 113 { 114 handler.handleMethodAnnotations(method, container, classInjectors); 115 } 116 } 117 } 118 processMethodAnnotations(container, handlers, visitedMethods, clazz.getSuperclass(), classInjectors); 120 } 121 122 public static void processFieldAnnotations(InjectionContainer container, Collection <InjectionHandler> handlers, Class clazz, Map <AccessibleObject , Injector> classInjectors) 123 { 124 if (clazz == null || clazz.equals(Object .class)) 125 { 126 return; 127 } 128 129 if (handlers != null) 130 { 131 Field [] fields = clazz.getDeclaredFields(); 132 for (Field field : fields) 133 { 134 log.trace("process field annotation for " + field.toGenericString()); 135 for (InjectionHandler handler : handlers) 136 { 137 handler.handleFieldAnnotations(field, container, classInjectors); 138 } 139 } 140 } 141 142 processFieldAnnotations(container, handlers, clazz.getSuperclass(), classInjectors); 144 } 145 146 public static void processClassAnnotations(InjectionContainer container, Collection <InjectionHandler> handlers, Class clazz) 147 { 148 if (clazz == null || clazz.equals(Object .class)) 149 { 150 return; 151 } 152 153 if (handlers != null) 154 { 155 for (InjectionHandler handler : handlers) 156 { 157 handler.handleClassAnnotations(clazz, container); 158 } 159 } 160 161 processClassAnnotations(container, handlers, clazz.getSuperclass()); 163 } 164 165 public static Map <AccessibleObject , Injector> processAnnotations(InjectionContainer container, Collection <InjectionHandler> handlers, Class clazz) 166 { 167 Map <AccessibleObject , Injector> classInjectors = new HashMap <AccessibleObject , Injector>(); 168 HashSet <String > visitedMethods = new HashSet <String >(); 169 collapseXmlMethodInjectors(visitedMethods, clazz, container.getEncInjections(), classInjectors); 170 171 processClassAnnotations(container, handlers, clazz); 172 visitedMethods = new HashSet <String >(); 173 processMethodAnnotations(container, handlers, visitedMethods, clazz, classInjectors); 174 processFieldAnnotations(container, handlers, clazz, classInjectors); 175 return classInjectors; 176 } 177 178 public static AccessibleObject findInjectionTarget(ClassLoader loader, InjectionTarget target) 179 { 180 Class clazz = null; 181 try 182 { 183 clazz = loader.loadClass(target.getTargetClass()); 184 } 185 catch (ClassNotFoundException e) 186 { 187 throw new RuntimeException ("<injection-target> class: " + target.getTargetClass() + " was not found nin deployment"); 188 } 189 190 for (Field field : clazz.getDeclaredFields()) 191 { 192 if (target.getTargetName().equals(field.getName())) return field; 193 } 194 195 for (java.lang.reflect.Method method : clazz.getDeclaredMethods()) 196 { 197 if (method.getName().equals(target.getTargetName())) return method; 198 } 199 200 throw new RuntimeException ("<injection-target> could not be found: " + target.getTargetClass() + "." + target.getTargetName()); 201 202 } 203 204 public static String getEncName(Class type) 205 { 206 return "env/" + type.getName(); 207 } 208 209 public static String getEncName(Method method) 210 { 211 String encName = method.getName().substring(3); 212 if (encName.length() > 1) 213 { 214 encName = encName.substring(0, 1).toLowerCase() + encName.substring(1); 215 } 216 else 217 { 218 encName = encName.toLowerCase(); 219 } 220 221 encName = getEncName(method.getDeclaringClass()) + "/" + encName; 222 return encName; 223 } 224 225 public static String getEncName(Field field) 226 { 227 return getEncName(field.getDeclaringClass()) + "/" + field.getName(); 228 } 229 230 public static Object getAnnotation(Class annotation, EJBContainer container, Class annotatedClass, boolean isContainer) 231 { 232 if (isContainer) 233 { 234 return container.resolveAnnotation(annotation); 235 } 236 else 237 { 238 return annotatedClass.getAnnotation(annotation); 239 } 240 } 241 242 public static Object getAnnotation(Class annotation, EJBContainer container, Method method, boolean isContainer) 243 { 244 if (isContainer) 245 { 246 return container.resolveAnnotation(method, annotation); 247 } 248 else 249 { 250 return method.getAnnotation(annotation); 251 } 252 } 253 254 public static Object getAnnotation(Class annotation, EJBContainer container, Field field, boolean isContainer) 255 { 256 if (isContainer) 257 { 258 return container.resolveAnnotation(field, annotation); 259 } 260 else 261 { 262 return field.getAnnotation(annotation); 263 } 264 } 265 266 public static Class injectionTarget(String encName, Ref ref, InjectionContainer container, Map <String , Map <AccessibleObject , Injector>> classInjectors) 267 { 268 if (ref.getInjectionTarget() != null) 269 { 270 Class injectionType; 271 AccessibleObject ao = findInjectionTarget(container.getClassloader(), ref.getInjectionTarget()); 273 Map <AccessibleObject , Injector> injectors = classInjectors.get(ref.getInjectionTarget().getTargetClass()); 274 if (injectors == null) 275 { 276 injectors = new HashMap <AccessibleObject , Injector>(); 277 classInjectors.put(ref.getInjectionTarget().getTargetClass().trim(), injectors); 278 } 279 if (ao instanceof Field ) 280 { 281 injectionType = ((Field ) ao).getType(); 282 injectors.put(ao, new JndiFieldInjector((Field ) ao, encName, container.getEnc())); 283 } 284 else 285 { 286 injectionType = ((Method ) ao).getParameterTypes()[0]; 287 injectors.put(ao, new JndiMethodInjector((Method ) ao, encName, container.getEnc())); 288 } 289 return injectionType; 290 } 291 else 292 { 293 return null; 294 } 295 296 } 297 } 298 | Popular Tags |