KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > net > protocol > tcm > MessageMonitorImpl


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

4 package com.tc.net.protocol.tcm;
5
6 import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong;
7
8 import com.tc.text.StringFormatter;
9
10 import java.util.HashMap JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.Map JavaDoc;
13
14 public class MessageMonitorImpl implements MessageMonitor {
15   
16   private final Map JavaDoc counters = new HashMap JavaDoc();
17   private final StringFormatter formatter = new StringFormatter();
18   
19   public void newIncomingMessage(TCMessage message) {
20     getOrCreateMessageCounter(message.getMessageType()).newIncomingMessage(message);
21   }
22
23   public void newOutgoingMessage(TCMessage message) {
24     getOrCreateMessageCounter(message.getMessageType()).newOutgoingMessage(message);
25   }
26   
27   private MessageCounter getOrCreateMessageCounter(Object JavaDoc key) {
28     synchronized (counters) {
29       MessageCounter rv = (MessageCounter) counters.get(key);
30       if (rv == null) {
31         rv = new MessageCounter(formatter, key.toString());
32         counters.put(key, rv);
33       }
34       return rv;
35     }
36   }
37   
38   public String JavaDoc toString() {
39     StringBuffer JavaDoc rv = new StringBuffer JavaDoc();
40     String JavaDoc nl = System.getProperty("line.separator");
41     rv.append("Message monitor").append(nl);
42     synchronized (counters) {
43       for (Iterator JavaDoc i=counters.values().iterator(); i.hasNext();) {
44         rv.append(i.next()).append(nl);
45       }
46     }
47     rv.append(nl);
48     return rv.toString();
49   }
50   
51   private static class MessageCounter {
52
53     private final String JavaDoc name;
54     private final SynchronizedLong incomingCount = new SynchronizedLong(0);
55     private final SynchronizedLong incomingData = new SynchronizedLong(0);
56
57     private final SynchronizedLong outgoingCount = new SynchronizedLong(0);
58     private final SynchronizedLong outgoingData = new SynchronizedLong(0);
59     private final StringFormatter formatter;
60
61     private MessageCounter(StringFormatter formatter, String JavaDoc name) {
62       this.formatter = formatter;
63       this.name = formatter.rightPad(25, name);
64     }
65
66     private synchronized void newIncomingMessage(TCMessage message) {
67       incomingCount.increment();
68       incomingData.add(message.getTotalLength());
69     }
70
71     private synchronized void newOutgoingMessage(TCMessage message) {
72       outgoingCount.increment();
73       outgoingData.add(message.getTotalLength());
74     }
75     
76     public String JavaDoc toString() {
77       return name + "| IN: " + formatter.leftPad(30, incomingCount) + ", " + formatter.leftPad(30, incomingData) + "b"
78              + "| OUT: " + formatter.leftPad(30, outgoingCount) + ", " + formatter.leftPad(30, outgoingData) + "b";
79
80     }
81   }
82 }
83
Popular Tags