1 22 package org.jboss.aop.classpool; 23 24 import java.lang.reflect.Field ; 25 import java.security.AccessController ; 26 import java.security.PrivilegedActionException ; 27 import java.security.PrivilegedExceptionAction ; 28 import java.util.HashMap ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 33 import org.jboss.aop.AspectManager; 34 import org.jboss.aop.instrument.Instrumentor; 35 36 import javassist.ClassPool; 37 import javassist.scopedpool.ScopedClassPool; 38 import javassist.scopedpool.ScopedClassPoolFactory; 39 import javassist.scopedpool.ScopedClassPoolRepository; 40 import javassist.scopedpool.ScopedClassPoolRepositoryImpl; 41 42 48 public class AOPClassPoolRepository implements ScopedClassPoolRepository 49 { 50 private final static AOPClassPoolRepository instance = new AOPClassPoolRepository(); 51 52 53 protected final HashMap ucl2classes = new HashMap (); 54 55 56 AspectManager manager; 57 58 ScopedClassPoolRepository delegate; 59 60 public static AOPClassPoolRepository getInstance() 61 { 62 return instance; 63 } 64 65 private AOPClassPoolRepository() 66 { 67 this.delegate = ScopedClassPoolRepositoryImpl.getInstance(); 68 delegate.setClassPoolFactory(new AOPClassPoolFactory()); 69 } 70 71 72 public void setClassPoolFactory(ScopedClassPoolFactory factory) 73 { 74 delegate.setClassPoolFactory(factory); 75 } 76 77 public ScopedClassPoolFactory getClassPoolFactory() 78 { 79 return delegate.getClassPoolFactory(); 80 } 81 82 public boolean isPrune() 83 { 84 return delegate.isPrune(); 85 } 86 87 public void setPrune(boolean prune) 88 { 89 delegate.setPrune(prune); 90 } 91 92 public ScopedClassPool createScopedClassPool(ClassLoader cl, ClassPool src) 93 { 94 return delegate.createScopedClassPool(cl, src); 95 } 96 97 public ClassPool findClassPool(ClassLoader cl) 98 { 99 return delegate.findClassPool(cl); 100 } 101 102 public void setAspectManager(AspectManager manager) 103 { 104 this.manager = manager; 105 } 106 107 112 public Map getRegisteredCLs() 113 { 114 return delegate.getRegisteredCLs(); 115 } 116 117 120 public void clearUnregisteredClassLoaders() 121 { 122 delegate.clearUnregisteredClassLoaders(); 123 } 124 125 public ClassPool registerClassLoader(ClassLoader ucl) 126 { 127 return delegate.registerClassLoader(ucl); 128 } 129 130 public void unregisterClassLoader(ClassLoader cl) 131 { 132 delegate.unregisterClassLoader(cl); 133 } 134 135 public void registerClass(Class clazz) 136 { 137 HashSet classes = (HashSet ) ucl2classes.get(clazz.getClassLoader()); 138 if (classes == null) 139 { 140 classes = new HashSet (); 141 ucl2classes.put(clazz.getClassLoader(), classes); 142 } 143 classes.add(clazz); 144 } 145 146 public void perfomUnregisterClassLoader(ClassLoader cl) 147 { 148 if (System.getSecurityManager() == null) 149 { 150 UnregisterClassLoaderAction.NON_PRIVILEGED.unregister(this, cl); 151 } 152 else 153 { 154 UnregisterClassLoaderAction.PRIVILEGED.unregister(this, cl); 155 } 156 } 157 158 private void doUnregisterClassLoader(ClassLoader cl) 159 { 160 synchronized (delegate.getRegisteredCLs()) 161 { 162 HashSet classes = (HashSet ) ucl2classes.remove(cl); 163 if (classes != null) 164 { 165 Iterator it = classes.iterator(); 166 while (it.hasNext()) 167 { 168 Object clazz = it.next(); 169 synchronized (manager.getAdvisors()) 170 { 171 manager.getAdvisors().remove(clazz); 172 Class advisedClass = (Class )clazz; 173 try 174 { 175 Field f = advisedClass.getDeclaredField(Instrumentor.HELPER_FIELD_NAME); 177 f.setAccessible(true); 178 f.set(null, null); 179 } 180 catch(NoSuchFieldException e) 181 { 182 System.out.println("[warn] Error unsetting advisor for " + advisedClass.getName() + " " + e); 183 } 184 catch(IllegalAccessException e) 185 { 186 System.out.println("[warn] Error unsetting advisor for " + advisedClass.getName() + " " + e); 187 } 188 } 189 } 190 } 191 } 192 } 193 194 195 interface UnregisterClassLoaderAction 196 { 197 void unregister(AOPClassPoolRepository repository, ClassLoader loader); 198 199 UnregisterClassLoaderAction PRIVILEGED = new UnregisterClassLoaderAction() 200 { 201 public void unregister(final AOPClassPoolRepository repository, final ClassLoader loader) 202 { 203 try 204 { 205 AccessController.doPrivileged(new PrivilegedExceptionAction () 206 { 207 public Object run() 208 { 209 repository.doUnregisterClassLoader(loader); 210 return null; 211 } 212 }); 213 } 214 catch (PrivilegedActionException e) 215 { 216 Exception ex = e.getException(); 217 if (ex instanceof RuntimeException ) 218 { 219 throw (RuntimeException )ex; 220 } 221 throw new RuntimeException (ex); 222 } 223 } 224 }; 225 226 UnregisterClassLoaderAction NON_PRIVILEGED = new UnregisterClassLoaderAction() 227 { 228 public void unregister(AOPClassPoolRepository repository, ClassLoader loader) 229 { 230 repository.doUnregisterClassLoader(loader); 231 } 232 }; 233 } 234 } 235 | Popular Tags |