KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > deployment > ScopedJBossClassPool


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.deployment;
23
24 import java.io.File JavaDoc;
25 import java.lang.ref.WeakReference JavaDoc;
26 import java.net.URL JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 import org.jboss.aop.AspectManager;
30 import org.jboss.aop.classpool.AOPClassPool;
31 import org.jboss.aop.classpool.AOPClassPoolRepository;
32 import org.jboss.aop.deployment.LoaderRepositoryUrlUtil.UrlInfo;
33 import org.jboss.mx.loading.HeirarchicalLoaderRepository3;
34 import org.jboss.mx.loading.LoaderRepository;
35 import org.jboss.mx.loading.RepositoryClassLoader;
36
37 import javassist.ClassPool;
38 import javassist.CtClass;
39 import javassist.NotFoundException;
40 import javassist.scopedpool.ScopedClassPoolRepository;
41
42 /**
43  * A classpool in JBoss backed by a scoped (HierarchicalLoaderRepository) loader repository
44  *
45  * @author <a HREF="kabir.khan@jboss.com">Kabir Khan</a>
46  * @version $Revision: 1.1 $
47  */

48 public class ScopedJBossClassPool extends JBossClassPool
49 {
50    final static LoaderRepositoryUrlUtil LOADER_REPOSITORY_UTIL = new LoaderRepositoryUrlUtil();
51    
52    WeakReference JavaDoc repository = null;
53    UrlInfo urlInfo;
54
55    protected ScopedJBossClassPool(ClassLoader JavaDoc cl, ClassPool src, ScopedClassPoolRepository repository, File JavaDoc tmp, URL JavaDoc tmpURL)
56    {
57       super(cl, src, repository, tmp, tmpURL);
58       
59       boolean parentFirst = false;
60       LoaderRepository loaderRepository = null;
61       ClassLoader JavaDoc prnt = cl;
62       while (prnt != null)
63       {
64          if (prnt instanceof RepositoryClassLoader)
65          {
66             loaderRepository = ((RepositoryClassLoader)prnt).getLoaderRepository();
67             if (loaderRepository instanceof HeirarchicalLoaderRepository3)
68             {
69                parentFirst = ((HeirarchicalLoaderRepository3)loaderRepository).getUseParentFirst();
70             }
71             break;
72          }
73          prnt = cl.getParent();
74       }
75       
76       super.childFirstLookup = !parentFirst;
77    }
78    
79
80    private HeirarchicalLoaderRepository3 getRepository()
81    {
82       //FIXME - Once Javassist > 3.3.0 is out use getClassLoader0() and get rid of try/catch
83
ClassLoader JavaDoc cl = null;
84       try
85       {
86          cl = getClassLoader();
87       }
88       catch (RuntimeException JavaDoc e)
89       {
90          //Ignore, the ScopedClassPoll throws an exception if pool is not associated with a cl
91
}
92       if (cl != null)
93       {
94          return (HeirarchicalLoaderRepository3)((RepositoryClassLoader)cl).getLoaderRepository();
95       }
96       return null;
97    }
98
99    private URL JavaDoc getResourceUrlForClass(String JavaDoc resourcename)
100    {
101       HeirarchicalLoaderRepository3 repo = getRepository();
102       return repo.getResource(resourcename, super.getClassLoader());
103    }
104    
105    private boolean isMine(URL JavaDoc url)
106    {
107       HeirarchicalLoaderRepository3 repo = getRepository();
108       if (repo != null)
109       {
110          //The URL of the class loaded with my scoped classloader
111
if (url != null)
112          {
113             urlInfo = LOADER_REPOSITORY_UTIL.getURLInfo(getRepository(), urlInfo);
114             
115             URL JavaDoc[] myUrls = urlInfo.getLocalUrls();
116             String JavaDoc resource = url.toString();
117             for (int i = 0 ; i < myUrls.length ; i++)
118             {
119                if (resource.indexOf(myUrls[i].toString()) >= 0)
120                {
121                   return true;
122                }
123             }
124             return false;
125          }
126       }
127       return true;
128    }
129
130    public CtClass getCached(String JavaDoc classname)
131    {
132       if (classname == null)
133       {
134          return null;
135       }
136
137       if (generatedClasses.get(classname) != null)
138       {
139          //It is a new class, and this callback is probably coming from the frozen check when creating a new nested class
140
return super.getCached(classname);
141       }
142       
143       //Is this from the scoped classloader itself of from the parent?
144
String JavaDoc resourcename = getResourceName(classname);
145       URL JavaDoc url = getResourceUrlForClass(resourcename);
146       boolean isMine = isMine(url);
147       
148       if (isMine)
149       {
150          if (super.childFirstLookup)
151          {
152             //Parent delegation is false, attempt to get this class out of ourselves
153
CtClass clazz = super.getCachedLocally(classname);
154             if (clazz == null)
155             {
156                clazz = createCtClass(classname, false);
157                if (clazz != null)
158                {
159                   lockInCache(clazz);
160                }
161             }
162             if (clazz != null)
163             {
164                return clazz;
165             }
166          }
167          return super.getCached(classname);
168       }
169       
170
171       try
172       {
173          ClassPool pool = getCorrectPoolForResource(url);
174          return pool.get(classname);
175       }
176       catch (NotFoundException e)
177       {
178       }
179       return null;
180    }
181
182    private ClassPool getCorrectPoolForResource(URL JavaDoc url)
183    {
184       synchronized(AspectManager.getRegisteredCLs())
185       {
186          for(Iterator JavaDoc it = AspectManager.getRegisteredCLs().values().iterator() ; it.hasNext() ; )
187          {
188             AOPClassPool candidate = (AOPClassPool)it.next();
189             if (candidate.isUnloadedClassLoader())
190             {
191                AspectManager.instance().unregisterClassLoader(candidate.getClassLoader());
192                continue;
193             }
194             
195             if (candidate.getClassLoader() instanceof RepositoryClassLoader)
196             {
197                //Sometimes the ClassLoader is a proxy for MBeanProxyExt?!
198
RepositoryClassLoader rcl = (RepositoryClassLoader)candidate.getClassLoader();
199                URL JavaDoc[] urls = rcl.getClasspath();
200                String JavaDoc resource = url.toString();
201                for (int i = 0 ; i < urls.length ; i++)
202                {
203                   if (resource.indexOf(urls[i].toString()) >= 0)
204                   {
205                      return candidate;
206                   }
207                }
208             }
209          }
210       }
211
212       return AOPClassPool.createAOPClassPool(ClassPool.getDefault(), AOPClassPoolRepository.getInstance());
213    }
214 }
215
Popular Tags