next up previous contents
Next: NotificationBus Up: Building Blocks Previous: ReplicatedHashtable   Contents

DistributedTree

Similar to DistributedHashtable this class also provides replication of a data structure across multiple processes. However, a tree structure instead of a hashtable is replicated by DistributedTree. Updates are multicast to all group members reliably and in the same order using the underlying channel.

The tree consists of a root and zero or more child nodes. Each node can be either another subtree, or a leaf node. A node has a name and a value. The value can be any object that is serializable. A node in the tree is identified by concatenating all nodes from the root to it, separated with '/' characters, e.g.

/a/b/c.

New nodes can be added dynamically. Existing nodes (also entire subtrees) can be removed. Values can be attached to an existing node. Whenever the tree is modified events will be sent for which listeners can register. Listeners have to implement interface DistributedTreeListener:

  public interface DistributedTreeListener {
      void nodeAdded(String fqn, Serializable element);
      void nodeRemoved(String fqn);
      void nodeModified(String fqn, Serializable old_element, Serializable new_element);
  }

The methods provided by DistributedTree are listed below (not all methods shown):

  public class DistributedTree {
       public void         add(String fqn);
       public void         add(String fqn, Serializable element);
       public void         remove(String fqn);
       public boolean      exists(String fqn);
       public Serializable get(String fqn);
       public void         set(String fqn, Serializable element);
       public Vector       getChildrenNames(String fqn);
  }

The two add() methods add a new node. The first method assigns no value to the node, whereas the second does. Note that it does not matter whether or not parent nodes exists: an addition of "/a/b/c/d" to a tree "/a/b" would create nodes "/a/b/c" and "/a/b/c/d". However, if a value was given, it would be assigned only to the latter.

The remove() method removes a node from the tree. If the node is a subtree itself, all nodes under it will be removed recursively. E.g. the removal of "/" from "/a/b" would trigger 3 nodeRemoved() notifications: "/a/b", "/a" and "/" (in this order)4.2.

The exists() method tests whether a given node exists in the tree.

The get() method returns either the value associated with the given node, or null if the node cannot be found or there is no value attached.

Method set() attaches a value to a given node. It fails if the node does not exist. Use add(String, Serializable) instead if the node should be created if not existent.

Method getChildrenNames() furnishes a list of the fully qualified names of all children nodes of a given node. This gives a programmer modest navigation possibilities within the tree.

There is a demo application in org.javagroups.demos.DistributedTreeDemo.


next up previous contents
Next: NotificationBus Up: Building Blocks Previous: ReplicatedHashtable   Contents
Bela Ban 2002-11-16