FileDocCategorySizeDatePackage
ScopedJBossClassPool.javaAPI DocJBoss 4.2.17472Fri Jul 13 21:02:24 BST 2007org.jboss.aop.deployment

ScopedJBossClassPool

public class ScopedJBossClassPool extends JBossClassPool
A classpool in JBoss backed by a scoped (HierarchicalLoaderRepository) loader repository
author
Kabir Khan
version
$Revision: 1.1 $

Fields Summary
static final LoaderRepositoryUrlUtil
LOADER_REPOSITORY_UTIL
WeakReference
repository
org.jboss.aop.deployment.LoaderRepositoryUrlUtil.UrlInfo
urlInfo
ThreadLocal
lastPool
Constructors Summary
protected ScopedJBossClassPool(ClassLoader cl, ClassPool src, ScopedClassPoolRepository repository, File tmp, URL tmpURL)


             
   
      super(cl, src, repository, tmp, tmpURL);
      
      boolean parentFirst = false;
      LoaderRepository loaderRepository = null;
      ClassLoader prnt = cl;
      while (prnt != null)
      {
         if (prnt instanceof RepositoryClassLoader)
         {
            loaderRepository = ((RepositoryClassLoader)prnt).getLoaderRepository();
            if (loaderRepository instanceof HeirarchicalLoaderRepository3)
            {
               parentFirst = ((HeirarchicalLoaderRepository3)loaderRepository).getUseParentFirst();
            }
            break;
         }
         prnt = cl.getParent();
      }
      
      super.childFirstLookup = !parentFirst;
   
Methods Summary
public javassist.CtClassgetCached(java.lang.String classname)

      if (classname == null)
      {
         return null;
      }

      if (generatedClasses.get(classname) != null)
      {
         //It is a new class, and this callback is probably coming from the frozen check when creating a new nested class
         return super.getCached(classname);
      }
      
      //Is this from the scoped classloader itself of from the parent?
      String resourcename = getResourceName(classname);
      URL url = getResourceUrlForClass(resourcename);
      boolean isMine = isMine(url);
      
      if (isMine)
      {
         if (super.childFirstLookup)
         {
            //Parent delegation is false, attempt to get this class out of ourselves
            CtClass clazz = super.getCachedLocally(classname);
            if (clazz == null)
            {
               clazz = createCtClass(classname, false);
               if (clazz != null)
               {
                  lockInCache(clazz);
               }
            }
            if (clazz != null)
            {
               return clazz;
            }
         }
         return super.getCached(classname);
      }
      

      try
      {
         ClassPool pool = getCorrectPoolForResource(url);
         if (pool != lastPool.get())
         {
            lastPool.set(pool);
            return pool.get(classname);
         }
      }
      catch (NotFoundException e)
      {
      }
      catch(StackOverflowError e)
      {
         throw e;
      }
      finally
      {
         lastPool.set(null);
      }

      return null;
   
private javassist.ClassPoolgetCorrectPoolForResource(java.net.URL url)

      synchronized(AspectManager.getRegisteredCLs())
      {
         for(Iterator it = AspectManager.getRegisteredCLs().values().iterator() ; it.hasNext() ; )
         {
            AOPClassPool candidate = (AOPClassPool)it.next();
            if (candidate.isUnloadedClassLoader())
            {
               AspectManager.instance().unregisterClassLoader(candidate.getClassLoader());
               continue;
            }
            
            if (candidate.getClassLoader() instanceof RepositoryClassLoader)
            {
               //Sometimes the ClassLoader is a proxy for MBeanProxyExt?!
               RepositoryClassLoader rcl = (RepositoryClassLoader)candidate.getClassLoader();
               URL[] urls = rcl.getClasspath();
               String resource = url.toString();
               for (int i = 0 ; i < urls.length ; i++)
               {
                  if (resource.indexOf(urls[i].toString()) >= 0)
                  {
                     return candidate;
                  }
               }
            }
         }
      }

      return AOPClassPool.createAOPClassPool(ClassPool.getDefault(), AOPClassPoolRepository.getInstance());
   
private org.jboss.mx.loading.HeirarchicalLoaderRepository3getRepository()

      //FIXME - Once Javassist > 3.3.0 is out use getClassLoader0() and get rid of try/catch
      ClassLoader cl = null;
      try
      {
         cl =  getClassLoader();
      }
      catch (RuntimeException e)
      {
         //Ignore, the ScopedClassPoll throws an exception if pool is not associated with a cl
      }
      if (cl != null)
      {
         return (HeirarchicalLoaderRepository3)((RepositoryClassLoader)cl).getLoaderRepository();
      }
      return null;
   
private java.net.URLgetResourceUrlForClass(java.lang.String resourcename)

      HeirarchicalLoaderRepository3 repo = getRepository();
      return repo.getResource(resourcename, super.getClassLoader());
   
private booleanisMine(java.net.URL url)

      HeirarchicalLoaderRepository3 repo = getRepository();
      if (repo != null)
      {
         //The URL of the class loaded with my scoped classloader
         if (url != null)
         {
            urlInfo = LOADER_REPOSITORY_UTIL.getURLInfo(getRepository(), urlInfo);
            
            URL[] myUrls = urlInfo.getLocalUrls();
            String resource = url.toString();
            for (int i = 0 ; i < myUrls.length ; i++)
            {
               if (resource.indexOf(myUrls[i].toString()) >= 0)
               {
                  return true;
               }
            }
            return false;
         }
      }
      return true;