diff artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java @ 292:39c0ff00d188

Introduced a hook concept - currently used for 'post-feed' and 'post-advance'. artifacts/trunk@2327 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 13 Jul 2011 13:12:08 +0000
parents d52947ce8629
children a367a0d011af
line wrap: on
line diff
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java	Wed Jul 13 11:00:30 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java	Wed Jul 13 13:12:08 2011 +0000
@@ -14,6 +14,7 @@
 import de.intevation.artifacts.ArtifactContextFactory;
 import de.intevation.artifacts.ArtifactFactory;
 import de.intevation.artifacts.CallContext;
+import de.intevation.artifacts.Hook;
 import de.intevation.artifacts.ServiceFactory;
 import de.intevation.artifacts.UserFactory;
 
@@ -21,6 +22,7 @@
 import de.intevation.artifactdatabase.rest.HTTPServer;
 
 import java.util.ArrayList;
+import java.util.List;
 
 import org.apache.log4j.Logger;
 
@@ -104,6 +106,11 @@
     public static final String CALLCONTEXT_LISTENER =
         "/artifact-database/callcontext-listener";
 
+    /**
+     * XPath that points to configuration nodes for hooks.
+     */
+    public static final String HOOKS =
+        "/artifact-database/hooks/hook";
 
     public static final String HTTP_SERVER =
         "/artifact-database/rest-server/http-server/text()";
@@ -151,6 +158,10 @@
      */
     protected CallContext.Listener callContextListener;
 
+    protected List<Hook> postFeedHooks;
+
+    protected List<Hook> postAdvanceHooks;
+
     /**
      * byte array holding the export signing secret.
      */
@@ -428,6 +439,69 @@
         }
     }
 
+    protected void loadHooks() {
+        logger.info("loading hooks");
+
+        postFeedHooks    = new ArrayList<Hook>();
+        postAdvanceHooks = new ArrayList<Hook>();
+
+        NodeList nodes = Config.getNodeSetXPath(HOOKS);
+
+        for (int i = 0, len = nodes.getLength(); i < len; i++) {
+            Node   cfg     = nodes.item(i);
+            String applies = Config.getStringXPath(cfg, "@applies");
+
+            if (applies == null || applies.length() == 0) {
+                continue;
+            }
+
+            Hook     hook  = loadHook(cfg);
+            String[] apply = applies.split(",");
+
+            for (String a: apply) {
+                a = a.trim().toLowerCase();
+
+                if (a.equals("post-feed")) {
+                    postFeedHooks.add(hook);
+                }
+                else if (a.equals("post-advance")) {
+                    postAdvanceHooks.add(hook);
+                }
+            }
+        }
+    }
+
+    protected Hook loadHook(Node hookCfg) {
+        if (hookCfg == null) {
+            return null;
+        }
+
+        Hook hook = null;
+
+        String className = Config.getStringXPath(hookCfg, "@class");
+
+        try {
+            Class clazz = Class.forName(className);
+            hook        = (Hook) clazz.newInstance();
+
+            hook.setup(hookCfg);
+        }
+        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);
+        }
+
+        return hook;
+    }
+
     /**
      * Fetches the export signing secret from the global configuration.
      * If none is found if defaults to the DEFAULT_EXORT_SECRET which
@@ -456,6 +530,7 @@
         loadUserFactory();
         loadCallContextListener();
         loadHTTPServer();
+        loadHooks();
     }
 
     /**
@@ -518,6 +593,14 @@
         return callContextListener;
     }
 
+    public List<Hook> getPostFeedHooks() {
+        return postFeedHooks;
+    }
+
+    public List<Hook> getPostAdvanceHooks() {
+        return postAdvanceHooks;
+    }
+
     public HTTPServer getHTTPServer() {
         return httpServer;
     }

http://dive4elements.wald.intevation.org