1 package org.jgroups.blocks; 2 3 import org.jgroups.Message; 4 import org.jgroups.MessageListener; 5 6 import java.util.HashSet ; 7 8 18 public class MessageListenerAdapter implements MessageListener { 19 20 protected MessageListener stateListener; 21 22 protected final HashSet messageListeners = new HashSet (); 23 24 protected MessageListener[] messageListenersCache = new MessageListener[0]; 27 28 36 public MessageListenerAdapter() { 37 this(null); 38 } 39 40 48 public MessageListenerAdapter(MessageListener mainListener) { 49 if (mainListener != null) { 50 stateListener = mainListener; 51 addMessageListener(mainListener); 52 } 53 } 54 55 61 public byte[] getState() { 62 if (stateListener != null) 63 return stateListener.getState(); 64 else 65 return null; 66 } 67 68 74 public void receive(Message msg) { 75 for (int i = 0; i < messageListenersCache.length; i++) 76 messageListenersCache[i].receive(msg); 77 } 78 79 83 public void setState(byte[] state) { 84 if (stateListener != null) 85 stateListener.setState(state); 86 } 87 88 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 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 116 public void setStateListener(MessageListener listener) { 117 stateListener = listener; 118 } 119 } 120 | Popular Tags |