KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > AOPClassPool


1 /***************************************
2  * *
3  * JBoss: The OpenSource J2EE WebOS *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  ***************************************/

9
10 package org.jboss.aop;
11
12 import java.util.Iterator JavaDoc;
13 import java.util.Map JavaDoc;
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 /**
22  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
23  * @version $Revision: 1.28 $
24  */

25 public class AOPClassPool extends ClassPool
26 {
27    protected AspectManager manager;
28    protected ClassLoader JavaDoc dcl;
29    protected LoaderClassPath classPath;
30    protected SoftValueHashMap softcache = new SoftValueHashMap();
31
32    public AOPClassPool(ClassLoader JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc classname)
83    {
84       CtClass clazz = getCachedLocally(classname);
85       if (clazz == null)
86       {
87          Map JavaDoc registeredCLs = manager.getRegisteredCLs();
88          synchronized (registeredCLs)
89          {
90             Iterator JavaDoc 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       // *NOTE* NEED TO TEST WHEN SUPERCLASS IS IN ANOTHER UCL!!!!!!
109
return clazz;
110    }
111
112    protected void cacheCtClass(String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc toClass(CtClass ct, ClassLoader JavaDoc loader) throws CannotCompileException
167    {
168       //We need to pass up the classloader stored in this pool, as the default implementation uses the Thread context cl.
169
//In the case of JSP's in Tomcat, org.apache.jasper.servlet.JasperLoader will be stored here, while it's parent
170
//org.jboss.web.tomcat.tc5.WebCtxLoader$ENCLoader is used as the Thread context cl. The invocation class needs to
171
// be generated in the JasperLoader classloader since in the case of method invocations, the package name will be
172
//the same as for the class generated from the jsp, i.e. org.apache.jsp. For classes belonging to org.apache.jsp,
173
//JasperLoader does NOT delegate to its parent if it cannot find them.
174
lockInCache(ct);
175       return super.toClass(ct, dcl);
176
177    }
178
179 }
180
Popular Tags