001: package org.contineo.web;
002:
003: import org.apache.commons.lang.StringUtils;
004: import org.apache.commons.lang.text.StrSubstitutor;
005:
006: import org.springframework.util.Log4jConfigurer;
007:
008: import java.io.File;
009: import java.io.FileInputStream;
010: import java.io.FileNotFoundException;
011: import java.io.FileOutputStream;
012: import java.io.IOException;
013:
014: import java.util.Properties;
015:
016: import javax.servlet.ServletContext;
017: import javax.servlet.ServletContextEvent;
018: import javax.servlet.ServletContextListener;
019:
020: /**
021: * Listener that initialises relevant system stuffs during application startup
022: *
023: * @author Alessandro Gasparini
024: * @version $Id:$
025: * @since 3.0
026: */
027: public class ApplicationInitializer implements ServletContextListener {
028: private static final String CONTINEO_APP_PLUGINSDIR = "contineo.app.pluginsdir";
029: private static final String CONTINEO_APP_ROOTDIR = "contineo.app.rootdir";
030: private static final String CONTINEO_HOME = "CONTINEO_HOME";
031: private static final String CONTINEO_APP_PLUGINREGISTRY = "contineo.app.pluginregistry";
032: private static final String WEB_INF_BOOT_PROPERTIES = "WEB-INF/boot.properties";
033:
034: /**
035: * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
036: */
037: public void contextDestroyed(ServletContextEvent sce) {
038: Log4jConfigurer.shutdownLogging();
039: }
040:
041: public static Properties loadBootProperties(ServletContext context) {
042: Properties boot = new Properties();
043:
044: try {
045: boot.load(new FileInputStream(context
046: .getRealPath(WEB_INF_BOOT_PROPERTIES)));
047: } catch (Exception e) {
048: e.printStackTrace();
049: }
050:
051: return boot;
052: }
053:
054: /**
055: * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
056: */
057: public void contextInitialized(ServletContextEvent sce) {
058: ServletContext context = sce.getServletContext();
059: Properties boot = loadBootProperties(context);
060: String contineoHome = boot.getProperty(CONTINEO_HOME);
061:
062: // replace system properties
063: if (contineoHome.indexOf("$") != -1) {
064: contineoHome = StrSubstitutor
065: .replaceSystemProperties(contineoHome);
066: }
067:
068: boot.setProperty(CONTINEO_HOME,
069: initContineoHomePath(contineoHome));
070: boot.setProperty(CONTINEO_APP_ROOTDIR, initRootPath(context));
071: boot.setProperty(CONTINEO_APP_PLUGINSDIR,
072: initPluginsPath(context));
073: boot.setProperty(CONTINEO_APP_PLUGINREGISTRY,
074: initPluginRegistry());
075:
076: try {
077: String log4jPath = context
078: .getRealPath("/WEB-INF/classes/log4j.xml");
079: System.out.println("log4jPath = " + log4jPath);
080: Log4jConfigurer.initLogging(log4jPath);
081: } catch (FileNotFoundException e) {
082: e.printStackTrace();
083: }
084:
085: saveBootProperties(boot, context);
086:
087: // Initialize plugins
088: org.contineo.util.PluginRegistry.getInstance().init();
089: }
090:
091: private String initPluginRegistry() {
092: System.setProperty(CONTINEO_APP_PLUGINREGISTRY,
093: "org.contineo.web.PluginRegistry");
094:
095: return "org.contineo.web.PluginRegistry";
096: }
097:
098: public static void saveBootProperties(Properties boot,
099: ServletContext context) {
100: // Save properties for the next bootstrap
101: try {
102: boot.store(new FileOutputStream(context
103: .getRealPath(WEB_INF_BOOT_PROPERTIES)), "");
104: } catch (IOException e) {
105: e.printStackTrace();
106: }
107: }
108:
109: protected String initContineoHomePath(String contineoHome) {
110: String homePath = StringUtils.replace(contineoHome, "\\", "/");
111: homePath = StringUtils.removeEnd(homePath, "/");
112: System.err.println("CONTINEO_HOME = " + homePath);
113: System.setProperty(CONTINEO_HOME, homePath);
114:
115: return homePath;
116: }
117:
118: protected String initRootPath(final ServletContext context) {
119: String rootPath = StringUtils.replace(context
120: .getRealPath(StringUtils.EMPTY), "\\", "/");
121: rootPath = StringUtils.removeEnd(rootPath, "/");
122: System.setProperty(CONTINEO_APP_ROOTDIR, rootPath);
123:
124: return rootPath;
125: }
126:
127: protected String initPluginsPath(final ServletContext context) {
128: String pluginsPath = StringUtils.replace(context
129: .getRealPath(StringUtils.EMPTY), "\\", "/");
130: pluginsPath = StringUtils.removeEnd(pluginsPath, "/");
131: pluginsPath += "/WEB-INF/plugins";
132:
133: File dir = new File(pluginsPath);
134: dir.mkdirs();
135: dir.mkdir();
136: System.setProperty(CONTINEO_APP_PLUGINSDIR, pluginsPath);
137:
138: return pluginsPath;
139: }
140: }
|