dwr通过WebContextFactory获取请求对象

来源:互联网 发布:ov7670 丢失像素数据 编辑:程序博客网 时间:2024/05/21 09:05
WebContextFactory是工厂,方法都是静态的,持有一个Builder实例和Builder的接口

WebContextBuilder是产品的制造机器,他的实现类可以自定义,不同的实现类,对应不同的产品制造方式。

WebContext 是产品

WebContextFactory对WebContextBuilder进行了封装,让购买者和产品的生产进行解耦

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public class WebContextFactory  
  2. {  
  3.     public static WebContext get()  
  4.     {  
  5.         if (builder == null)  
  6.         {  
  7.             log.warn("Missing WebContextBuilder. Is DWR setup properly?");  
  8.             return null;  
  9.         }  
  10.   
  11.         return builder.get();  
  12.     }  
  13.   
  14.   
  15.     private static WebContextBuilder builder = null;  
  16.   
  17.     //此方法在容器启动时调用,所以WebContextBuilder的实现类可以自己定义,在启动时注入  
  18.     public static void attach(Container container)  
  19.     {  
  20.         WebContextFactory.builder = container.getBean(WebContextBuilder.class);  
  21.     }  
  22.   
  23.   
  24.     public interface WebContextBuilder  
  25.     {  
  26.   
  27.         WebContext get();  
  28.   
  29.   
  30.         void engageThread(Container container, HttpServletRequest request, HttpServletResponse response);  
  31.   
  32.   
  33.         void disengageThread();  
  34.     }  
  35.   
  36.     /** 
  37.      * The log stream 
  38.      */  
  39.     private static final Log log = LogFactory.getLog(WebContextFactory.class);  
  40. }  

DwrServlet 类webContextBuilder 的使用

doPost方法里使用构建器webContextBuilder 负责本线程webContext对象的生产和销毁

在后面Handler方法里使用工厂WebContextFactory生产产品

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public void init(ServletConfig servletConfig) throws ServletException  
  2.     {  
  3.         super.init(servletConfig);  
  4.   
  5.         try  
  6.         {  
  7.             StartupUtil.logStartup(getClass().getSimpleName(), servletConfig);  
  8.   
  9.   
  10.             container = createContainer(servletConfig);  
  11.             //DwrServlet启动的时候赋予webContextBuilder的实现类对象  
  12.             webContextBuilder = container.getBean(WebContextBuilder.class);  
  13.   
  14.   
  15.             configureContainer(container, servletConfig);  
  16.         }  
  17.         catch (ServletException ex)  
  18.         {  
  19.             throw ex;  
  20.         }  
  21.         catch (Exception ex)  
  22.         {  
  23.             log.fatal("init failed", ex);  
  24.             throw new ServletException(ex);  
  25.         }  
  26.         finally  
  27.         {  
  28.             if (webContextBuilder != null)  
  29.             {  
  30.                 webContextBuilder.disengageThread();  
  31.             }  
  32.         }  
  33.     }  
  34.     @Override  
  35.     public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException  
  36.     {  
  37.         try  
  38.         {  
  39.         //由webContextBuilder生产一个webContext对象,  
  40.         //此webContext对象在此线程的后面整个请求处理的过程中都能通过WebContextFactory.get()访问到  
  41.         //webContextBuilder内部使用ThreadLocal来保存webContext对象,所以只用本线程能访问  
  42.             webContextBuilder.engageThread(container, request, response);  
  43.   
  44.   
  45.             UrlProcessor processor = container.getBean(UrlProcessor.class);  
  46.             processor.handle(request, response);  
  47.         }  
  48.         finally  
  49.         {   //由webContextBuilder回收一个webContext产品  
  50.             webContextBuilder.disengageThread();  
  51.         }  
  52.     }  
0 0
原创粉丝点击