1 5 package com.tc.management.exposed; 6 7 import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong; 8 9 import com.tc.cluster.ClusterEventListener; 10 import com.tc.management.AbstractTerracottaMBean; 11 12 import java.util.ArrayList ; 13 import java.util.List ; 14 15 import javax.management.AttributeChangeNotification ; 16 import javax.management.MBeanNotificationInfo ; 17 import javax.management.NotCompliantMBeanException ; 18 import javax.management.Notification ; 19 20 public class TerracottaCluster extends AbstractTerracottaMBean implements TerracottaClusterMBean, ClusterEventListener { 21 22 public static final String THIS_NODE_CONNECTED = "com.tc.cluster.event.thisNodeConnected"; 23 public static final String THIS_NODE_DISCONNECTED = "com.tc.cluster.event.thisNodeDisconnected"; 24 public static final String NODE_CONNECTED = "com.tc.cluster.event.nodeConnected"; 25 public static final String NODE_DISCONNECTED = "com.tc.cluster.event.nodeDisconnected"; 26 public static final String [] ALL_EVENTS = new String [] { THIS_NODE_CONNECTED, THIS_NODE_DISCONNECTED, 27 NODE_CONNECTED, NODE_DISCONNECTED }; 28 public static final String DESCRIPTION = "Terracotta Cluster Event Notification"; 29 30 private final List nodes; 31 32 private String thisNodeId; 33 private boolean isThisNodeConnected; 34 35 private final SynchronizedLong notificationSequence = new SynchronizedLong(0L); 36 37 public TerracottaCluster() 38 throws NotCompliantMBeanException { 39 super(TerracottaClusterMBean.class, true); 40 nodes = new ArrayList (); 41 } 42 43 public void reset() { 44 } 46 47 public String getNodeId() { 48 return thisNodeId; 49 } 50 51 public String [] getNodesInCluster() { 52 synchronized (nodes) { 53 String [] rv = new String [nodes.size()]; 54 nodes.toArray(rv); 55 return rv; 56 } 57 } 58 59 public boolean isConnected() { 60 return isThisNodeConnected; 61 } 62 63 public MBeanNotificationInfo [] getNotificationInfo() { 64 return new MBeanNotificationInfo [] { new MBeanNotificationInfo (ALL_EVENTS, AttributeChangeNotification .class 65 .getName(), DESCRIPTION) }; 66 } 67 68 71 public void nodeConnected(String nodeId) { 72 synchronized (nodes) { 73 nodes.add(nodeId); 74 } 75 makeNotification(NODE_CONNECTED, nodeId); 76 } 77 78 81 public void nodeDisconnected(String nodeId) { 82 synchronized (nodes) { 83 nodes.remove(nodeId); 84 } 85 makeNotification(NODE_DISCONNECTED, nodeId); 86 } 87 88 91 public void thisNodeConnected(String thisNode, String [] nodesCurrentlyInCluster) { 92 synchronized (nodes) { 93 thisNodeId = thisNode; 94 isThisNodeConnected = true; 95 for (int i = 0; i < nodesCurrentlyInCluster.length; i++) { 96 nodes.add(nodesCurrentlyInCluster[i]); 97 } 98 } 99 makeNotification(THIS_NODE_CONNECTED, thisNodeId); 100 } 101 102 105 public void thisNodeDisconnected(String thisNode) { 106 synchronized (nodes) { 107 isThisNodeConnected = false; 108 nodes.clear(); 109 nodes.add(thisNodeId); 110 } 111 makeNotification(THIS_NODE_DISCONNECTED, thisNodeId); 112 } 113 114 private void makeNotification(String event, String msg) { 115 sendNotification(new Notification (event, this, notificationSequence.increment(), msg)); 116 } 117 } 118 | Popular Tags |