diff artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java @ 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 1eb7863136f4
children eb8dbfa7125d
line wrap: on
line diff
--- 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;
     }

http://dive4elements.wald.intevation.org