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. LCA(倍增在线算法) codevs 2370 小机房的树

    codevs 2370 小机房的树 时间限制: 1 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond 题目描述 Description 小机房有棵焕狗种的树,树上有N个节点, ...

  2. Codeforces Round #300 D. Weird Chess 水题

    D. Weird Chess Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/538/proble ...

  3. 使用Spring配置shiro时,自定义Realm中属性无法使用注解注入解决办法

    先来看问题    纠结了几个小时终于找到了问题所在,因为shiro的realm属于Filter,简单说就是初始化realm时,spring还未加载相关业务Bean,那么解决办法就是将springmvc ...

  4. Ext中的get、getDom、getCmp、getBody、getDoc的区别

    Ext中的get.getDom.getCmp.getBody.getDoc的区别Ext中包含了几个以get开头的方法,这些方法可以用来得到文档中DOM.得到当前文档中的组件.得到Ext元素等,在使用中 ...

  5. spring整合mybatis是如何配置事务的?

    作者:郭无心链接:https://www.zhihu.com/question/30206875/answer/84675373来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...

  6. mysql 语句or效率问题

    今天看一同事代码中sql语句的拼接,看到where column=? or column=? .... 一直循环遍历下去,即根据传递进来的数组长度构造sql查询(mysql库) for(int i= ...

  7. ssh 远程登陆指定端口

    ssh 到指定端口  ssh -p xx user@ip      xx 为 端口号    user为用户名   ip为要登陆的ip SSH 原理及远程登录 http://www.ruanyifeng ...

  8. 2013年,移动App设计的13大精髓

    摘要:在 过去的一年里,移动成主流也让众多的移动应用如雨后春笋般层出不穷,在众多开发者从中获利的同时竞争也愈演愈烈,如何才能保证自己立于不败之地?用户是上 帝,一切还得从应用说起.本文总结了新一年里A ...

  9. Could not find com.android.support.constraint:constraint-layout的问题解决

    这几天使用android studio的各种坑之一: Error:Could not find com.android.support.constraint:constraint-layout:1.0 ...

  10. [Todo] Nodejs学习及Spider实验(包括php入门学习、React入门学习)

    /Users/baidu/Documents/Data/Interview/Web-Server开发 深入浅出Node.js-f46c http://blog.csdn.net/u012273376/ ...