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. BZOJ 1176 Mokia CDQ分治+树状数组

    1176: [Balkan2007]Mokia Time Limit: 30 Sec  Memory Limit: 162 MBSubmit: 1854  Solved: 821[Submit][St ...

  2. Tarjan 算法详解

    一个神奇的算法,求最大连通分量用O(n)的时间复杂度,真实令人不可思议. 废话少说,先上题目 题目描述: 给出一个有向图G,求G连通分量的个数和最大连通分量. 输入: n,m,表示G有n个点,m条边 ...

  3. php回溯

    $sl = debug_backtrace(); 返回的$sl 是一个二维数组 包含如下元素: function string 当前的函数名,参见: __FUNCTION__. line intege ...

  4. hdu 5206 Four Inages Strategy 判断是否是正方形

    Four Inages Strategy Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem. ...

  5. 如何监控ActiveMQ

    如何监控ActiveMQ在ActiveMQ 4.x中你可以监控代理并查看使用了哪些目标,和连接一起的活动和订阅使用下面的工具进行查看: JMX和JMX控制台,如jConsole http://java ...

  6. Swift使用NSKeyedArchiver进行数据持久化保存的经验

    iOS提供了几种数据持久化保存的方法,有NSKeyedArchiver,Property List,NSUserDefaults和CoreData.我学习下来,觉得保存应用内的诸如列表,记录这些东西, ...

  7. 2016.3 idea 注册码

    idea 最新官方版本:2016.3 idea 注册码 1.下载最新idea 下载地址:https://www.jetbrains.com/idea/ 2.安装 Windows 直接下载 .exe 文 ...

  8. MAC 更新SVN到1.8

    经过谷歌和百度N次后,最终搞定SVN的升级,Intellij Idea和Xcode5.1都能够正常使用. 步骤: 1. 下载Subverion的Max安装版.(推荐.使用其它brew和port都试过, ...

  9. .NET:字符集和编码学习总结

    背景 一直没有深入的学习字符集和编码的知识(现在也没有深入),今天查阅了一些资料,弄明白了一些事情,本文就简单记录一下. 字符集和编码 字符集是指一些符号组成的集合,编码是对指定字符集如何表示为字节的 ...

  10. windows服务与计划任务

    1. 打开Visual Studio 2012新建一个project Solution: 2. 选择Windows->windows Service,修改服务名称:MyFirstService: ...