1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.management.stats.printers; |
8 | |
|
9 | |
import org.mule.management.stats.RouterStatistics; |
10 | |
import org.mule.util.StringUtils; |
11 | |
|
12 | |
import java.io.OutputStream; |
13 | |
import java.io.Writer; |
14 | |
import java.util.Collection; |
15 | |
import java.util.Iterator; |
16 | |
import java.util.Map; |
17 | |
import java.util.StringTokenizer; |
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
public class XMLPrinter extends AbstractTablePrinter |
25 | |
{ |
26 | |
|
27 | |
|
28 | |
|
29 | |
protected static final int XML_INDENT_SIZE = 2; |
30 | |
|
31 | |
public XMLPrinter(Writer out) |
32 | |
{ |
33 | 0 | super(out); |
34 | 0 | } |
35 | |
|
36 | |
public XMLPrinter(OutputStream out) |
37 | |
{ |
38 | 0 | super(out); |
39 | 0 | } |
40 | |
|
41 | |
public String[] getHeaders() |
42 | |
{ |
43 | 0 | String[] column = new String[42]; |
44 | 0 | column[0] = "Service Name"; |
45 | 0 | column[1] = "Service Pool Max Size"; |
46 | 0 | column[2] = "Service Pool Size"; |
47 | 0 | column[3] = "Thread Pool Size"; |
48 | 0 | column[4] = "Current Queue Size"; |
49 | 0 | column[5] = "Max Queue Size"; |
50 | 0 | column[6] = "Avg Queue Size"; |
51 | 0 | column[7] = "Sync Events Received"; |
52 | 0 | column[8] = "Async Events Received"; |
53 | 0 | column[9] = "Total Events Received"; |
54 | 0 | column[10] = "Sync Events Sent"; |
55 | 0 | column[11] = "Async Events Sent"; |
56 | 0 | column[12] = "ReplyTo Events Sent"; |
57 | 0 | column[13] = "Total Events Sent"; |
58 | 0 | column[14] = "Executed Events"; |
59 | 0 | column[15] = "Execution Messages"; |
60 | 0 | column[16] = "Fatal Messages"; |
61 | 0 | column[17] = "Min Execution Time"; |
62 | 0 | column[18] = "Max Execution Time"; |
63 | 0 | column[19] = "Avg Execution Time"; |
64 | 0 | column[20] = "Total Execution Time"; |
65 | 0 | column[21] = "Router"; |
66 | 0 | column[22] = "Type"; |
67 | 0 | column[23] = "Total Received"; |
68 | 0 | column[24] = "Total Routed"; |
69 | 0 | column[25] = "Not Routed"; |
70 | 0 | column[26] = "Caught Events"; |
71 | 0 | column[27] = "Providers"; |
72 | 0 | column[28] = ""; |
73 | 0 | column[29] = "Providers"; |
74 | 0 | column[30] = "Router"; |
75 | 0 | column[31] = "Router"; |
76 | 0 | column[32] = "Type"; |
77 | 0 | column[33] = "Total Received"; |
78 | 0 | column[34] = "Total Routed"; |
79 | 0 | column[35] = "Not Routed"; |
80 | 0 | column[36] = "Caught Events"; |
81 | 0 | column[37] = "Providers"; |
82 | 0 | column[38] = ""; |
83 | 0 | column[39] = "Providers"; |
84 | 0 | column[40] = "Router"; |
85 | 0 | column[41] = "Sample Period"; |
86 | 0 | return column; |
87 | |
} |
88 | |
|
89 | |
protected int getRouterInfo(RouterStatistics stats, String[] col, int index) |
90 | |
{ |
91 | 0 | index++; |
92 | 0 | if (stats.isInbound()) |
93 | |
{ |
94 | 0 | col[index++] = "Inbound"; |
95 | |
} |
96 | |
else |
97 | |
{ |
98 | 0 | col[index++] = "Outbound"; |
99 | |
} |
100 | |
|
101 | 0 | col[index++] = String.valueOf(stats.getTotalReceived()); |
102 | 0 | col[index++] = String.valueOf(stats.getTotalRouted()); |
103 | 0 | col[index++] = String.valueOf(stats.getNotRouted()); |
104 | 0 | col[index++] = String.valueOf(stats.getCaughtMessages()); |
105 | |
|
106 | 0 | index++; |
107 | 0 | Map routed = stats.getRouted(); |
108 | 0 | if (!routed.isEmpty()) |
109 | |
{ |
110 | 0 | Iterator it = routed.entrySet().iterator(); |
111 | |
|
112 | 0 | StringBuffer buf = new StringBuffer(40); |
113 | 0 | while (it.hasNext()) |
114 | |
{ |
115 | 0 | Map.Entry e = (Map.Entry) it.next(); |
116 | 0 | buf.append(e.getKey()).append('=').append(e.getValue()); |
117 | 0 | if (it.hasNext()) |
118 | |
{ |
119 | 0 | buf.append(';'); |
120 | |
} |
121 | 0 | } |
122 | 0 | col[index++] = buf.toString(); |
123 | 0 | } |
124 | |
else |
125 | |
{ |
126 | 0 | col[index++] = ""; |
127 | |
} |
128 | 0 | index += 2; |
129 | |
|
130 | 0 | return index; |
131 | |
} |
132 | |
|
133 | |
public void print(Collection stats) |
134 | |
{ |
135 | 0 | println("<?xml version=\"1.0\" encoding=\"US-ASCII\"?>"); |
136 | 0 | println("<Components>"); |
137 | 0 | String[][] table = getTable(stats); |
138 | 0 | boolean router = false; |
139 | 0 | boolean providers = false; |
140 | |
|
141 | 0 | int indentLevel = 1; |
142 | |
|
143 | 0 | for (int i = 1; i < table.length; i++) |
144 | |
{ |
145 | 0 | println("<Service name=\"" + table[i][0] + "\">", indentLevel); |
146 | 0 | indentLevel++; |
147 | 0 | for (int j = 1; j < table[i].length; j++) |
148 | |
{ |
149 | 0 | if (StringUtils.equals(table[0][j], "Router")) |
150 | |
{ |
151 | 0 | if (!router) |
152 | |
{ |
153 | 0 | println("<Router type=\"" + table[i][++j] + "\">", indentLevel); |
154 | 0 | indentLevel++; |
155 | 0 | router = true; |
156 | |
} |
157 | |
else |
158 | |
{ |
159 | 0 | indentLevel--; |
160 | 0 | println("</Router>", indentLevel); |
161 | 0 | router = false; |
162 | |
} |
163 | |
} |
164 | 0 | else if (StringUtils.equals(table[0][j], "Providers")) |
165 | |
{ |
166 | 0 | if (StringUtils.isEmpty(table[i][j + 1]) && StringUtils.equals(table[0][j + 2], "Providers")) |
167 | |
{ |
168 | 0 | println("<Providers/>", indentLevel); |
169 | 0 | j += 2; |
170 | |
} |
171 | |
else |
172 | |
{ |
173 | 0 | if (!providers) |
174 | |
{ |
175 | 0 | println("<Providers>", indentLevel); |
176 | 0 | indentLevel++; |
177 | 0 | providers = true; |
178 | |
} |
179 | |
else |
180 | |
{ |
181 | 0 | indentLevel--; |
182 | 0 | println("</Providers>", indentLevel); |
183 | 0 | providers = false; |
184 | |
} |
185 | |
} |
186 | |
} |
187 | |
else |
188 | |
{ |
189 | 0 | if (providers) |
190 | |
{ |
191 | 0 | printProviderStatsXml(table[i][j], indentLevel); |
192 | |
} |
193 | |
else |
194 | |
{ |
195 | 0 | println("<Statistic name=\"" + table[0][j] + "\" value=\"" + table[i][j] + "\"/>", |
196 | |
indentLevel); |
197 | |
} |
198 | |
} |
199 | |
} |
200 | 0 | indentLevel--; |
201 | 0 | println("</Service>", indentLevel); |
202 | |
} |
203 | 0 | indentLevel--; |
204 | 0 | println("</Components>", indentLevel); |
205 | 0 | } |
206 | |
|
207 | |
public void println(String s, int indentLevel) |
208 | |
{ |
209 | 0 | final String indent = StringUtils.repeat(' ', indentLevel * XML_INDENT_SIZE); |
210 | 0 | println(indent + s); |
211 | 0 | } |
212 | |
|
213 | |
protected void printProviderStatsXml(String stats, int indentLevel) |
214 | |
{ |
215 | 0 | if (StringUtils.isBlank(stats) || "-".equals(stats)) |
216 | |
{ |
217 | 0 | return; |
218 | |
} |
219 | |
|
220 | 0 | StringTokenizer st = new StringTokenizer(stats, ";"); |
221 | |
|
222 | 0 | if (st.countTokens() == 0) |
223 | |
{ |
224 | 0 | StringBuffer buf = new StringBuffer(); |
225 | 0 | buf.append("<Provider name=\""); |
226 | 0 | int i = stats.indexOf("="); |
227 | 0 | buf.append(stats.substring(0, i)); |
228 | 0 | buf.append("\" value=\""); |
229 | 0 | buf.append(stats.substring(i + 1)); |
230 | 0 | buf.append("\"/>"); |
231 | 0 | println(buf.toString(), indentLevel); |
232 | 0 | } |
233 | |
else |
234 | |
{ |
235 | |
String token; |
236 | 0 | while (st.hasMoreTokens()) |
237 | |
{ |
238 | 0 | StringBuffer buf = new StringBuffer(); |
239 | 0 | token = st.nextToken(); |
240 | 0 | buf.append("<Provider name=\""); |
241 | 0 | int i = token.indexOf("="); |
242 | 0 | buf.append(token.substring(0, i)); |
243 | 0 | buf.append("\" value=\""); |
244 | 0 | buf.append(token.substring(i + 1)); |
245 | 0 | buf.append("\"/>"); |
246 | 0 | println(buf.toString(), indentLevel); |
247 | 0 | } |
248 | |
} |
249 | 0 | } |
250 | |
} |