上善若水
          In general the OO style is to use a lot of little objects with a lot of little methods that give us a lot of plug points for overriding and variation. To do is to be -Nietzsche, To bei is to do -Kant, Do be do be do -Sinatra
          posts - 146,comments - 147,trackbacks - 0
          <2014年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿(4)

          隨筆分類(157)

          隨筆檔案(125)

          收藏夾(13)

          Java GC

          Java General

          NoSQL

          Tech General

          Tech Master

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 895621
          • 排名 - 42

          最新評論

          閱讀排行榜

          評論排行榜

          概述

          在很多框架的設計中,都有類似Channel鏈的設計,類似Decorator模式或Chain Of Responsibility模式,可以向這個Channel注冊不同的Handler,用戶請求可以穿越這個由多個Handler組成的Channel,執行響應的切面邏輯,在最后一個Handler或者另一個Processor處理用于自定義的業務邏輯,然后生成的響應可以逆著方向從這個Channel回來。Structs2中的Interceptor、Servlet中Filter都采用這種設計。這種設計為面向切面編程提供了遍歷,然而目前的Handler設計更多的像是Chain Of Responsibility模式,它的處理邏輯只能從鏈頭走到鏈尾,而沒有返回的路程。引入ScopedHandler的目的就是用于解決這個問題。

          實現

          Structs2中Interceptor實現使用傳入ActionInvaction來調用Channel中的后繼Interceptor:
          public class MyInterceptor extends AbstractInterceptor {
              @Override
               public String intercept(ActionInvocation ai) throws Exception {
                  try {
                       // Add user customized logic here when request come into the channel
                       return ai.invoke();
                  } finally {
                       // Add user customized logic here when response pass out across the channel
                  }        
              } 
          }
          類似的,Servlet中的Filter實現也是使用FilterChain來調用Channel中后繼的Filter:
          public class MyFilter implements Filter {
              @Override
              public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
                  try {
                      // Add user customized logic here when request come into the channel
                      chain.doFilter(request, response);
                  } finally {
                      // Add user customized logic here when response pass out across the channel
                  }
              }
          }

          ScopedHandler采用了不同的實現方式,首先它繼承自HandlerWrapper,因而它使用HandlerWrapper中的Handler字段來構建Handler鏈,然而并不是所有的Handler都是ScopedHandler,因而ScopedHandler內部還定義了_nextScope字段用于創建在這條Handler鏈表中的ScopedHandler鏈表,以及_outerScope字段用于將自己始終和這個ScopedHandler鏈表中的最外層ScopedHandler相連,對這個ScopedHandler的最外層列表,其_outScope字段為null。而ScopedHandler要實現的行為是,假設A、B、C都是ScopedHandler,并且它們組成鏈表:A->B->C,那么當調用A.handle()方法時的調用堆棧是:
          A.handle()
          |-A.doScope()
          |--B.doScope()
          |----C.doScope()
          |-----A.doHandle()
          |------B.doHandle()
          |-------C.doHandle()
          而如果A、B是ScopedHandler,X、Y是其他的Handler,并且它們組成鏈表:A->X->B->Y,那么當調用A.handle()方法時的調用棧是:
          A.handle()
          |-A.doScope()
          |--B.doScope()
          |---A.doHandle()
          |----X.handle()
          |-----B.doHandle()
          |------Y.handle()

          這種行為主要用于Servlet框架的實現,它可以保證在doScope()方法中做一些初始化工作,并且配置環境,而在后繼調用中都可以使用這些配置好的環境,并且doHandle()的順序還是使用原來定義的Handler鏈表順序,即使有些Handler并不是ScopedHandler。

          在實現中,其Handler鏈表由HandlerWrapper構建,在doStart()方法中計算_nextScope字段以及_outerScope字段,在handle()方法中,如果_outerScope為null,則調用doScope()方法,否則調用doHandle()方法:
              @Override
              public final void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
              {
                  if (_outerScope==null)  
                      doScope(target,baseRequest,request, response);
                  else 
                      doHandle(target,baseRequest,request, response);
              }
          在執行完doScope()方法后,調用nextScope()方法,該方法順著_nextScope鏈表走,直到盡頭,后調用doHandle()方法:
              public final void nextScope(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
              {
                  if (_nextScope!=null)
                      _nextScope.doScope(target,baseRequest,request, response);
                  else if (_outerScope!=null)
                      _outerScope.doHandle(target,baseRequest,request, response);
                  else 
                      doHandle(target,baseRequest,request, response);
              }
          而doHandle()方法在完成是調用nextHandle()方法,它也沿著_nextScope鏈表走,只要_nextScope和_handler相同,則調用其doHandle()方法,但是如果_nextScope和_handler不同,則調用_handler中的handle()方法,用于處理在ScopedHandler鏈表中插入非ScopedHandler的情況:
              public final void nextHandle(String target, final Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
              {
                  if (_nextScope!=null && _nextScope==_handler)
                      _nextScope.doHandle(target,baseRequest,request, response);
                  else if (_handler!=null)
                      _handler.handle(target,baseRequest, request, response);
              }

          使用

          在ScopedHandler的測試用例中給出了一個非常好的例子。首先有一個TestHandler繼承自ScopedHandler:
              private class TestHandler extends ScopedHandler {
                  private final String _name;

                  private TestHandler(String name) {
                      _name=name;
                  }

                  @Override
                  public void doScope(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
                      try {
                          _history.append(">S").append(_name);
                          super.nextScope(target,baseRequest,request, response);
                      } finally {
                          _history.append("<S").append(_name);
                      }
                  }

                  @Override
                  public void doHandle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
                      try {
                          _history.append(">W").append(_name);
                          super.nextHandle(target,baseRequest,request,response);
                      } finally {
                          _history.append("<W").append(_name);
                      }
                  }
              }
          然后有非ScopedHandler的實現:
              private class OtherHandler extends HandlerWrapper {
                  private final String _name;

                  private OtherHandler(String name) {
                      _name=name;
                  }

                  @Override
                  public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
                      try {
                          _history.append(">H").append(_name);
                          super.handle(target,baseRequest,request, response);
                      } finally {
                          _history.append("<H").append(_name);
                      }
                  }
              }
          查看一下Test Case的執行結果:
              @Test
              public void testDouble() throws Exception
              {
                  TestHandler handler0 = new TestHandler("0");
                  OtherHandler handlerA = new OtherHandler("A");
                  TestHandler handler1 = new TestHandler("1");
                  OtherHandler handlerB = new OtherHandler("B");
                  handler0.setHandler(handlerA);
                  handlerA.setHandler(handler1);
                  handler1.setHandler(handlerB);
                  handler0.start();
                  handler0.handle("target",null,null,null);
                  handler0.stop();
                  String history=_history.toString();
                  System.err.println(history);
                  assertEquals(">S0>S1>W0>HA>W1>HB<HB<W1<HA<W0<S1<S0",history);
              }
          posted on 2014-05-07 23:49 DLevin 閱讀(1422) 評論(3)  編輯  收藏 所屬分類: Jetty

          FeedBack:
          # re: 深入Jetty源碼之ScopedHandler
          2014-05-08 08:25 | 金利鎖業
          支持博主分享啊  回復  更多評論
            
          # re: 深入Jetty源碼之ScopedHandler
          2014-05-08 08:52 | regale
          請問_history是怎么傳進去的?  回復  更多評論
            
          # re: 深入Jetty源碼之ScopedHandler
          2014-05-08 09:17 | DLevin
          代碼沒有貼全,TestHandler和OtherHandler都是Test類中的內部類,而_history是Test類的成員@regale
            回復  更多評論
            
          主站蜘蛛池模板: 定远县| 化州市| 芜湖县| 顺昌县| 龙川县| 宝清县| 和顺县| 军事| 五华县| 阿鲁科尔沁旗| 怀仁县| 康马县| 舞钢市| 左权县| 盱眙县| 筠连县| 屯门区| 喀喇沁旗| 揭阳市| 左权县| 安图县| 上犹县| 谷城县| 河源市| 宣威市| 丘北县| 山阳县| 建水县| 松溪县| 富裕县| 离岛区| 盐亭县| 汾阳市| 元江| 安仁县| 娱乐| 烟台市| 顺义区| 东阿县| 澄迈县| 四子王旗|