diff artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java @ 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 b0a949d3fe09
children d96bcb40dbf9
line wrap: on
line diff
--- 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 :

http://dive4elements.wald.intevation.org