001: package org.claros.commons.configuration;
002:
003: import java.io.File;
004: import java.io.FileInputStream;
005: import java.io.IOException;
006: import java.util.HashMap;
007: import java.util.Iterator;
008: import java.util.Properties;
009:
010: import javax.servlet.ServletException;
011: import javax.servlet.http.HttpServlet;
012:
013: import org.apache.commons.digester.Digester;
014: import org.apache.commons.logging.Log;
015: import org.apache.commons.logging.LogFactory;
016: import org.apache.log4j.PropertyConfigurator;
017: import org.claros.commons.db.DbConfig;
018: import org.claros.commons.db.DbConfigList;
019: import org.claros.commons.exception.FatalException;
020: import org.claros.commons.utility.Utility;
021: import org.xml.sax.SAXException;
022:
023: public class Initiator extends HttpServlet {
024: private static final long serialVersionUID = -3372714955993332699L;
025:
026: private static Log log = null;
027:
028: /**
029: * Initialization of the servlet. <br>
030: *
031: * @throws ServletException if an error occure
032: */
033: public void init() throws ServletException {
034: // first: initiate paths so that we can find files.
035: // everything else depends on this. So it is very
036: // important
037: try {
038: initiatePaths();
039: } catch (Exception e) {
040: System.out
041: .println("Claros Base System could not initialize paths on the web application.");
042: throw new FatalException(e);
043: }
044:
045: // now initiate logger.
046: try {
047: initiateLogger();
048: log = LogFactory.getLog(Initiator.class);
049: } catch (Exception e) {
050: System.out
051: .println("Claros Base System could not initialize logging paths.");
052: throw new FatalException(e);
053: }
054:
055: // now initiate the db's and start database pool.
056: try {
057: initiateDb();
058: HashMap dbList = DbConfigList.getDbList();
059: if (dbList != null) {
060: Iterator iter = dbList.keySet().iterator();
061: DbConfig db = null;
062: while (iter.hasNext()) {
063: db = (DbConfig) dbList.get(iter.next());
064: db.getDataSource();
065: log.debug("DbConfig read with parameters. "
066: + db.toString());
067: }
068: }
069: } catch (Exception e) {
070: log
071: .fatal("Claros Base System could not initialize the databases");
072: throw new FatalException(e);
073: }
074:
075: // are there any batch jobs?
076:
077: }
078:
079: private void initiateDb() {
080: try {
081: Digester digester = new Digester();
082: digester.setValidating(false);
083: digester.addObjectCreate("claros-config/db-config",
084: "org.claros.commons.db.DbConfigList");
085: digester.addObjectCreate("claros-config/db-config/db",
086: "org.claros.commons.db.DbConfig");
087: digester.addCallMethod("claros-config/db-config/db/id",
088: "setId", 0);
089: digester.addCallMethod(
090: "claros-config/db-config/db/database",
091: "setDatabase", 0);
092: digester.addCallMethod("claros-config/db-config/db/driver",
093: "setDriver", 0);
094: digester.addCallMethod("claros-config/db-config/db/login",
095: "setLogin", 0);
096: digester.addCallMethod(
097: "claros-config/db-config/db/password",
098: "setPassword", 0);
099: digester.addSetNext("claros-config/db-config/db",
100: "addDbConfig", "org.claros.commons.db.DbConfig");
101: digester.parse(Paths.getCfgFolder() + "/config.xml");
102: } catch (IOException e) {
103: log.fatal(
104: "Could not find config.xml file in your config path.("
105: + Paths.getCfgFolder() + ")", e);
106: } catch (SAXException e) {
107: log
108: .fatal(
109: "Could not validate config.xml file or could not read its contents",
110: e);
111: }
112: }
113:
114: private void initiatePaths() {
115: String prefix = Utility.replaceAllOccurances(
116: getServletContext().getRealPath("/"), "\\", "/");
117: if (prefix.endsWith("/")) {
118: prefix = prefix.substring(0, prefix.length() - 1);
119: }
120: Paths.setPrefix(prefix);
121: Paths.setLogFolder(prefix + "/WEB-INF/log");
122: Paths.setCfgFolder(prefix + "/WEB-INF/config");
123: Paths.setLibFolder(prefix + "/WEB-INF/lib");
124: Paths.setResFolder(prefix + "/WEB-INF/resources");
125: Paths.setClsFolder(prefix + "/WEB-INF/classes");
126: Paths.setDbFolder(prefix + "/WEB-INF/db");
127: }
128:
129: private void initiateLogger() {
130: try {
131: Properties prop = new Properties();
132:
133: FileInputStream is = new FileInputStream(new File(Paths
134: .getCfgFolder()
135: + "/log.properties"));
136: prop.load(is);
137: is.close();
138:
139: Iterator iter = prop.keySet().iterator();
140: String key = null;
141: String val = null;
142: while (iter.hasNext()) {
143: key = (String) iter.next();
144: val = prop.getProperty(key);
145: if (val != null) {
146: val = Utility.replaceAllOccurances(val,
147: "%logpath%", Paths.getLogFolder());
148: prop.setProperty(key, val);
149: }
150: }
151: PropertyConfigurator.configure(prop);
152: } catch (Exception e) {
153: System.setProperty("log4j.rootCategory", "debug, common");
154: System.setProperty("log4j.appender.common",
155: "org.apache.log4j.RollingFileAppender");
156: System.setProperty("log4j.appender.common.File", Paths
157: .getLogFolder()
158: + "default.log");
159: System.setProperty("log4j.appender.common.MaxFileSize",
160: "5000KB");
161: System.setProperty("log4j.appender.common.MaxBackupIndex",
162: "5");
163: System.setProperty("log4j.appender.common.layout",
164: "org.apache.log4j.PatternLayout");
165: System.setProperty(
166: "log4j.appender.common.layout.ConversionPattern",
167: "%5p [%t] (%F:%L) - %m%n");
168: }
169: }
170: }
|