1 25 package org.objectweb.easybeans.client; 26 27 import static org.objectweb.easybeans.enhancer.injection.InjectionClassAdapter.JAVA_LANG_OBJECT; 28 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.util.List ; 32 import java.util.Map ; 33 34 import org.objectweb.asm.ClassReader; 35 import org.objectweb.asm.ClassWriter; 36 import org.objectweb.easybeans.deployment.annotations.analyzer.ScanClassVisitor; 37 import org.objectweb.easybeans.deployment.annotations.metadata.ClassAnnotationMetadata; 38 import org.objectweb.easybeans.deployment.annotations.metadata.EjbJarAnnotationMetadata; 39 import org.objectweb.easybeans.enhancer.Enhancer; 40 import org.objectweb.easybeans.enhancer.EnhancerException; 41 import org.objectweb.easybeans.enhancer.client.ClientLifeCycleAdapter; 42 import org.objectweb.easybeans.enhancer.injection.InjectionClassAdapter; 43 import org.objectweb.easybeans.loader.EasyBeansClassLoader; 44 import org.objectweb.easybeans.log.JLog; 45 import org.objectweb.easybeans.log.JLogFactory; 46 47 53 public class ClientEnhancer extends Enhancer { 54 55 58 private static JLog logger = JLogFactory.getLog(ClientEnhancer.class); 59 60 66 public ClientEnhancer(final ClassLoader loader, final EjbJarAnnotationMetadata ejbJarAnnotationMetadata, 67 final Map <String , Object > map) { 68 super(loader, ejbJarAnnotationMetadata, map); 69 } 70 71 78 public static void enhance(final ClassLoader loader, final List <String > classesToEnhance, 79 final Map <String , Object > map) throws EnhancerException { 80 EjbJarAnnotationMetadata ejbJarAnnotationMetadata = new EjbJarAnnotationMetadata(); 81 ScanClassVisitor scanVisitor = new ScanClassVisitor(ejbJarAnnotationMetadata); 82 logger.info("ClassLoader used = {0}", loader); 83 for (String clazz : classesToEnhance) { 84 read(clazz, loader, scanVisitor, ejbJarAnnotationMetadata); 85 } 86 87 ClientEnhancer clientEnhancer = new ClientEnhancer(loader, ejbJarAnnotationMetadata, map); 88 clientEnhancer.enhance(); 89 } 90 91 92 100 private static void read(final String className, final ClassLoader loader, final ScanClassVisitor scanVisitor, 101 final EjbJarAnnotationMetadata ejbJarAnnotationMetadata) throws EnhancerException { 102 String readingClass = className; 103 if (!className.toLowerCase().endsWith(".class")) { 104 readingClass = className + ".class"; 105 } 106 107 InputStream is = loader.getResourceAsStream(readingClass); 108 109 logger.info("Visiting class {0}", className); 110 try { 111 new ClassReader(is).accept(scanVisitor, false); 112 } catch (IOException e) { 113 throw new EnhancerException("Cannot read the given class '" + className + "'.", e); 114 } 115 116 ClassAnnotationMetadata classMetadata = ejbJarAnnotationMetadata.getClassAnnotationMetadata(className); 118 String superClassName = classMetadata.getSuperName(); 119 if (!superClassName.equals(JAVA_LANG_OBJECT)) { 120 read(superClassName, loader, scanVisitor, ejbJarAnnotationMetadata); 121 } 122 try { 123 is.close(); 124 } catch (IOException e) { 125 throw new EnhancerException("Cannot close the input stream for class '" + className + "'.", e); 126 } 127 128 } 129 130 134 @Override 135 public void enhance() throws EnhancerException { 136 for (ClassAnnotationMetadata classAnnotationMetadata : getEjbJarAnnotationMetadata() 137 .getClassAnnotationMetadataCollection()) { 138 ClassReader cr = getClassReader(classAnnotationMetadata); 139 ClassWriter cw = new ClassWriter(true); 140 InjectionClassAdapter injectionClassAdapter = new InjectionClassAdapter(classAnnotationMetadata, cw, 141 getMap(), true); 142 ClientLifeCycleAdapter clientLifeCycleAdapter = new ClientLifeCycleAdapter(classAnnotationMetadata, 143 injectionClassAdapter); 144 cr.accept(clientLifeCycleAdapter, false); 145 146 ((EasyBeansClassLoader) getClassLoader()).addClassDefinition(classAnnotationMetadata.getClassName() 147 .replace("/", "."), cw.toByteArray()); 148 } 149 } 150 151 } 152 | Popular Tags |