5.2 管道任务
Container 的 invoke 方法的调用实际就是调用 Pipeline 的 invoke 方法,Pipeline 包含了容器要执行的任务,一个 valve 代表一个任务,BaseValve是容器的核心任务。阀类似过滤器,执行完成后会调用下一个阀,基础阀总是在最后执行。下面是本章 Pipeline 的实现
public class SimplePipeline implements Pipeline {
// 构造函数
public SimplePipeline(Container container) {
setContainer(container);
}
// Pipeline 属性设置
protected Valve basic = null;
protected Container container = null;
protected Valve valves[] = new Valve[0];
// 成员变量的 getter 和 setter 方法
...
public void setBasic(Valve valve) {
this.basic = valve;
((Contained) valve).setContainer(container);
}
// 添加 Valve
public void addValve(Valve valve) {
if (valve instanceof Contained)
((Contained) valve).setContainer(this.container);
synchronized (valves) {
Valve results[] = new Valve[valves.length +1];
System.arraycopy(valves, 0, results, 0, valves.length);
results[valves.length] = valve;
valves = results;
}
}
// Container 调用 invoke 方法执行处理逻辑
public void invoke(Request request, Response response) {
// 调用第一个 Valve
(new SimplePipelineValveContext()).invokeNext(request, response);
}
// 本章的 ValveContext 实现
protected class SimplePipelineValveContext implements ValveContext {
protected int stage = 0;
// 遍历 Pipeline 的所有 Valve
public void invokeNext(Request request, Response response) {
int subscript = stage;
stage = stage + 1;
if (subscript < valves.length) {
valves[subscript].invoke(request, response, this);
} else if ((subscript == valves.length) && (basic != null)) {
basic.invoke(request, response, this);
} else { throw new ServletException("No valve"); }
}
}
}
5.5 Wrapper 应用
- ex05.pyrmont.core.SimpleLoader: SimpleLoader 负责 Servlet 的类加载工作。
- ex05.pyrmont.core.SimplePipeline : 如前所述
- ex05.pyrmont.core.
4
4
4
4
4
4
4
4
4
4
网友评论