当前位置: 代码迷 >> Web前端 >> 2、Jforum核心类 Jforum
  详细解决方案

2、Jforum核心类 Jforum

热度:1910   发布时间:2012-11-08 08:48:11.0
二、Jforum核心类 Jforum
<!-- 核心servlet 处理全部请求 -->
    <servlet>
		<servlet-name>jforum</servlet-name>
		<servlet-class>net.jforum.JForum</servlet-class>
		
		<init-param>
			<param-name>development</param-name>
			<param-value>true</param-value>
		</init-param>
    </servlet>

	<!-- Installer -->
	<!-- 初始化servlet -->
    <servlet>
    	<servlet-name>install</servlet-name>
    	<servlet-class>net.jforum.InstallServlet</servlet-class>
    	
    	<init-param>
			<param-name>development</param-name>
			<param-value>true</param-value>
		</init-param>
    </servlet>

    <!-- Mapping -->
    <servlet-mapping>
    	<servlet-name>install</servlet-name>
    	<url-pattern>/install/install.page</url-pattern>
    </servlet-mapping>
 
	<servlet-mapping>
    	<servlet-name>jforum</servlet-name>
    	<url-pattern>*.page</url-pattern>
    </servlet-mapping>

?

?

package net.jforum;


/**
 *集成基类Servlet JForumBaseServlet
 *JForumBaseServlet 中也是负责对一起基本配置的加载
 *搭配环境
 */
public class JForum extends JForumBaseServlet {
	private static boolean isDatabaseUp;

	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		super.startApplication();

		isDatabaseUp = ForumStartup.startDatabase();
		try {
			Connection conn = DBConnection.getImplementation().getConnection();
			
			/**
			 * 所有的SystemGlobals.getValue都是获取配置文件中的信息,至于他是如何加载配置文件
			 */
			conn.setAutoCommit(!(SystemGlobals
					.getBoolValue("database.use.transactions")));

			MySQLVersionWorkarounder dw = new MySQLVersionWorkarounder();
			dw.handleWorkarounds(conn);

			JForumExecutionContext ex = JForumExecutionContext.get();
			ex.setConnection(conn);
			JForumExecutionContext.set(ex);

			ForumStartup.startForumRepository();
			// 字面意思理解,加载排行,表情,板块信息
			RankingRepository.loadRanks();
			SmiliesRepository.loadSmilies();
			BanlistRepository.loadBanlist();
		} catch (Throwable e) {
			throw new ForumStartupException("Error while starting jforum", e);
		} finally {
			JForumExecutionContext.finish();
		}
	}

	/**
	 * 这个方法是重点,他负调用其他的业务层,处理数据
	 */
	public void service(HttpServletRequest req, HttpServletResponse res)
			throws IOException, ServletException {
		Writer out = null;

		/**
		 * JForumContext保存了容器的一些基本信息 从他的属性可以看出来 
		 * contextPath : 容器路径 不知道怎么理解
		 * servletExtension : servlet的拓展
		 * request :servlet原始请求信息
		 * response : 
		 * isEncodingDisabled : boolean 
		 * isBot : boolean
		 */
		JForumContext forumContext = null;
		RequestContext request = null;
		ResponseContext response = null;
		
		/**
		 * 获取默认编码 在config/database下面 不同的数据库都有
		 */
		String encoding = SystemGlobals.getValue("encoding");
		try {
			JForumExecutionContext ex = JForumExecutionContext.get();

			req.getParameterNames();
			
			/**
			 * WebRequestContext 这个类也非常重要,
			 * 它负责对请求的一些参数处理
			 */
			request = new WebRequestContext(req);
			response = new WebResponseContext(res);

			checkDatabaseStatus();
            
			/**
			 * 创建 JForumContext对象 封装容器基本信息
			 */
			forumContext = new JForumContext(request.getContextPath(),
					SystemGlobals.getValue("servlet.extension"), request,
					response);

			ex.setForumContext(forumContext);

			JForumExecutionContext.set(ex);

			/**
			 * context是封装在jar包里面的容器模板
			 */
			SimpleHash context = JForumExecutionContext.getTemplateContext();

			ControllerUtils utils = new ControllerUtils();
			utils.refreshSession();

			/**
			 * 下面是系列的通用处理过程,把一些配置信息加载到容器当中
			 * ,对我们改装jforum功能作用不大
			 */
			context.put("logged", SessionFacade.isLogged());

			SecurityRepository.load(SessionFacade.getUserSession().getUserId());

			utils.prepareTemplateContext(context, forumContext);

			String module = request.getModule();

			String moduleClass = (module != null) ? ModulesRepository
					.getModuleClass(module) : null;

			if (moduleClass == null) {
				response.sendError(404);
			} else {
				boolean shouldBan = shouldBan(request.getRemoteAddr());

				if (!(shouldBan)) {
					context.put("moduleName", module);
					context.put("action", request.getAction());
				} else {
					moduleClass = ModulesRepository.getModuleClass("forums");
					context.put("moduleName", "forums");
					((WebRequestContext) request).changeAction("banned");
				}

				if ((shouldBan)
						&& (SystemGlobals
								.getBoolValue("banlist.send.403forbidden"))) {
					response.sendError(403);
				} else {
					context.put("language", I18n.getUserLanguage());
					context.put("session", SessionFacade.getUserSession());
					context.put("request", req);
					context.put("response", response);

					out = processCommand(out, request, response, encoding,
							context, moduleClass);
				}
			}
		} catch (Exception e) {
			handleException(out, response, encoding, e, request);
		} finally {
			handleFinally(out, forumContext, response);
		}
	}
}

?

?

?? 如果需要 跟踪数据的话 毫无疑问是从这个类开始,以及编码 等其他信息均可在这里得到。