标签:tomcat 源码 安全 优化 classloader
详细资料见
http://blog.csdn.net/dlf123321/article/details/39957175
package org.apache.catalina; import java.beans.PropertyChangeListener; public interface Loader { public ClassLoader getClassLoader(); public Container getContainer(); //载入器通常与一个context级别的容器相联 public void setContainer(Container container); public DefaultContext getDefaultContext(); public void setDefaultContext(DefaultContext defaultContext); public boolean getDelegate(); //Delegate 代表 委托 public void setDelegate(boolean delegate); //就是类加载器是否会把加载的任务委托给其父类加载器 public String getInfo(); public boolean getReloadable(); //表明是否支持载入器的自动重载 public void setReloadable(boolean reloadable); public void addPropertyChangeListener(PropertyChangeListener listener); public void addRepository(String repository); public String[] findRepositories(); public boolean modified(); //如果容器中的一个或多个类被修改了 modified就会返回true public void removePropertyChangeListener(PropertyChangeListenerlistener); }
private WebappClassLoader createClassLoader() throws Exception { //loadClass为字符串 //默认为 private String loaderClass ="org.apache.catalina.loader.WebappClassLoader"; //可通过setLoadClass方法更改 Class<?> clazz = Class.forName(loaderClass); WebappClassLoader classLoader = null; //在构造WebAppLoader时 又构造函数的参数指定 if (parentClassLoader == null) { // Will cause a ClassCast is the class does not extend WCL, but // this is on purpose (the exception will be caught and rethrown) classLoader = (WebappClassLoader) clazz.newInstance(); } else { Class<?>[] argTypes = { ClassLoader.class }; Object[] args = { parentClassLoader }; Constructor<?> constr = clazz.getConstructor(argTypes); classLoader = (WebappClassLoader) constr.newInstance(args); } return classLoader; }
public void run() { if (debug >= 1) log("BACKGROUND THREAD Starting"); // Loop until the termination semaphore is set //整段代码包含在while循环中 //在前面threadDone已经被设置为false //直到程序关闭时,threadDone才会变为true while (!threadDone) { // Wait for our check interval threadSleep(); if (!started) break; try { // Perform our modification check if (!classLoader.modified()) continue; } catch (Exception e) { log(sm.getString("webappLoader.failModifiedCheck"), e); continue; } // Handle a need for reloading notifyContext(); break; } if (debug >= 1) log("BACKGROUND THREAD Stopping"); } private void threadSleep() { //让程序休眠一段时间 时间由checkInterval指定 try { Thread.sleep(checkInterval * 1000L); } catch (InterruptedException e) { ; } }
private void notifyContext() { WebappContextNotifier notifier = new WebappContextNotifier(); (new Thread(notifier)).start(); } protected class WebappContextNotifier implements Runnable { /** * Perform the requested notification. */ public void run() { ((Context) container).reload(); } }WebappContextNotifier是webapploader的内部类。
private static final String[] packageTriggers = { "javax", // Java extensions "org.xml.sax", // SAX 1 & 2 "org.w3c.dom", // DOM 1 & 2 "org.apache.xerces", // Xerces 1 & 2 "org.apache.xalan" // Xalan };
为了达到更好的性能,会缓存已经载入的类,这样一来下次在使用这个类的时候,就不用再起加载了。
缓存分两级,一级在本地执行,由webappclassloader实例来管理。package org.apache.catalina.loader; import java.net.URL; import java.security.cert.Certificate; import java.util.jar.Manifest; public class ResourceEntry { public long lastModifled = -1; // Binary content of the resource.public byte[] binaryContent = null; public Class loadedClass = null; // URL source from where the object was loaded. public URL source = null; // URL of the codebase from where the object was loaded. public URL CodeBase = null; public Manifest manifest = null; public Certificate[] certificates = null; }
标签:tomcat 源码 安全 优化 classloader
原文地址:http://blog.csdn.net/dlf123321/article/details/40584923