前言
上篇文章讲到了 Wrapper 的启动。在这篇文章中初次提到了 Pipeline 和 Valve。Pipeline的实现类是 StandardPipeline,StandardPipeline 继承自 LifecycleBase,而 Valve 的实现类则比较多,这些实现类都继承自基类 ValveBase,而 ValveBase 继承自 LifecycleMBeanBase。


1. Pipeline 与 Valve 的初始化
在 ContainerBase 中有一个 Pipeline 类型的对象,其声明为:

/**
 * The Pipeline object with which this Container is associated.
 */
protected final Pipeline pipeline = new StandardPipeline(this);
public StandardPipeline(Container container) {
    super();
    setContainer(container);
}

/**
 * The Container with which this Pipeline is associated.
 */
protected Container container = null;

@Override
public void setContainer(Container container) {
    this.container = container;
}

可以看出,每一个 Container 对象都包含了一个 Pipeline 对象,包括 Engine、Host、Context、Wrapper。

public StandardEngine() {

    super();
    pipeline.setBasic(new StandardEngineValve());
    /* Set the jmvRoute using the system property jvmRoute */
    try {
        setJvmRoute(System.getProperty("jvmRoute"));
    } catch(Exception ex) {
        log.warn(sm.getString("standardEngine.jvmRouteFail"));
    }
    // By default, the engine will hold the reloading thread
    backgroundProcessorDelay = 10;

}
public StandardHost() {

    super();
    pipeline.setBasic(new StandardHostValve());

}
public StandardContext() {

    super();
    pipeline.setBasic(new StandardContextValve());
    broadcaster = new NotificationBroadcasterSupport();
    // Set defaults
    if (!Globals.STRICT_SERVLET_COMPLIANCE) {
        // Strict servlet compliance requires all extension mapped servlets
        // to be checked against welcome files
        resourceOnlyServlets.add("jsp");
    }
}
public StandardWrapper() {

    super();
    swValve=new StandardWrapperValve();
    pipeline.setBasic(swValve);
    broadcaster = new NotificationBroadcasterSupport();

}
@Override
public void setBasic(Valve valve) {

    // Change components if necessary
    Valve oldBasic = this.basic;
    if (oldBasic == valve)
        return;

    // Stop the old component if necessary
    if (oldBasic != null) {
        if (getState().isAvailable() && (oldBasic instanceof Lifecycle)) {
            try {
                ((Lifecycle) oldBasic).stop();
            } catch (LifecycleException e) {
                log.error(sm.getString("standardPipeline.basic.stop"), e);
            }
        }
        if (oldBasic instanceof Contained) {
            try {
                ((Contained) oldBasic).setContainer(null);
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
            }
        }
    }

    // Start the new component if necessary
    if (valve == null)
        return;
    if (valve instanceof Contained) {
        ((Contained) valve).setContainer(this.container);
    }
    if (getState().isAvailable() && valve instanceof Lifecycle) {
        try {
            ((Lifecycle) valve).start();
        } catch (LifecycleException e) {
            log.error(sm.getString("standardPipeline.basic.start"), e);
            return;
        }
    }

    // Update the pipeline
    Valve current = first;
    while (current != null) {
        if (current.getNext() == oldBasic) {
            current.setNext(valve);
            break;
        }
        current = current.getNext();
    }

    this.basic = valve;

}

在每个容器的构造函数中,都调用了 Pipeline#setBasic 方法给各自的 Pipeline 都设置了一个 Valve 对象。
可以看出,每个不同的 Container 对应的 Valve 实现类是不同的。这些不同的 Valve 实现类的父类都是 ValveBase。

public StandardEngineValve() {
    super(true);
}
public StandardHostValve() {
    super(true);
}
public StandardContextValve() {
    super(true);
}
 public StandardWrapperValve() {
    super(true);
}
public ValveBase(boolean asyncSupported) {
    this.asyncSupported = asyncSupported;
}

可以看出在这四个类的构造方法中,都设置了 ValveBase 里的 asyncSupported 属性为true。

2. Pipeline#initInternal、startInternal 方法
在容器启动的时候(ContainerBase#startInternal)调用了其成员属性 Pipeline 的 start 方法。StandardPipeline,重写了 LifecycleBase 的 initInternal 和 startInternal 方法

@Override
protected void initInternal() {
    // NOOP
}


/**
 * Start {@link Valve}s) in this pipeline and implement the requirements
 * of {@link LifecycleBase#startInternal()}.
 *
 * @exception LifecycleException if this component detects a fatal error
 *  that prevents this component from being used
 */
@Override
protected synchronized void startInternal() throws LifecycleException {

    // Start the Valves in our pipeline (including the basic), if any
    Valve current = first;
    if (current == null) {
        current = basic;
    }
    while (current != null) {
        if (current instanceof Lifecycle)
            ((Lifecycle) current).start();
        current = current.getNext();
    }

    setState(LifecycleState.STARTING);
}

StandardPipeline 的 initInternal 方法是一个空实现,而 startInternal 方法,则是依次调用自己关联的 Valve 的 start 方法。

3. ValveBase#initInternal、startInternal、backgroundProcess 方法
Valve 的实现类 StandardEngineValve、StandardHostValve、StandardContextValve 都没有重载 initInternal 、 startInternal 以及 backgroundProcess 方法, StandardWrapperValve 也没有重载 startInternal 方法,重载了 initInternal,但是 StandardWrapperValve#initInternal 方法是一个空方法。

@Override
protected void initInternal() throws LifecycleException {
    super.initInternal();
    containerLog = getContainer().getLogger();
}


/**
 * Start this component and implement the requirements
 * of {@link org.apache.catalina.util.LifecycleBase#startInternal()}.
 *
 * @exception LifecycleException if this component detects a fatal error
 *  that prevents this component from being used
 */
@Override
protected synchronized void startInternal() throws LifecycleException {
    setState(LifecycleState.STARTING);
}


/**
 * Execute a periodic task, such as reloading, etc. This method will be
 * invoked inside the classloading context of this container. Unexpected
 * throwables will be caught and logged.
 */
@Override
public void backgroundProcess() {
    // NOOP by default
}

ValveBase#initInternal、startInternal、backgroundProcess 都很简单。


小结
本文分析了 Pipeline 和 Valve 的初始化,以及 init 和 start 过程,这两个组件的初始化、init、start 相对来说比较简单。Pipeline 和 Valve 真正起作用的时候是在 Connector 使用容器 Container 处理请求的时候,Connector 会找到自己关联的 Service 的里的 Container 对象(也就是 Engine 对象),然后获取这个对象的 Pipeline,通过这个 Pipeline 对象获取 Pipeline 对象的 Valve 对象,最后通过调用 Valve 对象的 invoke 方法来处理请求,如下面的代码

connector.getService().getContainer().getPipeline()
    .getFirst().invoke(request, response);

当然,这些内容将在后面的文章中揭秘,本文在此不多做讨论。
关于 Valve#invoke 方法,每个实现类的内容都不相同,这里也不多做描述了。


客官
36 声望25 粉丝