KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.aop.classpool;
23
24 import java.lang.ref.WeakReference JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
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 /**
38  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
39  * @version $Revision: 56792 $
40  */

41 public class AOPClassPool extends ScopedClassPool
42 {
43    /** Classnames of classes that will be created - we do not want to look for these in other pools */
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 JavaDoc 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 JavaDoc cl)
65    {
66       classLoader = new WeakReference JavaDoc(cl);
67    }
68    
69    public void registerGeneratedClass(String JavaDoc 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 JavaDoc classname)
81    {
82       CtClass clazz = getCachedLocally(classname);
83       if (clazz == null)
84       {
85          boolean isLocal = false;
86
87          //FIXME - Once Javassist > 3.3.0 is out use getClassLoader0() and get rid of try/catch
88
ClassLoader JavaDoc cl = null;
89          try
90          {
91             cl = getClassLoader();
92          }
93          catch (RuntimeException JavaDoc e)
94          {
95             //Ignore, the ScopedClassPoll throws an exception if pool is not associated with a cl
96
}
97
98          if (cl != null)
99          {
100             String JavaDoc classResourceName = getResourceName(classname);
101             isLocal = isLocalResource(classResourceName);
102          }
103          
104          if (!isLocal)
105          {
106             Object JavaDoc o = generatedClasses.get(classname);
107             if (o == null)
108             {
109                Map JavaDoc registeredCLs = AspectManager.getRegisteredCLs();
110                synchronized (registeredCLs)
111                {
112                   Iterator JavaDoc 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                      //Do not check classpools for scoped classloaders
123
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       // *NOTE* NEED TO TEST WHEN SUPERCLASS IS IN ANOTHER UCL!!!!!!
139
return clazz;
140    }
141    
142    protected String JavaDoc getResourceName(String JavaDoc 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 JavaDoc resourceName)
156    {
157       String JavaDoc classResourceName = getResourceName(resourceName);
158       Boolean JavaDoc isLocal = (Boolean JavaDoc)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 JavaDoc 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);//Avoid use of the softclasscache
178
}
179
180       return clazz;
181    }
182
183
184    public static AOPClassPool createAOPClassPool(ClassLoader JavaDoc 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 JavaDoc toString()
195    {
196       ClassLoader JavaDoc cl = null;
197       try
198       {
199          cl = getClassLoader();
200       }
201       catch(IllegalStateException JavaDoc ignore)
202       {
203       }
204       return super.toString() + " - dcl " + cl;
205    }
206 }
207
Popular Tags