tomcat责任链设计模式 FilterChain原理解析

转自:http://javapolo.iteye.com/blog/1287747

今天晚上花了些时间debug了下tomcat,注意观察了下tomcat内部过滤器的实现,其实tomcat内部过滤器采用了责任链的设计模式,

(其实struts2拦截器那一块采用了相似的设计模式),以下是个人对源码的解读,

ApplicationFilterChain详解

首先是对该类的定义的介绍

/**
 * Implementation of javax.servlet.FilterChain used to manage
 * the execution of a set of filters for a particular request.  When the
 * set of defined filters has all been executed, the next call to
 * doFilter() will execute the servlet's service()
 * method itself.
 *
 * @author Craig R. McClanahan
 * @version $Id: ApplicationFilterChain.java 1078022 2011-03-04 15:52:01Z markt $
 */

final class ApplicationFilterChain implements FilterChain, CometFilterChain

第一个疑问是该过滤器链里面的过滤器源于哪里?

答案是该类里面包含了一个ApplicationFilterConfig对象,而该对象则是个filter容器

    /**
     * Filters.
     */
    private ApplicationFilterConfig[] filters =
        new ApplicationFilterConfig[0];

 


以下是ApplicationFilterConfig类的声明
org.apache.catalina.core.ApplicationFilterConfig

Implementation of a javax.servlet.FilterConfig useful in managing the filter instances instantiated when a web application is first started.
当web容器启动是ApplicationFilterConfig自动实例化,它会从该web工程的web.xml文件中读取配置的filter信息,然后装进该容器
 下个疑问是它如何执行该过滤器容器里面的filter呢?

答案是通过pos它来标识当前ApplicationFilterChain(当前过滤器链)执行到哪个过滤器

    /**
     * The int which is used to maintain the current position
     * in the filter chain.
     */
    private int pos = 0;


通过n来记录当前过滤器链里面拥有的过滤器数目

   /**
     * The int which gives the current number of filters in the chain.
     */
    private int n = 0;



通过addFilter方法向容器中添加一个filter(参数即为容器初始化生成的filterConfig对象)

   /**
     * Add a filter to the set of filters that will be executed in this chain.
     *
     * @param filterConfig The FilterConfig for the servlet to be executed
     */
    void addFilter(ApplicationFilterConfig filterConfig)


ApplicationFilterChain采用责任链设计模式达到对不同过滤器的执行
首先ApplicationFilterChain 会调用它重写FilterChain的doFilter方法,然后doFilter里面会调用
 internalDoFilter(request,response)方法;该方法使过滤器容器拿到每个过滤器,然后调用它们重写Filter接口里面的dofilter方法

以下是ApplicationFilterChain 里面重写FilterChain里面的doFilter方法的描述

    /**
     * Invoke the next filter in this chain, passing the specified request
     * and response.  If there are no more filters in this chain, invoke
     * the service() method of the servlet itself.
     *
     * @param request The servlet request we are processing
     * @param response The servlet response we are creating
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet exception occurs
     */
    @Override
    public void doFilter(ServletRequest request, ServletResponse response)


以下是internalDoFilter的部分代码

// Call the next filter if there is one
        if (pos < n) {
            //先拿到下个过滤器,将指针向下移动一位
            ApplicationFilterConfig filterConfig = filters[pos++];
            Filter filter = null;
            try {
                //获取当前指向的filter实例
                filter = filterConfig.getFilter();
                support.fireInstanceEvent(InstanceEvent.BEFORE_FILTER_EVENT,
                                          filter, request, response);
               
                if (request.isAsyncSupported() && "false".equalsIgnoreCase(
                        filterConfig.getFilterDef().getAsyncSupported())) {
                    request.setAttribute(Globals.ASYNC_SUPPORTED_ATTR,
                            Boolean.FALSE);
                }
                if( Globals.IS_SECURITY_ENABLED ) {
                    final ServletRequest req = request;
                    final ServletResponse res = response;
                    Principal principal =
                        ((HttpServletRequest) req).getUserPrincipal();

                    Object[] args = new Object[]{req, res, this};
                    SecurityUtil.doAsPrivilege
                        ("doFilter", filter, classType, args, principal);
                   
                } else { 
                    //filter调用doFilter(request, response, this)方法
                    //ApplicationFilterChain里面的filter都实现了filter                   //接口
                    filter.doFilter(request, response, this);
                }


以下是Filter接口doFilter定义如下
 public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)

过滤器链里面的filter在调用dofilter完成后,会继续调用chain.doFilter(request,response)方法,而这个chain其实就是applicationfilterchain,所以调用过程又回到了上面调用dofilter和调用internalDoFilter方法,这样执行直到里面的过滤器全部执行

当filte都调用完成后,它就会初始化相应的servlet,(例如jsp资源,默认它会开启一个 JspServlet对象)

  

     // We fell off the end of the chain -- call the servlet instance
        try {
            if (ApplicationDispatcher.WRAP_SAME_OBJECT) {
                lastServicedRequest.set(request);
                lastServicedResponse.set(response);
            }

            support.fireInstanceEvent(InstanceEvent.BEFORE_SERVICE_EVENT,
                                      servlet, request, response);


举个例子
假如访问的是个jsp,首先开启一个 JspServlet对象,然后该JspServlet对象会调用它的service方法

以下是个JspServlet的定义以及service方法的描述

public class JspServlet extends HttpServlet implements PeriodicEventListener

 public void service (HttpServletRequest request,
                             HttpServletResponse response)

 //jspFile may be configured as an init-param for this servlet instance     //该jspUri代表访问jsp的相对路径 
        String jspUri = jspFile;


拿到该路径后它就会去判断该jsp是否已经预编译过
            boolean precompile = preCompile(request);

           serviceJspFile(request, response, jspUri, null, precompile);
如果已经编译则直接调用该jsp对应的servlet的_jspService方法
否则先编译在调用

假如是个html页面的访问则直接调用DefaultServlet做相应的处理

简单的分析了下,作为一个学习笔记吧

 

你可能感兴趣的:(tomcat)