# HG changeset patch # User Ingo Weinzierl # Date 1315577215 0 # Node ID 089c6f7794b57acfe0f6ea1aa5656ab315a09049 # Parent 3168af23aec51c5f60baa0331db027c242a478aa Integrated a messaging system for Artifacts and Collections that started background threads. artifacts/trunk@2688 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 3168af23aec5 -r 089c6f7794b5 ChangeLog --- 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 + + * 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 * artifacts/src/main/java/de/intevation/artifacts/CallContext.java, diff -r 3168af23aec5 -r 089c6f7794b5 artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactCallContext.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 getBackgroundMessages() { + return database.getBackgroundMessages( + artifact.getArtifact().identifier()); + } + + public Long getTimeToLive() { return artifact.getTTL(); } diff -r 3168af23aec5 -r 089c6f7794b5 artifact-database/src/main/java/de/intevation/artifactdatabase/ArtifactDatabaseImpl.java --- 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 backgroundIds; + protected HashSet backgroundIds; + + /** + * A list of background messages for Artifacts and Collections. + */ + protected Map> backgroundMsgs; + protected CallContext.Listener callContextListener; @@ -464,7 +473,8 @@ logger.debug("new ArtifactDatabaseImpl"); - backgroundIds = new HashSet(); + backgroundIds = new HashSet(); + backgroundMsgs = new HashMap>(); 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 backgroundMsgs + * 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 Message 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 messages = backgroundMsgs.get(uuid); + + if (messages == null) { + messages = new LinkedList(); + backgroundMsgs.put(uuid, messages); + } + + messages.addLast(msg); + } + } + public Set getLockedIds() { synchronized (backgroundIds) { return new HashSet(backgroundIds); } } + /** + * Returns the background Messages for a specific Artifact or + * Collection. + * + * @param uuid The Artifact's or Collection's UUID. + * + * @return a List of Messages or null if no messages are + * existing. + */ + public LinkedList getBackgroundMessages(String uuid) { + logger.debug("Retrieve background message for: " + uuid); + + synchronized (backgroundMsgs) { + return backgroundMsgs.get(uuid); + } + } + public String [][] artifactFactoryNamesAndDescriptions() { return factoryNamesAndDescription; } diff -r 3168af23aec5 -r 089c6f7794b5 artifact-database/src/main/java/de/intevation/artifactdatabase/CollectionCallContext.java --- 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 getBackgroundMessages() { + log.debug("CollectionCallContext.addBackgroundMessage NOT IMPLEMENTED"); + return null; + } + + public Long getTimeToLive() { log.debug("CollectionCallContext.getTimeToLive - NOT IMPLEMENTED"); return null; diff -r 3168af23aec5 -r 089c6f7794b5 artifacts/src/main/java/de/intevation/artifacts/CallContext.java --- 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 getBackgroundMessages(); + + /** * Access to the global context of the runtime system. * @return The global context. */ diff -r 3168af23aec5 -r 089c6f7794b5 artifacts/src/main/java/de/intevation/artifacts/Message.java --- /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 Ingo Weinzierl + */ +public interface Message extends Serializable { + + String getText(); +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :