1
2
3
4
5
6
7
8
9
10
11 package org.mule.management.stats;
12
13 import org.mule.api.endpoint.ImmutableEndpoint;
14 import org.mule.api.management.stats.Statistics;
15 import org.mule.management.stats.printers.SimplePrinter;
16
17 import java.io.PrintWriter;
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23
24
25
26
27
28 public class RouterStatistics implements Statistics
29 {
30
31
32
33
34 private static final long serialVersionUID = 4540482357430845065L;
35
36 public static final int TYPE_INBOUND = 1;
37 public static final int TYPE_OUTBOUND = 2;
38 public static final int TYPE_RESPONSE = 3;
39 public static final int TYPE_BINDING = 4;
40
41 private boolean enabled;
42 private long notRouted;
43 private long caughtInCatchAll;
44 private long totalRouted;
45 private long totalReceived;
46 private Map routed;
47 private int type;
48
49
50
51
52 public synchronized void clear()
53 {
54 notRouted = 0;
55 totalRouted = 0;
56 totalReceived = 0;
57 caughtInCatchAll = 0;
58 routed.clear();
59 }
60
61
62
63
64 public boolean isEnabled()
65 {
66 return enabled;
67 }
68
69 public void logSummary()
70 {
71 logSummary(new SimplePrinter(System.out));
72 }
73
74 public void logSummary(PrintWriter printer)
75 {
76 printer.print(this);
77 }
78
79
80
81
82 public synchronized void setEnabled(boolean b)
83 {
84 enabled = b;
85 }
86
87
88
89
90 public RouterStatistics(int type)
91 {
92 super();
93 this.type = type;
94 routed = new HashMap();
95 }
96
97
98
99
100
101
102 public void incrementRoutedMessage(Collection endpoints)
103 {
104 if (endpoints == null || endpoints.isEmpty())
105 {
106 return;
107 }
108 List list = new ArrayList(endpoints);
109 synchronized (list)
110 {
111 for (int i = 0; i < list.size(); i++)
112 {
113 incrementRoutedMessage(list.get(i));
114 }
115 }
116 }
117
118
119
120
121
122
123 public synchronized void incrementRoutedMessage(Object endpoint)
124 {
125 if (endpoint == null)
126 {
127 return;
128 }
129
130 String name;
131 if (endpoint instanceof ImmutableEndpoint)
132 {
133 name = ((ImmutableEndpoint)endpoint).getName();
134 }
135 else
136 {
137 name = endpoint.toString();
138 }
139
140 Long cpt = (Long) routed.get(name);
141 long count = 0;
142
143 if (cpt != null)
144 {
145 count = cpt.longValue();
146 }
147
148
149
150 routed.put(name, new Long(++count));
151
152 totalRouted++;
153 totalReceived++;
154 }
155
156
157
158
159 public synchronized void incrementNoRoutedMessage()
160 {
161 notRouted++;
162 totalReceived++;
163 }
164
165
166
167
168 public synchronized void incrementCaughtMessage()
169 {
170 caughtInCatchAll++;
171 }
172
173
174
175
176 public final long getCaughtMessages()
177 {
178 return caughtInCatchAll;
179 }
180
181
182
183
184 public final long getNotRouted()
185 {
186 return notRouted;
187 }
188
189
190
191
192 public final long getTotalReceived()
193 {
194 return totalReceived;
195 }
196
197
198
199
200 public final long getTotalRouted()
201 {
202 return totalRouted;
203 }
204
205
206
207
208 public final long getRouted(String endpointName)
209 {
210 Long l = (Long) routed.get(endpointName);
211
212 if (l == null)
213 {
214 return 0;
215 }
216 else
217 {
218 return l.longValue();
219 }
220 }
221
222 public boolean isInbound()
223 {
224 return type == TYPE_INBOUND;
225 }
226
227 public Map getRouted()
228 {
229 return routed;
230 }
231 }