1 16 17 18 package org.apache.tomcat.util.loader; 19 20 21 import java.io.File ; 22 import java.lang.reflect.Method ; 23 import java.net.MalformedURLException ; 24 import java.net.URL ; 25 import java.util.ArrayList ; 26 import java.util.Enumeration ; 27 import java.util.Hashtable ; 28 import java.util.StringTokenizer ; 29 import java.util.Vector ; 30 31 32 45 public final class Loader { 46 47 private static final boolean DEBUG=true; private static final boolean FLAT=false; 50 52 53 protected static final String CATALINA_HOME_TOKEN = "${catalina.home}"; 54 protected static final String CATALINA_BASE_TOKEN = "${catalina.base}"; 55 56 57 59 60 63 private static Loader daemon = null; 64 65 ModuleListener listener; 67 68 69 71 protected Repository commonRepository = null; 72 protected Repository catalinaRepository = null; 73 protected Repository sharedRepository = null; 74 75 protected ClassLoader catalinaLoader = null; 76 private String [] args; 77 private Hashtable repositories=new Hashtable (); 78 79 80 81 83 84 private void initClassLoaders() { 85 try { 86 commonRepository = initRepository("common", null); 87 catalinaRepository = initRepository("server", commonRepository); 88 catalinaLoader = catalinaRepository.getClassLoader(); 89 sharedRepository = initRepository("shared", commonRepository); 90 } catch (Throwable t) { 91 log("Class loader creation threw exception", t); 92 System.exit(1); 93 } 94 } 95 96 public Repository createRepository(String name, Repository parent) { 97 Repository lg=new Repository(this); 98 99 lg.setName(name); 100 101 lg.setParent( parent ); 102 103 repositories.put(name, lg); 104 105 if( listener != null ) { 106 listener.repositoryAdd(lg); 107 } 108 return lg; 109 } 110 111 public Enumeration getRepositoryNames() { 112 return repositories.keys(); 113 } 114 115 123 private Repository initRepository(String name, Repository parent) 124 throws Exception 125 { 126 String value = LoaderProperties.getProperty(name + ".loader"); 127 128 Repository lg=createRepository(name, parent ); 129 if( DEBUG ) log( "Creating loading group " + name + " - " + value); 130 131 if ((value == null) || (value.equals(""))) 132 return lg; 133 134 ArrayList unpackedList = new ArrayList (); 135 ArrayList packedList = new ArrayList (); 136 ArrayList urlList = new ArrayList (); 137 138 Vector repo=split( value ); 139 Enumeration elems=repo.elements(); 140 while (elems.hasMoreElements()) { 141 String repository = (String )elems.nextElement(); 142 143 boolean packed = false; 145 if (repository.startsWith(CATALINA_HOME_TOKEN)) { 146 repository = LoaderProperties.getCatalinaHome() 147 + repository.substring(CATALINA_HOME_TOKEN.length()); 148 } else if (repository.startsWith(CATALINA_BASE_TOKEN)) { 149 repository = LoaderProperties.getCatalinaBase() 150 + repository.substring(CATALINA_BASE_TOKEN.length()); 151 } 152 153 try { 155 urlList.add(new URL (repository)); 156 continue; 157 } catch (MalformedURLException e) { 158 } 160 161 if (repository.endsWith("*.jar")) { 162 packed = true; 163 repository = repository.substring 164 (0, repository.length() - "*.jar".length()); 165 } 166 if (packed) { 167 packedList.add(new File (repository)); 168 } else { 169 unpackedList.add(new File (repository)); 170 } 171 } 172 173 File [] unpacked = (File []) unpackedList.toArray(new File [0]); 174 File [] packed = (File []) packedList.toArray(new File [0]); 175 URL [] urls = (URL []) urlList.toArray(new URL [0]); 176 177 initRepository(lg, unpacked, packed, urls, parent); 180 181 188 189 return lg; 191 } 192 193 200 private void processCLI() { 201 if( args.length > 0 && 202 (args[0].toLowerCase().endsWith(".tomcat") || 203 args[0].toLowerCase().endsWith("loader.properties") )) { 204 String props=args[0]; 205 String args2[]=new String [args.length-1]; 206 System.arraycopy(args, 1, args2, 0, args2.length); 207 args=args2; 208 LoaderProperties.setPropertiesFile(props); 209 } 210 } 211 212 219 public void main() 220 throws Exception 221 { 222 processCLI(); 223 224 LoaderProperties.setCatalinaHome(); 226 LoaderProperties.setCatalinaBase(); 227 228 initClassLoaders(); 229 230 Thread.currentThread().setContextClassLoader(catalinaLoader); 231 232 securityPreload(catalinaLoader); 233 234 240 String startupClasses=LoaderProperties.getProperty("loader.auto-startup", 241 "org.apache.catalina.startup.CatalinaModuleListener"); 242 Vector v=split( startupClasses ); 243 244 for( int i=0; i<v.size(); i++ ) { 245 String startupCls=(String )v.elementAt(i); 246 247 if (DEBUG) 248 log("Loading startup class " + startupCls); 249 250 Class startupClass = 251 catalinaLoader.loadClass(startupCls); 252 253 Object startupInstance = startupClass.newInstance(); 254 255 if( startupInstance instanceof ModuleListener ) { 256 addModuleListener((ModuleListener)startupInstance); 257 258 listener.setLoader(this); 260 261 listener.start(); 264 } else { 265 Class paramTypes[] = new Class [0]; 266 Object paramValues[] = new Object [0]; 267 Method method = 268 startupInstance.getClass().getMethod("execute", paramTypes); 269 if( method==null ) 270 method = startupInstance.getClass().getMethod("start", paramTypes); 271 method.invoke(startupInstance, paramValues); 272 } 273 274 } 275 } 276 277 public Repository getRepository( String name ) { 278 return (Repository)repositories.get(name); 279 } 280 281 private static void securityPreload(ClassLoader loader) 282 throws Exception { 283 284 if( System.getSecurityManager() == null ){ 285 return; 286 } 287 288 String value=LoaderProperties.getProperty("security.preload"); 289 Vector repo=split( value ); 290 Enumeration elems=repo.elements(); 291 while (elems.hasMoreElements()) { 292 String classN = (String )elems.nextElement(); 293 try { 294 loader.loadClass( classN); 295 } catch( Throwable t ) { 296 } 298 } 299 } 300 301 303 public String [] getArgs() { 304 return args; 305 } 306 307 312 public static void main(String args[]) { 313 314 try { 315 if (daemon == null) { 316 daemon = new Loader(); 317 daemon.args=args; 318 319 try { 320 daemon.main(); 321 } catch (Throwable t) { 322 t.printStackTrace(); 323 return; 324 } 325 } 326 } catch (Throwable t) { 327 t.printStackTrace(); 328 } 329 330 } 331 332 public void setCatalinaHome(String s) { 333 System.setProperty( "catalina.home", s ); 334 } 335 336 public void setCatalinaBase(String s) { 337 System.setProperty( "catalina.base", s ); 338 } 339 340 346 public Module getModule(ClassLoader cl ) { 347 if( cl instanceof ModuleClassLoader ) { 348 return ((ModuleClassLoader)cl).getModule(); 349 } 350 return null; 351 } 352 353 372 private void initRepository(Repository lg, File unpacked[], 373 File packed[], URL urls[], Repository parent) 374 throws Exception 375 { 376 StringBuffer sb=new StringBuffer (); 377 378 ArrayList list = new ArrayList (); 380 381 if (unpacked != null) { 383 for (int i = 0; i < unpacked.length; i++) { 384 File file = unpacked[i]; 385 if (!file.exists() || !file.canRead()) { 386 if (DEBUG) 387 log(" Not found: "+ file.getAbsolutePath()); 388 continue; 389 } 390 if (DEBUG) 391 sb.append(" "+ file.getAbsolutePath()); 392 String cPath=file.getCanonicalPath(); 393 URL url=null; 394 if( cPath.toLowerCase().endsWith(".jar") || 395 cPath.toLowerCase().endsWith(".zip") ) { 396 url = new URL ("file", null, cPath); 397 } else { 398 url = new URL ("file", null, cPath + File.separator); 399 } 400 if( ! FLAT ) { 401 addLoader(lg, parent, new URL [] { url }); 402 } else { 403 list.add(url); 404 } 405 } 406 } 407 408 if (packed != null) { 410 for (int i = 0; i < packed.length; i++) { 411 File directory = packed[i]; 412 if (!directory.isDirectory() || !directory.exists() || 413 !directory.canRead()) { 414 if (DEBUG) 415 log(" Not found: "+ directory.getAbsolutePath()); 416 continue; 417 } 418 String filenames[] = directory.list(); 419 for (int j = 0; j < filenames.length; j++) { 420 String filename = filenames[j].toLowerCase(); 421 if (!filename.endsWith(".jar")) 422 continue; 423 File file = new File (directory, filenames[j]); 424 if (DEBUG) 425 sb.append(" "+ file.getAbsolutePath()); 426 URL url = new URL ("file", null, 427 file.getCanonicalPath()); 428 if( ! FLAT ) { 429 addLoader(lg, parent, new URL [] { url }); 430 } else { 431 list.add(url); 432 } 433 } 434 } 435 } 436 437 if (urls != null) { 439 for (int i = 0; i < urls.length; i++) { 440 if( ! FLAT ) { 441 addLoader(lg, parent, new URL [] { urls[i] }); 442 } else { 443 list.add(urls[i]); 444 } 445 if (DEBUG) 446 sb.append(" "+ urls[i]); 447 } 448 } 449 450 452 if (DEBUG) 454 log("Creating new class loader " + lg.getName() + " " + sb.toString()); 455 456 457 URL [] array = (URL []) list.toArray(new URL [list.size()]); 458 if( array.length > 0 ) { 459 addLoader(lg, parent, array); 460 } 461 } 462 463 468 private void addLoader(Repository lg, Repository parent, URL array[]) 469 throws Exception 470 { 471 Module module=new Module(); 472 473 module.setParent( parent ); 474 module.setClasspath( array ); 475 476 lg.addModule(module); 477 478 } 479 480 private static Vector split( String value ) { 481 Vector result=new Vector (); 482 StringTokenizer tokenizer = new StringTokenizer (value, ","); 483 while (tokenizer.hasMoreElements()) { 484 String repository = tokenizer.nextToken(); 485 repository.trim(); 486 if( ! "".equals(repository) ) 487 result.addElement(repository); 488 } 489 return result; 490 } 491 492 void notifyModuleStart(Module module) { 493 if(listener!=null) listener.moduleStart(module); 494 } 495 496 void notifyModuleStop(Module module) { 497 listener.moduleStop(module); 498 } 499 500 508 public void addModuleListener(ModuleListener listener) { 509 this.listener=listener; 510 } 511 512 private static void log(String s) { 513 System.err.println("Main: " + s); 514 } 515 516 private static void log( String msg, Throwable t ) { 517 System.err.println("Main: " + msg); 518 t.printStackTrace(); 519 } 520 521 } 522 | Popular Tags |