001: package org.nemesis.forum.config;
002:
003: import java.io.InputStream;
004:
005: import org.apache.commons.digester.Digester;
006: import org.apache.commons.logging.Log;
007: import org.apache.commons.logging.LogFactory;
008:
009: /**
010: * @author dlaurent
011: *
012: * Loads up nemesis-forum-config.xml and
013: * returns a singleton instance of Config.
014: *
015: * usage: ConfigLoader.getInstance().getConfig()
016: */
017: public class ConfigLoader {
018: private static Log log = LogFactory.getLog(ConfigLoader.class);
019:
020: private static Object lockObject = new Object();
021:
022: private Config config;
023:
024: private static ConfigLoader singleton;
025:
026: private ConfigLoader() {
027: }
028:
029: public static ConfigLoader getInstance() {
030: if (singleton == null) {
031: singleton = new ConfigLoader();
032: }
033: return singleton;
034: }
035:
036: public Config getConfig() {
037: if (config != null) {
038: return config;
039: }
040: synchronized (lockObject) {
041:
042: InputStream is;
043: is = getClass().getClassLoader().getResourceAsStream(
044: "nemesis-forum-config.xml");
045: if (is == null) {
046: is = getClass().getClassLoader().getResourceAsStream(
047: "/nemesis-forum-config.xml");
048: }
049: config = new Config();
050:
051: try {
052:
053: Digester digester = new Digester();
054: digester.push(config);
055: digester.setValidating(false);
056: //rules
057: digester.addCallMethod(
058: "nemesis-config/db-config/provider-class",
059: "setJDBCConnectionProviderClass", 0);
060: digester.addCallMethod(
061: "nemesis-config/db-config/init-param",
062: "addJDBCProviderProperties", 2);
063: digester
064: .addCallParam(
065: "nemesis-config/db-config/init-param/param-name",
066: 0);
067: digester
068: .addCallParam(
069: "nemesis-config/db-config/init-param/param-value",
070: 1);
071:
072: digester.addCallMethod(
073: "nemesis-config/extended-properties/user",
074: "addUserExtendedProperties", 0);
075: digester.addCallMethod(
076: "nemesis-config/extended-properties/forum",
077: "addForumExtendedProperties", 0);
078: digester.addCallMethod(
079: "nemesis-config/extended-properties/message",
080: "addMessageExtendedProperties", 0);
081:
082: digester.addCallMethod("nemesis-config/auto-index",
083: "setAutoIndex", 0,
084: new String[] { "java.lang.Boolean" });
085:
086: //parse
087: digester.parse(is);
088: is.close();
089:
090: //------------------
091: log.info("load config :" + config.toString());
092: return config;
093: } catch (Exception e) {
094: log.fatal("load config", e);
095: throw new RuntimeException(e.getMessage());
096: }
097:
098: }
099: }
100:
101: }
|