environmentclassloader.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 897 行 · 第 1/2 页

JAVA
897
字号
        _addLoaderListeners.set(listeners, this);      }      if (! listeners.contains(listener))	listeners.add(listener);    }    listener.addLoader(this);  }  /**   * Returns the listeners.   */  protected ArrayList<AddLoaderListener> getLoaderListeners()  {    ArrayList<AddLoaderListener> listeners;    listeners = new ArrayList<AddLoaderListener>();    // add the descendent listeners    synchronized (_addLoaderListeners) {      ClassLoader loader;      for (loader = this; loader != null; loader = loader.getParent()) {        if (loader instanceof EnvironmentClassLoader) {          ArrayList<AddLoaderListener> childListeners;          childListeners = _addLoaderListeners.getLevel(loader);          if (childListeners != null)            listeners.addAll(childListeners);        }      }    }    return listeners;  }  /**   * Adds a listener to detect class loader changes.   */  @Override  protected void configureEnhancerEvent()  {    ArrayList<AddLoaderListener> listeners = getLoaderListeners();        for (int i = 0;	 listeners != null && i < listeners.size();	 i++) {      listeners.get(i).addLoader(this);    }  }  /**   * Adds the URL to the URLClassLoader.   */  @Override  public void addURL(URL url)  {    super.addURL(url);    _pendingScanUrls.add(url);  }  /**   * Adds a scan listener.   */  public void addScanListener(ScanListener listener)  {    if (_scanListeners == null)      _scanListeners = new ArrayList<ScanListener>();    int i = 0;    for (; i < _scanListeners.size(); i++) {      if (listener.getPriority() < _scanListeners.get(i).getPriority())	break;    }    _scanListeners.add(i, listener);        ArrayList<URL> urlList = new ArrayList<URL>();    for (URL url : getURLs()) {      if (! _pendingScanUrls.contains(url))	urlList.add(url);    }    if (urlList.size() > 0) {      try {	make();      } catch (Exception e) {	log().log(Level.WARNING, e.toString(), e);		if (_configException == null)	  _configException = e;      }            ArrayList<ScanListener> selfList = new ArrayList<ScanListener>();      selfList.add(listener);      ScanManager scanManager = new ScanManager(selfList);      for (URL url : urlList) {	scanManager.scan(this, url);      }    }  }  /**   * Called when the <class-loader> completes.   */  @Override  public void validate()  {    super.validate();  }  @Override  public void scan()  {    configureEnhancerEvent();        ArrayList<URL> urlList = new ArrayList<URL>(_pendingScanUrls);    _pendingScanUrls.clear();    try {      if (_scanListeners != null && urlList.size() > 0) {	try {	  make();	} catch (Exception e) {	  log().log(Level.WARNING, e.toString(), e);		  if (_configException == null)	    _configException = e;	}      	ScanManager scanManager = new ScanManager(_scanListeners);      	for (URL url : urlList) {	  scanManager.scan(this, url);	}      }      // configureEnhancerEvent();    } catch (Exception e) {      log().log(Level.WARNING, e.toString(), e);	      if (_configException == null)	_configException = e;    }  }  /**   * Starts the config phase of the environment.   */  private void config()  {    sendAddLoaderEvent();          ArrayList<EnvironmentListener> listeners = getEnvironmentListeners();    int size = listeners.size();    for (int i = 0; listeners != null && i < size; i++) {      EnvironmentListener listener = listeners.get(i);      listener.environmentConfigure(this);    }        // _isConfigComplete = true;  }  /**   * Starts the config phase of the environment.   */  private void bind()  {    config();          ArrayList<EnvironmentListener> listeners = getEnvironmentListeners();    int size = listeners.size();    for (int i = 0; listeners != null && i < size; i++) {      EnvironmentListener listener = listeners.get(i);      listener.environmentBind(this);    }        _isConfigComplete = true;  }  /**   * Marks the environment of the class loader as started.  The   * class loader itself doesn't use this, but a callback might.   */  public void start()  {    if (! _lifecycle.toStarting())      return;    sendAddLoaderEvent();        bind();          ArrayList<EnvironmentListener> listeners = getEnvironmentListeners();    int size = listeners.size();    for (int i = 0; listeners != null && i < size; i++) {      EnvironmentListener listener = listeners.get(i);      listener.environmentStart(this);    }    _lifecycle.toActive();  }  /**   * Stops the environment, closing down any resources.   *   * The resources are closed in the reverse order that they're started   */  @Override  public void stop()  {    if (! _lifecycle.toDestroy())      return;    ArrayList<EnvironmentListener> listeners = getEnvironmentListeners();    Thread thread = Thread.currentThread();    ClassLoader oldLoader = thread.getContextClassLoader();    thread.setContextClassLoader(this);    try {      // closing down in reverse      if (listeners != null) {        for (int i = listeners.size() - 1; i >= 0; i--) {          EnvironmentListener listener = listeners.get(i);          try {	    listener.environmentStop(this);          } catch (Throwable e) {            log().log(Level.WARNING, e.toString(), e);          }        }      }    } finally {      thread.setContextClassLoader(oldLoader);      // drain the thread pool for GC      ResinThreadPoolExecutor.getThreadPool().stopEnvironment(this);     }  }  /**   * Copies the loader.   */  protected void replace(EnvironmentClassLoader source)  {    super.replace(source);    // XXX: might need separate class    _owner = source._owner;    _attributes = source._attributes;    if (source._listeners != null) {      if (_listeners == null)        _listeners = new ArrayList<EnvironmentListener>();      _listeners.addAll(source._listeners);      source._listeners.clear();    }    /*    _isStarted = source._isStarted;    _isActive = source._isActive;    _isStopped = source._isStopped;    */  }  /**   * Destroys the class loader.   */  @Override  public void destroy()  {    try {      WeakStopListener stopListener = _stopListener;      _stopListener = null;      // make sure it's stopped first      stop();      super.destroy();      ClassLoader parent = getParent();      for (; parent != null; parent = parent.getParent()) {        if (parent instanceof EnvironmentClassLoader) {          EnvironmentClassLoader loader = (EnvironmentClassLoader) parent;          loader.removeListener(stopListener);        }      }    } finally {      _owner = null;      _attributes = null;      _listeners = null;      _scanListeners = null;    }  }  @Override  public String toString()  {    if (getId() != null)      return getClass().getSimpleName() + "[" + getId() + "]";    else {      return getClass().getSimpleName() + "[]";    }  }  /**   * Initializes the environment   */  public static synchronized void initializeEnvironment()  {    if (_isStaticInit)      return;    _isStaticInit = true;    ClassLoader systemLoader = ClassLoader.getSystemClassLoader();    Thread thread = Thread.currentThread();    ClassLoader oldLoader = thread.getContextClassLoader();    try {      thread.setContextClassLoader(systemLoader);      // #2281      // PolicyImpl.init();      EnvironmentStream.setStdout(System.out);      EnvironmentStream.setStderr(System.err);      try {        Vfs.initJNI();      } catch (Throwable e) {      }      /*      if (System.getProperty("org.xml.sax.driver") == null)        System.setProperty("org.xml.sax.driver", "com.caucho.xml.Xml");      */      Properties props = System.getProperties();      /*      if (props.get("java.util.logging.manager") == null) {        props.put("java.util.logging.manager",                  "com.caucho.log.LogManagerImpl");      }      */            ClassLoader envClassLoader	= EnvironmentClassLoader.class.getClassLoader();      boolean isGlobalLoadable = false;      try {	Class cl = Class.forName("com.caucho.naming.InitialContextFactoryImpl",				 false,				 systemLoader);	isGlobalLoadable = (cl != null);      } catch (Exception e) {	log().log(Level.FINER, e.toString(), e);      }	      if (isGlobalLoadable) {	// These properties require Resin to be at the system loader		if (props.get("java.naming.factory.initial") == null) {	  props.put("java.naming.factory.initial",		    "com.caucho.naming.InitialContextFactoryImpl");	}	props.put("java.naming.factory.url.pkgs", "com.caucho.naming");	EnvironmentProperties.enableEnvironmentSystemProperties(true);	String oldBuilder = props.getProperty("javax.management.builder.initial");	if (oldBuilder == null) {	  oldBuilder = "com.caucho.jmx.MBeanServerBuilderImpl";	  props.put("javax.management.builder.initial", oldBuilder);	}	/*	  props.put("javax.management.builder.initial",	  "com.caucho.jmx.EnvironmentMBeanServerBuilder");	*/	if (MBeanServerFactory.findMBeanServer(null).size() == 0)	  MBeanServerFactory.createMBeanServer("Resin");		ManagementFactory.getPlatformMBeanServer();      }      TransactionManagerImpl tm = TransactionManagerImpl.getInstance();      // TransactionManagerImpl.setLocal(tm);      //Jndi.bindDeep("java:comp/TransactionManager", tm);      UserTransactionProxy ut = UserTransactionProxy.getInstance();      Jndi.bindDeep("java:comp/env/jmx/MBeanServer",                    Jmx.getGlobalMBeanServer());      Jndi.bindDeep("java:comp/env/jmx/GlobalMBeanServer",                    Jmx.getGlobalMBeanServer());            Jndi.bindDeep("java:comp/UserTransaction", ut);      // server/16g0      // Applications are incorrectly using TransactionManager      // as an extended UserTransaction      Jndi.bindDeep("java:comp/TransactionManager", tm);      Jndi.bindDeep("java:/TransactionManager", tm);            Jndi.bindDeep("java:comp/ThreadPool",		    ResinThreadPoolExecutor.getThreadPool());      /*      try {        Jndi.rebindDeep("java:comp/ORB",                        new com.caucho.iiop.orb.ORBImpl());      } catch (Exception e) {        e.printStackTrace();      }      */      // J2EEManagedObject.register(new JTAResource(tm));    } catch (NamingException e) {      log().log(Level.FINE, e.toString(), e);    } catch (Throwable e) {      e.printStackTrace();    } finally {      thread.setContextClassLoader(oldLoader);    }  }  private static final Logger log()  {    if (_log == null)      _log = Logger.getLogger(EnvironmentClassLoader.class.getName());    return _log;  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?