KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > JForumBaseServlet


1 /*
2  * Copyright (c) Rafael Steil
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
45 import java.io.File JavaDoc;
46 import java.util.Date JavaDoc;
47
48 import javax.servlet.ServletConfig JavaDoc;
49 import javax.servlet.ServletException JavaDoc;
50 import javax.servlet.http.HttpServlet JavaDoc;
51
52 import net.jforum.exceptions.ForumStartupException;
53 import net.jforum.repository.BBCodeRepository;
54 import net.jforum.repository.ModulesRepository;
55 import net.jforum.repository.Tpl;
56 import net.jforum.util.I18n;
57 import net.jforum.util.bbcode.BBCodeHandler;
58 import net.jforum.util.preferences.ConfigKeys;
59 import net.jforum.util.preferences.SystemGlobals;
60
61 import org.apache.log4j.Logger;
62 import org.apache.log4j.xml.DOMConfigurator;
63
64 import freemarker.template.Configuration;
65
66 /**
67  * @author Rafael Steil
68  * @version $Id: JForumBaseServlet.java,v 1.18 2006/03/01 13:17:23 rafaelsteil Exp $
69  */

70 public class JForumBaseServlet extends HttpServlet JavaDoc
71 {
72     private static Logger logger = Logger.getLogger(JForumBaseServlet.class);
73
74     protected boolean debug;
75
76     protected void startFrontController()
77     {
78         try {
79             SystemGlobals.loadQueries(SystemGlobals.getValue(ConfigKeys.SQL_QUERIES_GENERIC));
80             SystemGlobals.loadQueries(SystemGlobals.getValue(ConfigKeys.SQL_QUERIES_DRIVER));
81
82             ConfigLoader.loadDaoImplementation();
83             ConfigLoader.listenForChanges();
84             ConfigLoader.startSearchIndexer();
85             ConfigLoader.startSummaryJob();
86         }
87         catch (Exception JavaDoc e) {
88             throw new ForumStartupException("Error while starting JForum", e);
89         }
90     }
91
92     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc
93     {
94         super.init(config);
95
96         try {
97             String JavaDoc appPath = config.getServletContext().getRealPath("");
98             debug = "true".equals(config.getInitParameter("development"));
99
100             DOMConfigurator.configure(appPath + "/WEB-INF/log4j.xml");
101
102             logger.info("Starting JForum. Debug mode is " + debug);
103
104             ConfigLoader.startSystemglobals(appPath);
105             ConfigLoader.startCacheEngine();
106
107             // Configure the template engine
108
Configuration templateCfg = new Configuration();
109             templateCfg.setDirectoryForTemplateLoading(new File JavaDoc(SystemGlobals.getApplicationPath() + "/templates"));
110             templateCfg.setTemplateUpdateDelay(2);
111             templateCfg.setSetting("number_format", "#");
112             templateCfg.setSharedVariable("startupTime", new Long JavaDoc(new Date JavaDoc().getTime()));
113
114             ModulesRepository.init(SystemGlobals.getValue(ConfigKeys.CONFIG_DIR));
115
116             this.loadConfigStuff();
117
118             if (!this.debug) {
119                 templateCfg.setTemplateUpdateDelay(3600);
120             }
121
122             JForumExecutionContext.setTemplateConfig(templateCfg);
123         }
124         catch (Exception JavaDoc e) {
125             throw new ForumStartupException("Error while starting JForum", e);
126         }
127     }
128
129     protected void loadConfigStuff() throws Exception JavaDoc
130     {
131         ConfigLoader.loadUrlPatterns();
132         I18n.load();
133         Tpl.load(SystemGlobals.getValue(ConfigKeys.TEMPLATES_MAPPING));
134
135         // BB Code
136
BBCodeRepository.setBBCollection(new BBCodeHandler().parse());
137     }
138 }
139
Popular Tags