1 22 package org.jboss.aop.classpool; 23 24 import java.lang.ref.WeakReference ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 28 import org.jboss.aop.AspectManager; 29 30 import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap; 31 import javassist.ClassPool; 32 import javassist.CtClass; 33 import javassist.NotFoundException; 34 import javassist.scopedpool.ScopedClassPool; 35 import javassist.scopedpool.ScopedClassPoolRepository; 36 37 41 public class AOPClassPool extends ScopedClassPool 42 { 43 44 protected ConcurrentReaderHashMap generatedClasses = new ConcurrentReaderHashMap(); 45 46 protected ConcurrentReaderHashMap localResources = new ConcurrentReaderHashMap(); 47 48 static 49 { 50 ClassPool.doPruning = false; 51 ClassPool.releaseUnmodifiedClassFile = false; 52 } 53 54 public AOPClassPool(ClassLoader cl, ClassPool src, ScopedClassPoolRepository repository) 55 { 56 super(cl, src, repository); 57 } 58 59 protected AOPClassPool(ClassPool src, ScopedClassPoolRepository repository) 60 { 61 this(null, src, repository); 62 } 63 64 public void setClassLoader(ClassLoader cl) 65 { 66 classLoader = new WeakReference (cl); 67 } 68 69 public void registerGeneratedClass(String className) 70 { 71 generatedClasses.put(className, className); 72 } 73 74 public void close() 75 { 76 super.close(); 77 AOPClassPoolRepository.getInstance().perfomUnregisterClassLoader(getClassLoader()); 78 } 79 80 public CtClass getCached(String classname) 81 { 82 CtClass clazz = getCachedLocally(classname); 83 if (clazz == null) 84 { 85 boolean isLocal = false; 86 87 ClassLoader cl = null; 89 try 90 { 91 cl = getClassLoader(); 92 } 93 catch (RuntimeException e) 94 { 95 } 97 98 if (cl != null) 99 { 100 String classResourceName = getResourceName(classname); 101 isLocal = isLocalResource(classResourceName); 102 } 103 104 if (!isLocal) 105 { 106 Object o = generatedClasses.get(classname); 107 if (o == null) 108 { 109 Map registeredCLs = AspectManager.getRegisteredCLs(); 110 synchronized (registeredCLs) 111 { 112 Iterator it = registeredCLs.values().iterator(); 113 while (it.hasNext()) 114 { 115 AOPClassPool pool = (AOPClassPool) it.next(); 116 if (pool.isUnloadedClassLoader()) 117 { 118 AspectManager.instance().unregisterClassLoader(pool.getClassLoader()); 119 continue; 120 } 121 122 if (pool.getClass().getName().equals("org.jboss.aop.deployment.ScopedJBossClassPool")) 124 { 125 continue; 126 } 127 128 clazz = pool.getCachedLocally(classname); 129 if (clazz != null) 130 { 131 return clazz; 132 } 133 } 134 } 135 } 136 } 137 } 138 return clazz; 140 } 141 142 protected String getResourceName(String classname) 143 { 144 final int lastIndex = classname.lastIndexOf('$'); 145 if (lastIndex < 0) 146 { 147 return classname.replaceAll("[\\.]", "/") + ".class"; 148 } 149 else 150 { 151 return classname.substring(0, lastIndex).replaceAll("[\\.]", "/") + classname.substring(lastIndex) + ".class"; 152 } 153 } 154 155 protected boolean isLocalResource(String resourceName) 156 { 157 String classResourceName = getResourceName(resourceName); 158 Boolean isLocal = (Boolean )localResources.get(classResourceName); 159 if (isLocal != null) 160 { 161 return isLocal.booleanValue(); 162 } 163 boolean localResource = getClassLoader().getResource(classResourceName) != null; 164 localResources.put(classResourceName, localResource ? Boolean.TRUE : Boolean.FALSE); 165 return localResource; 166 } 167 168 public synchronized CtClass getLocally(String classname) 169 throws NotFoundException 170 { 171 softcache.remove(classname); 172 CtClass clazz = (CtClass) classes.get(classname); 173 if (clazz == null) 174 { 175 clazz = createCtClass(classname, true); 176 if (clazz == null) throw new NotFoundException(classname); 177 lockInCache(clazz); } 179 180 return clazz; 181 } 182 183 184 public static AOPClassPool createAOPClassPool(ClassLoader cl, ClassPool src, ScopedClassPoolRepository repository) 185 { 186 return (AOPClassPool)AspectManager.getClassPoolFactory().create(cl, src, repository); 187 } 188 189 public static AOPClassPool createAOPClassPool(ClassPool src, ScopedClassPoolRepository repository) 190 { 191 return (AOPClassPool)AspectManager.getClassPoolFactory().create(src, repository); 192 } 193 194 public String toString() 195 { 196 ClassLoader cl = null; 197 try 198 { 199 cl = getClassLoader(); 200 } 201 catch(IllegalStateException ignore) 202 { 203 } 204 return super.toString() + " - dcl " + cl; 205 } 206 } 207 | Popular Tags |