comparison 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
comparison
equal deleted inserted replaced
330:3168af23aec5 331:089c6f7794b5
24 import de.intevation.artifacts.CallContext; 24 import de.intevation.artifacts.CallContext;
25 import de.intevation.artifacts.CallMeta; 25 import de.intevation.artifacts.CallMeta;
26 import de.intevation.artifacts.CollectionItem; 26 import de.intevation.artifacts.CollectionItem;
27 import de.intevation.artifacts.GlobalContext; 27 import de.intevation.artifacts.GlobalContext;
28 import de.intevation.artifacts.Hook; 28 import de.intevation.artifacts.Hook;
29 import de.intevation.artifacts.Message;
29 import de.intevation.artifacts.Service; 30 import de.intevation.artifacts.Service;
30 import de.intevation.artifacts.ServiceFactory; 31 import de.intevation.artifacts.ServiceFactory;
31 import de.intevation.artifacts.User; 32 import de.intevation.artifacts.User;
32 import de.intevation.artifacts.UserFactory; 33 import de.intevation.artifacts.UserFactory;
33 34
39 40
40 import java.util.Arrays; 41 import java.util.Arrays;
41 import java.util.Date; 42 import java.util.Date;
42 import java.util.HashMap; 43 import java.util.HashMap;
43 import java.util.HashSet; 44 import java.util.HashSet;
45 import java.util.LinkedList;
44 import java.util.List; 46 import java.util.List;
47 import java.util.Map;
45 import java.util.Set; 48 import java.util.Set;
46 49
47 import javax.xml.xpath.XPathConstants; 50 import javax.xml.xpath.XPathConstants;
48 51
49 import org.apache.commons.codec.binary.Base64; 52 import org.apache.commons.codec.binary.Base64;
419 /** 422 /**
420 * A set of ids of artifact which currently running in background. 423 * A set of ids of artifact which currently running in background.
421 * This artifacts should not be removed from the database by the 424 * This artifacts should not be removed from the database by the
422 * database cleaner. 425 * database cleaner.
423 */ 426 */
424 protected HashSet<Integer> backgroundIds; 427 protected HashSet<Integer> backgroundIds;
428
429 /**
430 * A list of background messages for Artifacts and Collections.
431 */
432 protected Map<String, LinkedList<Message>> backgroundMsgs;
433
425 434
426 protected CallContext.Listener callContextListener; 435 protected CallContext.Listener callContextListener;
427 436
428 /** 437 /**
429 * Hooks that are executed after an artifact has been fed. 438 * Hooks that are executed after an artifact has been fed.
462 */ 471 */
463 public ArtifactDatabaseImpl(FactoryBootstrap bootstrap, Backend backend) { 472 public ArtifactDatabaseImpl(FactoryBootstrap bootstrap, Backend backend) {
464 473
465 logger.debug("new ArtifactDatabaseImpl"); 474 logger.debug("new ArtifactDatabaseImpl");
466 475
467 backgroundIds = new HashSet<Integer>(); 476 backgroundIds = new HashSet<Integer>();
477 backgroundMsgs = new HashMap<String, LinkedList<Message>>();
468 478
469 setupArtifactCollectionFactory(bootstrap); 479 setupArtifactCollectionFactory(bootstrap);
470 setupArtifactFactories(bootstrap); 480 setupArtifactFactories(bootstrap);
471 setupServices(bootstrap); 481 setupServices(bootstrap);
472 setupUserFactory(bootstrap); 482 setupUserFactory(bootstrap);
639 break; 649 break;
640 default: 650 default:
641 logger.warn("operation not allowed in fromBackground"); 651 logger.warn("operation not allowed in fromBackground");
642 } 652 }
643 removeIdFromBackground(artifact.getId()); 653 removeIdFromBackground(artifact.getId());
654 removeBackgroundMessages(artifact.getArtifact().identifier());
644 } 655 }
645 656
646 /** 657 /**
647 * Removes an artifact's database id from the set of backgrounded 658 * Removes an artifact's database id from the set of backgrounded
648 * artifacts. The database cleaner is now able to remove it safely 659 * artifacts. The database cleaner is now able to remove it safely
650 * @param id The database id of the artifact. 661 * @param id The database id of the artifact.
651 */ 662 */
652 protected void removeIdFromBackground(int id) { 663 protected void removeIdFromBackground(int id) {
653 synchronized (backgroundIds) { 664 synchronized (backgroundIds) {
654 backgroundIds.remove(id); 665 backgroundIds.remove(id);
666 }
667 }
668
669
670 /**
671 * Removes all messages that have been added to the <i>backgroundMsgs</i>
672 * list.
673 *
674 * @param uuid The UUID of an artifact or collection.
675 */
676 protected void removeBackgroundMessages(String uuid) {
677 logger.debug("Remove background messages for: " + uuid);
678
679 synchronized (backgroundMsgs) {
680 backgroundMsgs.remove(uuid);
655 } 681 }
656 } 682 }
657 683
658 /** 684 /**
659 * Adds an artifact's database id to the set of artifacts 685 * Adds an artifact's database id to the set of artifacts
666 synchronized (backgroundIds) { 692 synchronized (backgroundIds) {
667 backgroundIds.add(Integer.valueOf(id)); 693 backgroundIds.add(Integer.valueOf(id));
668 } 694 }
669 } 695 }
670 696
697 /**
698 * Adds a <i>Message</i> to the background messages list of the Artifact or
699 * Collection.
700 *
701 * @param uuid The UUID of the Artifact or Collection.
702 * @param msg The message that should be added to the background messages
703 * list.
704 */
705 public void addBackgroundMessage(String uuid, Message msg) {
706 logger.debug("Add new background messsage for: " + uuid);
707
708 synchronized (backgroundMsgs) {
709 LinkedList<Message> messages = backgroundMsgs.get(uuid);
710
711 if (messages == null) {
712 messages = new LinkedList<Message>();
713 backgroundMsgs.put(uuid, messages);
714 }
715
716 messages.addLast(msg);
717 }
718 }
719
671 public Set<Integer> getLockedIds() { 720 public Set<Integer> getLockedIds() {
672 synchronized (backgroundIds) { 721 synchronized (backgroundIds) {
673 return new HashSet<Integer>(backgroundIds); 722 return new HashSet<Integer>(backgroundIds);
723 }
724 }
725
726 /**
727 * Returns the background <i>Message</i>s for a specific Artifact or
728 * Collection.
729 *
730 * @param uuid The Artifact's or Collection's UUID.
731 *
732 * @return a <i>List</i> of <i>Message</i>s or null if no messages are
733 * existing.
734 */
735 public LinkedList<Message> getBackgroundMessages(String uuid) {
736 logger.debug("Retrieve background message for: " + uuid);
737
738 synchronized (backgroundMsgs) {
739 return backgroundMsgs.get(uuid);
674 } 740 }
675 } 741 }
676 742
677 public String [][] artifactFactoryNamesAndDescriptions() { 743 public String [][] artifactFactoryNamesAndDescriptions() {
678 return factoryNamesAndDescription; 744 return factoryNamesAndDescription;

http://dive4elements.wald.intevation.org