KickJava   Java API By Example, From Geeks To Geeks.

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


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

18 public class MessageListenerAdapter implements MessageListener {
19     
20     protected MessageListener stateListener;
21     
22     protected final HashSet JavaDoc messageListeners = new HashSet JavaDoc();
23     
24     // we need this cache, because every call to messageListeners.iterator()
25
// would generate few new objects, but iteration over the cache would not.
26
protected MessageListener[] messageListenersCache = new MessageListener[0];
27     
28     /**
29      * Create default instance of this class. Newly created instance will have
30      * no message or state listeners. You have to use
31      * {@link #addMessageListener(MessageListener)} or
32      * {@link #removeMessageListener(MessageListener)} to add or remove message
33      * listeners, and {@link #setStateListener(MessageListener)} to set listener
34      * that will participate in state transfer.
35      */

36     public MessageListenerAdapter() {
37         this(null);
38     }
39
40     /**
41      * Create instance of this class. <code>mainListener</code> is a main
42      * listener instance that received message notifications and can get and
43      * set group state.
44      *
45      * @param mainListener instance of {@link MessageListener} that will
46      * provide state messages.
47      */

48     public MessageListenerAdapter(MessageListener mainListener) {
49         if (mainListener != null) {
50             stateListener = mainListener;
51             addMessageListener(mainListener);
52         }
53     }
54
55     /**
56      * Get state from state listener if present.
57      *
58      * @return current state of the group state or <code>null</code> if no state
59      * listeners were registered.
60      */

61     public byte[] getState() {
62         if (stateListener != null)
63             return stateListener.getState();
64         else
65             return null;
66     }
67
68     /**
69      * Receive message from group. This method will send this message to each
70      * message listener that was registered in this adapter.
71      *
72      * @param msg message to distribute within message listeners.
73      */

74     public void receive(Message msg) {
75         for (int i = 0; i < messageListenersCache.length; i++)
76             messageListenersCache[i].receive(msg);
77     }
78
79     /**
80      * Set state of ths group. This method will delegate call to state listener
81      * if it was previously registered.
82      */

83     public void setState(byte[] state) {
84         if (stateListener != null)
85             stateListener.setState(state);
86     }
87     
88     /**
89      * Add message listener to this adapter. This method registers
90      * <code>listener</code> for message notification.
91      * <p>
92      * Note, state notification will not be used.
93      */

94     public synchronized void addMessageListener(MessageListener listener) {
95         if (messageListeners.add(listener))
96             messageListenersCache =
97                 (MessageListener[])messageListeners.toArray(
98                     new MessageListener[messageListeners.size()]);
99     }
100     
101     /**
102      * Remove message listener from this adapter. This method deregisters
103      * <code>listener</code> from message notification.
104      */

105     public synchronized void removeMessageListener(MessageListener listener) {
106         if (messageListeners.remove(listener))
107             messageListenersCache =
108                 (MessageListener[])messageListeners.toArray(
109                     new MessageListener[messageListeners.size()]);
110     }
111     
112     /**
113      * Register <code>listener</code> for state notification events. There can
114      * be only one state listener per adapter.
115      */

116     public void setStateListener(MessageListener listener) {
117         stateListener = listener;
118     }
119 }
120
Popular Tags