1 22 package org.jboss.aop.deployment; 23 24 import java.io.File ; 25 import java.lang.ref.WeakReference ; 26 import java.net.URL ; 27 import java.util.Iterator ; 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 48 public class ScopedJBossClassPool extends JBossClassPool 49 { 50 final static LoaderRepositoryUrlUtil LOADER_REPOSITORY_UTIL = new LoaderRepositoryUrlUtil(); 51 52 WeakReference repository = null; 53 UrlInfo urlInfo; 54 55 protected ScopedJBossClassPool(ClassLoader cl, ClassPool src, ScopedClassPoolRepository repository, File tmp, URL tmpURL) 56 { 57 super(cl, src, repository, tmp, tmpURL); 58 59 boolean parentFirst = false; 60 LoaderRepository loaderRepository = null; 61 ClassLoader 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 ClassLoader cl = null; 84 try 85 { 86 cl = getClassLoader(); 87 } 88 catch (RuntimeException e) 89 { 90 } 92 if (cl != null) 93 { 94 return (HeirarchicalLoaderRepository3)((RepositoryClassLoader)cl).getLoaderRepository(); 95 } 96 return null; 97 } 98 99 private URL getResourceUrlForClass(String resourcename) 100 { 101 HeirarchicalLoaderRepository3 repo = getRepository(); 102 return repo.getResource(resourcename, super.getClassLoader()); 103 } 104 105 private boolean isMine(URL url) 106 { 107 HeirarchicalLoaderRepository3 repo = getRepository(); 108 if (repo != null) 109 { 110 if (url != null) 112 { 113 urlInfo = LOADER_REPOSITORY_UTIL.getURLInfo(getRepository(), urlInfo); 114 115 URL [] myUrls = urlInfo.getLocalUrls(); 116 String 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 classname) 131 { 132 if (classname == null) 133 { 134 return null; 135 } 136 137 if (generatedClasses.get(classname) != null) 138 { 139 return super.getCached(classname); 141 } 142 143 String resourcename = getResourceName(classname); 145 URL url = getResourceUrlForClass(resourcename); 146 boolean isMine = isMine(url); 147 148 if (isMine) 149 { 150 if (super.childFirstLookup) 151 { 152 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 url) 183 { 184 synchronized(AspectManager.getRegisteredCLs()) 185 { 186 for(Iterator 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 RepositoryClassLoader rcl = (RepositoryClassLoader)candidate.getClassLoader(); 199 URL [] urls = rcl.getClasspath(); 200 String 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 |