Tomcat之源码分析二

StandardWrapper、StandardContext、StandardHost、StandardEngine四类标准容器均继承于基本容器ContainerBase
4类容器启动请求流转方向 & 启动时pipeline调用方向
在这里插入图片描述

Engine & Host

Context容器的父容器通常是Host容器,也有可能是其他实现。Engine容器表示Cataline的整个servlet引擎。如果使用了Engine容器,那么它总是处于容器层级的最顶层。添加到Engine容器中的子容器通常是Host | Context的实现。

当部署Tomcat需要支持多个虚拟机的时候,就需要Engine容器。

// docBase
/Users/zhfeng/code/Tomcat-Study/webapps/app1
// appBase
webapps/app1

Host容器的必要性

在这里插入图片描述
在这里插入图片描述

StandardServer

org.apache.catalina.server接口的实例表示Catalina的整个servlet引擎,囊括了所有的组件。服务器组件使用一种优雅的方式来启动/关闭整个系统,不需要再对连接器和容器分别启动/关闭。

当启动服务器组件时,会启动其中所有的组件,然后她就无限期地等待关闭命令。如果想要关闭系统,可以向指定端口发送一条关闭命令,就会关闭所有组件。

服务器组件使用了另一个组件(即服务组件)来包含其他组件,如一个容器组件&一个/多个连接器组件。

服务器组件,实现org.apache.catalina.server接口,主要功能:服务器关闭功能 & 与Server.xml 。

StandardService

StandardService是服务组件,其中只包括两种组件:Connector组件 & Servlet 组件。

Digester

与其他类型的容器不同,StandardContext实例必须有一个监听器,这个监听器会负责配置StandardContext实例,设置成功后会将StandardContext实例的变量configured设置为true。

在Tomcat实例部署中,StandardContext类的标准监听器是ContextConfig类的一个实例。其最重要的功能是读取&解析默认的web.xml文件和应用程序自定义的web.xml文件,将XML元素转换为Java对象。默认的web.xml文件位于Catalina_Home目录下的conf目录中。

应用程序中自定义的web.xml文件位于WEF-INF目录中。

StandardContext:

public synchronized void start() throws LifecycleException {

        // Notify our interested LifecycleListeners
        lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null);
        if (debug >= 1)log("Processing start(), current available=" + getAvailable());
        setAvailable(false);
        setConfigured(false);
        boolean ok = true;

        ...
        // Initialize character set mapper
        getCharsetMapper();

        //创建 ApplicationContext上下文
        postWorkDirectory();

        ...

        // Binding thread
        ClassLoader oldCCL = bindThread();

        // Standard container startup
        if (debug >= 1)log("Processing standard container startup");

        if (ok) {
            try {
                addDefaultMapper(this.mapperClass);
                started = true;
                // Start our subordinate components, if any
                // 创建WebappClassLoader 载入器。
                // 处理 /WEB-INF/classes /WEB-INF/lib目录下的类文件
                if ((loader != null) && (loader instanceof Lifecycle)) {
                    ((Lifecycle) loader).start();
                }
               ...

                // Start our Mappers, if any
                Mapper mappers[] = findMappers();
                for (int i = 0; i < mappers.length; i++) {
                    if (mappers[i] instanceof Lifecycle){
                        ((Lifecycle) mappers[i]).start();
                    }
                }
                System.out.println("start StandardContext 6 ...");
                // Start our child containers, if any
                Container children[] = findChildren();
                for (int i = 0; i < children.length; i++) {
                    if (children[i] instanceof Lifecycle){
                        ((Lifecycle) children[i]).start();
                    }
                }

                // Start the Valves in our pipeline (including the basic),
                // if any
                if (pipeline instanceof Lifecycle){
                    ((Lifecycle) pipeline).start();
                }
                
                lifecycle.fireLifecycleEvent(START_EVENT, null);
                ...
            } finally {
                // Unbinding thread
                unbindThread(oldCCL);
            }

        }
        ...
    }

ContextConfig:

private synchronized void start() {

        context.setConfigured(false);
        ok = true;

        // Set properties based on DefaultContext
        Container container = context.getParent();
        if( !context.getOverride() ) {
            if( container instanceof Host ) {
                ((Host)container).importDefaultContext(context);
                container = container.getParent();
            }
            if( container instanceof Engine ) {
                ((Engine)container).importDefaultContext(context);
            }
        }

        // 处理默认配置 System.getProperty("catalina.base")+/conf/web.xml
        defaultConfig();
		//处理应用程序web.xml /StandardContext.parent.name/StandardContext.name/WEB-INF/web.xml
        applicationConfig();
        ...
        // Make our application available if no problems were encountered
        if (ok)
            context.setConfigured(true);
        else {
            log(sm.getString("contextConfig.unavailable"));
            context.setConfigured(false);
        }

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
作为一个AI机器人,我并没有能力进行源码分析。但是我可以提供一些关于Tomcat8.5的相关信息。 Tomcat8.5是Apache Tomcat的一个版本,是一个开源的Web容器,使用Java编写,可用于部署Java Web应用程序。Tomcat8.5增加了对Java Servlet 3.1、JavaServer Pages 2.3、Java Unified Expression Language 3.0以及WebSocket 1.1的支持。Tomcat8.5还增强了对SSL/TLS的支持,包括支持ALPN协议和OCSP Stapling。Tomcat8.5还提供了一些新的特性,如异步I/O和NIO2。 在Tomcat8.5源码中,主要涉及到的模块有:Catalina、Coyote、Jasper、WebSocket和Tomcat Native。Catalina模块是Tomcat的核心模块,负责处理HTTP请求和响应。Coyote模块负责处理HTTP请求的接收和响应。Jasper模块负责处理JSP页面的编译和执行。WebSocket模块提供了基于WebSocket协议的通信。Tomcat Native模块提供了对APR(Apache Portable Runtime)库的支持,可以提高Tomcat的性能和可靠性。 在Tomcat8.5源码中,还有一些其他的模块,如:Cluster、Manager、Realm、Valve等。Cluster模块提供了集群支持,可以让多个Tomcat实例共享会话信息。Manager模块提供了Web应用程序管理的功能。Realm模块提供了认证和授权的支持。Valve模块提供了一些过滤器和拦截器,可以对HTTP请求和响应进行处理。 总体来说,Tomcat8.5源码是一个庞大而复杂的项目,需要对JavaWeb开发有一定的了解才能进行源码分析。如果有兴趣了解Tomcat8.5的源码,可以先从官方文档和源码注释入手,逐步深入了解各个模块的实现原理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值