KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > blocks > MembershipListenerAdapter


1 package org.jgroups.blocks;
2
3 import org.jgroups.Address;
4 import org.jgroups.MembershipListener;
5 import org.jgroups.View;
6
7 import java.util.HashSet JavaDoc;
8
9 /**
10  * This class provides multiplexing possibilities for {@link MembershipListener}
11  * instances. Usually, we have more than one instance willing to listen to
12  * membership messages. {@link PullPushAdapter} allows only one instance of
13  * {@link MembershipListener} to be registered for message notification. With
14  * help of this class you can overcome this limitation.
15  *
16  * @author Roman Rokytskyy (rrokytskyy@acm.org)
17  */

18
19 public class MembershipListenerAdapter implements MembershipListener {
20     
21     protected final HashSet JavaDoc membershipListeners = new HashSet JavaDoc();
22     protected MembershipListener[] membershipListenersCache =
23         new MembershipListener[0];
24
25     /**
26      * Notify membership listeners to temporarily stop sending messages into
27      * a channel. This method in turn calls same method of all registered
28      * membership listener.
29      */

30     public void block() {
31         for(int i = 0; i < membershipListenersCache.length; i++)
32             membershipListenersCache[i].block();
33     }
34
35     /**
36      * Notify membership listener that some node was suspected. This method
37      * in turn passes suspected member address to all registered membership
38      * listeners.
39      */

40     public void suspect(Address suspected_mbr) {
41         for(int i = 0; i < membershipListenersCache.length; i++)
42             membershipListenersCache[i].suspect(suspected_mbr);
43     }
44
45     /**
46      * Notify membership listener that new view was accepted. This method in
47      * turn passes new view to all registered membership listeners.
48      */

49     public void viewAccepted(View new_view) {
50         for(int i = 0; i < membershipListenersCache.length; i++)
51             membershipListenersCache[i].viewAccepted(new_view);
52     }
53
54     /**
55      * Add membership listener to this adapter. This method registers
56      * <code>listener</code> to be notified when membership event is generated.
57      *
58      * @param listener instance of {@link MembershipListener} that should be
59      * added to this adapter.
60      */

61     public synchronized void addMembershipListener(MembershipListener listener) {
62         if (membershipListeners.add(listener))
63             membershipListenersCache =
64                 (MembershipListener[])membershipListeners.toArray(
65                     new MembershipListener[membershipListeners.size()]);
66     }
67
68     /**
69      * Remove membership listener from this adapter. This method deregisters
70      * <code>listener</code> from notification when membership event is generated.
71      *
72      * @param listener instance of {@link MembershipListener} that should be
73      * removed from this adapter.
74      */

75     public synchronized void removeMembershipListener(MembershipListener listener) {
76         if (membershipListeners.remove(listener))
77             membershipListenersCache =
78                 (MembershipListener[])membershipListeners.toArray(
79                     new MembershipListener[membershipListeners.size()]);
80
81     }
82     
83 }
Popular Tags