dynamicclassloader.java

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

JAVA
2,097
字号
							  bBuf);	    if (enhancedBuffer != null) {	      bBuf = enhancedBuffer;	      bLen = enhancedBuffer.length;	      if (_isVerbose)		verbose(name, String.valueOf(transformer));	    }	    /* RSN-109	       } catch (RuntimeException e) {	       throw e;	       } catch (Error e) {	       throw e;	    */	  } catch (EnhancerRuntimeException e) {            e.printStackTrace();	    throw e;	  } catch (Throwable e) {            e.printStackTrace();	    log().log(Level.WARNING, e.toString(), e);	  }	}      }      try {        cl = defineClass(entry.getName(),			 bBuf, 0, bLen,			 entry.getCodeSource());      } catch (RuntimeException e) {        log().log(Level.FINE, entry.getName() + " [" + e.toString() + "]", e);	throw e;      } catch (Exception e) {        log().log(Level.FINE, entry.getName() + " [" + e.toString() + "]", e);        ClassNotFoundException exn;        exn = new ClassNotFoundException(entry.getName() + " [" + e + "]", e);        //exn.initCause(e);        throw exn;      }      entry.setEntryClass(cl);      if (entry.postLoad()) {        _dependencies.add(AlwaysModified.create());        _dependencies.setModified(true);      }      return cl;    }  }  /**   * Gets the named resource   *   * @param name name of the resource   */  @Override  public URL getResource(String name)  {    if (_resourceCache == null) {      long expireInterval = getDependencyCheckInterval();      _resourceCache = new TimedCache<String,URL>(256, expireInterval);    }    URL url = _resourceCache.get(name);    if (url == NULL_URL)      return null;    else if (url != null)      return url;        if (name.startsWith("/"))      name = name.substring(1);        if (name.endsWith("/"))      name = name.substring(0, name.length() - 1);    boolean isNormalJdkOrder = isNormalJdkOrder(name);    if (isNormalJdkOrder) {      url = getParentResource(name);      if (url != null)        return url;    }    ArrayList<Loader> loaders = getLoaders();    for (int i = 0; loaders != null && i < loaders.size(); i++) {      Loader loader = loaders.get(i);      url = loader.getResource(name);      if (url != null) {        _resourceCache.put(name, url);        return url;      }    }    if (! isNormalJdkOrder) {      url = getParentResource(name);      if (url != null)        return url;    }    _resourceCache.put(name, NULL_URL);    return null;  }  private URL getParentResource(String name)  {    ClassLoader parent = getParent();    ClassLoader systemLoader = ClassLoader.getSystemClassLoader();    URL url;    if (parent != null) {      url = parent.getResource(name);      if (url != null) {        _resourceCache.put(name, url);        return url;      }    }    else if (this != systemLoader) {      url = getSystemResource(name);      if (url != null) {        _resourceCache.put(name, url);        return url;      }    }    return null;  }  /**   * Opens a stream to a resource somewhere in the classpath   *   * @param name the path to the resource   *   * @return an input stream to the resource   */  @Override  public InputStream getResourceAsStream(String name)  {    if (name.startsWith("/"))      name = name.substring(1);        if (name.endsWith("/"))      name = name.substring(0, name.length() - 1);        boolean isNormalJdkOrder = isNormalJdkOrder(name);    InputStream is = null;    if (isNormalJdkOrder) {      is = getParentResourceAsStream(name);      if (is != null)        return is;    }    ArrayList<Loader> loaders = getLoaders();    for (int i = 0; loaders != null && i < loaders.size(); i++) {      Loader loader = loaders.get(i);      is = loader.getResourceAsStream(name);      if (is != null)        return is;    }    if (! isNormalJdkOrder) {      is = getParentResourceAsStream(name);    }    return is;  }  private InputStream getParentResourceAsStream(String name)  {    ClassLoader parent = getParent();    ClassLoader systemLoader = ClassLoader.getSystemClassLoader();    InputStream is;    if (parent != null) {      is = parent.getResourceAsStream(name);      if (is != null)        return is;    }    else if (this != systemLoader) {      is = getSystemResourceAsStream(name);      if (is != null) {        return is;      }    }    return null;  }  /**   * Returns an enumeration of matching resources.   */  @Override  public Enumeration<URL> findResources(String name)  {    if (name.startsWith("/"))      name = name.substring(1);    // server/249b    /*    if (name.endsWith("/"))      name = name.substring(0, name.length() - 1);    */        Vector<URL> resources = new Vector<URL>();    ArrayList<Loader> loaders = getLoaders();    if (loaders != null) {      for (int i = 0; i < loaders.size(); i++) {	Loader loader = loaders.get(i);	loader.getResources(resources, name);      }    }    return resources.elements();  }  /**   * Returns the full library path for the name.   */  @Override  public String findLibrary(String name)  {    String systemName = System.mapLibraryName(name);    ArrayList<Loader> loaders = getLoaders();    for (int i = 0; i < loaders.size(); i++) {      Loader loader = loaders.get(i);      Path path = loader.getPath(systemName);      if (path != null && path.canRead()) {        return path.getNativePath();      }    }    for (int i = 0; i < _nativePath.size(); i++) {      Path path = _nativePath.get(i);      if (path.canRead())	return path.getNativePath();    }    return super.findLibrary(name);  }  /**   * Returns the matching single-level path.   */  public Path findPath(String name)  {    ArrayList<Loader> loaders = getLoaders();    for (int i = 0; i < loaders.size(); i++) {      Loader loader = loaders.get(i);      Path path = loader.getPath(name);      if (path != null && path.canRead()) {        return path;      }    }    return null;  }  /**   * Returns true if the class loader should use the normal order,   * i.e. looking at the parents first.   */  private boolean isNormalJdkOrder(String className)  {    if (_priorityPackages != null) {      String canonName = className.replace('/', '.');      canonName = canonName.replace('\\', '.');      for (String priorityPackage : _priorityPackages) {        if (canonName.startsWith(priorityPackage))          return false;      }    }    if (! _useServletHack)      return true;    String canonName = className.replace('/', '.');    canonName = canonName.replace('\\', '.');    String []pkgs = _parentPriorityPackages;    for (int i = 0; pkgs != null && i < pkgs.length; i++) {      if (canonName.startsWith(pkgs[i]))        return true;    }    return false;  }  /**   * stops the class loader.   */  public void stop()  {  }  /**   * Destroys the class loader.   */  public void destroy()  {    try {      stop();    } catch (Throwable e) {    }    if (! _lifecycle.toDestroying())      return;    try {      ClassLoader parent = getParent();      for (; parent != null; parent = parent.getParent()) {        if (parent instanceof DynamicClassLoader) {          DynamicClassLoader loader = (DynamicClassLoader) parent;          if (_closeListener != null)            loader.removeListener(_closeListener);        }      }      ArrayList<ClassLoaderListener> listeners = _listeners;      _listeners = null;      Thread thread = Thread.currentThread();      ClassLoader oldLoader = thread.getContextClassLoader();      try {        // Use reverse order so the last resources will be destroyed first.        if (listeners != null) {          for (int i = listeners.size() - 1; i >= 0; i--) {            ClassLoaderListener listener = listeners.get(i);            try {              thread.setContextClassLoader(this);              listener.classLoaderDestroy(this);            } catch (Throwable e) {              log().log(Level.WARNING, e.toString(), e);            }          }        }      } finally {        thread.setContextClassLoader(oldLoader);      }      ArrayList<Loader> loaders = getLoaders();      for (int i = loaders.size() - 1; i >= 0; i--) {        Loader loader = loaders.get(i);        try {          loader.destroy();        } catch (Throwable e) {          log().log(Level.WARNING, e.toString(), e);        }      }    } finally {      _closeListener = null;      _listeners = null;      _entryCache = null;      _makeList = null;      _loaders = null;      _jarLoader = null;      _dependencies = null;      _permissions = null;      _securityManager = null;      _codeSource = null;      _classFileTransformerList = null;      _lifecycle.toDestroy();    }  }  /**   * Sets the old loader if not destroyed.   */  public static void setOldLoader(Thread thread, ClassLoader oldLoader)  {    if (! (oldLoader instanceof DynamicClassLoader)) {      thread.setContextClassLoader(oldLoader);      return;    }    DynamicClassLoader dynLoader = (DynamicClassLoader) oldLoader;    if (! dynLoader.isDestroyed())      thread.setContextClassLoader(oldLoader);    else      thread.setContextClassLoader(ClassLoader.getSystemClassLoader());  }  public ClassLoader getInstrumentableClassLoader()  {    return this;  }    public ClassLoader getThrowawayClassLoader()  {    return getNewTempClassLoader();  }    public ClassLoader getNewTempClassLoader()  {    return new TempDynamicClassLoader(this);  }  /**   * Copies the loader.   */  protected void replace(DynamicClassLoader source)  {    _id = source._id;    _loaders.addAll(source._loaders);    _jarLoader = source._jarLoader;    _dependencies = source._dependencies;    _makeList = source._makeList;    _useServletHack = source._useServletHack;    _parentPriorityPackages = source._parentPriorityPackages;    if (source._listeners != null) {      if (_listeners == null)        _listeners = new ArrayList<ClassLoaderListener>();      _listeners.addAll(source._listeners);      source._listeners.clear();    }    _securityManager = source._securityManager;    if (source._permissions != null) {      if (_permissions == null)        _permissions = new ArrayList<Permission>();      _permissions.addAll(source._permissions);    }    _codeSource = source._codeSource;    _lifecycle.copyState(source._lifecycle);  }  @Override  public String toString()  {    if (_id != null)      return getClass().getSimpleName() +  "[" + _id + "]";    else      return getClass().getSimpleName() + getLoaders();  }  private static L10N L()  {    if (_L == null)      _L = new L10N(DynamicClassLoader.class);    return _L;  }  private static Logger log()  {    if (_log == null)      _log = Logger.getLogger(DynamicClassLoader.class.getName());    return _log;  }  // XXX: GC issues  /*n  protected void finalize()  {    destroy();  }  */  static {    URL url = null;    try {      url = new URL("file:/caucho/null");    } catch (Throwable e) {    }    NULL_URL = url;  }}

⌨️ 快捷键说明

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