1 22 package org.jboss.aop.advice; 23 24 import java.lang.ref.WeakReference ; 25 26 import org.jboss.aop.AspectManager; 27 28 33 public abstract class AspectFactoryWithClassLoaderSupport implements AspectFactory, AspectFactoryWithClassLoader 34 { 35 private WeakReference loader; 36 37 38 private ThreadLocal scopedClassLoader = new ThreadLocal (); 39 40 protected AspectFactoryWithClassLoaderSupport() 41 { 42 } 43 44 public void setClassLoader(ClassLoader cl) 45 { 46 this.loader = new WeakReference (cl); 47 } 48 49 protected ClassLoader getLoader() 50 { 51 ClassLoader scopedClassLoader = peekScopedClassLoader(); 52 if (scopedClassLoader != null) 53 { 54 return scopedClassLoader; 55 } 56 if (loader != null) 57 { 58 return (ClassLoader )loader.get(); 59 } 60 return null; 61 } 62 63 public void pushScopedClassLoader(ClassLoader scopedCl) 64 { 65 scopedClassLoader.set(scopedCl); 66 } 67 68 public void popScopedClassLoader() 69 { 70 scopedClassLoader.set(null); 71 } 72 73 public ClassLoader peekScopedClassLoader() 74 { 75 return (ClassLoader )scopedClassLoader.get(); 76 } 77 78 protected Class loadClass(String name) throws ClassNotFoundException 79 { 80 ClassLoader cl = getLoader(); 81 if (cl == null) 82 { 83 ClassLoader tcl = Thread.currentThread().getContextClassLoader(); 84 if (AspectManager.verbose) System.out.println("Using context classloader " + tcl + " to load aspect " + name); 85 return tcl.loadClass(name); 86 } 87 else 88 { 89 if (AspectManager.verbose) System.out.println("Using scoped classloader " + cl + " to load aspect " + name); 90 return cl.loadClass(name); 91 } 92 } 93 94 } 95 | Popular Tags |