1 16 17 18 package org.apache.tomcat.util.loader; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 import java.net.URLClassLoader ; 25 import java.util.Enumeration ; 26 import java.util.Vector ; 27 28 52 53 65 public class ModuleClassLoader 66 extends URLClassLoader 67 { 68 private static final boolean DEBUG=false; private static final boolean DEBUGNF=false; 74 76 77 public ModuleClassLoader(URL repositories[], ClassLoader parent) { 78 super(repositories, parent); 79 } 80 81 82 public ModuleClassLoader(URL repositories[]) { 83 super(repositories); 84 } 85 86 87 89 protected Repository repository; 90 91 99 protected boolean delegate = false; 100 101 105 protected long lastJarAccessed = 0L; 106 107 110 protected boolean started = false; 111 112 protected Module module; 113 114 116 117 120 126 127 132 void setDelegate(boolean delegate) { 133 this.delegate = delegate; 134 } 135 136 void setRepository(Repository lg ) { 137 this.repository=lg; 138 } 139 140 void setModule(Module webappLoader) { 141 this.module=webappLoader; 142 } 143 144 150 Module getModule() { 151 return module; 152 } 153 154 void setWorkDir(File s) { 155 } 157 158 168 void addRepository(String repository) { 169 try { 171 URL url = new URL (repository); 172 super.addURL(url); 173 } catch (MalformedURLException e) { 174 IllegalArgumentException iae = new IllegalArgumentException 175 ("Invalid repository: " + repository); 176 iae.initCause(e); 177 throw iae; 179 } 180 } 181 182 188 boolean modified() { 189 if (DEBUG) 190 log("modified() false"); 191 192 return (false); 194 } 195 196 198 199 207 public Class findClass(String name) throws ClassNotFoundException { 208 209 Class clazz = null; 210 211 try { 212 clazz = super.findClass(name); 213 } catch (RuntimeException e) { 214 if (DEBUG) 215 log("findClass() -->RuntimeException " + name, e); 216 throw e; 217 } catch( ClassNotFoundException ex ) { 218 if (DEBUGNF) 219 log("findClass() NOTFOUND " + name); 220 throw ex; 221 } 222 223 if (clazz == null) { if (DEBUGNF) 225 log("findClass() NOTFOUND throw CNFE " + name); 226 throw new ClassNotFoundException (name); 227 } 228 229 if (DEBUG) { 231 if( clazz.getClassLoader() != this ) 232 log("findClass() FOUND " + clazz + " Loaded by " + clazz.getClassLoader()); 233 else 234 log("findClass() FOUND " + clazz ); 235 } 236 return (clazz); 237 } 238 239 251 public Class findLocalClass(String name) throws ClassNotFoundException 252 { 253 Class clazz = findLoadedClass(name); 254 if (clazz != null) { 255 if (DEBUG) 256 log("findLocalClass() - FOUND " + name); 257 return (clazz); 258 } 259 260 return findClass(name); 261 } 262 263 264 265 266 273 public URL findResource(final String name) { 274 275 URL url = null; 276 277 url = super.findResource(name); 278 279 if(url==null) { 280 } 283 284 if (url==null && DEBUG) { 285 if (DEBUGNF) log("findResource() NOTFOUND " + name ); 286 return null; 287 } 288 289 if (DEBUG) log("findResource() found " + name + " " + url ); 290 return (url); 291 } 292 293 294 303 public Enumeration findResources(String name) throws IOException { 304 Vector result=new Vector (); 305 306 Enumeration myRes=super.findResources(name); 307 if( myRes!=null ) { 308 while( myRes.hasMoreElements() ) { 309 result.addElement(myRes.nextElement()); 310 } 311 } 312 314 return result.elements(); 315 316 } 317 318 320 323 public URL getResource(String name) { 324 325 URL url = null; 326 327 if (delegate) { 329 url=getResourceParentDelegate(name); 330 if(url!=null ) return url; 331 } 332 333 url = findResource(name); 335 if (url != null) { 336 if (DEBUG) 338 log("getResource() found locally " + delegate + " " + name + " " + url); 339 return (url); 340 } 341 342 if( repository!=null ) { 345 url=repository.findResource(this, name); 346 if(url!=null ) { 347 if( DEBUG ) 348 log("getResource() FOUND from group " + repository.getName() + " " + name + " " + url); 349 return url; 350 } 351 } 352 353 if( !delegate ) { 355 url=getResourceParentDelegate(name); 356 if(url!=null ) return url; 357 } 358 359 360 if (DEBUGNF) 362 log("getResource() NOTFOUND " + delegate + " " + name + " " + url); 363 return (null); 364 365 } 366 367 private URL getResourceParentDelegate(String name) { 369 URL url=null; 370 ClassLoader loader = getParent(); 371 372 if (loader == null) { 373 loader = getSystemClassLoader(); 374 if (url != null) { 375 if (DEBUG) 376 log("getResource() found by system " + delegate + " " + name + " " + url); 377 return (url); 378 } 379 } else { 380 url = loader.getResource(name); 381 if (url != null) { 382 if (DEBUG) 383 log("getResource() found by parent " + delegate + " " + name + " " + url); 384 return (url); 385 } 386 } 387 388 return url; 389 } 390 391 416 public Class loadClass(String name, boolean resolve) 417 throws ClassNotFoundException 418 { 419 420 Class clazz = null; 421 422 if (!started) { 424 throw new ThreadDeath (); 426 } 427 428 clazz = findLoadedClass(name); 430 if (clazz != null) { 431 if (DEBUG) 432 log("loadClass() FOUND findLoadedClass " + name + " , " + resolve); 433 if (resolve) resolveClass(clazz); 434 return (clazz); 435 } 436 437 try { 440 clazz = getSystemClassLoader().loadClass(name); 441 if (clazz != null) { 442 if (resolve) resolveClass(clazz); 446 return (clazz); 447 } 448 } catch (ClassNotFoundException e) { 449 } 451 452 boolean delegateLoad = delegate; 455 if (delegateLoad) { 457 458 ClassLoader loader = getParent(); 459 if( loader != null ) { 460 try { 461 clazz = loader.loadClass(name); 462 if (clazz != null) { 463 if (DEBUG) 464 log("loadClass() FOUND by parent " + delegate + " " + name + " , " + resolve); 465 if (resolve) 466 resolveClass(clazz); 467 return (clazz); 468 } 469 } catch (ClassNotFoundException e) { 470 ; 471 } 472 } 473 } 474 475 try { 477 clazz = findClass(name); 478 if (clazz != null) { 479 if (resolve) resolveClass(clazz); 482 return (clazz); 483 } 484 } catch (ClassNotFoundException e) { 485 ; 486 } 487 488 if( repository!=null ) { 491 Class cls=repository.findClass(this, name); 492 if(cls!=null ) { 493 if( DEBUG ) 494 log("loadClass(): FOUND from group " + repository.getName() + " " + name); 495 if (resolve) resolveClass(clazz); 496 return cls; 497 } 498 } 499 500 if (!delegateLoad) { 502 ClassLoader loader = getParent(); 503 if( loader != null ) { 504 try { 505 clazz = loader.loadClass(name); 506 if (clazz != null) { 507 if (DEBUG) 508 log("loadClass() FOUND parent " + delegate + " " + name + " , " + resolve); 509 if (resolve) resolveClass(clazz); 510 return (clazz); 511 } 512 } catch (ClassNotFoundException e) { 513 ; 514 } 515 } 516 } 517 518 if( DEBUGNF ) log("loadClass(): NOTFOUND " + name ); 519 throw new ClassNotFoundException (name); 520 } 521 522 523 525 526 527 532 void start() { 533 534 started = true; 535 536 } 537 538 542 boolean isStarted() { 543 return started; 544 } 545 546 547 552 void stop() { 553 554 started = false; 555 556 } 557 558 559 560 561 571 protected boolean validate(String name) { 572 573 if (name == null) 574 return false; 575 if (name.startsWith("java.")) 576 return false; 577 578 return true; 579 580 } 581 582 583 585 private void log(String s ) { 586 System.err.println("ModuleCL: " + s); 587 } 588 private void log(String s, Throwable t ) { 589 System.err.println("ModuleCL: " + s); 590 t.printStackTrace(); 591 } 592 593 Object debugObj=new Object (); 594 595 598 public String toString() { 599 600 StringBuffer sb = new StringBuffer ("ModuleCL "); 601 sb.append(debugObj).append(" delegate: "); 602 sb.append(delegate); 603 sb.append(" cp: "); 605 URL cp[]=super.getURLs(); 606 if (cp != null ) { 607 for (int i = 0; i <cp.length; i++) { 608 sb.append(" "); 609 sb.append(cp[i].getFile()); 610 } 611 } 612 if (getParent() != null) { 613 sb.append("\r\n----------> Parent: "); 614 sb.append(getParent().toString()); 615 sb.append("\r\n"); 616 } 617 return (sb.toString()); 618 } 619 } 620 621 | Popular Tags |