KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > rmi > replication > ReplicationStrategy


1 package org.sapia.ubik.rmi.replication;
2
3 import org.sapia.ubik.net.ServerAddress;
4
5 import java.util.Set JavaDoc;
6
7
8 /**
9  * This class implements the logic that selects the next server to which a replicated command
10  * should be dispatched.
11  *
12  * @author Yanick Duchesne
13  * <dl>
14  * <dt><b>Copyright:</b><dd>Copyright &#169; 2002-2004 <a HREF="http://www.sapia-oss.org">Sapia Open Source Software</a>. All Rights Reserved.</dd></dt>
15  * <dt><b>License:</b><dd>Read the license.txt file of the jar or visit the
16  * <a HREF="http://www.sapia-oss.org/license.html">license page</a> at the Sapia OSS web site</dd></dt>
17  * </dl>
18  */

19 public class ReplicationStrategy {
20   private Set JavaDoc _visited;
21   private Set JavaDoc _targets;
22   private Set JavaDoc _siblings;
23
24   /**
25    * @param visited the <code>Set</code> of <code>ServerAddress</code>es of the hosts
26    * that have already been visited.
27    * @param targets the <code>Set</code> of <code>ServerAddress</code>es corresponding to
28    * targeted hosts - if <code>null</code>, then the strategy assumes that all hosts must
29    * be visited.
30    * @param existing the <code>Set</code> of <code>ServerAddress</code>es corresponding to
31    * the existing hosts.
32    */

33   public ReplicationStrategy(Set JavaDoc visited, Set JavaDoc targets, Set JavaDoc existing) {
34     _visited = visited;
35     _targets = targets;
36     _siblings = existing;
37   }
38
39   /**
40    * @return the <code>ServerAddress</code> of the next sibling host to which replication
41    * should be made. <code>null</code> is returned if there is no "next" host to which to
42    * replicate - all hosts have been visited. <b>Note</b>: if not null, the returned address
43    * is added to the set of visited ones.
44    */

45   public ServerAddress selectNextSibling() {
46     ServerAddress toReturn;
47
48     if (_targets == null) {
49       _siblings.removeAll(_visited);
50
51       if (_siblings.size() == 0) {
52         return null;
53       }
54
55       toReturn = (ServerAddress) _siblings.iterator().next();
56     } else {
57       _siblings.retainAll(_targets);
58       _siblings.removeAll(_visited);
59
60       if (_siblings.size() == 0) {
61         return null;
62       }
63
64       toReturn = (ServerAddress) _siblings.iterator().next();
65     }
66
67     _visited.add(toReturn);
68
69     return toReturn;
70   }
71 }
72
Popular Tags