changeset 94:d2cee1040062

new servlet lifecycle listener to shut down Spring ApplicationBean.
author casties
date Thu, 20 Oct 2016 14:08:16 +0200
parents 27bf167d09f1
children 12af756065b5
files src/main/java/de/mpiwg/itgroup/ismi/entry/beans/AbstractBean.java src/main/java/de/mpiwg/itgroup/ismi/entry/beans/ApplicationBean.java src/main/java/de/mpiwg/itgroup/ismi/servlets/ServletLifecycleService.java src/main/resources/openmind-context.xml src/main/webapp/WEB-INF/web.xml
diffstat 5 files changed, 68 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/main/java/de/mpiwg/itgroup/ismi/entry/beans/AbstractBean.java	Mon Oct 17 19:52:28 2016 +0200
+++ b/src/main/java/de/mpiwg/itgroup/ismi/entry/beans/AbstractBean.java	Thu Oct 20 14:08:16 2016 +0200
@@ -14,8 +14,8 @@
 public abstract class AbstractBean implements Serializable{
 	
 	private static final long serialVersionUID = -8646299519985691771L;
-	private static String BEAN_APP = "ApplicationBean1";
-	private static String BEAN_SESSION = "Session";
+	public static String BEAN_APP = "ApplicationBean1";
+	public static String BEAN_SESSION = "Session";
 	
 	public Object getRequestBean(String name){
 		return FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get(name);
--- a/src/main/java/de/mpiwg/itgroup/ismi/entry/beans/ApplicationBean.java	Mon Oct 17 19:52:28 2016 +0200
+++ b/src/main/java/de/mpiwg/itgroup/ismi/entry/beans/ApplicationBean.java	Thu Oct 20 14:08:16 2016 +0200
@@ -575,4 +575,8 @@
 		}
 		return publicCodexList;
 	}
+	
+	public ServiceRegistry getServiceRegistry() {
+	    return this.serviceRegistry;
+	}
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/mpiwg/itgroup/ismi/servlets/ServletLifecycleService.java	Thu Oct 20 14:08:16 2016 +0200
@@ -0,0 +1,60 @@
+/**
+ * 
+ */
+package de.mpiwg.itgroup.ismi.servlets;
+
+import javax.servlet.ServletContext;
+import javax.servlet.ServletContextEvent;
+import javax.servlet.ServletContextListener;
+
+import org.apache.log4j.Logger;
+import org.mpi.openmind.repository.services.ServiceRegistry;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.support.AbstractApplicationContext;
+
+import de.mpiwg.itgroup.ismi.entry.beans.AbstractBean;
+import de.mpiwg.itgroup.ismi.entry.beans.ApplicationBean;
+
+/**
+ * Class to handle ServletContextListener events for startup and shutdown.
+ * 
+ * Should be added with a <listener> element in web.xml.
+ * 
+ * @author casties
+ *
+ */
+public class ServletLifecycleService implements ServletContextListener {
+
+    private static Logger logger = Logger.getLogger(ServletLifecycleService.class);
+
+    /* (non-Javadoc)
+     * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
+     */
+    @Override
+    public void contextInitialized(ServletContextEvent ctxe) {
+        // TODO: inititialize Spring bean here?
+        logger.info("Servlet context initialized.");        
+    }
+    
+    /* (non-Javadoc)
+     * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
+     */
+    @Override
+    public void contextDestroyed(ServletContextEvent ctxe) {
+        ServletContext srvCtx = ctxe.getServletContext();
+        // get Spring ApplicationBean from context
+        ApplicationBean app = (ApplicationBean) srvCtx.getAttribute(AbstractBean.BEAN_APP);
+        if (app != null) {
+            // get Spring ApplicationContext from ApplicationBean
+            ServiceRegistry srvReg = app.getServiceRegistry();
+            ApplicationContext context = srvReg.getApplicationContext();
+            if (context != null) {
+                // run Spring shutdown hook
+                logger.info("Closing Spring context.");
+                ((AbstractApplicationContext) context).close();
+            }
+        }
+        logger.info("Servlet context destroyed.");
+    }
+
+}
--- a/src/main/resources/openmind-context.xml	Mon Oct 17 19:52:28 2016 +0200
+++ b/src/main/resources/openmind-context.xml	Thu Oct 20 14:08:16 2016 +0200
@@ -63,7 +63,7 @@
         <!-- <property name="cronExpression" value="0 0/10 * * * ?"/> -->
         
     </bean>
-        
+    
     <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"
         destroy-method="destroy">
         <property name="triggers">
--- a/src/main/webapp/WEB-INF/web.xml	Mon Oct 17 19:52:28 2016 +0200
+++ b/src/main/webapp/WEB-INF/web.xml	Thu Oct 20 14:08:16 2016 +0200
@@ -75,7 +75,7 @@
     <param-value>resources.application</param-value>
   </context-param>
   <listener>
-    <listener-class>org.mpi.openmind.repository.services.ServiceRegistry</listener-class>
+    <listener-class>de.mpiwg.itgroup.ismi.servlets.ServletLifecycleService</listener-class>
   </listener>
   <listener>
     <listener-class>com.sun.faces.config.ConfigureListener</listener-class>