KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > clustering > ChangeManager


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.clustering;
11
12 import java.util.Iterator JavaDoc;
13 import java.util.Map JavaDoc;
14
15 import org.mmbase.core.event.*;
16 import org.mmbase.module.core.*;
17 import org.mmbase.module.corebuilders.InsRel;
18
19 /**
20  * This utility class contains the methods for broadcasting/registering changes on nodes. It is
21  * available as 'getChangeManager()' from the StorageManagerFactory.
22  *
23  * @author Pierre van Rooden
24  * @version $Id: ChangeManager.java,v 1.12 2006/03/31 19:09:04 michiel Exp $
25  * @see org.mmbase.storage.StorageManagerFactory#getChangeManager
26  */

27 public final class ChangeManager {
28
29      /**
30      * Commit all changes stored in a Changes map.
31      * Clears the change status of all changed nodes, then broadcasts changes to the
32      * nodes' builders.
33      * @param changes a map with node/change value pairs
34      */

35     public void commit(Map JavaDoc changes) {
36         for (Iterator JavaDoc i = changes.entrySet().iterator(); i.hasNext(); ) {
37             Map.Entry JavaDoc e = (Map.Entry JavaDoc)i.next();
38             MMObjectNode node = (MMObjectNode)e.getKey();
39             String JavaDoc change = (String JavaDoc)e.getValue();
40             commit(node, change);
41             i.remove();
42         }
43     }
44
45     /**
46      * Commits the change to a node.
47      * Fires the node change events through the EventManager.
48      * Then clears 'changed' state on the node.
49      * @param node the node to commit the change of
50      * @param change the type of change: "n": new, "c": commit, "d": delete, "r" : relation changed
51      */

52     public void commit(MMObjectNode node, String JavaDoc change) {
53         MMObjectBuilder builder = node.getBuilder();
54         //create a new local node event
55
NodeEvent event = NodeEventHelper.createNodeEventInstance(node, NodeEvent.oldTypeToNewType(change), null);
56
57         //regardless of wether this is a relatione event we fire a node event first
58
EventManager.getInstance().propagateEvent(event);
59
60         //if the changed node is a relation, we fire a relation event as well
61
if(builder instanceof InsRel) {
62             RelationEvent relEvent = NodeEventHelper.createRelationEventInstance(node, NodeEvent.oldTypeToNewType(change), null);
63
64             //the relation event broker will make shure that listeners
65
//for node-relation changes to a specific builder, will be
66
//notified if this builder is either source or destination type
67
//in the relation event
68
EventManager.getInstance().propagateEvent(relEvent);
69         }
70
71         node.clearChanged();
72     }
73 }
74
Popular Tags