1 29 30 package com.caucho.loader; 31 32 import com.caucho.vfs.Depend; 33 import com.caucho.vfs.Dependency; 34 import com.caucho.vfs.Path; 35 36 import java.security.Permission ; 37 import java.util.ArrayList ; 38 39 42 public class Environment { 43 private static ArrayList <EnvironmentListener> _globalEnvironmentListeners = 44 new ArrayList <EnvironmentListener>(); 45 46 private static ArrayList <ClassLoaderListener> _globalLoaderListeners = 47 new ArrayList <ClassLoaderListener>(); 48 49 52 public static EnvironmentClassLoader getEnvironmentClassLoader() 53 { 54 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 55 56 for (; loader != null; loader = loader.getParent()) { 57 if (loader instanceof EnvironmentClassLoader) 58 return (EnvironmentClassLoader) loader; 59 } 60 61 return null; 62 } 63 64 69 public static void addEnvironmentListener(EnvironmentListener listener) 70 { 71 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 72 73 addEnvironmentListener(listener, loader); 74 } 75 76 82 public static void addEnvironmentListener(EnvironmentListener listener, 83 ClassLoader loader) 84 { 85 for (; loader != null; loader = loader.getParent()) { 86 if (loader instanceof EnvironmentClassLoader) { 87 ((EnvironmentClassLoader) loader).addListener(listener); 88 return; 89 } 90 } 91 92 _globalEnvironmentListeners.add(listener); 93 } 94 95 100 public static void removeEnvironmentListener(EnvironmentListener listener) 101 { 102 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 103 104 removeEnvironmentListener(listener, loader); 105 } 106 107 113 public static void removeEnvironmentListener(EnvironmentListener listener, 114 ClassLoader loader) 115 { 116 for (; loader != null; loader = loader.getParent()) { 117 if (loader instanceof EnvironmentClassLoader) { 118 ((EnvironmentClassLoader) loader).removeListener(listener); 119 return; 120 } 121 } 122 123 _globalEnvironmentListeners.remove(listener); 124 } 125 126 132 public static void addChildEnvironmentListener(EnvironmentListener listener) 133 { 134 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 135 136 addChildEnvironmentListener(listener, loader); 137 } 138 139 145 public static void addChildEnvironmentListener(EnvironmentListener listener, 146 ClassLoader loader) 147 { 148 for (; loader != null; loader = loader.getParent()) { 149 if (loader instanceof EnvironmentClassLoader) { 150 ((EnvironmentClassLoader) loader).addChildListener(listener); 151 return; 152 } 153 } 154 } 155 156 161 public static void addClassLoaderListener(ClassLoaderListener listener) 162 { 163 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 164 165 addClassLoaderListener(listener, loader); 166 } 167 168 174 public static void addClassLoaderListener(ClassLoaderListener listener, 175 ClassLoader loader) 176 { 177 for (; loader != null; loader = loader.getParent()) { 178 if (loader instanceof EnvironmentClassLoader) { 179 ((EnvironmentClassLoader) loader).addListener(listener); 180 return; 181 } 182 } 183 184 _globalLoaderListeners.add(listener); 185 } 186 187 193 public static void addCloseListener(Object obj) 194 { 195 addClassLoaderListener(new CloseListener(obj)); 196 } 197 198 201 public static void init() 202 { 203 init(Thread.currentThread().getContextClassLoader()); 204 } 205 206 209 public static void init(ClassLoader loader) 210 { 211 for (; loader != null; loader = loader.getParent()) { 212 if (loader instanceof EnvironmentClassLoader) { 213 ((EnvironmentClassLoader) loader).init(); 214 return; 215 } 216 } 217 218 for (int i = 0; i < _globalLoaderListeners.size(); i++) { 219 ClassLoaderListener listener = _globalLoaderListeners.get(i); 220 221 listener.classLoaderInit(null); 222 } 223 } 224 225 228 public static void start() 229 throws Throwable 230 { 231 start(Thread.currentThread().getContextClassLoader()); 232 } 233 234 237 public static void start(ClassLoader loader) 238 throws Throwable 239 { 240 for (; loader != null; loader = loader.getParent()) { 241 if (loader instanceof EnvironmentClassLoader) { 242 ((EnvironmentClassLoader) loader).start(); 243 return; 244 } 245 } 246 247 init(loader); 248 249 for (int i = 0; i < _globalEnvironmentListeners.size(); i++) { 250 EnvironmentListener listener = _globalEnvironmentListeners.get(i); 251 252 listener.environmentStart(null); 253 } 254 } 255 256 259 public static void stop() 260 { 261 stop(Thread.currentThread().getContextClassLoader()); 262 } 263 264 267 public static void stop(ClassLoader loader) 268 { 269 for (; loader != null; loader = loader.getParent()) { 270 if (loader instanceof EnvironmentClassLoader) { 271 ((EnvironmentClassLoader) loader).stop(); 272 return; 273 } 274 } 275 276 ArrayList <EnvironmentListener> listeners; 277 listeners = new ArrayList <EnvironmentListener>(); 278 listeners.addAll(_globalEnvironmentListeners); 279 _globalEnvironmentListeners.clear(); 280 281 for (int i = 0; i < listeners.size(); i++) { 282 EnvironmentListener listener = listeners.get(i); 283 284 listener.environmentStop(null); 285 } 286 } 287 288 293 public static void addDependency(Dependency depend) 294 { 295 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 296 297 addDependency(depend, loader); 298 } 299 300 306 public static void addDependency(Dependency depend, 307 ClassLoader loader) 308 { 309 for (; loader != null; loader = loader.getParent()) { 310 if (loader instanceof EnvironmentClassLoader) { 311 ((EnvironmentClassLoader) loader).addDependency(depend); 312 return; 313 } 314 } 315 } 316 317 320 public static DynamicClassLoader getDynamicClassLoader() 321 { 322 Thread thread = Thread.currentThread(); 323 324 return getDynamicClassLoader(thread.getContextClassLoader()); 325 } 326 327 332 public static DynamicClassLoader getDynamicClassLoader(ClassLoader loader) 333 { 334 for (; loader != null; loader = loader.getParent()) { 335 if (loader instanceof DynamicClassLoader) { 336 return (DynamicClassLoader) loader; 337 } 338 } 339 340 return null; 341 } 342 343 348 public static void addDependency(Path path) 349 { 350 addDependency(new Depend(path)); 351 } 352 353 359 public static void addDependency(Path path, ClassLoader loader) 360 { 361 addDependency(new Depend(path), loader); 362 } 363 364 371 public static Object getAttribute(String name) 372 { 373 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 374 375 return getAttribute(name, loader); 376 } 377 378 381 public static long getDependencyCheckInterval() 382 { 383 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 384 385 for (; loader != null; loader = loader.getParent()) { 386 if (loader instanceof DynamicClassLoader) 387 return ((DynamicClassLoader) loader).getDependencyCheckInterval(); 388 } 389 390 return DynamicClassLoader.getGlobalDependencyCheckInterval(); 391 } 392 393 396 public static long getDependencyCheckInterval(ClassLoader loader) 397 { 398 for (; loader != null; loader = loader.getParent()) { 399 if (loader instanceof DynamicClassLoader) 400 return ((DynamicClassLoader) loader).getDependencyCheckInterval(); 401 } 402 403 return DynamicClassLoader.getGlobalDependencyCheckInterval(); 404 } 405 406 414 public static Object getAttribute(String name, ClassLoader loader) 415 { 416 for (; loader != null; loader = loader.getParent()) { 417 if (loader instanceof EnvironmentClassLoader) { 418 Object value = ((EnvironmentClassLoader) loader).getAttribute(name); 419 420 if (value != null) 421 return value; 422 } 423 } 424 425 return null; 426 } 427 428 435 public static Object getLevelAttribute(String name) 436 { 437 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 438 439 return getLevelAttribute(name, loader); 440 } 441 442 450 public static Object getLevelAttribute(String name, ClassLoader loader) 451 { 452 for (; loader != null; loader = loader.getParent()) { 453 if (loader instanceof EnvironmentClassLoader) { 454 return ((EnvironmentClassLoader) loader).getAttribute(name); 455 } 456 } 457 458 return null; 459 } 460 461 469 public static Object setAttribute(String name, Object value) 470 { 471 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 472 473 return setAttribute(name, value, loader); 474 } 475 476 485 public static Object setAttribute(String name, 486 Object value, 487 ClassLoader loader) 488 { 489 for (; loader != null; loader = loader.getParent()) { 490 if (loader instanceof EnvironmentClassLoader) { 491 EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader; 492 493 Object oldValue = envLoader.getAttribute(name); 494 495 envLoader.setAttribute(name, value); 496 497 if (oldValue != null) 498 return oldValue; 499 } 500 } 501 502 return null; 503 } 504 505 512 public static void addPermission(Permission perm) 513 { 514 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 515 516 addPermission(perm, loader); 517 } 518 519 526 public static void addPermission(Permission perm, ClassLoader loader) 527 { 528 for (; loader != null; loader = loader.getParent()) { 529 if (loader instanceof EnvironmentClassLoader) { 530 EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader; 531 532 envLoader.addPermission(perm); 533 } 534 } 535 } 536 537 540 public static Object getOwner() 541 { 542 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 543 544 return getOwner(loader); 545 } 546 547 550 public static Object getOwner(ClassLoader loader) 551 { 552 for (; loader != null; loader = loader.getParent()) { 553 if (loader instanceof EnvironmentClassLoader) { 554 EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader; 555 556 Object owner = envLoader.getOwner(); 557 558 if (owner != null) 559 return owner; 560 } 561 } 562 563 return null; 564 } 565 566 569 public static void setConfigException(Throwable e) 570 { 571 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 572 573 for (; loader != null; loader = loader.getParent()) { 574 if (loader instanceof EnvironmentClassLoader) { 575 EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader; 576 577 envLoader.setConfigException(e); 578 579 return; 580 } 581 } 582 } 583 584 587 public static Throwable getConfigException() 588 { 589 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 590 591 for (; loader != null; loader = loader.getParent()) { 592 if (loader instanceof EnvironmentClassLoader) { 593 EnvironmentClassLoader envLoader = (EnvironmentClassLoader) loader; 594 595 if (envLoader.getConfigException() != null) 596 return envLoader.getConfigException(); 597 } 598 } 599 600 return null; 601 } 602 603 606 public static void closeGlobal() 607 { 608 ArrayList <ClassLoaderListener> listeners; 609 listeners = new ArrayList <ClassLoaderListener>(); 610 listeners.addAll(_globalLoaderListeners); 611 _globalLoaderListeners.clear(); 612 613 for (int i = 0; i < listeners.size(); i++) { 614 ClassLoaderListener listener = listeners.get(i); 615 616 listener.classLoaderDestroy(null); 617 } 618 } 619 } 620 | Popular Tags |