1 25 26 package org.objectweb.easybeans.deployment.annotations.helper.bean; 27 28 import java.util.ArrayList ; 29 import java.util.HashMap ; 30 import java.util.LinkedList ; 31 import java.util.List ; 32 import java.util.Map ; 33 34 import javax.ejb.Remove ; 35 36 import org.objectweb.asm.Type; 37 import org.objectweb.easybeans.container.session.stateful.interceptors.RemoveAlwaysInterceptor; 38 import org.objectweb.easybeans.container.session.stateful.interceptors.RemoveOnlyWithoutExceptionInterceptor; 39 import org.objectweb.easybeans.deployment.annotations.InterceptorType; 40 import org.objectweb.easybeans.deployment.annotations.JClassInterceptor; 41 import org.objectweb.easybeans.deployment.annotations.JMethod; 42 import org.objectweb.easybeans.deployment.annotations.exceptions.ResolverException; 43 import org.objectweb.easybeans.deployment.annotations.impl.JInterceptors; 44 import org.objectweb.easybeans.deployment.annotations.metadata.ClassAnnotationMetadata; 45 import org.objectweb.easybeans.deployment.annotations.metadata.EjbJarAnnotationMetadata; 46 import org.objectweb.easybeans.deployment.annotations.metadata.MethodAnnotationMetadata; 47 import org.objectweb.easybeans.naming.interceptors.ENCManager; 48 import static org.objectweb.easybeans.deployment.annotations.helper.bean.InheritanceInterfacesHelper.JAVA_LANG_OBJECT; 49 53 public final class InterceptorsClassResolver { 54 55 58 private static final JMethod EASYBEANS_INTERCEPTOR = new JMethod(0, "intercept", 59 "(Lorg/objectweb/easybeans/api/EasyBeansInvocationContext;)Ljava/lang/Object;", 60 null, new String [] {"java/lang/Exception"}); 61 62 63 66 private InterceptorsClassResolver() { 67 68 } 69 70 71 76 public static void resolve(final ClassAnnotationMetadata classAnnotationMetadata) throws ResolverException { 77 78 List <String > easyBeansInterceptorsClasses = new ArrayList <String >(); 80 easyBeansInterceptorsClasses.add(Type.getInternalName(ENCManager.getInterceptorClass())); 82 List <JClassInterceptor> easyBeansGlobalInterceptors = new ArrayList <JClassInterceptor>(); 84 for (String easyBeansInterceptor : easyBeansInterceptorsClasses) { 85 easyBeansGlobalInterceptors.add(new JClassInterceptor(easyBeansInterceptor, EASYBEANS_INTERCEPTOR)); 86 } 87 classAnnotationMetadata.setGlobalEasyBeansInterceptors(easyBeansGlobalInterceptors); 88 89 91 92 List <String > externalInterceptorsClasses = new ArrayList <String >(); 94 95 99 100 LinkedList <ClassAnnotationMetadata> invertedInheritanceClassesList = 102 getInvertedSuperClassesMetadata(classAnnotationMetadata); 103 for (ClassAnnotationMetadata superMetaData : invertedInheritanceClassesList) { 106 JInterceptors classIinterceptors = superMetaData.getAnnotationInterceptors(); 107 if (classIinterceptors != null) { 108 for (String cls : classIinterceptors.getClasses()) { 109 externalInterceptorsClasses.add(cls); 110 } 111 } 112 } 113 114 115 Map <InterceptorType, List <JClassInterceptor>> externalMapClassInterceptors = 117 getInterceptors(classAnnotationMetadata.getClassName(), classAnnotationMetadata.getEjbJarAnnotationMetadata(), 118 externalInterceptorsClasses); 119 classAnnotationMetadata.setExternalUserInterceptors(externalMapClassInterceptors); 120 121 122 List <String > internalInterceptorsClasses = new ArrayList <String >(); 125 126 if (classAnnotationMetadata.isAroundInvokeMethodMetadata()) { 127 internalInterceptorsClasses.add(classAnnotationMetadata.getClassName()); 128 } 129 Map <InterceptorType, List <JClassInterceptor>> internalMapClassInterceptors = 131 getInterceptors(classAnnotationMetadata.getClassName(), classAnnotationMetadata.getEjbJarAnnotationMetadata(), 132 internalInterceptorsClasses); 133 classAnnotationMetadata.setInternalUserInterceptors(internalMapClassInterceptors); 134 135 136 for (MethodAnnotationMetadata methodAnnotationMetaData 138 : classAnnotationMetadata.getMethodAnnotationMetadataCollection()) { 139 140 Remove remove = methodAnnotationMetaData.getJRemove(); 142 if (remove != null) { 143 List <JClassInterceptor> easyBeansMethodGlobalInterceptors = new ArrayList <JClassInterceptor>(); 144 String classType = null; 145 if (remove.retainIfException()) { 147 classType = Type.getInternalName(RemoveOnlyWithoutExceptionInterceptor.class); 148 } else { 149 classType = Type.getInternalName(RemoveAlwaysInterceptor.class); 150 } 151 easyBeansMethodGlobalInterceptors.add(new JClassInterceptor(classType, EASYBEANS_INTERCEPTOR)); 152 153 methodAnnotationMetaData.setGlobalEasyBeansInterceptors(easyBeansMethodGlobalInterceptors); 155 } 156 157 JInterceptors methodAnnotationInterceptors = methodAnnotationMetaData.getAnnotationInterceptors(); 158 if (methodAnnotationInterceptors != null) { 159 List <String > methodInterceptorsClasses = new ArrayList <String >(); 160 for (String cls : methodAnnotationInterceptors.getClasses()) { 161 methodInterceptorsClasses.add(cls); 162 } 163 Map <InterceptorType, List <JClassInterceptor>> mapMethodInterceptors = 164 getInterceptors(classAnnotationMetadata.getClassName() + "/Method " 165 + methodAnnotationMetaData.getMethodName(), 166 classAnnotationMetadata.getEjbJarAnnotationMetadata(), methodInterceptorsClasses); 167 methodAnnotationMetaData.setUserInterceptors(mapMethodInterceptors); 168 } 169 170 } 171 172 } 173 174 175 184 private static Map <InterceptorType, List <JClassInterceptor>> getInterceptors(final String referencingName, 185 final EjbJarAnnotationMetadata ejbJarAnnotationMetadata, 186 final List <String > interceptorsClasses) throws ResolverException { 187 Map <InterceptorType, List <JClassInterceptor>> mapInterceptors = new HashMap <InterceptorType, List <JClassInterceptor>>(); 189 for (InterceptorType type : InterceptorType.values()) { 191 mapInterceptors.put(type, new ArrayList <JClassInterceptor>()); 192 } 193 194 int interceptorClassAnalyzed = 0; 195 196 for (String className : interceptorsClasses) { 198 ClassAnnotationMetadata interceptorMetadata = ejbJarAnnotationMetadata.getClassAnnotationMetadata(className); 199 if (interceptorMetadata == null) { 200 throw new ResolverException("No medata for interceptor class " + className 201 + " referenced by " + referencingName); 202 } 203 204 interceptorClassAnalyzed++; 206 207 if (interceptorMetadata.getClassName().contains("$")) { 209 throw new IllegalStateException ("Interceptor can't be defined in an inner class."); 210 } 211 212 213 InheritanceMethodResolver.resolve(interceptorMetadata); 215 216 217 LinkedList <ClassAnnotationMetadata> invertedInheritanceClassesList = 219 getInvertedSuperClassesMetadata(interceptorMetadata); 220 221 for (ClassAnnotationMetadata currentMetaData : invertedInheritanceClassesList) { 223 for (MethodAnnotationMetadata method : currentMetaData.getMethodAnnotationMetadataCollection()) { 225 if (method.isInherited()) { 227 continue; 228 } 229 JClassInterceptor jInterceptor = new JClassInterceptor(className, method.getJMethod(), 230 interceptorClassAnalyzed); 231 232 MethodAnnotationMetadata analyzedMethod = method; 237 MethodAnnotationMetadata methodSubClass = interceptorMetadata.getMethodAnnotationMetadata(method 238 .getJMethod()); 239 if (methodSubClass != null) { 240 analyzedMethod = methodSubClass; 241 } 242 243 244 if (analyzedMethod.isAroundInvoke()) { 246 addOnlyIfNotPresent(mapInterceptors.get(InterceptorType.AROUND_INVOKE), jInterceptor); 247 } 248 if (!currentMetaData.isBean()) { 250 if (analyzedMethod.isPostActivate()) { 252 addOnlyIfNotPresent(mapInterceptors.get(InterceptorType.POST_ACTIVATE), jInterceptor); 253 } 254 if (analyzedMethod.isPostConstruct()) { 255 addOnlyIfNotPresent(mapInterceptors.get(InterceptorType.POST_CONSTRUCT), jInterceptor); 256 } 257 if (analyzedMethod.isPreDestroy()) { 258 addOnlyIfNotPresent(mapInterceptors.get(InterceptorType.PRE_DESTROY), jInterceptor); 259 } 260 if (analyzedMethod.isPrePassivate()) { 261 addOnlyIfNotPresent(mapInterceptors.get(InterceptorType.PRE_PASSIVATE), jInterceptor); 262 } 263 } 264 } 265 } 266 267 } 268 269 270 271 272 return mapInterceptors; 273 } 274 275 281 private static void addOnlyIfNotPresent(final List <JClassInterceptor> interceptors, final JClassInterceptor jInterceptor) { 282 if (!interceptors.contains(jInterceptor)) { 283 interceptors.add(jInterceptor); 284 } 285 } 286 287 292 private static LinkedList <ClassAnnotationMetadata> getInvertedSuperClassesMetadata( 293 final ClassAnnotationMetadata classAnnotationMetadata) { 294 295 LinkedList <ClassAnnotationMetadata> superClassesList = new LinkedList <ClassAnnotationMetadata>(); 297 String superClassName = classAnnotationMetadata.getSuperName(); 298 while (!JAVA_LANG_OBJECT.equals(superClassName)) { 300 ClassAnnotationMetadata superMetaData = classAnnotationMetadata.getEjbJarAnnotationMetadata() 301 .getClassAnnotationMetadata(superClassName); 302 if (superMetaData != null) { 303 superClassName = superMetaData.getSuperName(); 304 superClassesList.addFirst(superMetaData); 305 } else { 306 superClassName = JAVA_LANG_OBJECT; 307 } 308 } 309 superClassesList.addLast(classAnnotationMetadata); 310 return superClassesList; 311 } 312 313 } 314 | Popular Tags |