RequestHandler,可以称之为请求处理器,在ControlServlet.init()中初始化:

public class ControlServlet extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config); // configure custom BSF engines
configureBsf(); // initialize the request handler
getRequestHandler();
} protected RequestHandler getRequestHandler() {
return RequestHandler.getRequestHandler(getServletContext());
}
}

ControlServlet没有为RequestHandler定义变量,而是放在ServletContext中。一个ServletContext只允许有一个RequestHandler。

public class RequestHandler {
public static RequestHandler getRequestHandler(ServletContext servletContext) {
RequestHandler rh = (RequestHandler) servletContext.getAttribute("_REQUEST_HANDLER_");
if (rh == null) {
rh = new RequestHandler();
servletContext.setAttribute("_REQUEST_HANDLER_", rh);
rh.init(servletContext);
}
return rh;
}
}

RequestHandler的构造器是空的,它的初始化放在init()中。

public class RequestHandler {
public void init(ServletContext context) {
this.context = context; // init the ControllerConfig, but don't save it anywhere
// 初始化ControllerConfig, 但是不要保存它
this.controllerConfigURL = ConfigXMLReader.getControllerConfigURL(context);
ConfigXMLReader.getControllerConfig(this.controllerConfigURL); this.viewFactory = new ViewFactory(this);
this.eventFactory = new EventFactory(this);
}
}

RequestHandler对应的配置文件是WEB-INF/controller.xml,透过ConfigXMLReader处理。OFBiz处理controller.xml使用了缓存,超时时会被自动清理。所以,当OFBiz类中需要ControllerConfig时,不会定义一个变量保存ControllerConfig,而只是定义一个controller.xml对应的URL变量,因为ConfigXMLReader以URL为Key缓存ControllerConfig。RequestHandler的doRequest()获取ControllerConfig就是这样处理的:

public class RequestHandler {
public void doRequest(HttpServletRequest request, HttpServletResponse response, String chain,
GenericValue userLogin, Delegator delegator) throws RequestHandlerException {
... ... // get the controllerConfig once for this method so we don't have to get it over and over inside the method
ConfigXMLReader.ControllerConfig controllerConfig = this.getControllerConfig();
Map<String, ConfigXMLReader.RequestMap> requestMapMap = controllerConfig.getRequestMapMap(); ... ...
} public ConfigXMLReader.ControllerConfig getControllerConfig() {
return ConfigXMLReader.getControllerConfig(this.controllerConfigURL);
}
}

RequestHandler包含了一个ViewFactory对象和一个EventFactory对象。ViewFactory对象的初始化由自身的构造器完成。

public class ViewFactory {
public ViewFactory(RequestHandler requestHandler) {
this.handlers = FastMap.newInstance();
this.requestHandler = requestHandler;
this.context = requestHandler.getServletContext(); // pre-load all the view handlers
try {
this.preLoadAll();
} catch (ViewHandlerException e) {
Debug.logError(e, module);
throw new GeneralRuntimeException(e);
}
}
}

ViewFactory初始时,调用preLoadAll()装入所有在controller.xml中定义的ViewHandler。

public class ViewFactory {
private void preLoadAll() throws ViewHandlerException {
Set<String> handlers = this.requestHandler.getControllerConfig().getViewHandlerMap().keySet();
if (handlers != null) {
for (String type: handlers) {
this.handlers.put(type, this.loadViewHandler(type));
}
} // load the "default" type
if (!this.handlers.containsKey("default")) {
try {
ViewHandler h = (ViewHandler) ObjectType.getInstance("org.ofbiz.webapp.view.JspViewHandler");
h.init(context);
this. handlers.put("default", h);
} catch (Exception e) {
throw new ViewHandlerException(e);
}
}
}
}

loadViewHandler()执行具体的ViewHandler初始化。

public class ViewFactory {
private ViewHandler loadViewHandler(String type) throws ViewHandlerException {
ViewHandler handler = null;
String handlerClass = this.requestHandler.getControllerConfig().getViewHandlerMap().get(type);
if (handlerClass == null) {
throw new ViewHandlerException("Unknown handler type: " + type);
} try {
handler = (ViewHandler) ObjectType.getInstance(handlerClass);
handler.setName(type);
handler.init(context);
} catch (ClassNotFoundException cnf) {
//throw new ViewHandlerException("Cannot load handler class", cnf);
Debug.logWarning("Warning: could not load view handler class because it was not found; note that some views may not work: " + cnf.toString(), module);
} catch (InstantiationException ie) {
throw new ViewHandlerException("Cannot get instance of the handler", ie);
} catch (IllegalAccessException iae) {
throw new ViewHandlerException(iae.getMessage(), iae);
} return handler;
}
}

ViewHandler对象创建后,紧接着执行其init()方法。以具体的VelocityViewHandler为例,其inti()方法就完成了VelocityEngine的初始化。

public class VelocityViewHandler extends AbstractViewHandler {
public void init(ServletContext context) throws ViewHandlerException {
try {
Debug.logInfo("[VelocityViewHandler.init] : Loading...", module);
ve = new VelocityEngine();
// set the properties
// use log4j for logging
// use classpath template loading (file loading will not work in WAR)
ve.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
"org.apache.velocity.runtime.log.Log4JLogSystem");
ve.setProperty("runtime.log.logsystem.log4j.category", module); Properties props = null;
try {
props = UtilProperties.getProperties(context.getResource("/WEB-INF/velocity.properties"));
Debug.logInfo("[VelocityViewHandler.init] : Loaded /WEB-INF/velocity.properties", module);
} catch (MalformedURLException e) {
Debug.logError(e, module);
}
if (props == null) {
props = new Properties();
Debug.logWarning("[VelocityViewHandler.init] : Cannot load /WEB-INF/velocity.properties. " +
"Using default properties.", module);
} // set the file loader path -- used to mount the webapp
if (context.getRealPath("/") != null) {
props.setProperty("file.resource.loader.path", context.getRealPath("/"));
Debug.logInfo("[VelocityViewHandler.init] : Got true webapp path, mounting as template path.", module);
} ve.init(props);
} catch (Exception e) {
throw new ViewHandlerException(e.getMessage(), e);
}
}
}

OFBiz:初始RequestHandler的更多相关文章

  1. ofbiz进击 第六节。 --OFBiz配置之[widget.properties] 配置属性的分析

    配置内容分析如下 # -- 定义上下文使用者 -- security.context =default # -- 定义密码限制长度最小值 -- password.length.min =5 # -- ...

  2. 【转】Ofbiz学习经验谈

    不可否认,OFBiz这个开源的系统功能是非常强大的,涉及到的东西太多了,其实对我们现在而言,最有用的只有这么几个:实体引擎.服务引擎.WebTools.用户权限管理.最先要提醒各位的是,在配置一个OF ...

  3. [OFBiz]开发 三

    1. Debug不要在Eclipse中使用Ant来启动ofbiz, 因为在Eclipse中无法kill掉Ant的进程,而ofbiz又没有提供stop的方法.(有一个hook shutdown的方法,但 ...

  4. OFBIZ bug_ControlServlet.java:233:ERROR

    错误日志: [java] 2014-09-26 10:12:17,031 (http-bio-0.0.0.0-8443-exec-5) [ ControlServlet.java:233:ERROR] ...

  5. OFBIZ bug_ControlServlet.java:239:ERROR

    错误日志: [java] 2014-09-23 00:11:34,877 (http-bio-0.0.0.0-8080-exec-4) [ ControlServlet.java:141:INFO ] ...

  6. [OFBiz]简介 二

    1. 执行ant run-install后,生成了55个ofbiz的jar.加上最初的E:\apache-ofbiz-10.04\framework\entity\lib\ofbiz-minerva. ...

  7. OFBIZ分享:利用Nginx +Memcached架设高性能的服务

    近年来利用Nginx和Memcached来提高站点的服务性能的作法,如一夜春风般的遍及大江南北,越来越多的门户站点和电子商务平台都採用它们来为自己的用户提供更好的服务体验.如:网易.淘宝.京东.凡客等 ...

  8. OFBiz:解析doRequest()

    这里的doRequest()是指RequestHandler中的同名函数: public void doRequest(HttpServletRequest request, HttpServletR ...

  9. OFBiz:添加样式【转】

    原文地址:http://www.cnblogs.com/ofbiz/p/3205851.html 1. 打开themes文件夹,拷贝一份样式作为自己的样式更改初始样式,我这里拷贝的是flatgrey文 ...

随机推荐

  1. [转载]for循环的执行顺序

    原文地址:for循环的执行顺序作者:想飞上天的美人鱼   for循环的执行顺序用如下表达式: for(expression1;expression2;expression3) { expression ...

  2. [转]Android Studio开发入门-引用jar及so文件

    注意: 1.jar包在app的libs目录 2.so文件放在src/main”目录中名为“jniLibs”的目录 一.引用jar文件    1.将jar文件复制.粘贴到app的libs目录中:    ...

  3. linux开发node相关的工具

    epel-release yum install epel-release node yum install nodejs mongodb 安装mongodb服务器端 yum install mong ...

  4. Java:volatile 关键字的一点理解

    背景 学了六年C#,一直没有使用过 volatile,对多线程编程也是偶尔才会使用,这次学习 Java 又遇到了 volatile,准备稍微深入的了解一下. volatile 的作用? 几乎所有支持这 ...

  5. 在Android 5.0中使用JobScheduler

    在Android 5.0中使用JobScheduler 原文链接 : using-the-jobscheduler-api-on-android-lollipop 译者 : Mr.Simple 校对者 ...

  6. 将List集合中的map对象转为List<对象>形式--封装类

    import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Li ...

  7. 深入理解Apache Flink核心技术

    深入理解Apache Flink核心技术 2016年02月18日 17:04:03 阅读数:1936 标签: Apache-Flink数据流程序员JVM   版权声明:本文为博主原创文章,未经博主允许 ...

  8. Dede(织梦) CMS SQL Injection Vulnerability

    测试方法: @Sebug.net   dis本站提供程序(方法)可能带有攻击性,仅供安全研究与教学之用,风险自负! # Dede Cms All Versions Sql Vulnerability ...

  9. Android -- 再来一发Json

    之前写过一篇Json的博客,当时用的是Gson包来解析的,这次来此自带的org.json来解析.传送门:<Gson解析复杂Json数据> JSON的结构 (1) Name/Value Pa ...

  10. Cognos TM1_10.1.1服务端安装

    出于对bi行业的强大热爱,出于对cognos tm1的强大兴趣,于是就想研究一下Cognos TM1(table manager one),今天就分享一下自己微不足道研究成果,真可谓是tm1的九牛一毛 ...