OFBiz:初始RequestHandler
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的更多相关文章
- ofbiz进击 第六节。 --OFBiz配置之[widget.properties] 配置属性的分析
配置内容分析如下 # -- 定义上下文使用者 -- security.context =default # -- 定义密码限制长度最小值 -- password.length.min =5 # -- ...
- 【转】Ofbiz学习经验谈
不可否认,OFBiz这个开源的系统功能是非常强大的,涉及到的东西太多了,其实对我们现在而言,最有用的只有这么几个:实体引擎.服务引擎.WebTools.用户权限管理.最先要提醒各位的是,在配置一个OF ...
- [OFBiz]开发 三
1. Debug不要在Eclipse中使用Ant来启动ofbiz, 因为在Eclipse中无法kill掉Ant的进程,而ofbiz又没有提供stop的方法.(有一个hook shutdown的方法,但 ...
- 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] ...
- 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 ] ...
- [OFBiz]简介 二
1. 执行ant run-install后,生成了55个ofbiz的jar.加上最初的E:\apache-ofbiz-10.04\framework\entity\lib\ofbiz-minerva. ...
- OFBIZ分享:利用Nginx +Memcached架设高性能的服务
近年来利用Nginx和Memcached来提高站点的服务性能的作法,如一夜春风般的遍及大江南北,越来越多的门户站点和电子商务平台都採用它们来为自己的用户提供更好的服务体验.如:网易.淘宝.京东.凡客等 ...
- OFBiz:解析doRequest()
这里的doRequest()是指RequestHandler中的同名函数: public void doRequest(HttpServletRequest request, HttpServletR ...
- OFBiz:添加样式【转】
原文地址:http://www.cnblogs.com/ofbiz/p/3205851.html 1. 打开themes文件夹,拷贝一份样式作为自己的样式更改初始样式,我这里拷贝的是flatgrey文 ...
随机推荐
- bzoj 4766: 文艺计算姬 -- 快速乘
4766: 文艺计算姬 Time Limit: 1 Sec Memory Limit: 128 MB Description "奋战三星期,造台计算机".小W响应号召,花了三星期 ...
- mysql内存参数整理和条调优以及内存统计
date:20140530auth:Jin 参考:http://dev.mysql.com/doc/refman/5.5/en/server-status-variables.html#http:// ...
- busdog is a filter driver for MS Windows (XP and above) to sniff USB traffic.
https://code.google.com/p/busdog/ busdog is a filter driver for MS Windows (XP and above) to sniff U ...
- VS2010 + IDA SDK 搭建IDA Plugin开发环境
http://www.h4ck.org.cn/2011/11/vs2010-idasdk6-2-ida-plugin-development/ 1. 执行菜单的File->New->Pro ...
- oracle定时任务(dbms_job)
author:skate time:2007-09-12 http://publish.it168.com/2006/0311/20060311017002.shtml 今天总结下Oracle的任务队 ...
- UVa-Ecological Premium
题目地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 源码管理--llorch的Visual Studio基本教程(四)
通用的演示样例说明: 本系列博客仅仅讨论工具的基础,不讨论不论什么语言. 甚至不讨论快捷键:-) 能够用鼠标就完毕本教程 IDE默认指代的是Visual Studio 2013 Community E ...
- 解决sqoop报错:SQLServerException: 将字符串转换为 uniqueidentifier 时失败。
报错栈: Error: java.io.IOException: Cannection handler cannot recover failure: at org.apache.sqoop.mapr ...
- Python的__getattribute__ vs __getattr__的妙用
这里的属性即包括属性变量,也包括属性方法.即类的变量和方法. 当访问某个实例属性时, getattribute会被无条件调用,如未实现自己的getattr方法,会抛出AttributeError提示找 ...
- ECShop 2.x 3.0代码执行漏洞分析
0×00 前言 ECShop是一款B2C独立网店系统,适合企业及个人快速构建个性化网上商店.2.x版本跟3.0版本存在代码执行漏洞. 0×01 漏洞原理 ECShop 没有对 $GLOBAL[‘_SE ...