1 22 package org.jboss.aop.microcontainer.integration; 23 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.HashSet ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.Set ; 31 32 import org.jboss.aop.Advisor; 33 import org.jboss.aop.AspectManager; 34 import org.jboss.aop.ReflectiveAspectBinder; 35 import org.jboss.aop.advice.AspectDefinition; 36 import org.jboss.aop.microcontainer.beans.ManagedAspectDefinition; 37 import org.jboss.aop.proxy.container.ContainerCache; 38 import org.jboss.aop.util.Advisable; 39 import org.jboss.aop.util.ClassInfoMethodHashing; 40 import org.jboss.classadapter.plugins.dependency.AbstractDependencyBuilder; 41 import org.jboss.classadapter.spi.ClassAdapter; 42 import org.jboss.classadapter.spi.Dependency; 43 import org.jboss.reflect.plugins.AnnotationValueFactory; 44 import org.jboss.reflect.plugins.introspection.IntrospectionTypeInfoFactoryImpl; 45 import org.jboss.reflect.spi.AnnotationInfo; 46 import org.jboss.reflect.spi.AnnotationValue; 47 import org.jboss.reflect.spi.ArrayInfo; 48 import org.jboss.reflect.spi.ArrayValue; 49 import org.jboss.reflect.spi.ClassInfo; 50 import org.jboss.reflect.spi.MethodInfo; 51 import org.jboss.reflect.spi.StringValue; 52 import org.jboss.reflect.spi.TypeInfo; 53 import org.jboss.reflect.spi.Value; 54 import org.jboss.repository.spi.MetaDataContext; 55 56 63 public class AOPDependencyBuilderDelegate extends AbstractDependencyBuilder 64 { 65 private static final String DEPENDENCY_CLASS_NAME = Dependency.class.getName(); 66 private static final String DEPENDENCY_NAME_ATTRIBUTE = "name"; 67 private static final IntrospectionTypeInfoFactoryImpl typeInfoFactory = new IntrospectionTypeInfoFactoryImpl(); 68 69 public List getDependencies(ClassAdapter classAdapter) 70 { 71 AspectManager manager = AspectManager.instance(); 72 try 73 { 74 ClassInfo classInfo = classAdapter.getClassInfo(); 75 String className = classInfo.getName(); 76 if (className != null) 77 { 78 if (manager.isNonAdvisableClassName(className)) 79 { 80 return super.getDependencies(classAdapter); 81 } 82 83 MetaDataContext metaDataContext = classAdapter.getMetaDataContext(); 84 85 ClassLoader loader = classAdapter.getClassLoader(); 86 if (loader == null) 87 { 88 loader = Thread.currentThread().getContextClassLoader(); 89 } 90 Class clazz = loader.loadClass(className); 91 92 Advisor advisor; 93 synchronized (ContainerCache.mapLock) 94 { 95 ContainerCache cache = ContainerCache.initialise(manager, clazz, metaDataContext); 96 advisor = cache.getAdvisor(); 97 } 98 99 ReflectiveAspectBinder binder = new ReflectiveAspectBinder(clazz, advisor); 100 Set aspects = binder.getAspects(); 101 102 ArrayList <Object > depends = new ArrayList <Object >(); 103 if (aspects != null && aspects.size() > 0) 104 { 105 Iterator it = aspects.iterator(); 106 while (it.hasNext()) 107 { 108 AspectDefinition def = (AspectDefinition) it.next(); 109 if (def instanceof ManagedAspectDefinition) 110 { 111 depends.add(def.getName()); 112 } 113 } 114 } 115 116 HashSet <Object > annotationDependencies = getAnnotationDependencies(classInfo, metaDataContext); 117 depends.addAll(annotationDependencies); 118 119 return depends; 120 } 121 return null; 122 123 } 124 catch (ClassNotFoundException e) 125 { 126 throw new RuntimeException (e); 127 } 128 } 129 130 private HashSet <Object > getAnnotationDependencies(ClassInfo classInfo, MetaDataContext metaDataContext) 131 { 132 try 133 { 134 HashSet <Object > dependencies = new HashSet <Object >(); 135 getClassAnnotationDependencies(classInfo, metaDataContext, dependencies); 136 getMethodAnnotationDependencies(classInfo, metaDataContext, dependencies); 137 return dependencies; 138 } 139 catch (RuntimeException e) 140 { 141 throw e; 142 } 143 catch (Exception e) 144 { 145 throw new RuntimeException (e); 146 } 147 } 148 149 private void getClassAnnotationDependencies(ClassInfo classInfo, MetaDataContext metaDataContext, HashSet <Object > dependencies) throws Exception 150 { 151 HashMap <String , ArrayList <String >> realMap = new HashMap <String , ArrayList <String >>(); 152 getRealClassAnnotationDependencies(classInfo, realMap); 153 HashMap <String , ArrayList <String >> metaMap = new HashMap <String , ArrayList <String >>(); 154 getMetaDataContextClassAnnotationDependencies(metaDataContext, metaMap); 155 addAllDependenciesToSet(dependencies, realMap, metaMap); 156 } 157 158 private void getRealClassAnnotationDependencies(ClassInfo classInfo, HashMap <String , ArrayList <String >> dependencies) throws Exception 159 { 160 AnnotationValue[] annotations = classInfo.getAnnotations(); 161 162 for (int i = 0 ; i < annotations.length ; i++) 163 { 164 getDependenciesForAnnotation(annotations[i].getType().getName(), annotations[i], dependencies); 165 } 166 } 167 168 private void getMetaDataContextClassAnnotationDependencies(MetaDataContext metaDataContext, HashMap <String , ArrayList <String >> dependencies) throws Exception 169 { 170 if (metaDataContext != null) 171 { 172 for (Iterator it = metaDataContext.getAnnotations().iterator() ; it.hasNext() ; ) 173 { 174 Object annotation = it.next(); 175 getDependenciesForMetaDataAnnotation(annotation, dependencies); 176 } 177 } 178 } 179 180 private void getMethodAnnotationDependencies(ClassInfo classInfo, MetaDataContext metaDataContext, HashSet <Object > dependencies) throws Exception 181 { 182 Map methodMap = ClassInfoMethodHashing.getMethodMap(classInfo); 183 if (methodMap != null) 184 { 185 for (Iterator it = methodMap.values().iterator() ; it.hasNext() ; ) 186 { 187 MethodInfo method = (MethodInfo)it.next(); 188 if (Advisable.isAdvisableMethod(method.getModifiers(), method.getName())) 189 { 190 HashMap <String , ArrayList <String >> classMap = new HashMap <String , ArrayList <String >>(); 191 getRealMethodAnnotationDependencies(method, classMap); 192 HashMap <String , ArrayList <String >> overrideMap = new HashMap <String , ArrayList <String >>(); 193 getMetaDataContextMethodAnnotationDependencies(method, metaDataContext, overrideMap); 194 addAllDependenciesToSet(dependencies, classMap, overrideMap); 195 } 196 } 197 } 198 } 199 200 private void getRealMethodAnnotationDependencies(MethodInfo methodInfo, HashMap <String , ArrayList <String >> dependencies) throws Exception 201 { 202 AnnotationValue[] annotations = methodInfo.getAnnotations(); 203 if (annotations != null) 204 { 205 for (int i = 0 ; i < annotations.length ; i++) 206 { 207 getDependenciesForAnnotation(annotations[i].getType().getName(), annotations[i], dependencies); 208 } 209 } 210 } 211 212 private void getMetaDataContextMethodAnnotationDependencies(MethodInfo method, MetaDataContext metaDataContext, HashMap <String , ArrayList <String >> dependencies) throws Exception 213 { 214 if (metaDataContext != null) 215 { 216 long hash = ClassInfoMethodHashing.methodHash(method); 217 List methodAnnotations = metaDataContext.getAnnotationsForMethod(hash); 218 for (Iterator it = methodAnnotations.iterator() ; it.hasNext() ; ) 219 { 220 Object annotation = it.next(); 221 getDependenciesForMetaDataAnnotation(annotation, dependencies); 222 } 223 } 224 } 225 226 private void getDependenciesForMetaDataAnnotation(Object annotation, HashMap <String , ArrayList <String >> dependencies) throws Exception 227 { 228 AnnotationInfo info; 229 Class clazz = annotation.getClass().getInterfaces()[0]; 230 try 231 { 232 info = (AnnotationInfo)typeInfoFactory.getTypeInfo(clazz); 233 } 234 catch (RuntimeException e) 235 { 236 throw new RuntimeException ("Error creating annotation for " + clazz.getName(), e); 238 } 239 AnnotationValue value = AnnotationValueFactory.createAnnotationValue(typeInfoFactory, typeInfoFactory, info, annotation); 240 getDependenciesForAnnotation(info.getName(), value, dependencies); 241 } 242 243 private void getDependenciesForAnnotation(String topLevelAnnotationName, AnnotationValue annotation, HashMap <String , ArrayList <String >> dependencies) 244 { 245 if (annotation != null) 246 { 247 addAnnotationAttributeDependencies(topLevelAnnotationName, annotation, dependencies); 248 249 AnnotationValue[] annotationAnnotations = annotation.getAnnotationType().getAnnotations(); 250 for (int i = 0 ; i < annotationAnnotations.length ; i++) 251 { 252 if (annotationAnnotations[i].getAnnotationType().getName().equals(DEPENDENCY_CLASS_NAME)) 253 { 254 StringValue value = (StringValue)annotationAnnotations[i].getValue(DEPENDENCY_NAME_ATTRIBUTE); 255 StringValue dependency = (StringValue)annotation.getValue(value.getValue()); 256 addDependency(topLevelAnnotationName, dependency, dependencies); 257 } 258 } 259 } 260 } 261 262 private void addAnnotationAttributeDependencies(String topLevelAnnotationName, AnnotationValue annotation, HashMap <String , ArrayList <String >> dependencies) 263 { 264 MethodInfo[] attributes = annotation.getAnnotationType().getDeclaredMethods(); 265 if (attributes != null) 266 { 267 for (int i = 0 ; i < attributes.length ; i++) 268 { 269 Value value = annotation.getValue(attributes[i].getName()); 270 271 if (value instanceof AnnotationValue) 272 { 273 getDependenciesForAnnotation(topLevelAnnotationName, (AnnotationValue)value, dependencies); 274 } 275 else if (value instanceof ArrayValue) 276 { 277 ArrayValue arrVal = (ArrayValue)value; 278 TypeInfo type = ((ArrayInfo)arrVal.getType()).getComponentType(); 279 if (type instanceof AnnotationInfo) 280 { 281 Value[] values = arrVal.getValues(); 282 for (int j = 0 ; j < values.length ; j++) 283 { 284 getDependenciesForAnnotation(topLevelAnnotationName, (AnnotationValue)values[j], dependencies); 285 } 286 } 287 } 288 } 289 } 290 } 291 292 private void addDependency(String topLevelAnnotationName, StringValue dependency, HashMap <String , ArrayList <String >> dependencies) 293 { 294 ArrayList <String > list = dependencies.get(topLevelAnnotationName); 295 if (list == null) 296 { 297 list = new ArrayList <String >(); 298 dependencies.put(topLevelAnnotationName, list); 299 } 300 301 list.add(dependency.getValue()); 302 } 303 304 private void addAllDependenciesToSet(HashSet <Object > dependencies, HashMap <String , ArrayList <String >> classMap, HashMap <String , ArrayList <String >> overrideMap) 305 { 306 HashMap <String , ArrayList <String >> dependencyMap = mergeClassAndOverrideMaps(classMap, overrideMap); 307 if (dependencyMap.size() > 0) 308 { 309 for (ArrayList <String > deps : dependencyMap.values()) 310 { 311 dependencies.addAll(deps); 312 } 313 } 314 } 315 316 private HashMap <String , ArrayList <String >> mergeClassAndOverrideMaps(HashMap <String , ArrayList <String >> classMap, HashMap <String , ArrayList <String >> overrideMap) 317 { 318 if (classMap.size() == 0 && overrideMap.size() == 0) 319 { 320 return classMap; 321 } 322 if (classMap.size() > 0 && overrideMap.size() == 0) 323 { 324 return classMap; 325 } 326 if (classMap.size() == 0 && overrideMap.size() > 0) 327 { 328 return overrideMap; 329 } 330 331 for (String key : overrideMap.keySet()) 332 { 333 classMap.put(key, overrideMap.get(key)); 334 } 335 return classMap; 336 } 337 } 338 | Popular Tags |