1 9 10 package org.jboss.aop; 11 12 import java.util.Iterator ; 13 import java.util.Map ; 14 import org.jboss.aop.util.SoftValueHashMap; 15 import javassist.CannotCompileException; 16 import javassist.ClassPool; 17 import javassist.CtClass; 18 import javassist.LoaderClassPath; 19 import javassist.NotFoundException; 20 21 25 public class AOPClassPool extends ClassPool 26 { 27 protected AspectManager manager; 28 protected ClassLoader dcl; 29 protected LoaderClassPath classPath; 30 protected SoftValueHashMap softcache = new SoftValueHashMap(); 31 32 public AOPClassPool(ClassLoader cl, ClassPool src, AspectManager manager) 33 { 34 super(src); 35 this.manager = manager; 36 this.dcl = cl; 37 if (cl != null) 38 { 39 classPath = new LoaderClassPath(cl); 40 this.insertClassPath(classPath); 41 } 42 childFirstLookup = true; 43 } 44 45 protected AOPClassPool(ClassPool src, AspectManager manager) 46 { 47 this(null, src, manager); 48 } 49 50 public ClassLoader getClassLoader() 51 { 52 return dcl; 53 } 54 55 public void close() 56 { 57 this.removeClassPath(classPath); 58 classPath.close(); 59 classes.clear(); 60 softcache.clear(); 61 } 62 63 public synchronized void flushClass(String classname) 64 { 65 classes.remove(classname); 66 softcache.remove(classname); 67 } 68 69 public synchronized void soften(CtClass clazz) 70 { 71 if (AspectManager.prune) clazz.prune(); 72 classes.remove(clazz.getName()); 73 softcache.put(clazz.getName(), clazz); 74 } 75 76 77 public boolean isUnloadedClassLoader() 78 { 79 return false; 80 } 81 82 protected CtClass getCached(String classname) 83 { 84 CtClass clazz = getCachedLocally(classname); 85 if (clazz == null) 86 { 87 Map registeredCLs = manager.getRegisteredCLs(); 88 synchronized (registeredCLs) 89 { 90 Iterator it = registeredCLs.values().iterator(); 91 while (it.hasNext()) 92 { 93 AOPClassPool pool = (AOPClassPool) it.next(); 94 if (pool.isUnloadedClassLoader()) 95 { 96 AspectManager.instance().unregisterClassLoader(pool.dcl); 97 continue; 98 } 99 100 clazz = pool.getCachedLocally(classname); 101 if (clazz != null) 102 { 103 return clazz; 104 } 105 } 106 } 107 } 108 return clazz; 110 } 111 112 protected void cacheCtClass(String classname, CtClass c, boolean dynamic) 113 { 114 if (dynamic) 115 { 116 super.cacheCtClass(classname, c, dynamic); 117 } 118 else 119 { 120 if (AspectManager.prune) c.prune(); 121 softcache.put(classname, c); 122 } 123 } 124 125 public void lockInCache(CtClass c) 126 { 127 super.cacheCtClass(c.getName(), c, false); 128 } 129 130 protected CtClass getCachedLocally(String classname) 131 { 132 CtClass cached = (CtClass) classes.get(classname); 133 if (cached != null) return cached; 134 synchronized (softcache) 135 { 136 return (CtClass) softcache.get(classname); 137 } 138 } 139 140 public synchronized CtClass getLocally(String classname) 141 throws NotFoundException 142 { 143 softcache.remove(classname); 144 CtClass clazz = (CtClass) classes.get(classname); 145 if (clazz == null) 146 { 147 clazz = createCtClass(classname, true); 148 if (clazz == null) throw new NotFoundException(classname); 149 super.cacheCtClass(classname, clazz, false); 150 } 151 152 return clazz; 153 } 154 155 156 public static AOPClassPool createAOPClassPool(ClassLoader cl, ClassPool src, AspectManager manager) 157 { 158 return AspectManager.classPoolFactory.create(cl, src, manager); 159 } 160 161 public static AOPClassPool createAOPClassPool(ClassPool src, AspectManager manager) 162 { 163 return AspectManager.classPoolFactory.create(src, manager); 164 } 165 166 public Class toClass(CtClass ct, ClassLoader loader) throws CannotCompileException 167 { 168 lockInCache(ct); 175 return super.toClass(ct, dcl); 176 177 } 178 179 } 180 | Popular Tags |