KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > management > stats > RouterStatistics


1 /*
2  * $Id: RouterStatistics.java 3798 2006-11-04 04:07:14Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.management.stats;
12
13 import org.mule.management.stats.printers.SimplePrinter;
14 import org.mule.umo.endpoint.UMOEndpoint;
15 import org.mule.umo.endpoint.UMOImmutableEndpoint;
16
17 import java.io.PrintWriter JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 /**
25  * <code>RouterStatistics</code> todo
26  *
27  * @author <a HREF="mailto:S.Vanmeerhaege@gfdi.be">Vanmeerhaeghe Stéphane</a>
28  * @version $Revision: 3798 $
29  */

30 public class RouterStatistics implements Statistics
31 {
32
33     /**
34      * Serial version
35      */

36     private static final long serialVersionUID = 4540482357430845065L;
37
38     public static final int TYPE_INBOUND = 1;
39     public static final int TYPE_OUTBOUND = 2;
40     public static final int TYPE_RESPONSE = 3;
41
42     private boolean enabled;
43     private long notRouted;
44     private long caughtInCatchAll;
45     private long totalRouted;
46     private long totalReceived;
47     private Map JavaDoc routed;
48     private int type;
49
50     /**
51      * @see org.mule.management.stats.Statistics#clear()
52      */

53     public synchronized void clear()
54     {
55         notRouted = 0;
56         totalRouted = 0;
57         totalReceived = 0;
58         caughtInCatchAll = 0;
59         routed.clear();
60     }
61
62     /**
63      * @see org.mule.management.stats.Statistics#isEnabled()
64      */

65     public boolean isEnabled()
66     {
67         return enabled;
68     }
69
70     public void logSummary()
71     {
72         logSummary(new SimplePrinter(System.out));
73     }
74
75     public void logSummary(PrintWriter JavaDoc printer)
76     {
77         printer.print(this);
78     }
79
80     /**
81      * @see org.mule.management.stats.Statistics#setEnabled(boolean)
82      */

83     public synchronized void setEnabled(boolean b)
84     {
85         enabled = b;
86     }
87
88     /**
89      * The constructor
90      */

91     public RouterStatistics(int type)
92     {
93         super();
94         this.type = type;
95         routed = new HashMap JavaDoc();
96     }
97
98     /**
99      * Increment routed message for multiple endpoints
100      *
101      * @param endpoints The endpoint collection
102      */

103     public void incrementRoutedMessage(Collection JavaDoc endpoints)
104     {
105         if (endpoints == null || endpoints.isEmpty())
106         {
107             return;
108         }
109         List JavaDoc list = new ArrayList JavaDoc(endpoints);
110         synchronized (list)
111         {
112             for (int i = 0; i < list.size(); i++)
113             {
114                 incrementRoutedMessage((UMOEndpoint)list.get(i));
115             }
116         }
117     }
118
119     /**
120      * Increment routed message for an endpoint
121      *
122      * @param endpoint The endpoint
123      */

124     public synchronized void incrementRoutedMessage(UMOImmutableEndpoint endpoint)
125     {
126         if (endpoint == null)
127         {
128             return;
129         }
130
131         String JavaDoc name = endpoint.getName();
132
133         Long JavaDoc cpt = (Long JavaDoc)routed.get(name);
134         long count = 0;
135
136         if (cpt != null)
137         {
138             count = cpt.longValue();
139         }
140
141         // TODO we should probably use a MutableLong here,
142
// but that might be problematic for remote MBean access (serialization)
143
routed.put(name, new Long JavaDoc(++count));
144
145         totalRouted++;
146         totalReceived++;
147     }
148
149     /**
150      * Increment no routed message
151      */

152     public synchronized void incrementNoRoutedMessage()
153     {
154         notRouted++;
155         totalReceived++;
156     }
157
158     /**
159      * Increment no routed message
160      */

161     public synchronized void incrementCaughtMessage()
162     {
163         caughtInCatchAll++;
164     }
165
166     /**
167      * @return Returns the notRouted.
168      */

169     public final long getCaughtMessages()
170     {
171         return caughtInCatchAll;
172     }
173
174     /**
175      * @return Returns the notRouted.
176      */

177     public final long getNotRouted()
178     {
179         return notRouted;
180     }
181
182     /**
183      * @return Returns the totalReceived.
184      */

185     public final long getTotalReceived()
186     {
187         return totalReceived;
188     }
189
190     /**
191      * @return Returns the totalRouted.
192      */

193     public final long getTotalRouted()
194     {
195         return totalRouted;
196     }
197
198     /**
199      * @return Returns the totalRouted.
200      */

201     public final long getRouted(String JavaDoc endpointName)
202     {
203         Long JavaDoc l = (Long JavaDoc)routed.get(endpointName);
204
205         if (l == null)
206         {
207             return 0;
208         }
209         else
210         {
211             return l.longValue();
212         }
213     }
214
215     public boolean isInbound()
216     {
217         return type == TYPE_INBOUND;
218     }
219
220     public Map JavaDoc getRouted()
221     {
222         return routed;
223     }
224 }
225
Popular Tags