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