Tomcat类是整个tomcat的起点,负责加载所有的配置信息以及把配置信息解析转换成tomcat组件对象。
Context addWebapp(String contextPath, String baseDir) throws ServletException //添加context(这个可以理解为默认context) Context addContext(String contextPath, String baseDir) //添加context (这个是真正的上层业务逻辑的context) Wrapper addServlet(String contextPath, String servletName,String servletClass) public void start() throws LifecycleException { //Server和Connector的加载 getServer(); getConnector(); server.start(); } public Host getHost() { //随着context的加载而加载 if (host == null) { host = new StandardHost(); host.setName(hostname); getEngine().addChild( host ); } return host; } public Engine getEngine() { if(engine == null ) { getServer(); engine = new StandardEngine(); engine.setRealm(defaultRealm); service.setContainer(engine); } return engine; } |
从接口上看:一个Server中包含了多个Service,一个Service中包含多个Connector以及一个Engine,一个Engine中包含多个Host,一个Host下有多个Context,一个Context下有多个Wrapper。
但是从实现上可以看到一个tomcat其实内部只包含了一个Server,一个Server包含了一个Service,一个Service包含了一个Connector
interface Server extends Lifecycle{ public void addService(Service service); } public interface Service extends Lifecycle { public void addConnector(Connector connector); public void setContainer(Container container); } public class StandardService extends LifecycleMBeanBase implements Service { protected Connector connectors[] = new Connector[0]; protected Container container = null; } |
连接器
class Connector extends LifecycleMBeanBase{ protected ProtocolHandler protocolHandler = null; public Request createRequest() { Request request = new Request(); request.setConnector(this); return (request); } public Response createResponse() { Response response = new Response(); response.setConnector(this); return (response); } @Override protected void initInternal() throws LifecycleException { super.initInternal(); // Initialize adapter adapter = new CoyoteAdapter(this); protocolHandler.setAdapter(adapter); try { protocolHandler.init(); } catch (Exception e) { throw new LifecycleException (sm.getString ("coyoteConnector.protocolHandlerInitializationFailed"), e); } } protected void startInternal() throws LifecycleException { protocolHandler.start(); } } public interface ProtocolHandler {//协议托管 public void setAdapter(Adapter adapter); public Adapter getAdapter(); 其他组件生命周期的方法 start、init等等 } |
AbstractEndpoint:整个tomcat I/O流的处理框架,内部实现了tomcat底层通讯的所需要的一些基本组件。
public abstract class AbstractEndpoint { private volatile LimitLatch connectionLimitLatch = null; public static interface Handler { public Object getGlobal(); public void recycle(); } public abstract static class Acceptor implements Runnable { } } |
1.LimitLatch:用于限制tomcat并发数,当一个请求进来之后,只有等到了LimitLatch才能进行下一步的处理,否则只能等待
2.Aceptor:用于接受socket,并对socket进行封装
3.Handler:用于处理Aceptor处理后的Socket
public abstract class AbstractProtocol implements ProtocolHandler, MBeanRegistration { protected AbstractEndpoint endpoint = null; protected static class RecycledProcessors<P extends Processor<S>, S> extends ConcurrentLinkedQueue<Processor<S>>{ private transient AbstractConnectionHandler<S,P> handler; public Processor<S> poll() public boolean offer(Processor<S> processor) } protected abstract static class AbstractConnectionHandler<S,P extends Processor<S> implements AbstractEndpoint.Handler{ protected RecycledProcessors<P,S> recycledProcessors = new RecycledProcessors<P,S>(this); public SocketState process(SocketWrapper<S> socket, SocketStatus status) { ... state = processor.process(socket); } protected abstract P createProcessor(); } } public abstract class AbstractHttp11Protocol extends AbstractProtocol public abstract class AbstractHttp11JsseProtocol extends AbstractHttp11Protocol public class Http11NioProtocol extends AbstractHttp11JsseProtocol { public Http11NioProtocol() { endpoint=new NioEndpoint(); cHandler = new Http11ConnectionHandler(this); ((NioEndpoint) endpoint).setHandler(cHandler); } protected static class Http11ConnectionHandler extends AbstractConnectionHandler<NioChannel,Http11NioProcessor> implements Handler { public Http11NioProcessor createProcessor() { Http11NioProcessor processor = new Http11NioProcessor(proto.getMaxHttpHeaderSize(), (NioEndpoint)proto.endpoint,proto.getMaxTrailerSize()); ... register(processor); return processor; } } } public class NioEndpoint extends AbstractEndpoint { protected class Acceptor extends AbstractEndpoint.Acceptor { public void run(){ try { countUpOrAwaitConnection(); socket = serverSock.accept(); } catch (IOException ioe) { ... } if (running && !paused) { if (!setSocketOptions(socket)) { countDownConnection(); closeSocket(socket); } } protected boolean setSocketOptions(SocketChannel socket) { // Process the connection try { socket.configureBlocking(false); Socket sock = socket.socket(); socketProperties.setProperties(sock); NioChannel channel = nioChannels.poll(); if ( channel == null ) { // SSL setup if (sslContext != null) { … channel = new SecureNioChannel(socket, engine, bufhandler, selectorPool); } else { … channel = new NioChannel(socket, bufhandler); } } else { channel.setIOChannel(socket); } getPoller0().register(channel); } } } public class Poller implements Runnable { public void run() { Iterator<SelectionKey> iterator = keyCount > 0 ? selector.selectedKeys().iterator() : null; while (iterator != null && iterator.hasNext()) { SelectionKey sk = iterator.next(); KeyAttachment attachment = (KeyAttachment) sk.attachment(); // Attachment may be null if another thread has called // cancelledKey() if (attachment == null) { iterator.remove(); } else { attachment.access(); iterator.remove(); processKey(sk, attachment); } } } protected boolean processKey(SelectionKey sk, KeyAttachment attachment) { if (!processSocket(channel, SocketStatus.OPEN, true)){ processSocket(channel, SocketStatus.DISCONNECT, true); } } public boolean processSocket(NioChannel socket, SocketStatus status, boolean dispatch) { SocketProcessor sc = processorCache.poll(); if ( sc == null ) sc = new SocketProcessor(socket,status); else sc.reset(socket,status); if ( dispatch && getExecutor()!=null ) getExecutor().execute(sc); else sc.run(); } } protected class SocketProcessor implements Runnable { public void run() { if (status == null) { state = handler.process( (KeyAttachment) key.attachment(), SocketStatus.OPEN); } else { state = handler.process( (KeyAttachment) key.attachment(), status); } } } } |
Processor:协议处理器,把socket处理成request和response。
public interface Processor<S> { SocketState process(SocketWrapper<S> socketWrapper) throws IOException; } public abstract class AbstractProcessor<S> implements ActionHook, Processor<S> { protected Adapter adapter; protected AbstractEndpoint endpoint; public abstract SocketState process(SocketWrapper<S> socket) } public abstract class AbstractHttp11Processor<S> extends AbstractProcessor<S> { Request request ; Response response ; public SocketState process(SocketWrapper<S> socketWrapper){ setSocketWrapper(socketWrapper); prepareRequest(); //加一些Http的参数给request adapter.service(request, response); } } public class Http11NioProcessor extends AbstractHttp11Processor<NioChannel> { } |
Adaptor用于真正处理请求,也就是把Request和Response传递给上层的Web应用Container
public interface Adapter { //连接了底层的processor和上层的container public void service(Request req, Response res) } public class CoyoteAdapter implements Adapter { public void service(org.apache.coyote.Request req, org.apache.coyote.Response res){ Request request = (Request) req.getNote(ADAPTER_NOTES); //ServletRequest Response response = (Response) res.getNote(ADAPTER_NOTES);//ServletResponse boolean postParseSuccess = postParseRequest(req, request, res, response); connector.getService().getContainer().getPipeline().getFirst().invoke(request, response); //进入容器 } protected boolean postParseRequest(org.apache.coyote.Request req, Request request, org.apache.coyote.Response res, Response response){ //cookie、session、requestLine等等Http组件 connector.getMapper().map(serverName, decodedURI, version, request.getMappingData()); //设置本次请求的host、context、wrapper request.setContext((Context) request.getMappingData().context); request.setWrapper((Wrapper) request.getMappingData().wrapper); parseSessionCookiesId(req, request); parseSessionSslId(request); } } public final class Mapper { protected Host[] hosts = new Host[0]; protected static final class Host extends MapElement { public ContextList contextList = null; } protected static final class ContextList { public Context[] contexts = new Context[0]; } public synchronized void addHost(String name, String[] aliases,Object host) public void addContextVersion(String hostName, Object host, String path,String version, Object context, String[] welcomeResources,javax.naming.Context resources) public void map(MessageBytes host, MessageBytes uri, String version, MappingData mappingData) } public class MapperListener extends LifecycleMBeanBase{ private Mapper mapper = null; @Override public void startInternal() throws LifecycleException { setState(LifecycleState.STARTING); Engine engine = (Engine) connector.getService().getContainer(); addListeners(engine); Container[] conHosts = engine.findChildren(); for (Container conHost : conHosts) { Host host = (Host) conHost; if (!LifecycleState.NEW.equals(host.getState())) { registerHost(host); } } } private void registerHost(Host host) { mapper.addHost(host.getName(), aliases, host); for (Container container : host.findChildren()) { if (container.getState().isAvailable()) { registerContext((Context) container); } } } private void registerContext(Context context) { for (Container container : context.findChildren()) { registerWrapper((Wrapper) container); } } } |
生命周期接口:
public interface Lifecycle { LifeCycle所有需要的时间状态:BEFORE_INIT_EVENT、AFTER_INIT_EVENT、START_EVENT等等 public void init() throws LifecycleException; public void start() throws LifecycleException; public void stop() throws LifecycleException; public void destroy() throws LifecycleException; } public abstract class LifecycleBase implements Lifecycle { private LifecycleSupport lifecycle = new LifecycleSupport(this); public final synchronized void init() throws LifecycleException { if (!state.equals(LifecycleState.NEW)) { invalidTransition(Lifecycle.BEFORE_INIT_EVENT); } setStateInternal(LifecycleState.INITIALIZING, null, false); try { initInternal(); } catch (Throwable t) { ExceptionUtils.handleThrowable(t); setStateInternal(LifecycleState.FAILED, null, false); throw new LifecycleException( sm.getString("lifecycleBase.initFail",toString()), t); } setStateInternal(LifecycleState.INITIALIZED, null, false); } protected abstract void initInternal() throws LifecycleException; protected abstract void stopInternal() throws LifecycleException; ... } public final class LifecycleSupport { private LifecycleListener listeners[] = new LifecycleListener[0]; public void addLifecycleListener(LifecycleListener listener) public void fireLifecycleEvent(String type, Object data) { LifecycleEvent event = new LifecycleEvent(lifecycle, type, data); LifecycleListener interested[] = listeners; for (int i = 0; i < interested.length; i++) interested[i].lifecycleEvent(event); } } public abstract class LifecycleMBeanBase extends LifecycleBase implements MBeanRegistration |
容器的接口:
public interface Container extends Lifecycle { public void addChild(Container child); public Pipeline getPipeline(); public void backgroundProcess(); public void addContainerListener(ContainerListener listener); } public abstract class ContainerBase extends LifecycleMBeanBase{ protected Pipeline pipeline = new StandardPipeline(this); @Override public void backgroundProcess() { cluster.backgroundProcess(); loader.backgroundProcess(); manager.backgroundProcess(); realm.backgroundProcess(); Valve current = pipeline.getFirst(); while (current != null) { try { current.backgroundProcess(); } catch (Exception e) { log.warn(sm.getString("containerBase.backgroundProcess.valve", current), e); } current = current.getNext(); } fireLifecycleEvent(Lifecycle.PERIODIC_EVENT, null); } public void invoke(Request request, Response response) throws IOException, ServletException { pipeline.getFirst().invoke(request, response); } protected class ContainerBackgroundProcessor implements Runnable { public void run() { Thread.sleep(backgroundProcessorDelay * 1000L); processChildren(parent, cl); } protected void processChildren(Container container, ClassLoader cl) { container.backgroundProcess(); Container[] children = container.findChildren(); for (int i = 0; i < children.length; i++) { if (children[i].getBackgroundProcessorDelay() <= 0) { processChildren(children[i], cl); } } } } |
Container的默认实现为ContainerBase,然后对应的四个子类为StandardEngine、StandardHost、StandardContext、StandardWrapper。
public interface Pipeline { public void addValve(Valve valve); public Valve[] getValves(); } class StandardPipeline extends LifecycleBase implements Pipeline, Contained{ protected Valve first = null; protected Valve basic = null; } public interface Valve { public Valve getNext(); public void backgroundProcess(); public void invoke(Request request, Response response) } public abstract class ValveBase extends LifecycleMBeanBase implements Contained, Valve { protected Container container = null; public abstract void invoke(Request request, Response response)throws IOException, ServletException; } |
阀门控制整个请求的流向,Valve则是真正对请求进行处理,Container存储了所需要的所有原始信息
public interface Wrapper extends Container { public Servlet allocate() throws ServletException; public void load() throws ServletException; } class StandardWrapper extends ContainerBase implements ServletConfig, Wrapper{ protected volatile Servlet instance = null; protected InstanceSupport instanceSupport = new InstanceSupport(this); } final class StandardWrapperValve extends ValveBase { public final void invoke(Request request, Response response){ if (!unavailable) { servlet = wrapper.allocate(); } ApplicationFilterChain filterChain = factory.createFilterChain(request, wrapper, servlet); ... filterChain.doFilter(request.getRequest(), response.getResponse()); } public final class ApplicationFilterFactory { public ApplicationFilterChain createFilterChain(ServletRequest request, Wrapper wrapper, Servlet servlet){ filterChain = (ApplicationFilterChain) req.getFilterChain(); filterChain.setServlet(servlet); filterChain.setSupport(((StandardWrapper)wrapper).getInstanceSupport()); } } public final class InstanceSupport { private Wrapper wrapper = null; private InstanceListener listeners[] = new InstanceListener[0]; public void fireInstanceEvent(String type, Filter filter, ServletRequest request, ServletResponse response) { InstanceEvent event = new InstanceEvent(wrapper, filter, type, request, response); InstanceListener interested[] = listeners; for (int i = 0; i < interested.length; i++) interested[i].instanceEvent(event); } } |