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