environmentclassloader.java

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

JAVA
897
字号
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT.  See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * *   Free Software Foundation, Inc. *   59 Temple Place, Suite 330 *   Boston, MA 02111-1307  USA * * @author Scott Ferguson */package com.caucho.loader;import com.caucho.jca.UserTransactionProxy;import com.caucho.jmx.Jmx;import com.caucho.lifecycle.Lifecycle;import com.caucho.loader.enhancer.ScanListener;import com.caucho.loader.enhancer.ScanManager;import com.caucho.log.EnvironmentStream;import com.caucho.management.server.EnvironmentMXBean;import com.caucho.naming.Jndi;import com.caucho.transaction.TransactionManagerImpl;import com.caucho.util.ResinThreadPoolExecutor;import com.caucho.vfs.Vfs;import javax.management.MBeanServerFactory;import javax.naming.NamingException;import java.lang.management.ManagementFactory;import java.lang.reflect.Constructor;import java.net.URL;import java.util.ArrayList;import java.util.Hashtable;import java.util.Properties;import java.util.logging.Level;import java.util.logging.Logger;/** * Class loader which checks for changes in class files and automatically * picks up new jars. * * <p>DynamicClassLoaders can be chained creating one virtual class loader. * From the perspective of the JDK, it's all one classloader.  Internally, * the class loader chain searches like a classpath. */public class EnvironmentClassLoader extends DynamicClassLoader{  private static Logger _log;  private static boolean _isStaticInit;  // listeners invoked at the start of any child environment  private static EnvironmentLocal<ArrayList<EnvironmentListener>> _childListeners    = new EnvironmentLocal<ArrayList<EnvironmentListener>>();  // listeners invoked when a Loader is added  private static EnvironmentLocal<ArrayList<AddLoaderListener>> _addLoaderListeners    = new EnvironmentLocal<ArrayList<AddLoaderListener>>();  // The owning bean  private EnvironmentBean _owner;  // Class loader specific attributes  private Hashtable<String,Object> _attributes    = new Hashtable<String,Object>(8);  private ArrayList<ScanListener> _scanListeners;  private ArrayList<URL> _pendingScanUrls = new ArrayList<URL>();  // Array of listeners  // XXX: this used to be a weak reference list, but that caused problems  // server/306i  - can't be weak reference, instead create WeakStopListener  private ArrayList<EnvironmentListener> _listeners;  private WeakStopListener _stopListener;  // The state of the environment  private volatile Lifecycle _lifecycle = new Lifecycle();  private boolean _isConfigComplete;  private EnvironmentAdmin _admin;  private Throwable _configException;  /**   * Creates a new environment class loader.   */  protected EnvironmentClassLoader(ClassLoader parent, String id)  {    super(parent);    if (id != null)      setId(id);    // initializeEnvironment();    initListeners();  }  /**   * Creates a new environment class loader.   */  public static EnvironmentClassLoader create()  {    ClassLoader parent = Thread.currentThread().getContextClassLoader();    String id = null;        return create(parent, id);  }  /**   * Creates a new environment class loader.   */  public static EnvironmentClassLoader create(String id)  {    ClassLoader parent = Thread.currentThread().getContextClassLoader();        return create(parent, id);  }  /**   * Creates a new environment class loader.   */  public static EnvironmentClassLoader create(ClassLoader parent)  {    String id = null;        return create(parent, id);  }  /**   * Creates a new environment class loader.   */  public static EnvironmentClassLoader create(ClassLoader parent, String id)  {    String className = System.getProperty("caucho.environment.class.loader");    if (className != null) {      try {        Class cl = Thread.currentThread().getContextClassLoader().loadClass(className);        Constructor ctor = cl.getConstructor(new Class[] { ClassLoader.class, String.class});        Object instance = ctor.newInstance(parent, id);        return (EnvironmentClassLoader) instance;      }      catch (Exception e) {        e.printStackTrace();      }    }    return new EnvironmentClassLoader(parent, id);  }  /**   * Returns the environment's owner.   */  public EnvironmentBean getOwner()  {    return _owner;  }  /**   * Sets the environment's owner.   */  public void setOwner(EnvironmentBean owner)  {    _owner = owner;  }  /**   * Sets the config exception.   */  public void setConfigException(Throwable e)  {    if (_configException == null)      _configException = e;  }  /**   * Gets the config exception.   */  public Throwable getConfigException()  {    return _configException;  }  /**   * Returns true if the environment is active   */  public boolean isActive()  {    return _lifecycle.isActive();  }  /**   * Returns the admin   */  public EnvironmentMXBean getAdmin()  {    if (_admin == null) {      _admin = new EnvironmentAdmin(this);      _admin.register();    }    return _admin;  }  /**   * Initialize the environment.   */  @Override  public void init()  {    super.init();        initEnvironment();  }  protected void initEnvironment()  {    initializeEnvironment();  }  /**   * Returns the named attributes   */  public Object getAttribute(String name)  {    if (_attributes != null)      return _attributes.get(name);    else      return null;  }  /**   * Sets the named attributes   */  public Object setAttribute(String name, Object obj)  {    if (obj == null) {      if (_attributes == null)        return null;      else        return _attributes.remove(name);    }    if (_attributes == null)      _attributes = new Hashtable<String,Object>(8);    return _attributes.put(name, obj);  }  /**   * Removes the named attributes   */  public Object removeAttribute(String name)  {    if (_attributes == null)      return null;    else      return _attributes.remove(name);  }  /**   * Adds a listener to detect environment lifecycle changes.   */  public void addListener(EnvironmentListener listener)  {    synchronized (this) {      if (_listeners == null) {        _listeners = new ArrayList<EnvironmentListener>();        initListeners();      }    }    synchronized (_listeners) {      for (int i = _listeners.size() - 1; i >= 0; i--) {        EnvironmentListener oldListener = _listeners.get(i);        if (listener == oldListener) {          return;        }        else if (oldListener == null)          _listeners.remove(i);      }      _listeners.add(listener);    }    if (_lifecycle.isStarting()) {      listener.environmentBind(this);    }    if (_lifecycle.isStarting() && _isConfigComplete) {      listener.environmentStart(this);    }  }  /**   * Adds self as a listener.   */  private void initListeners()  {    ClassLoader parent = getParent();    for (; parent != null; parent = parent.getParent()) {      if (parent instanceof EnvironmentClassLoader) {        EnvironmentClassLoader loader = (EnvironmentClassLoader) parent;        if (_stopListener == null)          _stopListener = new WeakStopListener(this);        loader.addListener(_stopListener);        return;      }    }  }  /**   * Adds a listener to detect environment lifecycle changes.   */  public void removeListener(EnvironmentListener listener)  {    if (_listeners == null)      return;    synchronized (_listeners) {      for (int i = _listeners.size() - 1; i >= 0; i--) {        EnvironmentListener oldListener = _listeners.get(i);        if (listener == oldListener) {          _listeners.remove(i);          return;        }        else if (oldListener == null)          _listeners.remove(i);      }    }  }  /**   * Adds a child listener.   */  void addChildListener(EnvironmentListener listener)  {    synchronized (_childListeners) {      ArrayList<EnvironmentListener> listeners        = _childListeners.getLevel(this);      if (listeners == null) {        listeners = new ArrayList<EnvironmentListener>();        _childListeners.set(listeners, this);      }      listeners.add(listener);    }    if (_lifecycle.isStarting() && _isConfigComplete) {      listener.environmentStart(this);    }  }  /**   * Removes a child listener.   */  void removeChildListener(EnvironmentListener listener)  {    synchronized (_childListeners) {      ArrayList<EnvironmentListener> listeners        = _childListeners.getLevel(this);      if (listeners != null)        listeners.remove(listener);    }  }  /**   * Returns the listeners.   */  protected ArrayList<EnvironmentListener> getEnvironmentListeners()  {    ArrayList<EnvironmentListener> listeners;    listeners = new ArrayList<EnvironmentListener>();    // add the descendent listeners    synchronized (_childListeners) {      ClassLoader loader;      for (loader = this; loader != null; loader = loader.getParent()) {        if (loader instanceof EnvironmentClassLoader) {          ArrayList<EnvironmentListener> childListeners;          childListeners = _childListeners.getLevel(loader);          if (childListeners != null)            listeners.addAll(childListeners);        }      }    }    if (_listeners == null)      return listeners;    synchronized (_listeners) {      for (int i = 0; i < _listeners.size(); i++) {        EnvironmentListener listener = _listeners.get(i);        if (listener != null)          listeners.add(listener);        else {          _listeners.remove(i);          i--;        }      }    }    return listeners;  }  /**   * Adds a child listener.   */  public void addLoaderListener(AddLoaderListener listener)  {    synchronized (_addLoaderListeners) {      ArrayList<AddLoaderListener> listeners        = _addLoaderListeners.getLevel(this);      if (listeners == null) {        listeners = new ArrayList<AddLoaderListener>();

⌨️ 快捷键说明

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