JForumBaseServlet.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 5k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright (c) JForum Team
  3.  * All rights reserved.
  4.  * 
  5.  * Redistribution and use in source and binary forms, 
  6.  * with or without modification, are permitted provided 
  7.  * that the following conditions are met:
  8.  * 
  9.  * 1) Redistributions of source code must retain the above 
  10.  * copyright notice, this list of conditions and the 
  11.  * following  disclaimer.
  12.  * 2)  Redistributions in binary form must reproduce the 
  13.  * above copyright notice, this list of conditions and 
  14.  * the following disclaimer in the documentation and/or 
  15.  * other materials provided with the distribution.
  16.  * 3) Neither the name of "Rafael Steil" nor 
  17.  * the names of its contributors may be used to endorse 
  18.  * or promote products derived from this software without 
  19.  * specific prior written permission.
  20.  * 
  21.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT 
  22.  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
  23.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
  24.  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
  25.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  26.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
  27.  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
  29.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
  30.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
  31.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
  32.  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
  33.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
  34.  * IN CONTRACT, STRICT LIABILITY, OR TORT 
  35.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
  36.  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
  37.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  38.  * 
  39.  * This file creation date: 27/08/2004 - 18:22:10
  40.  * The JForum Project
  41.  * http://www.jforum.net
  42.  */
  43. package net.jforum;
  44. import java.io.File;
  45. import java.util.Date;
  46. import javax.servlet.ServletConfig;
  47. import javax.servlet.ServletException;
  48. import javax.servlet.http.HttpServlet;
  49. import net.jforum.exceptions.ForumStartupException;
  50. import net.jforum.repository.BBCodeRepository;
  51. import net.jforum.repository.ModulesRepository;
  52. import net.jforum.repository.Tpl;
  53. import net.jforum.util.I18n;
  54. import net.jforum.util.bbcode.BBCodeHandler;
  55. import net.jforum.util.preferences.ConfigKeys;
  56. import net.jforum.util.preferences.SystemGlobals;
  57. import org.apache.commons.lang.StringUtils;
  58. import org.apache.log4j.Logger;
  59. import org.apache.log4j.xml.DOMConfigurator;
  60. import freemarker.cache.FileTemplateLoader;
  61. import freemarker.cache.MultiTemplateLoader;
  62. import freemarker.cache.TemplateLoader;
  63. import freemarker.template.Configuration;
  64. /**
  65.  * @author Rafael Steil
  66.  * @version $Id: JForumBaseServlet.java,v 1.24 2007/09/21 15:54:31 rafaelsteil Exp $
  67.  */
  68. public class JForumBaseServlet extends HttpServlet
  69. {
  70. private static Logger logger = Logger.getLogger(JForumBaseServlet.class);
  71. protected boolean debug;
  72. protected void startApplication()
  73. {
  74. try {
  75. SystemGlobals.loadQueries(SystemGlobals.getValue(ConfigKeys.SQL_QUERIES_GENERIC));
  76. SystemGlobals.loadQueries(SystemGlobals.getValue(ConfigKeys.SQL_QUERIES_DRIVER));
  77. String filename = SystemGlobals.getValue(ConfigKeys.QUARTZ_CONFIG);
  78. SystemGlobals.loadAdditionalDefaults(filename);
  79. ConfigLoader.createLoginAuthenticator();
  80. ConfigLoader.loadDaoImplementation();
  81. ConfigLoader.listenForChanges();
  82. ConfigLoader.startSearchIndexer();
  83. ConfigLoader.startSummaryJob();
  84. }
  85. catch (Exception e) {
  86. throw new ForumStartupException("Error while starting JForum", e);
  87. }
  88. }
  89. public void init(ServletConfig config) throws ServletException
  90. {
  91. super.init(config);
  92. try {
  93. String appPath = config.getServletContext().getRealPath("");
  94. debug = "true".equals(config.getInitParameter("development"));
  95. DOMConfigurator.configure(appPath + "/WEB-INF/log4j.xml");
  96. logger.info("Starting JForum. Debug mode is " + debug);
  97. ConfigLoader.startSystemglobals(appPath);
  98. ConfigLoader.startCacheEngine();
  99. // Configure the template engine
  100. Configuration templateCfg = new Configuration();
  101. templateCfg.setTemplateUpdateDelay(2);
  102. templateCfg.setSetting("number_format", "#");
  103. templateCfg.setSharedVariable("startupTime", new Long(new Date().getTime()));
  104. // Create the default template loader
  105. String defaultPath = SystemGlobals.getApplicationPath() + "/templates";
  106. FileTemplateLoader defaultLoader = new FileTemplateLoader(new File(defaultPath));
  107. String extraTemplatePath = SystemGlobals.getValue(ConfigKeys.FREEMARKER_EXTRA_TEMPLATE_PATH);
  108. if (StringUtils.isNotBlank(extraTemplatePath)) {
  109. // An extra template path is configured, we need a MultiTemplateLoader
  110. FileTemplateLoader extraLoader = new FileTemplateLoader(new File(extraTemplatePath));
  111. TemplateLoader[] loaders = new TemplateLoader[] { extraLoader, defaultLoader };
  112. MultiTemplateLoader multiLoader = new MultiTemplateLoader(loaders);
  113. templateCfg.setTemplateLoader(multiLoader);
  114. else {
  115. // An extra template path is not configured, we only need the default loader
  116. templateCfg.setTemplateLoader(defaultLoader);
  117. }
  118. ModulesRepository.init(SystemGlobals.getValue(ConfigKeys.CONFIG_DIR));
  119. this.loadConfigStuff();
  120. if (!this.debug) {
  121. templateCfg.setTemplateUpdateDelay(3600);
  122. }
  123. JForumExecutionContext.setTemplateConfig(templateCfg);
  124. }
  125. catch (Exception e) {
  126. throw new ForumStartupException("Error while starting JForum", e);
  127. }
  128. }
  129. protected void loadConfigStuff()
  130. {
  131. ConfigLoader.loadUrlPatterns();
  132. I18n.load();
  133. Tpl.load(SystemGlobals.getValue(ConfigKeys.TEMPLATES_MAPPING));
  134. // BB Code
  135. BBCodeRepository.setBBCollection(new BBCodeHandler().parse());
  136. }
  137. }