changeset 311:1d517e051e95

Made backend listeners loadable at boot time. artifacts/trunk@2436 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 01 Aug 2011 14:17:09 +0000
parents 63122b9dee1d
children fa056f9c8a0c
files ChangeLog artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java artifact-database/src/main/java/de/intevation/artifactdatabase/BackendListener.java artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultBackendListener.java artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java
diffstat 6 files changed, 363 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Mon Aug 01 09:41:40 2011 +0000
+++ b/ChangeLog	Mon Aug 01 14:17:09 2011 +0000
@@ -1,5 +1,24 @@
 2011-08-01	Sascha L. Teichmann	<teichmann@intevation.de>
 
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java:
+	  Made BackListeners loadable at boot time. To be configured with XPATH
+	  '/artifact-database/backend-listeners/listener'.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java,
+	  artifact-database/src/main/java/de/intevation/artifactdatabase/BackendListener.java,
+	  artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultBackendListener.java,
+	  artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java:
+	  Wired listeners to backend.
+
+2011-08-01	Sascha L. Teichmann	<teichmann@intevation.de>
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/BackendListener.java,
+	  artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultBackendListener.java:
+	  Completed interface and the trival implementation.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java:
+	  Call the listeners for the new defined events.
+
 	* artifact-database/src/main/java/de/intevation/artifactdatabase/BackendListener.java:
 	  New. Interface to listener for backend events. TODO: Implement more
 	  events.
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java	Mon Aug 01 09:41:40 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java	Mon Aug 01 14:17:09 2011 +0000
@@ -478,6 +478,8 @@
         exportSecret = bootstrap.getExportSecret();
 
         wireWithBackend(backend);
+
+        setupBackendListeners(bootstrap);
     }
 
     public CallContext.Listener getCallContextListener() {
@@ -548,6 +550,16 @@
         setPostAdvanceHook(bootstrap.getPostAdvanceHooks());
     }
 
+    protected void setupBackendListeners(FactoryBootstrap bootstrap) {
+        List<BackendListener> bls = bootstrap.getBackendListeners();
+        if (bls != null) {
+            for (BackendListener listener: bls) {
+                listener.setup(context);
+            }
+            backend.addAllListeners(bls);
+        }
+    }
+
     protected void setupLifetimeListeners(FactoryBootstrap bootstrap) {
         this.lifetimeListeners = bootstrap.getLifetimeListeners();
     }
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Mon Aug 01 09:41:40 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Mon Aug 01 14:17:09 2011 +0000
@@ -358,6 +358,10 @@
         listeners.add(listener);
     }
 
+    public void addAllListeners(List<BackendListener> others) {
+        listeners.addAll(others);
+    }
+
     /**
      * Sets the factory lookup mechanism to decouple ArtifactDatabase
      * and Backend.
@@ -728,14 +732,14 @@
             throw new RuntimeException("failed insert artifact into database");
         }
 
-        fireArtifactStored(artifact);
+        fireCreatedArtifact(artifact);
 
         return id[0];
     }
 
-    protected void fireArtifactStored(Artifact artifact) {
+    protected void fireCreatedArtifact(Artifact artifact) {
         for (BackendListener listener: listeners) {
-            listener.artifactStored(artifact, this);
+            listener.createdArtifact(artifact, this);
         }
     }
 
@@ -779,10 +783,17 @@
         }.runWrite();
 
         if (success) {
-            fireArtifactStored(artifact.getArtifact());
+            fireStoredArtifact(artifact.getArtifact());
         }
     }
 
+    protected void fireStoredArtifact(Artifact artifact) {
+        for (BackendListener listener: listeners) {
+            listener.storedArtifact(artifact, this);
+        }
+    }
+
+
     public User createUser(
         final String      name,
         final Document    role,
@@ -831,7 +842,20 @@
             }
         };
 
-        return exec.runWrite() ? user[0] : null;
+        boolean success = exec.runWrite();
+
+        if (success) {
+            fireCreatedUser(user[0]);
+            return user[0];
+        }
+
+        return null;
+    }
+
+    protected void fireCreatedUser(User user) {
+        for (BackendListener listener: listeners) {
+            listener.createdUser(user, this);
+        }
     }
 
     public boolean deleteUser(final String identifier) {
@@ -891,7 +915,19 @@
             }
         };
 
-        return exec.runWrite();
+        boolean success = exec.runWrite();
+
+        if (success) {
+            fireDeletedUser(identifier);
+        }
+
+        return success;
+    }
+
+    protected void fireDeletedUser(String identifier) {
+        for (BackendListener listener: listeners) {
+            listener.deletedUser(identifier, this);
+        }
     }
 
     public User getUser(
@@ -1060,7 +1096,19 @@
             }
         };
 
-        return exec.runWrite() ? collection[0]: null;
+        boolean success = exec.runWrite();
+
+        if (success) {
+            fireCreatedCollection(collection[0]);
+            return collection[0];
+        }
+        return null;
+    }
+
+    protected void fireCreatedCollection(ArtifactCollection collection) {
+        for (BackendListener listener: listeners) {
+            listener.createdCollection(collection, this);
+        }
     }
 
     public ArtifactCollection getCollection(
@@ -1233,7 +1281,19 @@
                 return true;
             }
         };
-        return exec.runWrite();
+        boolean success = exec.runWrite();
+
+        if (success) {
+            fireDeletedCollection(collectionId);
+        }
+
+        return success;
+    }
+
+    protected void fireDeletedCollection(String identifier) {
+        for (BackendListener listener: listeners) {
+            listener.deletedCollection(identifier, this);
+        }
     }
 
     public Document getCollectionAttribute(final String collectionId) {
@@ -1274,7 +1334,7 @@
 
         final byte [] data = XMLUtils.toByteArray(attribute, true);
 
-        return sqlExecutor.new Instance() {
+        boolean success = sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
 
                 // set the column in collection items
@@ -1298,6 +1358,21 @@
                 return true;
             }
         }.runWrite();
+
+        if (success) {
+            fireChangedCollectionAttribute(collectionId, attribute);
+        }
+
+        return success;
+    }
+
+    protected void fireChangedCollectionAttribute(
+        String   collectionId,
+        Document document
+    ) {
+        for (BackendListener listener: listeners) {
+            listener.changedCollectionAttribute(collectionId, document, this);
+        }
     }
 
     public Document getCollectionItemAttribute(
@@ -1351,7 +1426,7 @@
 
         final byte [] data = XMLUtils.toByteArray(attribute, true);
 
-        return sqlExecutor.new Instance() {
+        boolean success = sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
 
                 // set the column in collection items
@@ -1376,6 +1451,24 @@
                 return true;
             }
         }.runWrite();
+
+        if (success) {
+            fireChangedCollectionItemAttribute(
+                collectionId, artifactId, attribute);
+        }
+
+        return success;
+    }
+
+    protected void fireChangedCollectionItemAttribute(
+        String collectionId,
+        String artifactId,
+        Document document
+    ) {
+        for (BackendListener listener: listeners) {
+            listener.changedCollectionItemAttribute(
+                collectionId, artifactId, document, this);
+        }
     }
 
     public boolean addCollectionArtifact(
@@ -1457,7 +1550,23 @@
                 return true;
             }
         };
-        return exec.runWrite();
+        boolean success = exec.runWrite();
+
+        if (success) {
+            fireAddedArtifactToCollection(artifactId, collectionId);
+        }
+
+        return success;
+    }
+
+    protected void fireAddedArtifactToCollection(
+        String artifactId,
+        String collectionId
+    ) {
+        for (BackendListener listener: listeners) {
+            listener.addedArtifactToCollection(
+                artifactId, collectionId, this);
+        }
     }
 
     public boolean removeCollectionArtifact(
@@ -1468,7 +1577,8 @@
             logger.debug("Invalid collection id: '" + collectionId + "'");
             return false;
         }
-        return sqlExecutor.new Instance() {
+
+        boolean success = sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
 
                 // fetch id, collection id and artitfact id
@@ -1508,6 +1618,22 @@
                 return true;
             }
         }.runWrite();
+
+        if (success) {
+            fireRemovedArtifactFromCollection(artifactId, collectionId);
+        }
+
+        return success;
+    }
+
+    protected void fireRemovedArtifactFromCollection(
+        String artifactId,
+        String collectionId
+    ) {
+        for (BackendListener listener: listeners) {
+            listener.removedArtifactFromCollection(
+                artifactId, collectionId, this);
+        }
     }
 
     public CollectionItem [] listCollectionArtifacts(
@@ -1574,7 +1700,7 @@
             return false;
         }
 
-        return sqlExecutor.new Instance() {
+        boolean success = sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
                 prepareStatement(SQL_UPDATE_COLLECTION_NAME);
                 stmnt.setString(1, name);
@@ -1585,6 +1711,18 @@
                 return true;
             }
         }.runWrite();
+
+        if (success) {
+            fireSetCollectionName(uuid, name);
+        }
+
+        return success;
+    }
+
+    protected void fireSetCollectionName(String identifier, String name) {
+        for (BackendListener listener: listeners) {
+            listener.setCollectionName(identifier, name);
+        }
     }
 
     public boolean loadAllArtifacts(final ArtifactLoadedCallback alc) {
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/BackendListener.java	Mon Aug 01 09:41:40 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/BackendListener.java	Mon Aug 01 14:17:09 2011 +0000
@@ -1,9 +1,51 @@
 package de.intevation.artifactdatabase;
 
 import de.intevation.artifacts.Artifact;
+import de.intevation.artifacts.ArtifactCollection;
+import de.intevation.artifacts.GlobalContext;
+import de.intevation.artifacts.User;
+
+import org.w3c.dom.Document;
 
 public interface BackendListener
 {
-    void artifactStored(Artifact artifact, Backend backend);
+    void setup(GlobalContext globalContext);
+
+    void createdArtifact(Artifact artifact, Backend backend);
+
+    void storedArtifact(Artifact artifact, Backend backend);
+
+    void createdUser(User user, Backend backend);
+
+    void deletedUser(String identifier, Backend backend);
+
+    void createdCollection(ArtifactCollection collection, Backend backend);
+
+    void deletedCollection(String identifier, Backend backend);
+
+    void changedCollectionAttribute(
+        String   identifier,
+        Document document,
+        Backend  backend);
+
+    void changedCollectionItemAttribute(
+        String   collectionId,
+        String   artifactId,
+        Document document,
+        Backend  backend);
+
+    void addedArtifactToCollection(
+        String  artifactId,
+        String  collectionId,
+        Backend backend);
+
+    void removedArtifactFromCollection(
+        String  artifactId,
+        String  collectionId,
+        Backend backend);
+
+    void setCollectionName(
+        String collectionId,
+        String name);
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultBackendListener.java	Mon Aug 01 09:41:40 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultBackendListener.java	Mon Aug 01 14:17:09 2011 +0000
@@ -1,6 +1,11 @@
 package de.intevation.artifactdatabase;
 
 import de.intevation.artifacts.Artifact;
+import de.intevation.artifacts.ArtifactCollection;
+import de.intevation.artifacts.GlobalContext;
+import de.intevation.artifacts.User;
+
+import org.w3c.dom.Document;
 
 import org.apache.log4j.Logger;
 
@@ -13,8 +18,86 @@
     }
 
     @Override
-    public void artifactStored(Artifact artifact, Backend backend) {
-        log.debug("artifactStored");
+    public void setup(GlobalContext globalContext) {
+        log.debug("setup");
+    }
+
+    @Override
+    public void createdArtifact(Artifact artifact, Backend backend) {
+        log.debug("createdArtifact");
+    }
+
+    @Override
+    public void storedArtifact(Artifact artifact, Backend backend) {
+        log.debug("storedArtifact");
+    }
+
+    @Override
+    public void createdUser(User user, Backend backend) {
+        log.debug("createdUser");
+    }
+
+    @Override
+    public void deletedUser(String identifier, Backend backend) {
+        log.debug("deletedUser");
+    }
+
+    @Override
+    public void createdCollection(
+        ArtifactCollection collection,
+        Backend            backend
+    ) {
+        log.debug("createdCollection");
+    }
+
+    @Override
+    public void deletedCollection(String identifier, Backend backend) {
+        log.debug("deletedCollection");
+    }
+
+    @Override
+    public void changedCollectionAttribute(
+        String   identifier,
+        Document document,
+        Backend  backend
+    ) {
+        log.debug("changedCollectionAttribute");
+    }
+
+    @Override
+    public void changedCollectionItemAttribute(
+        String   collectionId,
+        String   artifactId,
+        Document document,
+        Backend  backend
+    ) {
+        log.debug("changedCollectionItemAttribute");
+    }
+
+    @Override
+    public void addedArtifactToCollection(
+        String  artifactId,
+        String  collectionId,
+        Backend backend
+    ) {
+        log.debug("addedArtifactToCollection");
+    }
+
+    @Override
+    public void removedArtifactFromCollection(
+        String  artifactId,
+        String  collectionId,
+        Backend backend
+    ) {
+        log.debug("removedArtifactFromCollection");
+    }
+
+    @Override
+    public void setCollectionName(
+        String collectionId,
+        String name
+    ) {
+        log.debug("setCollectionName");
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java	Mon Aug 01 09:41:40 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/FactoryBootstrap.java	Mon Aug 01 14:17:09 2011 +0000
@@ -123,6 +123,9 @@
     public static final String LIFETIME_LISTENERS =
         "/artifact-database/lifetime-listeners/listener";
 
+    public static final String BACKEND_LISTENERS =
+        "/artifact-database/backend-listeners/listener";
+
     /**
      * Default export signing secret.
      * <strong>PLEASE CHANGE THE SECRET VIA THE XPATH EXPORT_SECRET
@@ -169,6 +172,8 @@
 
     protected List<LifetimeListener> lifetimeListeners;
 
+    protected List<BackendListener> backendListeners;
+
     /**
      * byte array holding the export signing secret.
      */
@@ -491,6 +496,49 @@
         lifetimeListeners = ltls;
     }
 
+    protected void loadBackendListeners() {
+        logger.info("loading backend listeners");
+
+        NodeList nodes = Config.getNodeSetXPath(BACKEND_LISTENERS);
+
+        if (nodes == null) {
+            logger.debug("no backend listeners configure");
+            return;
+        }
+
+        List<BackendListener> bls = new ArrayList<BackendListener>();
+
+        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);
+                BackendListener listener =
+                    (BackendListener)clazz.newInstance();
+
+                bls.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);
+            }
+        }
+
+        backendListeners = bls;
+    }
+
     protected void loadHooks() {
         logger.info("loading hooks");
 
@@ -584,6 +632,7 @@
         loadHTTPServer();
         loadHooks();
         loadLifetimeListeners();
+        loadBackendListeners();
     }
 
     /**
@@ -661,5 +710,9 @@
     public List<LifetimeListener> getLifetimeListeners() {
         return lifetimeListeners;
     }
+
+    public List<BackendListener> getBackendListeners() {
+        return backendListeners;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org