public class SimpleWrapper implements Wrapper, Pipeline { // the servlet instance private Servlet instance = null; private String servletClass; private Loader loader; private String name; private SimplePipeline pipeline = new SimplePipeline(this); protected Container parent = null; public SimpleWrapper() { pipeline.setBasic(new SimpleWrapperValve()); } public synchronized void addValve(Valve valve) { pipeline.addValve(valve); } ... }既然说到了容器,就得说说管道(每一级容器中,都有一个管道);把咱们的命令比做流水,在(流水)命令接触终于的servlet以前,会有一个长长的管道(SimplePipeline),管道里有一个一个的阀(Valve),每一个阀都会作一个任务!就这么简单,在管道里面有一个基础阀(SimpleWrapperValve),而这个基础阀就用来生成servlet,调用其service方法。
wrapper程序的类图例如如下:
流程例如如下
先是调用wrapper的invoke;
java
SimpleWrapper.java public void invoke(Request request, Response response) throws IOException, ServletException { pipeline.invoke(request, response); }再调用管道的invoke;
SimplePipeline.java public void invoke(Request request, Response response) throws IOException, ServletException { // Invoke the first Valve in this pipeline for this request (new SimplePipelineValveContext()).invokeNext(request, response); } SimplePipelineValveContext为SimplePipeline的内部类,做用就是循环所有的阀,最后调用基础阀(就是如下代码中的basic) SimplePipelineValveContext.java public void invokeNext(Request request, Response response) throws IOException, ServletException { int subscript = stage; stage = stage + 1; // Invoke the requested Valve for the current request thread 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"); } }
所以就有了mapper接口,咱们这里用的是事实上现类,simplecontextmapper。其map方法就能返回对应的wrapper。
web
public Container map(Request request, boolean update) { String requestURI = ((HttpRequest) request).getDecodedRequestURI(); String relativeURI = requestURI.substring(contextPath.length()); Wrapper wrapper = null; String name = context.findServletMapping(relativeURI); if (name != null) wrapper = (Wrapper) context.findChild(name); return (wrapper); }找到wrapper就和上一部分的过程同样了。
本章类图:
tomcat是组件化的软件。所有的组件都实现了Lifecycle接口,里面有start与stop方法;咱们现在想要的效果就是,我仅仅用启动一个组件系统就能帮我把所有的都启动,关闭也是同样。apache
看上去很是复杂,事实上很是easy
tomcat
public synchronized void start() throws LifecycleException { if (started) throw new LifecycleException("SimpleContext has already started"); // Notify our interested LifecycleListeners lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null); started = true; try { // Start our subordinate components, if any if ((loader != null) && (loader instanceof Lifecycle)) ((Lifecycle) loader).start(); // 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(); // Notify our interested LifecycleListeners lifecycle.fireLifecycleEvent(START_EVENT, null); } catch (Exception e) { e.printStackTrace(); }SimpleContext里面的各个组件依次启动就ok;
Bootstrap.java LifecycleListener listener = new SimpleContextLifecycleListener(); ((Lifecycle) context).addLifecycleListener(listener); simplecontext.java protected LifecycleSupport lifecycle = new LifecycleSupport(this); public void addLifecycleListener(LifecycleListener listener) { lifecycle.addLifecycleListener(listener); }执行时,如下的代码就是告诉所有关心SimpleContext的监听者:SimpleContext类作了BEFORE_START_EVENT这个动做!
lifecycle.fireLifecycleEvent(BEFORE_START_EVENT, null); LifecycleSupport.java public void fireLifecycleEvent(String type, Object data) { LifecycleEvent event = new LifecycleEvent(lifecycle, type, data); LifecycleListener interested[] = null; synchronized (listeners) { interested = (LifecycleListener[]) listeners.clone(); } for (int i = 0; i < interested.length; i++) //循环通知所有关注者 interested[i].lifecycleEvent(event); } // 一个详细的关注者 public class SimpleContextLifecycleListener implements LifecycleListener { @SuppressWarnings("unused") public void lifecycleEvent(LifecycleEvent event) { Lifecycle lifecycle = event.getLifecycle(); System.out.println("SimpleContextLifecycleListener's event " +event.getType().toString()); if (Lifecycle.START_EVENT.equals(event.getType())) { System.out.println("Starting context."); } else if (Lifecycle.STOP_EVENT.equals(event.getType())) { System.out.println("Stopping context."); } } }