1 29 30 package com.caucho.loader; 31 32 import com.caucho.jca.UserTransactionProxy; 33 import com.caucho.jmx.Jmx; 34 import com.caucho.log.EnvironmentStream; 35 import com.caucho.management.j2ee.J2EEManagedObject; 36 import com.caucho.management.j2ee.JTAResource; 37 import com.caucho.naming.Jndi; 38 import com.caucho.security.PolicyImpl; 39 import com.caucho.transaction.TransactionManagerImpl; 40 import com.caucho.util.ResinThreadPoolExecutor; 41 import com.caucho.vfs.Vfs; 42 43 import javax.management.MBeanServerFactory ; 44 import java.lang.reflect.Method ; 45 import java.util.ArrayList ; 46 import java.util.Hashtable ; 47 import java.util.Properties ; 48 import java.util.logging.Level ; 49 50 58 public class EnvironmentClassLoader extends DynamicClassLoader { 59 private static boolean _isStaticInit; 60 61 private static EnvironmentLocal<ArrayList <EnvironmentListener>> _childListeners 63 = new EnvironmentLocal<ArrayList <EnvironmentListener>>(); 64 65 private EnvironmentBean _owner; 67 68 private Hashtable <String ,Object > _attributes = 70 new Hashtable <String ,Object >(8); 71 72 private ArrayList <EnvironmentListener> _listeners; 76 private WeakStopListener _stopListener; 77 78 private volatile boolean _isStarted; 80 81 private volatile boolean _isActive; 83 84 private volatile boolean _isStopped; 86 87 private Throwable _configException; 88 89 92 public EnvironmentClassLoader() 93 { 94 super(Thread.currentThread().getContextClassLoader()); 95 96 98 initListeners(); 99 } 100 101 104 public EnvironmentClassLoader(ClassLoader parent) 105 { 106 super(parent); 107 108 110 initListeners(); 111 } 112 113 116 public EnvironmentBean getOwner() 117 { 118 return _owner; 119 } 120 121 124 public void setOwner(EnvironmentBean owner) 125 { 126 _owner = owner; 127 } 128 129 132 public void setConfigException(Throwable e) 133 { 134 if (_configException == null) 135 _configException = e; 136 } 137 138 141 public Throwable getConfigException() 142 { 143 return _configException; 144 } 145 146 149 public boolean isActive() 150 { 151 return _isActive; 152 } 153 154 157 public void init() 158 { 159 initializeEnvironment(); 160 161 super.init(); 162 } 163 164 167 public Object getAttribute(String name) 168 { 169 if (_attributes != null) 170 return _attributes.get(name); 171 else 172 return null; 173 } 174 175 178 public Object setAttribute(String name, Object obj) 179 { 180 if (obj == null) { 181 if (_attributes == null) 182 return null; 183 else 184 return _attributes.remove(name); 185 } 186 187 if (_attributes == null) 188 _attributes = new Hashtable <String ,Object >(8); 189 190 return _attributes.put(name, obj); 191 } 192 193 196 public Object removeAttribute(String name) 197 { 198 if (_attributes == null) 199 return null; 200 else 201 return _attributes.remove(name); 202 } 203 204 207 public void addListener(EnvironmentListener listener) 208 { 209 synchronized (this) { 210 if (_listeners == null) { 211 _listeners = new ArrayList <EnvironmentListener>(); 212 213 initListeners(); 214 } 215 } 216 217 synchronized (_listeners) { 218 for (int i = _listeners.size() - 1; i >= 0; i--) { 219 EnvironmentListener oldListener = _listeners.get(i); 220 221 if (listener == oldListener) { 222 return; 223 } 224 else if (oldListener == null) 225 _listeners.remove(i); 226 } 227 228 _listeners.add(listener); 229 } 230 231 if (_isStarted) { 232 try { 233 listener.environmentStart(this); 234 } catch (RuntimeException e) { 235 throw e; 236 } catch (Throwable e) { 237 throw new StartRuntimeException(e); 238 } 239 } 240 } 241 242 245 private void initListeners() 246 { 247 ClassLoader parent = getParent(); 248 249 for (; parent != null; parent = parent.getParent()) { 250 if (parent instanceof EnvironmentClassLoader) { 251 EnvironmentClassLoader loader = (EnvironmentClassLoader) parent; 252 253 if (_stopListener == null) 254 _stopListener = new WeakStopListener(this); 255 256 loader.addListener(_stopListener); 257 258 return; 259 } 260 } 261 } 262 263 266 public void removeListener(EnvironmentListener listener) 267 { 268 if (_listeners == null) 269 return; 270 271 synchronized (_listeners) { 272 for (int i = _listeners.size() - 1; i >= 0; i--) { 273 EnvironmentListener oldListener = _listeners.get(i); 274 275 if (listener == oldListener) { 276 _listeners.remove(i); 277 return; 278 } 279 else if (oldListener == null) 280 _listeners.remove(i); 281 } 282 } 283 } 284 285 288 void addChildListener(EnvironmentListener listener) 289 { 290 synchronized (_childListeners) { 291 ArrayList <EnvironmentListener> listeners 292 = _childListeners.getLevel(this); 293 294 if (listeners == null) { 295 listeners = new ArrayList <EnvironmentListener>(); 296 297 _childListeners.set(listeners, this); 298 } 299 300 listeners.add(listener); 301 } 302 303 if (_lifecycle.isStarting()) { 304 try { 305 listener.environmentStart(this); 306 } catch (Throwable e) { 307 log().log(Level.WARNING, e.toString(), e); 308 } 309 } 310 } 311 312 315 void removeChildListener(EnvironmentListener listener) 316 { 317 synchronized (_childListeners) { 318 ArrayList <EnvironmentListener> listeners 319 = _childListeners.getLevel(this); 320 321 if (listeners != null) 322 listeners.remove(listener); 323 } 324 } 325 326 329 protected ArrayList <EnvironmentListener> getEnvironmentListeners() 330 { 331 ArrayList <EnvironmentListener> listeners; 332 listeners = new ArrayList <EnvironmentListener>(); 333 334 synchronized (_childListeners) { 336 ClassLoader loader; 337 338 for (loader = this; loader != null; loader = loader.getParent()) { 339 if (loader instanceof EnvironmentClassLoader) { 340 ArrayList <EnvironmentListener> childListeners; 341 childListeners = _childListeners.getLevel(loader); 342 343 if (childListeners != null) 344 listeners.addAll(childListeners); 345 } 346 } 347 } 348 349 if (_listeners == null) 350 return listeners; 351 352 synchronized (_listeners) { 353 for (int i = 0; i < _listeners.size(); i++) { 354 EnvironmentListener listener = _listeners.get(i); 355 356 if (listener != null) 357 listeners.add(listener); 358 else { 359 _listeners.remove(i); 360 i--; 361 } 362 } 363 } 364 365 return listeners; 366 } 367 368 372 public void start() 373 { 374 synchronized (this) { 375 if (_isStarted) 376 return; 377 378 _isStarted = true; 379 } 380 381 try { 382 ArrayList <EnvironmentListener> listeners = getEnvironmentListeners(); 383 384 int size = listeners.size(); 385 for (int i = 0; listeners != null && i < size; i++) { 386 EnvironmentListener listener = listeners.get(i); 387 388 listener.environmentStart(this); 389 } 390 } catch (RuntimeException e) { 391 throw e; 392 } catch (Error e) { 393 throw e; 394 } catch (Throwable e) { 395 throw new StartRuntimeException(e); 396 } finally { 397 _isActive = true; 398 } 399 } 400 401 406 public void stop() 407 { 408 synchronized (this) { 409 if (_isStopped) 410 return; 411 412 _isStopped = true; 413 _isActive = false; 414 } 415 416 ArrayList <EnvironmentListener> listeners = getEnvironmentListeners(); 417 418 Thread thread = Thread.currentThread(); 419 ClassLoader oldLoader = thread.getContextClassLoader(); 420 thread.setContextClassLoader(this); 421 422 try { 423 if (listeners != null) { 425 for (int i = listeners.size() - 1; i >= 0; i--) { 426 EnvironmentListener listener = listeners.get(i); 427 428 try { 429 listener.environmentStop(this); 430 } catch (Throwable e) { 431 e.printStackTrace(); 432 log().log(Level.WARNING, e.toString(), e); 433 } 434 } 435 } 436 } finally { 437 thread.setContextClassLoader(oldLoader); 438 439 ResinThreadPoolExecutor.getThreadPool().stopEnvironment(this); 441 } 442 } 443 444 447 protected void replace(EnvironmentClassLoader source) 448 { 449 super.replace(source); 450 451 _owner = source._owner; 453 _attributes = source._attributes; 454 if (source._listeners != null) { 455 if (_listeners == null) 456 _listeners = new ArrayList <EnvironmentListener>(); 457 _listeners.addAll(source._listeners); 458 source._listeners.clear(); 459 } 460 461 _isStarted = source._isStarted; 462 _isActive = source._isActive; 463 _isStopped = source._isStopped; 464 } 465 466 469 public void destroy() 470 { 471 try { 472 WeakStopListener stopListener = _stopListener; 473 _stopListener = null; 474 475 stop(); 477 478 super.destroy(); 479 480 ClassLoader parent = getParent(); 481 for (; parent != null; parent = parent.getParent()) { 482 if (parent instanceof EnvironmentClassLoader) { 483 EnvironmentClassLoader loader = (EnvironmentClassLoader) parent; 484 485 loader.removeListener(stopListener); 486 } 487 } 488 } finally { 489 _owner = null; 490 _attributes = null; 491 _listeners = null; 492 } 493 } 494 495 public String toString() 496 { 497 if (getId() != null) 498 return "EnvironmentClassLoader$" + System.identityHashCode(this) + "[" + getId() + "]"; 499 else { 500 return "EnvironmentClassLoader$" + System.identityHashCode(this) + "[]"; 502 } 503 } 504 505 508 public static synchronized void initializeEnvironment() 509 { 510 if (_isStaticInit) 511 return; 512 513 _isStaticInit = true; 514 515 Thread thread = Thread.currentThread(); 516 ClassLoader oldLoader = thread.getContextClassLoader(); 517 try { 518 thread.setContextClassLoader(ClassLoader.getSystemClassLoader()); 519 520 ClassLoader loader = thread.getContextClassLoader(); 521 522 PolicyImpl.init(); 523 524 EnvironmentStream.setStdout(System.out); 525 EnvironmentStream.setStderr(System.err); 526 527 try { 528 Vfs.initJNI(); 529 } catch (Throwable e) { 530 } 531 532 if (System.getProperty("org.xml.sax.driver") == null) 533 System.setProperty("org.xml.sax.driver", "com.caucho.xml.Xml"); 534 535 Properties props = System.getProperties(); 536 537 if (props.get("java.util.logging.manager") == null) { 538 props.put("java.util.logging.manager", 539 "com.caucho.log.LogManagerImpl"); 540 } 541 542 if (props.get("java.naming.factory.initial") == null) { 543 props.put("java.naming.factory.initial", 544 "com.caucho.naming.InitialContextFactoryImpl"); 545 } 546 547 props.put("java.naming.factory.url.pkgs", "com.caucho.naming"); 548 549 EnvironmentProperties.enableEnvironmentSystemProperties(true); 550 551 TransactionManagerImpl tm = TransactionManagerImpl.getInstance(); 552 Jndi.bindDeep("java:comp/TransactionManager", tm); 554 555 UserTransactionProxy ut = UserTransactionProxy.getInstance(); 556 Jndi.bindDeep("java:comp/UserTransaction", ut); 557 558 Jndi.bindDeep("java:comp/ThreadPool", 559 ResinThreadPoolExecutor.getThreadPool()); 560 561 String oldBuilder = props.getProperty("javax.management.builder.initial"); 562 if (oldBuilder == null) 563 oldBuilder = "com.caucho.jmx.MBeanServerBuilderImpl"; 564 565 569 570 props.put("javax.management.builder.initial", oldBuilder); 571 572 if (MBeanServerFactory.findMBeanServer(null).size() == 0) 573 MBeanServerFactory.createMBeanServer("Resin"); 574 575 try { 576 Class cl = Class.forName("java.lang.management.ManagementFactory"); 577 Method method = cl.getMethod("getPlatformMBeanServer", new Class [0]); 578 method.invoke(null, new Object [0]); 579 } catch (Throwable e) { 580 } 581 582 Jndi.bindDeep("java:comp/env/jmx/MBeanServer", 583 Jmx.getContextMBeanServer()); 584 Jndi.bindDeep("java:comp/env/jmx/GlobalMBeanServer", 585 Jmx.getGlobalMBeanServer()); 586 587 J2EEManagedObject.register(new JTAResource(tm)); 588 589 } catch (Throwable e) { 590 e.printStackTrace(); 591 } finally { 592 thread.setContextClassLoader(oldLoader); 593 } 594 } 595 } 596 | Popular Tags |