001: package org.jgroups.jmx;
002:
003: import org.jgroups.*;
004: import org.w3c.dom.Element;
005: import org.w3c.dom.NodeList;
006: import org.w3c.dom.NamedNodeMap;
007: import org.w3c.dom.Attr;
008: import org.apache.commons.logging.Log;
009: import org.apache.commons.logging.LogFactory;
010:
011: import javax.management.MBeanServer;
012: import javax.management.MBeanServerFactory;
013: import java.io.Serializable;
014: import java.util.Map;
015:
016: /**
017: * @author Bela Ban
018: * @version $Id: JChannel.java,v 1.14 2006/08/10 08:05:00 belaban Exp $
019: */
020: public class JChannel implements JChannelMBean {
021: /** Ref to the original JGroups channel */
022: org.jgroups.JChannel channel;
023: String props = null;
024: String group_name = "TestGroup";
025: String object_name = null;
026: Log log = LogFactory.getLog(getClass());
027:
028: /*flag to indicate whether to receive blocks, if this is set to true, receive_views is set to true*/
029: private boolean receive_blocks = false;
030: /*flag to indicate whether to receive local messages
031: *if this is set to false, the JChannel will not receive messages sent by itself*/
032: private boolean receive_local_msgs = true;
033: /*flag to indicate whether the channel will reconnect (reopen) when the exit message is received*/
034: private boolean auto_reconnect = false;
035: /*flag t indicate whether the state is supposed to be retrieved after the channel is reconnected
036: *setting this to true, automatically forces auto_reconnect to true*/
037: private boolean auto_getstate = false;
038:
039: private String mbean_server_name = null;
040:
041: public JChannel() {
042: }
043:
044: public JChannel(org.jgroups.JChannel channel) {
045: this .channel = channel;
046: setValues();
047: }
048:
049: protected final void setValues() {
050: if (channel != null) {
051: props = getProperties();
052: group_name = channel.getClusterName();
053: receive_blocks = getReceiveBlockEvents();
054: receive_local_msgs = getReceiveLocalMessages();
055: auto_reconnect = getAutoReconnect();
056: auto_getstate = getAutoGetState();
057: }
058: }
059:
060: //void jbossInternalLifecycle(String method) throws Exception;
061: public org.jgroups.JChannel getChannel() {
062: return channel;
063: }
064:
065: public String getVersion() {
066: return Version.printDescription();
067: }
068:
069: public String getMBeanServerName() {
070: return mbean_server_name;
071: }
072:
073: public void setMBeanServerName(String n) {
074: this .mbean_server_name = n;
075: }
076:
077: public String getProperties() {
078: props = channel.getProperties();
079: return props;
080: }
081:
082: public void setProperties(String props) {
083: this .props = props;
084: }
085:
086: public String getObjectName() {
087: return object_name;
088: }
089:
090: public void setObjectName(String name) {
091: object_name = name;
092: }
093:
094: public int getNumberOfTasksInTimer() {
095: return channel.getNumberOfTasksInTimer();
096: }
097:
098: public String dumpTimerQueue() {
099: return channel.dumpTimerQueue();
100: }
101:
102: public void setClusterConfig(Element config) {
103: StringBuffer buffer = new StringBuffer();
104: NodeList stack = config.getChildNodes();
105: int length = stack.getLength();
106:
107: for (int s = 0; s < length; s++) {
108: org.w3c.dom.Node node = stack.item(s);
109: if (node.getNodeType() != org.w3c.dom.Node.ELEMENT_NODE)
110: continue;
111:
112: Element tag = (Element) node;
113: String protocol = tag.getTagName();
114: buffer.append(protocol);
115: NamedNodeMap attrs = tag.getAttributes();
116: int attrLength = attrs.getLength();
117: if (attrLength > 0)
118: buffer.append('(');
119: for (int a = 0; a < attrLength; a++) {
120: Attr attr = (Attr) attrs.item(a);
121: String name = attr.getName();
122: String value = attr.getValue();
123: buffer.append(name);
124: buffer.append('=');
125: buffer.append(value);
126: if (a < attrLength - 1)
127: buffer.append(';');
128: }
129: if (attrLength > 0)
130: buffer.append(')');
131: buffer.append(':');
132: }
133: // Remove the trailing ':'
134: buffer.setLength(buffer.length() - 1);
135: setProperties(buffer.toString());
136: if (log.isInfoEnabled())
137: log
138: .info("setting cluster properties from xml to: "
139: + props);
140: }
141:
142: public String getGroupName() {
143: if (channel != null)
144: group_name = channel.getClusterName();
145: return group_name;
146: }
147:
148: public void setGroupName(String group_name) {
149: this .group_name = group_name;
150: }
151:
152: public String getClusterName() {
153: return getGroupName();
154: }
155:
156: public void setClusterName(String cluster_name) {
157: setGroupName(cluster_name);
158: }
159:
160: public boolean getReceiveBlockEvents() {
161: if (channel != null)
162: receive_blocks = ((Boolean) channel.getOpt(Channel.BLOCK))
163: .booleanValue();
164: return receive_blocks;
165: }
166:
167: public void setReceiveBlockEvents(boolean flag) {
168: this .receive_blocks = flag;
169: if (channel != null)
170: channel.setOpt(Channel.BLOCK, new Boolean(flag));
171: }
172:
173: public boolean getReceiveLocalMessages() {
174: if (channel != null)
175: receive_local_msgs = ((Boolean) channel
176: .getOpt(Channel.LOCAL)).booleanValue();
177: return receive_local_msgs;
178: }
179:
180: public void setReceiveLocalMessages(boolean flag) {
181: this .receive_local_msgs = flag;
182: if (channel != null)
183: channel.setOpt(Channel.LOCAL, new Boolean(flag));
184: }
185:
186: public boolean getAutoReconnect() {
187: if (channel != null)
188: auto_reconnect = ((Boolean) channel
189: .getOpt(Channel.AUTO_RECONNECT)).booleanValue();
190: return auto_reconnect;
191: }
192:
193: public void setAutoReconnect(boolean flag) {
194: this .auto_reconnect = flag;
195: if (channel != null)
196: channel.setOpt(Channel.AUTO_RECONNECT, new Boolean(flag));
197: }
198:
199: public boolean getAutoGetState() {
200: if (channel != null)
201: auto_getstate = ((Boolean) channel
202: .getOpt(Channel.AUTO_GETSTATE)).booleanValue();
203: return auto_getstate;
204: }
205:
206: public void setAutoGetState(boolean flag) {
207: this .auto_getstate = flag;
208: if (channel != null)
209: channel.setOpt(Channel.AUTO_GETSTATE, new Boolean(flag));
210: }
211:
212: public boolean getStatsEnabled() {
213: return channel.statsEnabled();
214: }
215:
216: public void setStatsEnabled(boolean flag) {
217: channel.enableStats(flag);
218: }
219:
220: public Map dumpStats() {
221: return channel.dumpStats();
222: }
223:
224: public void resetStats() {
225: channel.resetStats();
226: }
227:
228: public long getSentMessages() {
229: return channel.getSentMessages();
230: }
231:
232: public long getSentBytes() {
233: return channel.getSentBytes();
234: }
235:
236: public long getReceivedMessages() {
237: return channel.getReceivedMessages();
238: }
239:
240: public long getReceivedBytes() {
241: return channel.getReceivedBytes();
242: }
243:
244: public void create() throws Exception {
245: if (channel != null)
246: channel.close();
247: channel = new org.jgroups.JChannel(props);
248: setOptions();
249: setValues();
250: MBeanServer server = (MBeanServer) MBeanServerFactory
251: .findMBeanServer(mbean_server_name).get(0);
252: JmxConfigurator.registerProtocols(server, channel,
253: getObjectName());
254: }
255:
256: public void start() throws Exception {
257: channel.connect(group_name);
258: }
259:
260: public void stop() {
261: if (channel != null)
262: channel.disconnect();
263: }
264:
265: public void destroy() {
266: MBeanServer server = (MBeanServer) MBeanServerFactory
267: .findMBeanServer(mbean_server_name).get(0);
268: JmxConfigurator.unregisterProtocols(server, channel,
269: getObjectName());
270: if (channel != null) {
271: channel.close();
272: channel = null;
273: }
274: }
275:
276: // public void jbossInternalLifecycle(String method) throws Exception {
277: // System.out.println("method: " + method);
278: // if (method == null)
279: // throw new IllegalArgumentException("Null method name");
280: //
281: // if (method.equals("create"))
282: // create();
283: // else if (method.equals("start"))
284: // start();
285: // else if (method.equals("stop"))
286: // stop();
287: // else if (method.equals("destroy"))
288: // destroy();
289: // else
290: // throw new IllegalArgumentException("Unknown lifecyle method " + method);
291: // }
292:
293: public View getView() {
294: return channel.getView();
295: }
296:
297: public String getViewAsString() {
298: View v = channel.getView();
299: return v != null ? v.toString() : "n/a";
300: }
301:
302: public Address getLocalAddress() {
303: return channel.getLocalAddress();
304: }
305:
306: public String getLocalAddressAsString() {
307: Address addr = getLocalAddress();
308: return addr != null ? addr.toString() : "n/a";
309: }
310:
311: /** @deprecated Use addChannelListener() instead */
312: public void setChannelListener(ChannelListener channel_listener) {
313: if (channel != null)
314: channel.addChannelListener(channel_listener);
315: }
316:
317: public void addChannelListener(ChannelListener listener) {
318: if (channel != null)
319: channel.addChannelListener(listener);
320: }
321:
322: public void removeChannelListener(ChannelListener l) {
323: if (channel != null)
324: channel.removeChannelListener(l);
325: }
326:
327: public boolean isOpen() {
328: return channel.isOpen();
329: }
330:
331: public boolean isConnected() {
332: return channel.isConnected();
333: }
334:
335: public int getNumMessages() {
336: return channel.getNumMessages();
337: }
338:
339: public String dumpQueue() {
340: return channel.dumpQueue();
341: }
342:
343: public String printProtocolSpec(boolean include_properties) {
344: return channel.printProtocolSpec(include_properties);
345: }
346:
347: public String toString(boolean print_details) {
348: return channel.toString(print_details);
349: }
350:
351: public void connect(String channel_name) throws ChannelException,
352: ChannelClosedException {
353: channel.connect(channel_name);
354: }
355:
356: public void disconnect() {
357: channel.disconnect();
358: }
359:
360: public void close() {
361: channel.close();
362: }
363:
364: public void shutdown() {
365: channel.shutdown();
366: }
367:
368: public void send(Message msg) throws ChannelNotConnectedException,
369: ChannelClosedException {
370: channel.send(msg);
371: }
372:
373: public void send(Address dst, Address src, Serializable obj)
374: throws ChannelNotConnectedException, ChannelClosedException {
375: channel.send(dst, src, obj);
376: }
377:
378: public void sendToAll(String msg)
379: throws ChannelNotConnectedException, ChannelClosedException {
380: send(null, null, msg);
381: }
382:
383: public void down(Event evt) {
384: channel.down(evt);
385: }
386:
387: public Object receive(long timeout)
388: throws ChannelNotConnectedException,
389: ChannelClosedException, TimeoutException {
390: return channel.receive(timeout);
391: }
392:
393: public Object peek(long timeout)
394: throws ChannelNotConnectedException,
395: ChannelClosedException, TimeoutException {
396: return channel.peek(timeout);
397: }
398:
399: public void blockOk() {
400: channel.blockOk();
401: }
402:
403: public boolean getState(Address target, long timeout)
404: throws ChannelNotConnectedException, ChannelClosedException {
405: return channel.getState(target, timeout);
406: }
407:
408: public void returnState(byte[] state) {
409: channel.returnState(state);
410: }
411:
412: public void returnState(byte[] state, String state_id) {
413: channel.returnState(state, state_id);
414: }
415:
416: private void setOptions() {
417: channel.setOpt(Channel.BLOCK, new Boolean(this .receive_blocks));
418: channel.setOpt(Channel.LOCAL, new Boolean(
419: this .receive_local_msgs));
420: channel.setOpt(Channel.AUTO_RECONNECT, new Boolean(
421: this .auto_reconnect));
422: channel.setOpt(Channel.AUTO_GETSTATE, new Boolean(
423: this.auto_getstate));
424: }
425:
426: }
|