001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * This file creation date: 27/08/2004 - 18:22:10
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum;
044:
045: import java.io.File;
046: import java.util.Date;
047:
048: import javax.servlet.ServletConfig;
049: import javax.servlet.ServletException;
050: import javax.servlet.http.HttpServlet;
051:
052: import net.jforum.exceptions.ForumStartupException;
053: import net.jforum.repository.BBCodeRepository;
054: import net.jforum.repository.ModulesRepository;
055: import net.jforum.repository.Tpl;
056: import net.jforum.util.I18n;
057: import net.jforum.util.bbcode.BBCodeHandler;
058: import net.jforum.util.preferences.ConfigKeys;
059: import net.jforum.util.preferences.SystemGlobals;
060:
061: import org.apache.commons.lang.StringUtils;
062: import org.apache.log4j.Logger;
063: import org.apache.log4j.xml.DOMConfigurator;
064:
065: import freemarker.cache.FileTemplateLoader;
066: import freemarker.cache.MultiTemplateLoader;
067: import freemarker.cache.TemplateLoader;
068: import freemarker.template.Configuration;
069:
070: /**
071: * @author Rafael Steil
072: * @version $Id: JForumBaseServlet.java,v 1.24 2007/09/21 15:54:31 rafaelsteil Exp $
073: */
074: public class JForumBaseServlet extends HttpServlet {
075: private static Logger logger = Logger
076: .getLogger(JForumBaseServlet.class);
077:
078: protected boolean debug;
079:
080: protected void startApplication() {
081: try {
082: SystemGlobals.loadQueries(SystemGlobals
083: .getValue(ConfigKeys.SQL_QUERIES_GENERIC));
084: SystemGlobals.loadQueries(SystemGlobals
085: .getValue(ConfigKeys.SQL_QUERIES_DRIVER));
086:
087: String filename = SystemGlobals
088: .getValue(ConfigKeys.QUARTZ_CONFIG);
089: SystemGlobals.loadAdditionalDefaults(filename);
090:
091: ConfigLoader.createLoginAuthenticator();
092: ConfigLoader.loadDaoImplementation();
093: ConfigLoader.listenForChanges();
094: ConfigLoader.startSearchIndexer();
095: ConfigLoader.startSummaryJob();
096: } catch (Exception e) {
097: throw new ForumStartupException(
098: "Error while starting JForum", e);
099: }
100: }
101:
102: public void init(ServletConfig config) throws ServletException {
103: super .init(config);
104:
105: try {
106: String appPath = config.getServletContext().getRealPath("");
107: debug = "true".equals(config
108: .getInitParameter("development"));
109:
110: DOMConfigurator.configure(appPath + "/WEB-INF/log4j.xml");
111:
112: logger.info("Starting JForum. Debug mode is " + debug);
113:
114: ConfigLoader.startSystemglobals(appPath);
115: ConfigLoader.startCacheEngine();
116:
117: // Configure the template engine
118: Configuration templateCfg = new Configuration();
119: templateCfg.setTemplateUpdateDelay(2);
120: templateCfg.setSetting("number_format", "#");
121: templateCfg.setSharedVariable("startupTime", new Long(
122: new Date().getTime()));
123:
124: // Create the default template loader
125: String defaultPath = SystemGlobals.getApplicationPath()
126: + "/templates";
127: FileTemplateLoader defaultLoader = new FileTemplateLoader(
128: new File(defaultPath));
129:
130: String extraTemplatePath = SystemGlobals
131: .getValue(ConfigKeys.FREEMARKER_EXTRA_TEMPLATE_PATH);
132:
133: if (StringUtils.isNotBlank(extraTemplatePath)) {
134: // An extra template path is configured, we need a MultiTemplateLoader
135: FileTemplateLoader extraLoader = new FileTemplateLoader(
136: new File(extraTemplatePath));
137: TemplateLoader[] loaders = new TemplateLoader[] {
138: extraLoader, defaultLoader };
139: MultiTemplateLoader multiLoader = new MultiTemplateLoader(
140: loaders);
141: templateCfg.setTemplateLoader(multiLoader);
142: } else {
143: // An extra template path is not configured, we only need the default loader
144: templateCfg.setTemplateLoader(defaultLoader);
145: }
146:
147: ModulesRepository.init(SystemGlobals
148: .getValue(ConfigKeys.CONFIG_DIR));
149:
150: this .loadConfigStuff();
151:
152: if (!this .debug) {
153: templateCfg.setTemplateUpdateDelay(3600);
154: }
155:
156: JForumExecutionContext.setTemplateConfig(templateCfg);
157: } catch (Exception e) {
158: throw new ForumStartupException(
159: "Error while starting JForum", e);
160: }
161: }
162:
163: protected void loadConfigStuff() {
164: ConfigLoader.loadUrlPatterns();
165: I18n.load();
166: Tpl.load(SystemGlobals.getValue(ConfigKeys.TEMPLATES_MAPPING));
167:
168: // BB Code
169: BBCodeRepository.setBBCollection(new BBCodeHandler().parse());
170: }
171: }
|