KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > ConfigLoader


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  * Created on 02/11/2004 12:45:37
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum;
44
45 import java.io.File JavaDoc;
46 import java.io.FileInputStream JavaDoc;
47 import java.io.IOException JavaDoc;
48 import java.util.Iterator JavaDoc;
49 import java.util.Map JavaDoc;
50 import java.util.Properties JavaDoc;
51
52 import net.jforum.cache.CacheEngine;
53 import net.jforum.cache.Cacheable;
54 import net.jforum.dao.DataAccessDriver;
55 import net.jforum.exceptions.CacheEngineStartupException;
56 import net.jforum.summary.SummaryScheduler;
57 import net.jforum.util.FileMonitor;
58 import net.jforum.util.preferences.ConfigKeys;
59 import net.jforum.util.preferences.QueriesFileListener;
60 import net.jforum.util.preferences.SystemGlobals;
61 import net.jforum.util.preferences.SystemGlobalsListener;
62 import net.jforum.util.search.SearchFacade;
63
64 import org.apache.log4j.Logger;
65 import org.quartz.SchedulerException;
66
67 /**
68  * General utilities methods for loading configurations for JForum.
69  *
70  * @author Rafael Steil
71  * @version $Id: ConfigLoader.java,v 1.20 2005/11/29 17:18:47 rafaelsteil Exp $
72  */

73 public class ConfigLoader
74 {
75     private static final Logger logger = Logger.getLogger(ConfigLoader.class);
76     private static CacheEngine cache;
77     
78     /**
79      * Start ( or restart ) <code>SystemGlobals</code>.
80      * This method loads all configuration keys set at
81      * <i>SystemGlobals.properties</i>, <i>&lt;user.name&gt;.properties</i>
82      * and database specific stuff.
83      *
84      * @param appPath The application root's directory
85      * @throws Exception
86      */

87     public static void startSystemglobals(String JavaDoc appPath) throws Exception JavaDoc
88     {
89         SystemGlobals.initGlobals(appPath, appPath + "/WEB-INF/config/SystemGlobals.properties");
90         SystemGlobals.loadAdditionalDefaults(SystemGlobals.getValue(ConfigKeys.DATABASE_DRIVER_CONFIG));
91         
92         if (new File JavaDoc(SystemGlobals.getValue(ConfigKeys.INSTALLATION_CONFIG)).exists()) {
93             SystemGlobals.loadAdditionalDefaults(SystemGlobals.getValue(ConfigKeys.INSTALLATION_CONFIG));
94         }
95     }
96
97     /**
98      * Loads module mappings for the system.
99      *
100      * @param baseConfigDir The directory where the file <i>modulesMapping.properties</i> is.
101      * @return The <code>java.util.Properties</code> instance, with the loaded modules
102      * @throws IOException
103      */

104     public static Properties JavaDoc loadModulesMapping(String JavaDoc baseConfigDir) throws IOException JavaDoc
105     {
106         Properties JavaDoc modulesMapping = new Properties JavaDoc();
107         modulesMapping.load(new FileInputStream JavaDoc(baseConfigDir + "/modulesMapping.properties"));
108         
109         return modulesMapping;
110     }
111     
112     /**
113      * Load url patterns.
114      * The method tries to load url patterns from <i>WEB-INF/config/urlPattern.properties</i>
115      *
116      * @throws IOException
117      */

118     public static void loadUrlPatterns() throws IOException JavaDoc {
119         Properties JavaDoc p = new Properties JavaDoc();
120         p.load(new FileInputStream JavaDoc(SystemGlobals.getValue(ConfigKeys.CONFIG_DIR)
121             + "/urlPattern.properties"));
122
123         Iterator JavaDoc iter = p.entrySet().iterator();
124         while (iter.hasNext()) {
125             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
126
127             ActionServletRequest.addUrlPattern((String JavaDoc)entry.getKey(), (String JavaDoc)entry.getValue());
128         }
129     }
130     
131     /**
132      * Listen for changes in common configuration files.
133      * The watched files are: <i>generic_queries.sql</i>,
134      * <i>&lt;database_name&gt;.sql</i>, <i>SystemGlobals.properties</i>
135      * and <i>&lt;user.name&gt;.properties</i>
136      */

137     public static void listenForChanges()
138     {
139         int fileChangesDelay = SystemGlobals.getIntValue(ConfigKeys.FILECHANGES_DELAY);
140         
141         if (fileChangesDelay > 0) {
142             // Queries
143
FileMonitor.getInstance().addFileChangeListener(new QueriesFileListener(),
144                     SystemGlobals.getValue(ConfigKeys.SQL_QUERIES_GENERIC), fileChangesDelay);
145
146             FileMonitor.getInstance().addFileChangeListener(new QueriesFileListener(),
147                     SystemGlobals.getValue(ConfigKeys.SQL_QUERIES_DRIVER), fileChangesDelay);
148
149             // System Properties
150
FileMonitor.getInstance().addFileChangeListener(new SystemGlobalsListener(),
151                     SystemGlobals.getValue(ConfigKeys.DEFAULT_CONFIG), fileChangesDelay);
152
153             ConfigLoader.listenInstallationConfig();
154         }
155     }
156     
157     public static void listenInstallationConfig()
158     {
159         int fileChangesDelay = SystemGlobals.getIntValue(ConfigKeys.FILECHANGES_DELAY);
160         
161         if (fileChangesDelay > 0) {
162             if (new File JavaDoc(SystemGlobals.getValue(ConfigKeys.INSTALLATION_CONFIG)).exists()) {
163                 FileMonitor.getInstance().addFileChangeListener(new SystemGlobalsListener(),
164                         SystemGlobals.getValue(ConfigKeys.INSTALLATION_CONFIG), fileChangesDelay);
165             }
166         }
167     }
168     
169     public static void loadDaoImplementation() throws Exception JavaDoc
170     {
171
172         // Start the dao.driver implementation
173
String JavaDoc driver = SystemGlobals.getValue(ConfigKeys.DAO_DRIVER);
174         
175         logger.info("Loading JDBC driver " + driver);
176         
177         Class JavaDoc c = Class.forName(driver);
178         DataAccessDriver d = (DataAccessDriver)c.newInstance();
179         DataAccessDriver.init(d);
180     }
181     
182     public static void startCacheEngine()
183     {
184         try {
185             String JavaDoc cacheImplementation = SystemGlobals.getValue(ConfigKeys.CACHE_IMPLEMENTATION);
186             logger.info("Using cache engine: " + cacheImplementation);
187             
188             cache = (CacheEngine)Class.forName(cacheImplementation).newInstance();
189             cache.init();
190             
191             String JavaDoc s = SystemGlobals.getValue(ConfigKeys.CACHEABLE_OBJECTS);
192             if (s == null || s.trim().equals("")) {
193                 logger.warn("Cannot find Cacheable objects to associate the cache engine instance.");
194                 return;
195             }
196             
197             String JavaDoc[] cacheableObjects = s.split(",");
198             for (int i = 0; i < cacheableObjects.length; i++) {
199                 logger.info("Creating an instance of " + cacheableObjects[i]);
200                 Object JavaDoc o = Class.forName(cacheableObjects[i].trim()).newInstance();
201                 
202                 if (o instanceof Cacheable) {
203                     ((Cacheable)o).setCacheEngine(cache);
204                 }
205                 else {
206                     logger.error(cacheableObjects[i] + " is not an instance of net.jforum.cache.Cacheable");
207                 }
208             }
209         }
210         catch (Exception JavaDoc e) {
211             throw new CacheEngineStartupException("Error while starting the cache engine", e);
212         }
213     }
214     
215     public static void stopCacheEngine()
216     {
217         if (cache != null) {
218             cache.stop();
219         }
220     }
221     
222     public static void startSearchIndexer()
223     {
224         SearchFacade.init();
225     }
226
227     /**
228      * Init a Job who will send e-mails to the all users with a summary of posts...
229      * @throws SchedulerException
230      * @throws IOException
231      */

232     public static void startSummaryJob() throws SchedulerException, IOException JavaDoc {
233         SummaryScheduler.startJob();
234     }
235 }
236
Popular Tags