KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > management > exposed > TerracottaCluster


1 /*
2  * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

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 JavaDoc;
13 import java.util.List JavaDoc;
14
15 import javax.management.AttributeChangeNotification JavaDoc;
16 import javax.management.MBeanNotificationInfo JavaDoc;
17 import javax.management.NotCompliantMBeanException JavaDoc;
18 import javax.management.Notification JavaDoc;
19
20 public class TerracottaCluster extends AbstractTerracottaMBean implements TerracottaClusterMBean, ClusterEventListener {
21
22   public static final String JavaDoc THIS_NODE_CONNECTED = "com.tc.cluster.event.thisNodeConnected";
23   public static final String JavaDoc THIS_NODE_DISCONNECTED = "com.tc.cluster.event.thisNodeDisconnected";
24   public static final String JavaDoc NODE_CONNECTED = "com.tc.cluster.event.nodeConnected";
25   public static final String JavaDoc NODE_DISCONNECTED = "com.tc.cluster.event.nodeDisconnected";
26   public static final String JavaDoc[] ALL_EVENTS = new String JavaDoc[] { THIS_NODE_CONNECTED, THIS_NODE_DISCONNECTED,
27       NODE_CONNECTED, NODE_DISCONNECTED };
28   public static final String JavaDoc DESCRIPTION = "Terracotta Cluster Event Notification";
29
30   private final List JavaDoc nodes;
31
32   private String JavaDoc thisNodeId;
33   private boolean isThisNodeConnected;
34
35   private final SynchronizedLong notificationSequence = new SynchronizedLong(0L);
36
37   public TerracottaCluster()
38       throws NotCompliantMBeanException JavaDoc {
39     super(TerracottaClusterMBean.class, true);
40     nodes = new ArrayList JavaDoc();
41   }
42
43   public void reset() {
44     // nothing to do
45
}
46
47   public String JavaDoc getNodeId() {
48     return thisNodeId;
49   }
50
51   public String JavaDoc[] getNodesInCluster() {
52     synchronized (nodes) {
53       String JavaDoc[] rv = new String JavaDoc[nodes.size()];
54       nodes.toArray(rv);
55       return rv;
56     }
57   }
58
59   public boolean isConnected() {
60     return isThisNodeConnected;
61   }
62
63   public MBeanNotificationInfo JavaDoc[] getNotificationInfo() {
64     return new MBeanNotificationInfo JavaDoc[] { new MBeanNotificationInfo JavaDoc(ALL_EVENTS, AttributeChangeNotification JavaDoc.class
65         .getName(), DESCRIPTION) };
66   }
67
68   /**
69    * ClusterEventListener callback method...
70    */

71   public void nodeConnected(String JavaDoc nodeId) {
72     synchronized (nodes) {
73       nodes.add(nodeId);
74     }
75     makeNotification(NODE_CONNECTED, nodeId);
76   }
77
78   /**
79    * ClusterEventListener callback method...
80    */

81   public void nodeDisconnected(String JavaDoc nodeId) {
82     synchronized (nodes) {
83       nodes.remove(nodeId);
84     }
85     makeNotification(NODE_DISCONNECTED, nodeId);
86 }
87
88   /**
89    * ClusterEventListener callback method...
90    */

91   public void thisNodeConnected(String JavaDoc thisNode, String JavaDoc[] 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   /**
103    * ClusterEventListener callback method...
104    */

105   public void thisNodeDisconnected(String JavaDoc 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 JavaDoc event, String JavaDoc msg) {
115     sendNotification(new Notification JavaDoc(event, this, notificationSequence.increment(), msg));
116   }
117 }
118
Popular Tags