001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.javagroups.protocols;
023:
024: import org.jgroups.Event;
025: import org.jgroups.Message;
026: import org.jgroups.stack.Protocol;
027: import org.jboss.logging.Logger;
028:
029: import java.util.Properties;
030: import java.util.Map;
031: import java.util.Iterator;
032:
033: /**
034: * A trival implementation of Protocol that traces all activity through
035: * it to its logger. This should be inserted between any two protocols
036: * you which to view the events between. Its supports a name property that
037: * allows you to insert the element multiple times in a stack to trace
038: * multiple protocols. An example config for the ClusterPartition for such
039: * a usage is:
040: *
041: <pre>
042: <mbean code="org.jboss.ha.framework.server.ClusterPartition"
043: name="jboss:service=JNDITestPartition">
044: <!-- -->
045: <attribute name="PartitionName">JNDITestPartition</attribute>
046: <attribute name="PartitionConfig">
047: <Config>
048: <TCP start_port="50001" bind_addr="172.17.66.55" />
049: <org.jboss.jgroups.protocols.EVENT_TRACE name="TCP-TCPPING-TRACE"
050: up_thread="false" down_thread="false" />
051: <TCPPING initial_hosts="lamia[50001]"
052: port_range="1" timeout="15000"
053: up_thread="false" down_thread="false" />
054: <MERGE2 min_interval="5000" max_interval="20000" />
055: <FD max_tries="4" timeout="15000" />
056: <VERIFY_SUSPECT timeout="15000" />
057: <pbcast.STABLE desired_avg_gossip="20000" />
058: <pbcast.NAKACK gc_lag="50" retransmit_timeout="600,1200,2400,4800" />
059:
060: <org.jboss.jgroups.protocols.EVENT_TRACE name="NAKACK-GMS-TRACE"
061: up_thread="false" down_thread="false" />
062: <pbcast.GMS join_timeout="15000" join_retry_timeout="5000"
063: shun="false" print_local_addr="true" />
064: <pbcast.STATE_TRANSFER />
065: </Config>
066: </attribute>
067: </mbean>
068: </pre>
069: * @author Scott.Stark@jboss.org
070: * @version $Revision: 57188 $
071: */
072: public class EVENT_TRACE extends Protocol {
073: private String name = "EVENT_TRACE";
074: private Logger log;
075:
076: public String getName() {
077: return name;
078: }
079:
080: /**
081: * @param props
082: * @return
083: */
084: public boolean setProperties(Properties props) {
085: super .setProperties(props);
086: name = props.getProperty("name", name);
087: log = Logger.getLogger("org.jboss.jgroups." + name);
088: return true;
089: }
090:
091: public void up(Event event) {
092: if (log.isTraceEnabled()) {
093: log.trace("up, event=" + event);
094: if (event.getType() == Event.MSG) {
095: Message msg = (Message) event.getArg();
096: msg.getHeaders();
097: log.trace("up, MsgHeader: " + printEventMsg(msg));
098: }
099: }
100: // Pass up the protocol stack
101: passUp(event);
102: }
103:
104: public void down(Event event) {
105: if (log.isTraceEnabled()) {
106: log.trace("down, event=" + event);
107: if (event.getType() == Event.MSG) {
108: Message msg = (Message) event.getArg();
109: log.trace("down, MsgHeader: " + printEventMsg(msg));
110: }
111: }
112: // Pass down the protocol stack
113: passDown(event);
114: }
115:
116: public String printEventMsg(Message msg) {
117: StringBuffer sb = new StringBuffer();
118: Map.Entry entry;
119: Map headers = msg.getHeaders();
120: if (headers != null) {
121: Iterator iter = headers.entrySet().iterator();
122: while (iter.hasNext()) {
123: entry = (Map.Entry) iter.next();
124: Object key = entry.getKey();
125: Object value = entry.getValue();
126: sb.append(key);
127: sb.append(", value(");
128: sb.append(value.getClass());
129: sb.append("): ");
130: sb.append(value.toString());
131: sb.append("\n");
132: }
133: }
134: return sb.toString();
135: }
136:
137: }
|