天天看点

Tomcat源码解析7——Host 容器

7.Host 容器

7.1 Context——web应用

每个Host包括若干个Context容器。

7.2 Host包含的与Engine相同的组件

PipeLine,cluster,realm,

7.3 HostConfig——生命周期监听器

一搜监听事件的实现方法,好家伙两个。后来仔细看了一下,第二个lifecycleEvent是在内部类ExpandedDirectoryRemovalListener中的。

第一个这个是HostConfig的监听的实现方法。HostConfig感兴趣的事件有

Lifecycle.PERIODIC_EVENT

Lifecycle.BEFORE_START_EVENT

Lifecycle.START_EVENT

Lifecycle.STOP_EVENT

@Override
    public void lifecycleEvent(LifecycleEvent event) {

        // Identify the host we are associated with
        try {
            host = (Host) event.getLifecycle();
            if (host instanceof StandardHost) {
                setCopyXML(((StandardHost) host).isCopyXML());
                setDeployXML(((StandardHost) host).isDeployXML());
                setUnpackWARs(((StandardHost) host).isUnpackWARs());
                setContextClass(((StandardHost) host).getContextClass());
            }
        } catch (ClassCastException e) {
            log.error(sm.getString("hostConfig.cce", event.getLifecycle()), e);
            return;
        }

        // Process the event that has occurred
        if (event.getType().equals(Lifecycle.PERIODIC_EVENT)) {
            check();
        } else if (event.getType().equals(Lifecycle.BEFORE_START_EVENT)) {
            beforeStart();
        } else if (event.getType().equals(Lifecycle.START_EVENT)) {
            start();
        } else if (event.getType().equals(Lifecycle.STOP_EVENT)) {
            stop();
        }
    }
           

搜到的第二个,说是为了解决57772这个bug。先不要理这个,先看主要的。

/*
     * The purpose of this class is to provide a way for HostConfig to get
     * a Context to delete an expanded WAR after the Context stops. This is to
     * resolve this issue described in Bug 57772. The alternative solutions
     * require either duplicating a lot of the Context.reload() code in
     * HostConfig or adding a new reload(boolean) method to Context that allows
     * the caller to optionally delete any expanded WAR.
     *
     * The LifecycleListener approach offers greater flexibility and enables the
     * behaviour to be changed / extended / removed in future without changing
     * the Context API.
     */

@Override
        public void lifecycleEvent(LifecycleEvent event) {
            if (Lifecycle.AFTER_STOP_EVENT.equals(event.getType())) {
                // The context has stopped.
                Context context = (Context) event.getLifecycle();

                // Remove the old expanded WAR.
                ExpandWar.delete(toDelete);

                // Reset the docBase to trigger re-expansion of the WAR.
                context.setDocBase(newDocBase);

                // Remove this listener from the Context else it will run every
                // time the Context is stopped.
                context.removeLifecycleListener(this);
            }
        }
           
7.4 Web应用的三种部署类型

1.Descriptor描述类型。

​ 解析指定的部署文件(.xml文件),加载Web应用,可以进行热部署。

  1. War包类型

    读取%CATALINA_HOME%/webapps目录下的web应用的war包。

  2. 目录类型

    读取%CATALINA_HOME%/webapps目录下的以目录形式存在的web应用。

继续阅读