JForum.java可以稱爲是jforum的引擎,它繼承自net.jforum.JForumBaseServlet類,並間接的繼承了HttpServlet。當第一次請求該應用的時候,servlet容器將加載該類,並調用其初始化方法init(ServletConfig config),並調用service方法處理請求。(但最好配置成加載應用的時候就初始化該類)
一、init(ServletConfig config)
(一) 調用其父類init(config)方法
該方法主要職責是加載主要配置文件,配置jfreemarker的模板引擎,並保存到net.jforum. JForumExecutionContext類。
詳細內容包括:
1. 獲取servletContext路徑;
2. 判斷應用開發模式;
3. 設置log4j文件路徑;
4. 調用ConfigLoader.startSystemglobals(appPath)加載全局變量文件SystemGlobals.properties以及用戶自定義的配置文件,例如,mysql.properties文件。
5. 調用ConfigLoader.startCacheEngine()啓動緩存引擎
l 獲取緩存引擎實現類,SystemGlobals.getValue(…)
l 加載並初始化緩存引擎類
l 獲取、加載並實例化可緩存的對象(主要是repository包下的類,如ForumRepository),同時將緩存引擎實例注入到可緩存的對象中。
6. 創建freemark的Configuration類實例,並進行相應設置
l templateCfg.setTemplateUpdateDelay(2);
l templateCfg.setSetting("number_format", "#");
l templateCfg.setSharedVariable("startupTime", new Long(new Date().getTime()));
l String defaultPath = SystemGlobals.getApplicationPath() + "/templates";
l FileTemplateLoader defaultLoader = new FileTemplateLoader(new File(defaultPath));
l templateCfg.setTemplateLoader(defaultLoader);
l 調用ModulesRepository.init(SystemGlobals.getValue(ConfigKeys.CONFIG_DIR)),加載modulesMapping.properties模塊映射文件
7. 調用this.loadConfigStuff()方法,加載其他屬性文件
l ConfigLoader.loadUrlPatterns(),加載urlPattern.properties文件
l I18n.load();加載國際化文件
l Tpl.load(SystemGlobals.getValue(ConfigKeys.TEMPLATES_MAPPING)),加載templatesMapping.properties文件
l BBCodeRepository.setBBCollection(new BBCodeHandler().parse());其加載並處理了bb_config.xml文件
8. 調用JForumExecutionContext.setTemplateConfig(templateCfg)
(二) 調用父類startApplication()方法
1. SystemGlobals.loadQueries(SystemGlobals.getValue(ConfigKeys.SQL_QUERIES_GENERIC));加載generic_quries.sql文件
2. SystemGlobals.loadQueries(SystemGlobals.getValue(ConfigKeys.SQL_QUERIES_DRIVER));加載相應數據庫的sql文件
3. String filename = SystemGlobals.getValue(ConfigKeys.QUARTZ_CONFIG);
4. SystemGlobals.loadAdditionalDefaults(filename);
5. 加載任務調度文件quartz-jforum.properites
6. ConfigLoader.createLoginAuthenticator();
7. 創建net.jforum.sso.DefaultLoginAuthenticator對象,並註冊到SystemGlobals對象中。
8. ConfigLoader.loadDaoImplementation();
9. 加載並初始化net.jforum.dao.mysql.MysqlDataAccessDriver類,該類是訪問各種DAO入口。
10. ConfigLoader.listenForChanges();
11. ConfigLoader.startSearchIndexer();
12. ConfigLoader.startSummaryJob();
(三) 調用ForumStartup.startDatabase()方法
創建DBConnection實例
(四) 獲得數據庫連接DBConnection.getImplementation().getConnection()
(五) 獲取net.jforum.JForumExecutionContext 的對象ex = JForumExecutionContext.get()並進行相關設置ex.setConnection(conn);JForumExecutionContext.set(ex);
(六) 調用ForumStartup.startForumRepository()
加載論壇主要信息,包括ForumDAO, CategoryDAO, ConfigDAO的相關信息以及論壇的總體數據(loadUsersInfo()如用戶總數,loadMostUsersEverOnline()最高峯值)
(七) RankingRepository.loadRanks()
(八) SmiliesRepository.loadSmilies()
(九) BanlistRepository.loadBanlist()
二、service(HttpServletRequest req, HttpServletResponse res)方法
(一) request = new WebRequestContext(req);對reqeust對象進行包裝,完成了對url路徑的解析,將module、action、paremeter等保存request對象的query文件屬性中。
(二) 實例化JForumContext對象forumContext = new
JForumContext(request.getContextPath(),SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION), request, response);
(三) 將forumContext註冊到JForumExecutionContext
ex = JForumExecutionContext.get();ex.setForumContext(forumContext);
JForumExecutionContext.set(ex);
(四) 進一步進行freemarker的配置模板引擎
l ontrollerUtils utils = new ControllerUtils();
l utils.refreshSession();
l context.put("logged", SessionFacade.isLogged());
l utils.prepareTemplateContext(context, forumContext);
context爲freemarker中的SimpleHash類對象,context是真正的主角,所有需要再頁面使用的變量都需放到該對象中。
(五) 調用this.processCommand(out, request, response, encoding, context, moduleClass)
l 獲取moduleClass所對應的Command對象實例
l 進行請求處理並返回Tamplate對象
調用template.process(JForumExecutionContext.getTemplateContext(), out)進行輸出顯示
三.幾個重要java類
1. net.jforum.util.preferences.SystemGlobls.java
存儲了系統的全局變量,通過該類可以獲取SystemGlobals.properties和相關配置文件的值。主要存儲內容包括:
l defaults = new Properties(),用於存儲SystemGlobals.properties中的屬性
l installation = new Properties(),用於存儲額外的屬性配置文件,主要是安裝jforum時候生成的jforum-custom.conf文件。
l additionalDefaultsList = new ArrayList(),用於記錄所加載過的附加的屬性文件名。
l queries = new Properties(),用於存儲sql語句
l transientValues = new Properties()存放瞬時變量
l objectProperties = new HashMap(),存儲對象
2. net.jforum.ConfigLoader.java
加載forum配置文件的通用工具類。主要方法及作用如下:
l startSystemglobals(String appPath)
該方法調用了SystemGlobals的方法加載了SystemGlobals.properties、jforum-custom.cof、mysql.properties文件。
l startCacheEngine()
實例化緩存引擎對象、實例化可緩存的對象(repository包下的類以及SessionFacade類)、將緩存引擎對象注入到可緩存的對象中。
l loadModulesMapping(String baseConfigDir)加載 ModulesMapping.proerties。
l loadUrlPatterns(),加載urlPattern.properties文件,解析保存到UrlPatternCollection類中
l createLoginAuthenticator(),創建登陸驗證對象,登記到SystemGloble.java對象中