1 22 package org.jboss.aop.contrib; 23 24 import java.io.IOException ; 25 import java.net.URL ; 26 import java.net.URLClassLoader ; 27 import java.util.ArrayList ; 28 29 import javassist.CannotCompileException; 30 import javassist.ClassPool; 31 import javassist.CtClass; 32 import javassist.LoaderClassPath; 33 import javassist.scopedpool.ScopedClassPool; 34 import javassist.scopedpool.ScopedClassPoolFactory; 35 import javassist.scopedpool.ScopedClassPoolRepository; 36 37 import org.jboss.aop.AspectManager; 38 import org.jboss.aop.classpool.AOPClassPool; 39 40 45 public class IDEClassPoolFactory implements ScopedClassPoolFactory 46 { 47 48 private ArrayList classPaths; 49 50 public IDEClassPoolFactory() 51 { 52 classPaths = new ArrayList (); 53 } 54 55 public ScopedClassPool create(ClassLoader loader, ClassPool pool, ScopedClassPoolRepository repository) 56 { 57 SimpleClassPool classPool = new SimpleClassPool(loader, pool, repository); 58 return classPool; 59 } 60 61 public ScopedClassPool create(ClassPool pool, ScopedClassPoolRepository repository) 62 { 63 SimpleClassPool classPool = new SimpleClassPool(pool, repository); 64 return classPool; 65 } 66 67 public void insertClasspath(String path) 68 { 69 try 70 { 71 classPaths.add(new URL (path)); 72 } 73 catch (Exception e) 74 { 75 e.printStackTrace(); 76 } 77 } 78 79 private class SimpleClassPool extends AOPClassPool 80 { 81 private Loader loader; 82 83 public SimpleClassPool(ClassLoader loader, ClassPool pool, ScopedClassPoolRepository repository) 84 { 85 super(loader, pool, repository); 86 loadClasspath(); 87 } 88 89 public SimpleClassPool(ClassPool pool, ScopedClassPoolRepository repository) 90 { 91 super(pool, repository); 92 loadClasspath(); 93 94 } 95 96 private void loadClasspath() 97 { 98 childFirstLookup = true; 99 100 URL urlPaths[] = (URL []) classPaths.toArray(new URL [classPaths.size()]); 101 loader = new Loader (urlPaths, Thread.currentThread().getContextClassLoader()); 102 setClassLoader(loader); 103 104 Thread.currentThread().setContextClassLoader(loader); 105 106 classPath = new LoaderClassPath(loader); 107 insertClassPath(classPath); 108 } 109 110 public Class toClass(CtClass cc) 111 throws CannotCompileException 112 { 113 try 114 { 115 return loader.loadClass(cc.getName()); 116 } 117 catch (Exception e) 118 { 119 try 120 { 121 return loader.loadClass(cc.getName(), cc.toBytecode()); 122 } 123 catch (IOException e2) 124 { 125 throw new CannotCompileException(e2); 126 } 127 } 128 } 129 } 130 131 private class Loader extends URLClassLoader 132 { 133 public Loader(URL urls[], ClassLoader src) 134 { 135 super(urls, src); 136 } 137 138 public Class loadClass(String name, byte[] classfile) 139 throws ClassFormatError 140 { 141 Class c = defineClass(name, classfile, 0, classfile.length); 142 143 resolveClass(c); 144 return c; 145 } 146 } 147 148 public ClassLoader getTCLIfScoped() 149 { 150 return null; 151 } 152 } 153 | Popular Tags |