changeset 310:63122b9dee1d

Added interface to listen events in the backend. artifacts/trunk@2435 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 01 Aug 2011 09:41:40 +0000
parents 86dd32b45d87
children 1d517e051e95
files ChangeLog 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
diffstat 4 files changed, 65 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Sun Jul 31 18:43:54 2011 +0000
+++ b/ChangeLog	Mon Aug 01 09:41:40 2011 +0000
@@ -1,3 +1,15 @@
+2011-08-01	Sascha L. Teichmann	<teichmann@intevation.de>
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/BackendListener.java:
+	  New. Interface to listener for backend events. TODO: Implement more
+	  events.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultBackendListener.java:
+	  New. Trivial implementation of BackendListener.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java:
+	  Added list of backend listeners. TODO: Add them at boot time.
+
 2011-07-31	Sascha L. Teichmann	<teichmann@intevation.de>
 
 	* artifact-database/pom.xml: Bumped Apache DBCP up to 1.4
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Sun Jul 31 18:43:54 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java	Mon Aug 01 09:41:40 2011 +0000
@@ -28,10 +28,13 @@
 import java.sql.Timestamp;
 import java.sql.Types;
 
+import java.util.List;
 import java.util.ArrayList;
 import java.util.Date;
 import java.util.HashMap;
 
+import java.util.concurrent.CopyOnWriteArrayList;
+
 import org.apache.log4j.Logger;
 
 import org.w3c.dom.Document;
@@ -134,6 +137,8 @@
 
     protected SQLExecutor sqlExecutor;
 
+    protected List<BackendListener> listeners;
+
     protected DBConfig config;
 
     /**
@@ -256,6 +261,7 @@
      * Default constructor
      */
     public Backend() {
+        listeners = new CopyOnWriteArrayList<BackendListener>();
     }
 
     public Backend(DBConfig config) {
@@ -348,6 +354,10 @@
         SQL_ALL_ARTIFACTS = sql.get("all.artifacts");
     }
 
+    public void addListener(BackendListener listener) {
+        listeners.add(listener);
+    }
+
     /**
      * Sets the factory lookup mechanism to decouple ArtifactDatabase
      * and Backend.
@@ -718,9 +728,17 @@
             throw new RuntimeException("failed insert artifact into database");
         }
 
+        fireArtifactStored(artifact);
+
         return id[0];
     }
 
+    protected void fireArtifactStored(Artifact artifact) {
+        for (BackendListener listener: listeners) {
+            listener.artifactStored(artifact, this);
+        }
+    }
+
     /**
      * Touches the access timestamp of a given artifact to prevent
      * that it will be removed from the database by the database cleaner.
@@ -744,7 +762,7 @@
      * artifact.
      */
     public void store(final PersistentArtifact artifact) {
-        sqlExecutor.new Instance() {
+        boolean success = sqlExecutor.new Instance() {
             public boolean doIt() throws SQLException {
                 prepareStatement(SQL_UPDATE);
                 stmnt.setInt(2, artifact.getId());
@@ -759,6 +777,10 @@
                 return true;
             }
         }.runWrite();
+
+        if (success) {
+            fireArtifactStored(artifact.getArtifact());
+        }
     }
 
     public User createUser(
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/BackendListener.java	Mon Aug 01 09:41:40 2011 +0000
@@ -0,0 +1,9 @@
+package de.intevation.artifactdatabase;
+
+import de.intevation.artifacts.Artifact;
+
+public interface BackendListener
+{
+    void artifactStored(Artifact artifact, Backend backend);
+}
+// 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/DefaultBackendListener.java	Mon Aug 01 09:41:40 2011 +0000
@@ -0,0 +1,21 @@
+package de.intevation.artifactdatabase;
+
+import de.intevation.artifacts.Artifact;
+
+import org.apache.log4j.Logger;
+
+public class DefaultBackendListener
+implements   BackendListener
+{
+    private static Logger log = Logger.getLogger(DefaultBackendListener.class);
+
+    public DefaultBackendListener() {
+    }
+
+    @Override
+    public void artifactStored(Artifact artifact, Backend backend) {
+        log.debug("artifactStored");
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
+

http://dive4elements.wald.intevation.org