/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

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