PageRenderTime 20ms CodeModel.GetById 8ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/webportal/src/main/java/au/org/emii/portal/web/ApplicationInit.java

http://alageospatialportal.googlecode.com/
Java | 153 lines | 82 code | 27 blank | 44 comment | 7 complexity | 813ff3e0de46e0637cb66931931f280b MD5 | raw file
  1package au.org.emii.portal.web;
  2
  3import au.org.emii.portal.config.ConfigurationLoaderStage1;
  4import au.org.emii.portal.settings.Settings;
  5import au.org.emii.portal.settings.SettingsImpl;
  6import au.org.emii.portal.util.PortalNamingImpl;
  7import au.org.emii.portal.util.ResolveHostNameImpl;
  8import java.util.logging.Level;
  9
 10import javax.servlet.ServletContext;
 11import javax.servlet.ServletContextEvent;
 12
 13import org.apache.log4j.Logger;
 14import org.springframework.beans.factory.InitializingBean;
 15import org.springframework.web.context.ContextLoaderListener;
 16import org.springframework.web.context.WebApplicationContext;
 17import org.springframework.web.context.support.WebApplicationContextUtils;
 18import org.zkoss.util.resource.Labels;
 19
 20
 21import zk.extra.BiocacheLabelLocator;
 22
 23/**
 24 * Housekeeping class for setting up objects in application scope.
 25 * 
 26 * Currently loads and processes the xml configuration file
 27 * @author geoff
 28 *
 29 * also initialises the webapp from ZK point of view
 30 */
 31public class ApplicationInit extends ContextLoaderListener{
 32
 33    public static final String CONFIGURATION_LOADER_ATTRIBUTE = "configurationLoader";
 34    public static final String CONFIGURATION_LOADER_THREAD_ATTRIBUTE = "configurationLoaderThread";
 35    public static final String PORTAL_MASTER_SESSION_ATTRIBUTE = "masterPortalSession";
 36    public static final String CONFIG_DIR_JVM_ARG = "WEBPORTAL_CONFIG_DIR";
 37
 38    /**
 39     * Logger instance
 40     */
 41    private Logger logger = Logger.getLogger(this.getClass());
 42
 43    /**
 44     * setup log4j - since we have no spring at this point, we have to call
 45     * some setters on it ourself...
 46     */
 47    private void initLog4j() {
 48        // Settings and portal naming objects will be replaced by spring once the system is up
 49        Settings settings = new SettingsImpl();
 50        settings.setConfigPath(System.getProperty(CONFIG_DIR_JVM_ARG));
 51        PortalNamingImpl portalNaming = new PortalNamingImpl();
 52        portalNaming.setSettings(settings);
 53        try {
 54            portalNaming.afterPropertiesSet();
 55        } catch (Exception ex) {
 56            logger.error("Error loading portal naming in application init",ex);
 57        }
 58
 59        Log4jLoader log4jLoader = new Log4jLoader();
 60        log4jLoader.setPortalNaming(portalNaming);
 61        log4jLoader.setResolveHostname(new ResolveHostNameImpl());
 62        log4jLoader.setSettings(settings);
 63        log4jLoader.load();
 64    }
 65
 66    @Override
 67    public void contextInitialized(ServletContextEvent sce) {
 68        // first log message can come from a log4j instance without substituting
 69        // variables, just so we know we at least got this far...
 70        logger.debug("================[WEB PORTAL APPLICATION INIT]================");
 71
 72        // quick sanity check that JVM arg spring will look for is really there
 73        // so we can give a friendlier error message than spring does
 74        if (System.getProperty(CONFIG_DIR_JVM_ARG) == null) {
 75            // If config dir not set, no point setting up log4j - will fail so use
 76            // the default log4j.xml file in the classpath to print the following
 77            // error message
 78            logger.error(
 79                    "Config file location not set.  You must supply the full " +
 80                    "path to a web portal configuration directory by starting your " +
 81                    "JVM with -D" + CONFIG_DIR_JVM_ARG + "=/full/path/to/config/dir");
 82        } else {
 83            // set log4j with app name and host name
 84            initLog4j();
 85
 86            // now the spring context gets loaded by superclass...
 87            super.contextInitialized(sce);
 88            ServletContext servletContext = sce.getServletContext();
 89            WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
 90
 91            /* configurationLoader is a daemon thread that runs for the duration
 92             * of the application - it is set to periodically reload the configuration.
 93             * We store the both the thread and the runnable in application scope so
 94             * we can kill the thread cleanly
 95             */
 96            ConfigurationLoaderStage1 configurationLoader = context.getBean(ConfigurationLoaderStage1.class);
 97            configurationLoader.setServletContext(servletContext);
 98            servletContext.setAttribute(CONFIGURATION_LOADER_ATTRIBUTE, configurationLoader);
 99            Thread configurationLoaderThread = new Thread(configurationLoader);
100
101            // set the name for debugging purposes.  We tostring the thread object
102            // so that the name will contain the memory address so we can distinguish
103            // between diferent instances of the same thread should this happen.
104            configurationLoaderThread.setName("ConfigurationLoader-instance-" + configurationLoaderThread.toString());
105            servletContext.setAttribute(CONFIGURATION_LOADER_THREAD_ATTRIBUTE, configurationLoaderThread);
106
107            // start the tread running and return control immediately
108            configurationLoaderThread.start();
109        }
110
111        //NC 2013-11-26: initialise the ZK Labels to include biocache WS i18n version. 
112        logger.debug("REGISTERING Biocache Labeller...");
113        Labels.register(new BiocacheLabelLocator());
114        logger.debug("* APPLICATION INIT: complete");
115
116
117
118    }
119    
120
121    /**
122     * FIXME - MOVE TO DEDICATED SHUTDOWN CLASS!!
123     * Called by servlet container when shutting down
124     */
125    @Override
126    public void contextDestroyed(ServletContextEvent sce) {
127        logger.debug("APPLICATION shutdown requested");
128        ServletContext servletContext = sce.getServletContext();
129        ConfigurationLoaderStage1 configurationLoader =
130                (ConfigurationLoaderStage1) servletContext.getAttribute(CONFIGURATION_LOADER_ATTRIBUTE);
131        Thread configurationLoaderThread =
132                (Thread) servletContext.getAttribute(CONFIGURATION_LOADER_THREAD_ATTRIBUTE);
133
134        /* it's entirely possible that the value hasn't been put in scope yet
135         * so protect against NPEs
136         */
137        if (configurationLoader != null) {
138            configurationLoader.stop();
139        }
140
141        // interrupt the thread which is likely in sleep state
142        if (configurationLoaderThread != null) {
143            configurationLoaderThread.interrupt();
144        }
145
146        // now remove the attributes from servlet session...
147        servletContext.removeAttribute(CONFIGURATION_LOADER_ATTRIBUTE);
148        servletContext.removeAttribute(CONFIGURATION_LOADER_THREAD_ATTRIBUTE);
149
150        super.contextDestroyed(sce);
151    }
152
153}