1 25 package org.objectweb.easybeans.tests.common.enhancer; 26 27 import java.io.InputStream ; 28 import java.net.URL ; 29 import java.net.URLClassLoader ; 30 import java.util.List ; 31 32 import org.objectweb.asm.ClassReader; 33 import org.objectweb.easybeans.deployment.annotations.analyzer.ScanClassVisitor; 34 import org.objectweb.easybeans.deployment.annotations.helper.ResolverHelper; 35 import org.objectweb.easybeans.deployment.annotations.metadata.ClassAnnotationMetadata; 36 import org.objectweb.easybeans.deployment.annotations.metadata.EjbJarAnnotationMetadata; 37 import org.objectweb.easybeans.deployment.annotations.metadata.MethodAnnotationMetadata; 38 import org.objectweb.easybeans.enhancer.Enhancer; 39 40 41 46 public final class ClassesEnhancer extends Enhancer { 47 48 51 public static enum TYPE {INTERCEPTOR, CALLBACK, ALL} 52 53 56 public static final String EXT_CLASS = ".class"; 57 58 63 public ClassesEnhancer(final ClassLoader loader, final EjbJarAnnotationMetadata ejbJarAnnotationMetadata) { 64 super(loader, ejbJarAnnotationMetadata, null); 65 } 66 67 73 public static void enhanceNewClassLoader(final List <String > classesToEnhance, final TYPE type) throws Exception { 74 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 75 ClassLoader childLoader = new URLClassLoader (new URL []{}, loader); 76 Thread.currentThread().setContextClassLoader(childLoader); 77 try{ 78 enhance(classesToEnhance, TYPE.INTERCEPTOR); 79 }finally{ 80 Thread.currentThread().setContextClassLoader(loader); 81 } 82 } 83 84 90 public static void enhance(final List <String > classesToEnhance, final TYPE type) throws Exception { 91 EjbJarAnnotationMetadata ejbJarAnnotationMetadata = new EjbJarAnnotationMetadata(); 92 ScanClassVisitor scanVisitor = new ScanClassVisitor(ejbJarAnnotationMetadata); 93 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 94 95 InputStream is = null; 96 for (String clazz : classesToEnhance) { 97 is = loader.getResourceAsStream(clazz); 98 new ClassReader(is).accept(scanVisitor, false); 99 } 100 101 ResolverHelper.resolve(ejbJarAnnotationMetadata); 102 103 104 for (ClassAnnotationMetadata classAnnotationMetadata : ejbJarAnnotationMetadata 106 .getClassAnnotationMetadataCollection()) { 107 if (classAnnotationMetadata.isBean()) { 108 classAnnotationMetadata.setGlobalEasyBeansInterceptors(null); 110 for (MethodAnnotationMetadata m : classAnnotationMetadata.getMethodAnnotationMetadataCollection()) { 111 m.setInterceptors(null); 112 } 113 } 114 } 115 116 ClassesEnhancer classesEnhancer = new ClassesEnhancer(loader, ejbJarAnnotationMetadata); 117 classesEnhancer.enhance(); 118 } 119 120 } 121 | Popular Tags |