1 22 package org.jboss.mx.loading; 23 24 import java.net.URL ; 25 import java.security.CodeSource ; 26 import java.security.Permission ; 27 import java.security.PermissionCollection ; 28 import java.security.Policy ; 29 import java.security.ProtectionDomain ; 30 import java.util.Enumeration ; 31 32 import javax.management.MBeanServer ; 33 import javax.management.MalformedObjectNameException ; 34 import javax.management.ObjectName ; 35 36 import org.jboss.logging.Logger; 37 import org.jboss.util.loading.Translatable; 38 39 58 public class UnifiedClassLoader extends RepositoryClassLoader 59 implements UnifiedClassLoaderMBean, Translatable 60 { 61 63 private static final Logger log = Logger.getLogger(UnifiedClassLoader.class); 64 65 67 68 protected URL url = null; 69 71 protected URL origURL = null; 72 73 80 public UnifiedClassLoader(URL url) 81 { 82 this(url, (URL ) null); 83 } 84 92 public UnifiedClassLoader(URL url, URL origURL) 93 { 94 this(url, origURL, UnifiedClassLoader.class.getClassLoader()); 95 } 96 97 105 public UnifiedClassLoader(URL url, URL origURL, ClassLoader parent) 106 { 107 super(url == null ? new URL []{} : new URL [] {url}, parent); 108 109 if (log.isTraceEnabled()) 110 log.trace("New jmx UCL with url " + url); 111 this.url = url; 112 this.origURL = origURL; 113 } 114 115 122 public UnifiedClassLoader(URL url, LoaderRepository repository) 123 { 124 this(url, null, repository); 125 } 126 135 public UnifiedClassLoader(URL url, URL origURL, LoaderRepository repository) 136 { 137 this(url, origURL); 138 139 this.setRepository(repository); 141 142 repository.addClassLoader(this); 144 } 145 146 155 public UnifiedClassLoader(final URL url, final MBeanServer server, 156 final ObjectName repositoryName) throws Exception 157 { 158 this(url, null, server, repositoryName); 159 } 160 171 public UnifiedClassLoader(final URL url, final URL origURL, 172 final MBeanServer server, final ObjectName repositoryName) throws Exception 173 { 174 this(url, origURL); 175 LoaderRepository rep = (LoaderRepository)server.invoke(repositoryName, 176 "registerClassLoader", 177 new Object [] {this}, 178 new String [] {getClass().getName()}); 179 this.setRepository(rep); 180 } 181 182 184 188 public ObjectName getObjectName() throws MalformedObjectNameException 189 { 190 String name = "jmx.loading:UCL="+Integer.toHexString(super.hashCode()); 191 return new ObjectName (name); 192 } 193 194 public void unregister() 195 { 196 super.unregister(); 197 this.origURL = null; 198 this.url = null; 199 } 200 201 203 public URL getURL() 204 { 205 return url; 206 } 207 208 210 public URL getOrigURL() 211 { 212 return origURL; 213 } 214 215 public synchronized Class loadClassImpl(String name, boolean resolve, int stopAt) 216 throws ClassNotFoundException 217 { 218 loadClassDepth ++; 219 boolean trace = log.isTraceEnabled(); 220 221 if( trace ) 222 log.trace("loadClassImpl, name="+name+", resolve="+resolve); 223 if( repository == null ) 224 { 225 try 227 { 228 return super.loadClass(name, resolve); 229 } 230 catch (ClassNotFoundException ignored) 231 { 232 } 233 String msg = "Invalid use of destroyed classloader, UCL destroyed at:"; 234 throw new ClassNotFoundException (msg, this.unregisterTrace); 235 } 236 237 242 boolean acquired = attempt(1); 243 while( acquired == false ) 244 { 245 249 try 250 { 251 if( trace ) 252 log.trace("Waiting for loadClass lock"); 253 this.wait(); 254 } 255 catch(InterruptedException ignore) 256 { 257 } 258 acquired = attempt(1); 259 } 260 261 ClassLoadingTask task = null; 262 try 263 { 264 Thread t = Thread.currentThread(); 265 if( loadLock.holds() == 1 ) 267 LoadMgr3.registerLoaderThread(this, t); 268 269 task = new ClassLoadingTask(name, this, t, stopAt); 271 274 UnifiedLoaderRepository3 ulr3 = (UnifiedLoaderRepository3) repository; 275 if( LoadMgr3.beginLoadTask(task, ulr3) == false ) 276 { 277 while( task.threadTaskCount != 0 ) 278 { 279 try 280 { 281 LoadMgr3.nextTask(t, task, ulr3); 282 } 283 catch(InterruptedException e) 284 { 285 break; 287 } 288 } 289 } 290 } 291 finally 292 { 293 if( loadLock.holds() == 1 ) 295 LoadMgr3.endLoadTask(task); 296 this.release(); 298 this.notifyAll(); 299 loadClassDepth --; 300 } 301 302 if( task.loadedClass == null ) 303 { 304 if( task.loadException instanceof ClassNotFoundException ) 305 throw (ClassNotFoundException ) task.loadException; 306 else if( task.loadException instanceof NoClassDefFoundError ) 307 throw (NoClassDefFoundError ) task.loadException; 308 else if( task.loadException != null ) 309 { 310 if( log.isTraceEnabled() ) 311 log.trace("Unexpected error during load of:"+name, task.loadException); 312 String msg = "Unexpected error during load of: "+name 313 + ", msg="+task.loadException.getMessage(); 314 ClassNotFoundException cnfe = new ClassNotFoundException (msg, task.loadException); 315 throw cnfe; 316 } 317 else 319 throw new IllegalStateException ("ClassLoadingTask.loadedTask is null, name: "+name); 320 } 321 322 return task.loadedClass; 323 } 324 325 327 341 protected PermissionCollection getPermissions(CodeSource cs) 342 { 343 CodeSource permCS = cs; 344 if( origURL != null ) 345 { 346 permCS = new CodeSource (origURL, cs.getCertificates()); 347 } 348 Policy policy = Policy.getPolicy(); 349 PermissionCollection perms = super.getPermissions(permCS); 350 PermissionCollection perms2 = super.getPermissions(cs); 351 PermissionCollection perms3 = policy.getPermissions(permCS); 352 Enumeration iter = perms2.elements(); 353 while( iter.hasMoreElements() ) 354 perms.add((Permission ) iter.nextElement()); 355 iter = perms3.elements(); 356 while( iter.hasMoreElements() ) 357 perms.add((Permission ) iter.nextElement()); 358 if( log.isTraceEnabled() ) 359 log.trace("getPermissions, url="+url+", origURL="+origURL+" -> "+perms); 360 return perms; 361 } 362 363 369 protected ProtectionDomain getProtectionDomain() 370 { 371 return getProtectionDomain(origURL != null ? origURL : url); 372 } 373 } 374 | Popular Tags |