KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > javagroups > protocols > EVENT_TRACE


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.javagroups.protocols;
23
24 import org.jgroups.Event;
25 import org.jgroups.Message;
26 import org.jgroups.stack.Protocol;
27 import org.jboss.logging.Logger;
28
29 import java.util.Properties JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 /**
34  * A trival implementation of Protocol that traces all activity through
35  * it to its logger. This should be inserted between any two protocols
36  * you which to view the events between. Its supports a name property that
37  * allows you to insert the element multiple times in a stack to trace
38  * multiple protocols. An example config for the ClusterPartition for such
39  * a usage is:
40  *
41  <pre>
42    <mbean code="org.jboss.ha.framework.server.ClusterPartition"
43          name="jboss:service=JNDITestPartition">
44     <!-- -->
45     <attribute name="PartitionName">JNDITestPartition</attribute>
46     <attribute name="PartitionConfig">
47         <Config>
48            <TCP start_port="50001" bind_addr="172.17.66.55" />
49            <org.jboss.jgroups.protocols.EVENT_TRACE name="TCP-TCPPING-TRACE"
50                up_thread="false" down_thread="false" />
51            <TCPPING initial_hosts="lamia[50001]"
52                port_range="1" timeout="15000"
53                 up_thread="false" down_thread="false" />
54            <MERGE2 min_interval="5000" max_interval="20000" />
55            <FD max_tries="4" timeout="15000" />
56            <VERIFY_SUSPECT timeout="15000" />
57            <pbcast.STABLE desired_avg_gossip="20000" />
58            <pbcast.NAKACK gc_lag="50" retransmit_timeout="600,1200,2400,4800" />
59
60            <org.jboss.jgroups.protocols.EVENT_TRACE name="NAKACK-GMS-TRACE"
61             up_thread="false" down_thread="false" />
62            <pbcast.GMS join_timeout="15000" join_retry_timeout="5000"
63               shun="false" print_local_addr="true" />
64            <pbcast.STATE_TRANSFER />
65         </Config>
66      </attribute>
67   </mbean>
68  </pre>
69  * @author Scott.Stark@jboss.org
70  * @version $Revision: 37459 $
71  */

72 public class EVENT_TRACE extends Protocol
73 {
74    private String JavaDoc name = "EVENT_TRACE";
75    private Logger log;
76
77    public String JavaDoc getName()
78    {
79       return name;
80    }
81
82    /**
83     * @param props
84     * @return
85     */

86    public boolean setProperties(Properties JavaDoc props)
87    {
88       super.setProperties(props);
89       name = props.getProperty("name", name);
90       log = Logger.getLogger("org.jboss.jgroups."+name);
91       return true;
92    }
93
94    public void up(Event event)
95    {
96       if( log.isTraceEnabled() )
97       {
98          log.trace("up, event="+event);
99          if( event.getType() == Event.MSG )
100          {
101             Message msg = (Message) event.getArg();
102             msg.getHeaders();
103             log.trace("up, MsgHeader: "+printEventMsg(msg));
104          }
105       }
106       // Pass up the protocol stack
107
passUp(event);
108    }
109
110    public void down(Event event)
111    {
112       if( log.isTraceEnabled() )
113       {
114          log.trace("down, event="+event);
115          if( event.getType() == Event.MSG )
116          {
117             Message msg = (Message) event.getArg();
118             log.trace("down, MsgHeader: "+printEventMsg(msg));
119          }
120       }
121       // Pass down the protocol stack
122
passDown(event);
123    }
124
125    public String JavaDoc printEventMsg(Message msg)
126    {
127       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
128       Map.Entry JavaDoc entry;
129       Map JavaDoc headers = msg.getHeaders();
130       if (headers != null)
131       {
132          Iterator JavaDoc iter = headers.entrySet().iterator();
133          while( iter.hasNext() )
134          {
135             entry = (Map.Entry JavaDoc) iter.next();
136             Object JavaDoc key = entry.getKey();
137             Object JavaDoc value = entry.getValue();
138             sb.append(key);
139             sb.append(", value(");
140             sb.append(value.getClass());
141             sb.append("): ");
142             sb.append(value.toString());
143             sb.append("\n");
144          }
145       }
146       return sb.toString();
147    }
148
149 }
Popular Tags