changeset 304:40b64b4aafce

Added lifetime listeners to be called when system is up and is going down. artifacts/trunk@2410 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 27 Jul 2011 13:51:30 +0000
parents 190aa68ae7a8
children f33401ea2a6c
files ChangeLog artifact-database/src/main/java/de/intevation/artifactdatabase/App.java artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java artifact-database/src/main/java/de/intevation/artifactdatabase/LifetimeListener.java
diffstat 5 files changed, 114 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Wed Jul 27 09:32:26 2011 +0000
+++ b/ChangeLog	Wed Jul 27 13:51:30 2011 +0000
@@ -1,3 +1,19 @@
+2011-07-27	Sascha L. Teichmann	<teichmann@intevation.de>
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/LifetimeListener.java:
+	  New. Interface instances of are called when system is up and is going down.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java:
+	  Load lifetime listeners from configuration. XPATH
+	  /artifact-database/lifetime-listeners/listeners/text()
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java:
+	  Call the listeners after start up and before shutdown.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/App.java:
+	  Trigger the start of the artifact database explicitly when the boot process
+	  is finished.
+
 2011-07-27	Sascha L. Teichmann	<teichmann@intevation.de>
 
 	* artifacts/src/main/java/de/intevation/artifacts/ArtifactDatabase.java:
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/App.java	Wed Jul 27 09:32:26 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/App.java	Wed Jul 27 13:51:30 2011 +0000
@@ -82,6 +82,8 @@
 
         cleaner.start();
 
+        db.start();
+
         httpServer.startAsServer(db);
     }
 }
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java	Wed Jul 27 09:32:26 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java	Wed Jul 27 13:51:30 2011 +0000
@@ -435,6 +435,8 @@
      */
     protected List<Hook> postAdvanceHooks;
 
+    protected List<LifetimeListener> lifetimeListeners;
+
     /**
      * Default constructor.
      */
@@ -468,6 +470,7 @@
         setupUserFactory(bootstrap);
         setupCallContextListener(bootstrap);
         setupHooks(bootstrap);
+        setupLifetimeListeners(bootstrap);
 
         context = bootstrap.getContext();
         context.put(GLOBAL_CONTEXT_KEY, this);
@@ -545,6 +548,10 @@
         setPostAdvanceHook(bootstrap.getPostAdvanceHooks());
     }
 
+    protected void setupLifetimeListeners(FactoryBootstrap bootstrap) {
+        this.lifetimeListeners = bootstrap.getLifetimeListeners();
+    }
+
     /**
      * Used to extract the user factory from the bootstrap.
      */
@@ -1719,5 +1726,24 @@
             throw new ArtifactDatabaseException(INTERNAL_ERROR);
         }
     }
+
+    public void start() {
+        if (lifetimeListeners == null || lifetimeListeners.isEmpty()) {
+            return;
+        }
+
+        for (LifetimeListener ltl: lifetimeListeners) {
+            ltl.systemUp(context);
+        }
+
+        Runtime.getRuntime().addShutdownHook(new Thread() {
+            @Override
+            public void run() {
+                for (LifetimeListener ltl: lifetimeListeners) {
+                    ltl.systemDown(context);
+                }
+            }
+        });
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java	Wed Jul 27 09:32:26 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java	Wed Jul 27 13:51:30 2011 +0000
@@ -120,6 +120,9 @@
     public static final String DEFAULT_HTTP_SERVER =
         "de.intevation.artifactdatabase.rest.Standalone";
 
+    public static final String LIFETIME_LISTENERS =
+        "/artifact-database/lifetime-listeners/listener";
+
     /**
      * Default export signing secret.
      * <strong>PLEASE CHANGE THE SECRET VIA THE XPATH EXPORT_SECRET
@@ -164,6 +167,8 @@
 
     protected List<Hook> postAdvanceHooks;
 
+    protected List<LifetimeListener> lifetimeListeners;
+
     /**
      * byte array holding the export signing secret.
      */
@@ -441,6 +446,51 @@
         }
     }
 
+    protected void loadLifetimeListeners() {
+        logger.info("loading lifetime listeners");
+
+        NodeList nodes = Config.getNodeSetXPath(LIFETIME_LISTENERS);
+
+        if (nodes == null) {
+            logger.debug("no lifetime listeners configure");
+            return;
+        }
+
+        List<LifetimeListener> ltls = new ArrayList<LifetimeListener>();
+
+        for (int i = 0, N = nodes.getLength(); i < N; ++i) {
+            Node node = nodes.item(i);
+            String className = node.getTextContent();
+            if (className == null
+            || (className = className.trim()).length() == 0) {
+                continue;
+            }
+            try {
+                Class clazz = Class.forName(className);
+                LifetimeListener listener =
+                    (LifetimeListener)clazz.newInstance();
+
+                listener.setup(Config.getConfig());
+
+                lifetimeListeners.add(listener);
+            }
+            catch (ClassNotFoundException cnfe) {
+                logger.error(cnfe.getLocalizedMessage(), cnfe);
+            }
+            catch (InstantiationException ie) {
+                logger.error(ie.getLocalizedMessage(), ie);
+            }
+            catch (ClassCastException cce) {
+                logger.error(cce.getLocalizedMessage(), cce);
+            }
+            catch (IllegalAccessException iae) {
+                logger.error(iae.getLocalizedMessage(), iae);
+            }
+        }
+
+        lifetimeListeners = ltls;
+    }
+
     protected void loadHooks() {
         logger.info("loading hooks");
 
@@ -533,6 +583,7 @@
         loadCallContextListener();
         loadHTTPServer();
         loadHooks();
+        loadLifetimeListeners();
     }
 
     /**
@@ -606,5 +657,9 @@
     public HTTPServer getHTTPServer() {
         return httpServer;
     }
+
+    public List<LifetimeListener> getLifetimeListeners() {
+        return lifetimeListeners;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/LifetimeListener.java	Wed Jul 27 13:51:30 2011 +0000
@@ -0,0 +1,15 @@
+package de.intevation.artifactdatabase;
+
+import de.intevation.artifacts.GlobalContext;
+
+import org.w3c.dom.Document;
+
+public interface LifetimeListener
+{
+    void setup(Document document);
+
+    void systemUp(GlobalContext globalContext);
+
+    void systemDown(GlobalContext globalContext);
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org