TOMCAT源碼閱讀總結(1)-TOMCAT的類加載體系

摘要

一直以來想看一下tomcat的源碼,能夠從頭至尾理解一遍在整個web請求的處理過程當中,容器完成的功能,容器怎麼把一個url的請求的處理遞交給了servlet,容器的pipeline是怎麼設計的,容器的filter怎麼實現的,如何維護session,好多好多的問題,有幸發現一篇文章Tomcat7.0源碼分析,讓我得以窺見容器的內部實現,從源碼之中能按着做者的思路來理解和閱讀。讀完以後,有了一些理解,可是不是很深入,藉着這個機會,寫了這篇總結,溫故而知新。讀源代碼,就是要反覆得讀,每次讀完都會先釋然,又會產生新的疑惑,再帶着問題重讀源碼,如此反覆,脈絡纔會愈來愈清晰。
整個文章按以下幾個主題:java

  • tomcat的類加載體系
  • server.xml文件的加載與解析
  • 生命週期管理
  • 中止與啓動服務
  • 請求原理分析
  • session管理分析

TOMCAT的類加載體系

Tomcat爲了webapp之間以及app和容器之間的資源(jar包)隔離,是經過自定義類加載器的方式來實現的。首先咱們看一下jdk的類加載器結構樹(爲了方便起見,將tomcat自定義的類加載器也放上去了)
clipboard.png
這個粗看會有一點困惑,jdk不是父親委派加載機制嗎?爲何這裏的AppClassLoader,ExtClassLoad不是繼承關係呢?其實這是一個誤解。加載器之間父子關係不是經過類繼承的方式,而是經過對象變量的方式,來實現的。web

private ClassLoader(Void unused, ClassLoader parent) {
     this.parent = parent;

而後在加載類的時候,會嘗試先讓parent來加載apache

protected Class<?> loadClass(String name, boolean resolve)
    throws ClassNotFoundException
{
    synchronized (getClassLoadingLock(name)) {
        // First, check if the class has already been loaded
        Class<?> c = findLoadedClass(name);
        if (c == null) {
            long t0 = System.nanoTime();
            try {
                if (parent != null) {
                    c = parent.loadClass(name, false);
                } else {
                    c = findBootstrapClassOrNull(name);
                }

這個就是父親委託加載機制的由來。以前找不到JDK的ExtClassLoader和AppClassLoader,後來才發現這兩個類原來是rt.jar包sum.msic.Launcher的內部類. Bootstrap加載sum.boot.class.path下的類,ExtClassPath加載java.ext.dirs下的類,AppClassLoader加載java.class.path下的類。固然也能夠在運行時經過參數( -XBootclasspath , -Djava.ext.dirs , -cp)分別指定。tomcat

好了,如今讓咱們回到tomcat的類加載器上來,看類樹上,tomcat自定義了WebappClassLoader和standardClassLoader,簡單的看,前者是給web應用用的,後者是給tomcat容器自己用的。咱們經過原來來進行分析。session

Bootstrap.java

public void init()
    throws Exception
{

      // Set Catalina path
      setCatalinaHome();
      setCatalinaBase();

      initClassLoaders();

      Thread.currentThread().setContextClassLoader(catalinaLoader);
      SecurityClassLoad.securityClassLoad(catalinaLoader);
// Load our startup class and call its process() method
if (log.isDebugEnabled())
    log.debug("Loading startup class");
//調用Catalina的setParentClassLoader方法,將sharedLoader設置爲全部WebappClassLoader的parent
Class<?> startupClass =
    catalinaLoader.loadClass
    ("org.apache.catalina.startup.Catalina");
Object startupInstance = startupClass.newInstance();

// Set the shared extensions class loader
if (log.isDebugEnabled())
    log.debug("Setting startup class properties");
String methodName = "setParentClassLoader";
Class<?> paramTypes[] = new Class[1];
paramTypes[0] = Class.forName("java.lang.ClassLoader");
Object paramValues[] = new Object[1];
paramValues[0] = sharedLoader;
Method method =
    startupInstance.getClass().getMethod(methodName, paramTypes);
method.invoke(startupInstance, paramValues);

catalinaDaemon = startupInstance;
     
}


private void initClassLoaders() {
        try {
            commonLoader = createClassLoader("common", null);
            if( commonLoader == null ) {
                // no config file, default to this loader - we might be in a 'single' env.
                commonLoader=this.getClass().getClassLoader();
            }
            catalinaLoader = createClassLoader("server", commonLoader);
            sharedLoader = createClassLoader("shared", commonLoader);
        } catch (Throwable t) {
            log.error("Class loader creation threw exception", t);
            System.exit(1);
        }
    }

private ClassLoader createClassLoader(String name, ClassLoader parent)
    throws Exception {
     //獲取各個類加載器相應的資源配置文件(common.loader,server.loader,shared.loader)
     //這些是具體配置在catalina.properties配置文件中的。以本機配置爲例
     //common.loader=${catalina.base}/lib,${catalina.base}/lib/*.jar,${catalina.home}/lib,${catalina.home}/lib/*.jar
     //server.loader=
     //shared.loader=
     
    String value = CatalinaProperties.getProperty(name + ".loader");
    if ((value == null) || (value.equals("")))
        return parent;

    ArrayList<String> repositoryLocations = new ArrayList<String>();
    ArrayList<Integer> repositoryTypes = new ArrayList<Integer>();
    int i;

    StringTokenizer tokenizer = new StringTokenizer(value, ",");
    while (tokenizer.hasMoreElements()) {
        String repository = tokenizer.nextToken();

        // Local repository
        boolean replace = false;
        String before = repository;
        while ((i=repository.indexOf(CATALINA_HOME_TOKEN))>=0) {
            replace=true;
            if (i>0) {
            repository = repository.substring(0,i) + getCatalinaHome()
                + repository.substring(i+CATALINA_HOME_TOKEN.length());
            } else {
                repository = getCatalinaHome()
                    + repository.substring(CATALINA_HOME_TOKEN.length());
            }
        }
        while ((i=repository.indexOf(CATALINA_BASE_TOKEN))>=0) {
            replace=true;
            if (i>0) {
            repository = repository.substring(0,i) + getCatalinaBase()
                + repository.substring(i+CATALINA_BASE_TOKEN.length());
            } else {
                repository = getCatalinaBase()
                    + repository.substring(CATALINA_BASE_TOKEN.length());
            }
        }
        if (replace && log.isDebugEnabled())
            log.debug("Expanded " + before + " to " + repository);

        // Check for a JAR URL repository
        try {
            new URL(repository);
            repositoryLocations.add(repository);
            repositoryTypes.add(ClassLoaderFactory.IS_URL);
            continue;
        } catch (MalformedURLException e) {
            // Ignore
        }

        if (repository.endsWith("*.jar")) {
            repository = repository.substring
                (0, repository.length() - "*.jar".length());
            repositoryLocations.add(repository);
            repositoryTypes.add(ClassLoaderFactory.IS_GLOB);
        } else if (repository.endsWith(".jar")) {
            repositoryLocations.add(repository);
            repositoryTypes.add(ClassLoaderFactory.IS_JAR);
        } else {
            repositoryLocations.add(repository);
            repositoryTypes.add(ClassLoaderFactory.IS_DIR);
        }
    }

    String[] locations = repositoryLocations.toArray(new String[0]);
    Integer[] types = repositoryTypes.toArray(new Integer[0]);
     //建立ClassLoader
    ClassLoader classLoader = ClassLoaderFactory.createClassLoader
        (locations, types, parent);

    // Retrieving MBean server
    MBeanServer mBeanServer = null;
    if (MBeanServerFactory.findMBeanServer(null).size() > 0) {
        mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
    } else {
        mBeanServer = ManagementFactory.getPlatformMBeanServer();
    }

    // Register the server classloader
    ObjectName objectName =
        new ObjectName("Catalina:type=ServerClassLoader,name=" + name);
    mBeanServer.registerMBean(classLoader, objectName);

    return classLoader;

}
//回看init方法,對應的securityClassLoad就是使用catalinaLoader完成tomcat核心類的加載的
public static void securityClassLoad(ClassLoader loader)
    throws Exception {

    if( System.getSecurityManager() == null ){
        return;
    }

    loadCorePackage(loader);
    loadLoaderPackage(loader);
    loadSessionPackage(loader);
    loadUtilPackage(loader);
    loadJavaxPackage(loader);
    loadCoyotePackage(loader);
    loadTomcatPackage(loader);
}

此處,完成了commonLoader,catalinaLoader和sharedLoader三個加載器的初始化,他們均是StandardClassLoader的實例,同時咱們能夠看到這三者之間的關係爲
clipboard.pngapp

接下去,咱們來看WebappLoaderwebapp

StandardContext.java
protected synchronized void startInternal() throws LifecycleException {
     //省略前邊代碼
if (getLoader() == null) {
    WebappLoader webappLoader = new WebappLoader(getParentClassLoader());
    webappLoader.setDelegate(getDelegate());
    setLoader(webappLoader);
}
//省略中間代碼
     if ((loader != null) && (loader instanceof Lifecycle))
         ((Lifecycle) loader).start();
//省略後邊代碼
}

WebappLoader.java
protected void startInternal() throws LifecycleException {

    if (log.isDebugEnabled())
        log.debug(sm.getString("webappLoader.starting"));

    if (container.getResources() == null) {
        log.info("No resources for " + container);
        setState(LifecycleState.STARTING);
        return;
    }

    // Register a stream handler factory for the JNDI protocol
    URLStreamHandlerFactory streamHandlerFactory =
        new DirContextURLStreamHandlerFactory();
    if (first) {
        first = false;
        try {
            URL.setURLStreamHandlerFactory(streamHandlerFactory);
        } catch (Exception e) {
            // Log and continue anyway, this is not critical
            log.error("Error registering jndi stream handler", e);
        } catch (Throwable t) {
            // This is likely a dual registration
            log.info("Dual registration of jndi stream handler: "
                    + t.getMessage());
        }
    }

    // Construct a class loader based on our current repositories list
    try {

        classLoader = createClassLoader();
        classLoader.setResources(container.getResources());
        classLoader.setDelegate(this.delegate);
        classLoader.setSearchExternalFirst(searchExternalFirst);
        if (container instanceof StandardContext) {
            classLoader.setAntiJARLocking(
                    ((StandardContext) container).getAntiJARLocking());
            classLoader.setClearReferencesStatic(
                    ((StandardContext) container).getClearReferencesStatic());
            classLoader.setClearReferencesStopThreads(
                    ((StandardContext) container).getClearReferencesStopThreads());
            classLoader.setClearReferencesStopTimerThreads(
                    ((StandardContext) container).getClearReferencesStopTimerThreads());
            classLoader.setClearReferencesThreadLocals(
                    ((StandardContext) container).getClearReferencesThreadLocals());
        }

        for (int i = 0; i < repositories.length; i++) {
            classLoader.addRepository(repositories[i]);
        }

        // Configure our repositories
        setRepositories();
        setClassPath();

        setPermissions();

        ((Lifecycle) classLoader).start();

        // Binding the Webapp class loader to the directory context
        DirContextURLStreamHandler.bind(classLoader,
                this.container.getResources());

        StandardContext ctx=(StandardContext)container;
        String path = ctx.getPath();
        if (path.equals("")) {
            path = "/";
        }
        ObjectName cloname = new ObjectName
            (MBeanUtils.getDomain(ctx) + ":type=WebappClassLoader,path="
            + path + ",host=" + ctx.getParent().getName());
        Registry.getRegistry(null, null)
            .registerComponent(classLoader, cloname, null);

    } catch (Throwable t) {
        log.error( "LifecycleException ", t );
        throw new LifecycleException("start: ", t);
    }

    setState(LifecycleState.STARTING);
}

private WebappClassLoader createClassLoader()
    throws Exception {

    Class<?> clazz = Class.forName(loaderClass);
    WebappClassLoader classLoader = null;

    if (parentClassLoader == null) {
        parentClassLoader = container.getParentClassLoader();
    }
    Class<?>[] argTypes = { ClassLoader.class };
    Object[] args = { parentClassLoader };
    Constructor<?> constr = clazz.getConstructor(argTypes);
    classLoader = (WebappClassLoader) constr.newInstance(args);

    return classLoader;

}

此處完成了WebappClassLoader的初始化,可見這個類加載器是對應一個Context的,即一個web應用。其parent應該是sharedLoader.源碼分析

clipboard.png

相關文章
相關標籤/搜索