changeset 331:089c6f7794b5

Integrated a messaging system for Artifacts and Collections that started background threads. artifacts/trunk@2688 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 09 Sep 2011 14:06:55 +0000
parents 3168af23aec5
children c53ec9fdc758 4f21bb238062
files ChangeLog artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactCallContext.java artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java artifact-database/src/main/java/de/intevation/artifactdatabase/CollectionCallContext.java artifacts/src/main/java/de/intevation/artifacts/CallContext.java artifacts/src/main/java/de/intevation/artifacts/Message.java
diffstat 6 files changed, 155 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Wed Sep 07 13:51:02 2011 +0000
+++ b/ChangeLog	Fri Sep 09 14:06:55 2011 +0000
@@ -1,3 +1,26 @@
+2011-09-09  Ingo Weinzierl <ingo@intevation.de>
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java:
+	  Artifacts and Collections that started a background process might add
+	  Messages to a message board now. They (currently just implemented for
+	  the Artifact) can add new messages and receive a list of messages via
+	  the CallContext objects. If an Artifact or Collection instance is
+	  removed from background, all its messages are removed as well.
+
+	* artifacts/src/main/java/de/intevation/artifacts/Message.java: New. The
+	  message interface. Currently, there is just a single getText() method
+	  defined.
+
+	* artifacts/src/main/java/de/intevation/artifacts/CallContext.java: Got
+	  two new methods to add new messages and retrieve a list of messages to
+	  the background messages.
+
+	* artifact-database/src/main/java/de/intevation/artifactdatabase/CollectionCallContext.java,
+	  artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactCallContext.java:
+	  Implement the new methods to add/get messages defined in CallContext.
+	  The CollectionCallContext just defines the two methods without real
+	  implementation (stub).
+
 2011-09-07  Ingo Weinzierl <ingo@intevation.de>
 
 	* artifacts/src/main/java/de/intevation/artifacts/CallContext.java,
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactCallContext.java	Wed Sep 07 13:51:02 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactCallContext.java	Fri Sep 09 14:06:55 2011 +0000
@@ -7,9 +7,12 @@
  */
 package de.intevation.artifactdatabase;
 
+import java.util.LinkedList;
+
 import org.apache.log4j.Logger;
 
 import de.intevation.artifacts.CallMeta;
+import de.intevation.artifacts.Message;
 
 import de.intevation.artifactdatabase.Backend.PersistentArtifact;
 
@@ -78,6 +81,17 @@
     }
 
 
+    public void addBackgroundMessage(Message msg) {
+        database.addBackgroundMessage(artifact.getArtifact().identifier(), msg);
+    }
+
+
+    public LinkedList<Message> getBackgroundMessages() {
+        return database.getBackgroundMessages(
+            artifact.getArtifact().identifier());
+    }
+
+
     public Long getTimeToLive() {
         return artifact.getTTL();
     }
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java	Wed Sep 07 13:51:02 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java	Fri Sep 09 14:06:55 2011 +0000
@@ -26,6 +26,7 @@
 import de.intevation.artifacts.CollectionItem;
 import de.intevation.artifacts.GlobalContext;
 import de.intevation.artifacts.Hook;
+import de.intevation.artifacts.Message;
 import de.intevation.artifacts.Service;
 import de.intevation.artifacts.ServiceFactory;
 import de.intevation.artifacts.User;
@@ -41,7 +42,9 @@
 import java.util.Date;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.LinkedList;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 import javax.xml.xpath.XPathConstants;
@@ -421,7 +424,13 @@
      * This artifacts should not be removed from the database by the
      * database cleaner.
      */
-    protected HashSet<Integer>  backgroundIds;
+    protected HashSet<Integer> backgroundIds;
+
+    /**
+     * A list of background messages for Artifacts and Collections.
+     */
+    protected Map<String, LinkedList<Message>> backgroundMsgs;
+
 
     protected CallContext.Listener callContextListener;
 
@@ -464,7 +473,8 @@
 
         logger.debug("new ArtifactDatabaseImpl");
 
-        backgroundIds = new HashSet<Integer>();
+        backgroundIds  = new HashSet<Integer>();
+        backgroundMsgs = new HashMap<String, LinkedList<Message>>();
 
         setupArtifactCollectionFactory(bootstrap);
         setupArtifactFactories(bootstrap);
@@ -641,6 +651,7 @@
                 logger.warn("operation not allowed in fromBackground");
         }
         removeIdFromBackground(artifact.getId());
+        removeBackgroundMessages(artifact.getArtifact().identifier());
     }
 
     /**
@@ -655,6 +666,21 @@
         }
     }
 
+
+    /**
+     * Removes all messages that have been added to the <i>backgroundMsgs</i>
+     * list.
+     *
+     * @param uuid The UUID of an artifact or collection.
+     */
+    protected void removeBackgroundMessages(String uuid) {
+        logger.debug("Remove background messages for: " + uuid);
+
+        synchronized (backgroundMsgs) {
+            backgroundMsgs.remove(uuid);
+        }
+    }
+
     /**
      * Adds an artifact's database id to the set of artifacts
      * running in backgroound. To be in this set prevents the
@@ -668,12 +694,52 @@
         }
     }
 
+    /**
+     * Adds a <i>Message</i> to the background messages list of the Artifact or
+     * Collection.
+     *
+     * @param uuid The UUID of the Artifact or Collection.
+     * @param msg The message that should be added to the background messages
+     * list.
+     */
+    public void addBackgroundMessage(String uuid, Message msg) {
+        logger.debug("Add new background messsage for: " + uuid);
+
+        synchronized (backgroundMsgs) {
+            LinkedList<Message> messages = backgroundMsgs.get(uuid);
+
+            if (messages == null) {
+                messages = new LinkedList<Message>();
+                backgroundMsgs.put(uuid, messages);
+            }
+
+            messages.addLast(msg);
+        }
+    }
+
     public Set<Integer> getLockedIds() {
         synchronized (backgroundIds) {
             return new HashSet<Integer>(backgroundIds);
         }
     }
 
+    /**
+     * Returns the background <i>Message</i>s for a specific Artifact or
+     * Collection.
+     *
+     * @param uuid The Artifact's or Collection's UUID.
+     *
+     * @return a <i>List</i> of <i>Message</i>s or null if no messages are
+     * existing.
+     */
+    public LinkedList<Message> getBackgroundMessages(String uuid) {
+        logger.debug("Retrieve background message for: " + uuid);
+
+        synchronized (backgroundMsgs) {
+            return backgroundMsgs.get(uuid);
+        }
+    }
+
     public String [][] artifactFactoryNamesAndDescriptions() {
         return factoryNamesAndDescription;
     }
--- a/artifact-database/src/main/java/de/intevation/artifactdatabase/CollectionCallContext.java	Wed Sep 07 13:51:02 2011 +0000
+++ b/artifact-database/src/main/java/de/intevation/artifactdatabase/CollectionCallContext.java	Fri Sep 09 14:06:55 2011 +0000
@@ -7,10 +7,13 @@
  */
 package de.intevation.artifactdatabase;
 
+import java.util.LinkedList;
+
 import org.apache.log4j.Logger;
 
 import de.intevation.artifacts.ArtifactCollection;
 import de.intevation.artifacts.CallMeta;
+import de.intevation.artifacts.Message;
 
 
 /**
@@ -57,6 +60,17 @@
     }
 
 
+    public void addBackgroundMessage(Message msg) {
+        log.debug("CollectionCallContext.addBackgroundMessage NOT IMPLEMENTED");
+    }
+
+
+    public LinkedList<Message> getBackgroundMessages() {
+        log.debug("CollectionCallContext.addBackgroundMessage NOT IMPLEMENTED");
+        return null;
+    }
+
+
     public Long getTimeToLive() {
         log.debug("CollectionCallContext.getTimeToLive - NOT IMPLEMENTED");
         return null;
--- a/artifacts/src/main/java/de/intevation/artifacts/CallContext.java	Wed Sep 07 13:51:02 2011 +0000
+++ b/artifacts/src/main/java/de/intevation/artifacts/CallContext.java	Fri Sep 09 14:06:55 2011 +0000
@@ -8,6 +8,8 @@
 
 package de.intevation.artifacts;
 
+import java.util.LinkedList;
+
 import org.w3c.dom.Document;
 import org.w3c.dom.Node;
 
@@ -71,6 +73,20 @@
     boolean isInBackground();
 
     /**
+     * Adds a background message for the current Artifact or Collection.
+     *
+     * @param msg The message.
+     */
+    void addBackgroundMessage(Message msg);
+
+    /**
+     * Returns the background messages of the current Artifact or Collection.
+     *
+     * @return the list of background messages.
+     */
+    LinkedList<Message> getBackgroundMessages();
+
+    /**
      * Access to the global context of the runtime system.
      * @return The global context.
      */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/de/intevation/artifacts/Message.java	Fri Sep 09 14:06:55 2011 +0000
@@ -0,0 +1,20 @@
+/*
+ * Copyright (c) 2011 by Intevation GmbH
+ *
+ * This program is free software under the LGPL (>=v2.1)
+ * Read the file LGPL.txt coming with the software for details
+ * or visit http://www.gnu.org/licenses/ if it does not exist.
+ */
+package de.intevation.artifacts;
+
+import java.io.Serializable;
+
+
+/**
+ * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
+ */
+public interface Message extends Serializable {
+
+    String getText();
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org