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: * Created on 02/11/2004 12:45:37
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum;
044:
045: import java.io.File;
046: import java.io.FileInputStream;
047: import java.io.IOException;
048: import java.util.Iterator;
049: import java.util.Map;
050: import java.util.Properties;
051:
052: import net.jforum.api.integration.mail.pop.POPJobStarter;
053: import net.jforum.cache.CacheEngine;
054: import net.jforum.cache.Cacheable;
055: import net.jforum.dao.DataAccessDriver;
056: import net.jforum.exceptions.CacheEngineStartupException;
057: import net.jforum.exceptions.ForumException;
058: import net.jforum.search.SearchFacade;
059: import net.jforum.sso.LoginAuthenticator;
060: import net.jforum.summary.SummaryScheduler;
061: import net.jforum.util.FileMonitor;
062: import net.jforum.util.preferences.ConfigKeys;
063: import net.jforum.util.preferences.QueriesFileListener;
064: import net.jforum.util.preferences.SystemGlobals;
065: import net.jforum.util.preferences.SystemGlobalsListener;
066:
067: import org.apache.log4j.Logger;
068: import org.quartz.SchedulerException;
069:
070: /**
071: * General utilities methods for loading configurations for JForum.
072: *
073: * @author Rafael Steil
074: * @version $Id: ConfigLoader.java,v 1.30 2007/07/27 15:42:56 rafaelsteil Exp $
075: */
076: public class ConfigLoader {
077: private static final Logger logger = Logger
078: .getLogger(ConfigLoader.class);
079: private static CacheEngine cache;
080:
081: /**
082: * Start ( or restart ) <code>SystemGlobals</code>.
083: * This method loads all configuration keys set at
084: * <i>SystemGlobals.properties</i>, <i><user.name>.properties</i>
085: * and database specific stuff.
086: *
087: * @param appPath The application root's directory
088: */
089: public static void startSystemglobals(String appPath) {
090: SystemGlobals.initGlobals(appPath, appPath
091: + "/WEB-INF/config/SystemGlobals.properties");
092: SystemGlobals.loadAdditionalDefaults(SystemGlobals
093: .getValue(ConfigKeys.DATABASE_DRIVER_CONFIG));
094:
095: if (new File(SystemGlobals
096: .getValue(ConfigKeys.INSTALLATION_CONFIG)).exists()) {
097: SystemGlobals.loadAdditionalDefaults(SystemGlobals
098: .getValue(ConfigKeys.INSTALLATION_CONFIG));
099: }
100: }
101:
102: /**
103: * Loads module mappings for the system.
104: *
105: * @param baseConfigDir The directory where the file <i>modulesMapping.properties</i> is.
106: * @return The <code>java.util.Properties</code> instance, with the loaded modules
107: */
108: public static Properties loadModulesMapping(String baseConfigDir) {
109: FileInputStream fis = null;
110:
111: try {
112: Properties modulesMapping = new Properties();
113: fis = new FileInputStream(baseConfigDir
114: + "/modulesMapping.properties");
115: modulesMapping.load(fis);
116:
117: return modulesMapping;
118: } catch (IOException e) {
119: throw new ForumException(e);
120: } finally {
121: if (fis != null) {
122: try {
123: fis.close();
124: } catch (Exception e) {
125: }
126: }
127: }
128: }
129:
130: public static void createLoginAuthenticator() {
131: String className = SystemGlobals
132: .getValue(ConfigKeys.LOGIN_AUTHENTICATOR);
133:
134: try {
135: LoginAuthenticator loginAuthenticator = (LoginAuthenticator) Class
136: .forName(className).newInstance();
137: SystemGlobals.setObjectValue(
138: ConfigKeys.LOGIN_AUTHENTICATOR_INSTANCE,
139: loginAuthenticator);
140: } catch (Exception e) {
141: throw new ForumException(
142: "Error while trying to create a login.authenticator instance ("
143: + className + "): " + e, e);
144: }
145: }
146:
147: /**
148: * Load url patterns.
149: * The method tries to load url patterns from <i>WEB-INF/config/urlPattern.properties</i>
150: */
151: public static void loadUrlPatterns() {
152: FileInputStream fis = null;
153:
154: try {
155: Properties p = new Properties();
156: fis = new FileInputStream(SystemGlobals
157: .getValue(ConfigKeys.CONFIG_DIR)
158: + "/urlPattern.properties");
159: p.load(fis);
160:
161: for (Iterator iter = p.entrySet().iterator(); iter
162: .hasNext();) {
163: Map.Entry entry = (Map.Entry) iter.next();
164: UrlPatternCollection.addPattern(
165: (String) entry.getKey(), (String) entry
166: .getValue());
167: }
168: } catch (IOException e) {
169: throw new ForumException(e);
170: } finally {
171: if (fis != null) {
172: try {
173: fis.close();
174: } catch (Exception e) {
175: }
176: }
177: }
178: }
179:
180: /**
181: * Listen for changes in common configuration files.
182: * The watched files are: <i>generic_queries.sql</i>,
183: * <i><database_name>.sql</i>, <i>SystemGlobals.properties</i>
184: * and <i><user.name>.properties</i>
185: */
186: public static void listenForChanges() {
187: int fileChangesDelay = SystemGlobals
188: .getIntValue(ConfigKeys.FILECHANGES_DELAY);
189:
190: if (fileChangesDelay > 0) {
191: // Queries
192: FileMonitor.getInstance().addFileChangeListener(
193: new QueriesFileListener(),
194: SystemGlobals
195: .getValue(ConfigKeys.SQL_QUERIES_GENERIC),
196: fileChangesDelay);
197:
198: FileMonitor.getInstance().addFileChangeListener(
199: new QueriesFileListener(),
200: SystemGlobals
201: .getValue(ConfigKeys.SQL_QUERIES_DRIVER),
202: fileChangesDelay);
203:
204: // System Properties
205: FileMonitor.getInstance().addFileChangeListener(
206: new SystemGlobalsListener(),
207: SystemGlobals.getValue(ConfigKeys.DEFAULT_CONFIG),
208: fileChangesDelay);
209:
210: ConfigLoader.listenInstallationConfig();
211: }
212: }
213:
214: public static void listenInstallationConfig() {
215: int fileChangesDelay = SystemGlobals
216: .getIntValue(ConfigKeys.FILECHANGES_DELAY);
217:
218: if (fileChangesDelay > 0) {
219: if (new File(SystemGlobals
220: .getValue(ConfigKeys.INSTALLATION_CONFIG)).exists()) {
221: FileMonitor
222: .getInstance()
223: .addFileChangeListener(
224: new SystemGlobalsListener(),
225: SystemGlobals
226: .getValue(ConfigKeys.INSTALLATION_CONFIG),
227: fileChangesDelay);
228: }
229: }
230: }
231:
232: public static void loadDaoImplementation() {
233: // Start the dao.driver implementation
234: String driver = SystemGlobals.getValue(ConfigKeys.DAO_DRIVER);
235:
236: logger.info("Loading JDBC driver " + driver);
237:
238: try {
239: Class c = Class.forName(driver);
240: DataAccessDriver d = (DataAccessDriver) c.newInstance();
241: DataAccessDriver.init(d);
242: } catch (Exception e) {
243: throw new ForumException(e);
244: }
245: }
246:
247: public static void startCacheEngine() {
248: try {
249: String cacheImplementation = SystemGlobals
250: .getValue(ConfigKeys.CACHE_IMPLEMENTATION);
251: logger.info("Using cache engine: " + cacheImplementation);
252:
253: cache = (CacheEngine) Class.forName(cacheImplementation)
254: .newInstance();
255: cache.init();
256:
257: String s = SystemGlobals
258: .getValue(ConfigKeys.CACHEABLE_OBJECTS);
259: if (s == null || s.trim().equals("")) {
260: logger
261: .warn("Cannot find Cacheable objects to associate the cache engine instance.");
262: return;
263: }
264:
265: String[] cacheableObjects = s.split(",");
266: for (int i = 0; i < cacheableObjects.length; i++) {
267: logger.info("Creating an instance of "
268: + cacheableObjects[i]);
269: Object o = Class.forName(cacheableObjects[i].trim())
270: .newInstance();
271:
272: if (o instanceof Cacheable) {
273: ((Cacheable) o).setCacheEngine(cache);
274: } else {
275: logger
276: .error(cacheableObjects[i]
277: + " is not an instance of net.jforum.cache.Cacheable");
278: }
279: }
280: } catch (Exception e) {
281: throw new CacheEngineStartupException(
282: "Error while starting the cache engine", e);
283: }
284: }
285:
286: public static void stopCacheEngine() {
287: if (cache != null) {
288: cache.stop();
289: }
290: }
291:
292: public static void startSearchIndexer() {
293: SearchFacade.init();
294: }
295:
296: /**
297: * Init a Job who will send e-mails to the all users with a summary of posts...
298: * @throws SchedulerException
299: * @throws IOException
300: */
301: public static void startSummaryJob() throws SchedulerException {
302: SummaryScheduler.startJob();
303: }
304:
305: public static void startPop3Integration() throws SchedulerException {
306: POPJobStarter.startJob();
307: }
308: }
|