001: /*
002: * $Id: XMLPrinter.java 10529 2008-01-25 05:58:36Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.management.stats.printers;
012:
013: import org.mule.management.stats.RouterStatistics;
014: import org.mule.util.StringUtils;
015:
016: import java.io.OutputStream;
017: import java.io.Writer;
018: import java.util.Collection;
019: import java.util.Iterator;
020: import java.util.Map;
021: import java.util.StringTokenizer;
022:
023: /**
024: * <code>XMLPrinter</code> prints event processing stats as a XML document
025: *
026: */
027: public class XMLPrinter extends AbstractTablePrinter {
028: /**
029: * Indentation step for XML pretty-printing.
030: */
031: protected static final int XML_INDENT_SIZE = 2;
032:
033: public XMLPrinter(Writer out) {
034: super (out);
035: }
036:
037: public XMLPrinter(OutputStream out) {
038: super (out);
039: }
040:
041: public String[] getHeaders() {
042: String[] column = new String[42];
043: column[0] = "Service Name";
044: column[1] = "Service Pool Max Size";
045: column[2] = "Service Pool Size";
046: column[3] = "Thread Pool Size";
047: column[4] = "Current Queue Size";
048: column[5] = "Max Queue Size";
049: column[6] = "Avg Queue Size";
050: column[7] = "Sync Events Received";
051: column[8] = "Async Events Received";
052: column[9] = "Total Events Received";
053: column[10] = "Sync Events Sent";
054: column[11] = "Async Events Sent";
055: column[12] = "ReplyTo Events Sent";
056: column[13] = "Total Events Sent";
057: column[14] = "Executed Events";
058: column[15] = "Execution Messages";
059: column[16] = "Fatal Messages";
060: column[17] = "Min Execution Time";
061: column[18] = "Max Execution Time";
062: column[19] = "Avg Execution Time";
063: column[20] = "Total Execution Time";
064: column[21] = "Router";
065: column[22] = "Type";
066: column[23] = "Total Received";
067: column[24] = "Total Routed";
068: column[25] = "Not Routed";
069: column[26] = "Caught Events";
070: column[27] = "Providers";
071: column[28] = "";
072: column[29] = "Providers";
073: column[30] = "Router";
074: column[31] = "Router";
075: column[32] = "Type";
076: column[33] = "Total Received";
077: column[34] = "Total Routed";
078: column[35] = "Not Routed";
079: column[36] = "Caught Events";
080: column[37] = "Providers";
081: column[38] = "";
082: column[39] = "Providers";
083: column[40] = "Router";
084: column[41] = "Sample Period";
085: return column;
086: }
087:
088: protected int getRouterInfo(RouterStatistics stats, String[] col,
089: int index) {
090: index++;
091: if (stats.isInbound()) {
092: col[index++] = "Inbound";
093: } else {
094: col[index++] = "Outbound";
095: }
096:
097: col[index++] = String.valueOf(stats.getTotalReceived());
098: col[index++] = String.valueOf(stats.getTotalRouted());
099: col[index++] = String.valueOf(stats.getNotRouted());
100: col[index++] = String.valueOf(stats.getCaughtMessages());
101:
102: index++;
103: Map routed = stats.getRouted();
104: if (!routed.isEmpty()) {
105: Iterator it = routed.entrySet().iterator();
106:
107: StringBuffer buf = new StringBuffer(40);
108: while (it.hasNext()) {
109: Map.Entry e = (Map.Entry) it.next();
110: buf.append(e.getKey()).append('=').append(e.getValue());
111: if (it.hasNext()) {
112: buf.append(';');
113: }
114: }
115: col[index++] = buf.toString();
116: } else {
117: col[index++] = "";
118: }
119: index += 2;
120:
121: return index;
122: }
123:
124: public void print(Collection stats) {
125: println("<?xml version=\"1.0\" encoding=\"US-ASCII\"?>");
126: println("<Components>");
127: String[][] table = getTable(stats);
128: boolean router = false;
129: boolean providers = false;
130:
131: int indentLevel = 1;
132:
133: for (int i = 1; i < table.length; i++) {
134: println("<Service name=\"" + table[i][0] + "\">",
135: indentLevel);
136: indentLevel++;
137: for (int j = 1; j < table[i].length; j++) {
138: if (StringUtils.equals(table[0][j], "Router")) {
139: if (!router) {
140: println("<Router type=\"" + table[i][++j]
141: + "\">", indentLevel);
142: indentLevel++;
143: router = true;
144: } else {
145: indentLevel--;
146: println("</Router>", indentLevel);
147: router = false;
148: }
149: } else if (StringUtils.equals(table[0][j], "Providers")) {
150: if (StringUtils.isEmpty(table[i][j + 1])
151: && StringUtils.equals(table[0][j + 2],
152: "Providers")) {
153: println("<Providers/>", indentLevel);
154: j += 2;
155: } else {
156: if (!providers) {
157: println("<Providers>", indentLevel);
158: indentLevel++;
159: providers = true;
160: } else {
161: indentLevel--;
162: println("</Providers>", indentLevel);
163: providers = false;
164: }
165: }
166: } else {
167: if (providers) {
168: printProviderStatsXml(table[i][j], indentLevel);
169: } else {
170: println("<Statistic name=\"" + table[0][j]
171: + "\" value=\"" + table[i][j] + "\"/>",
172: indentLevel);
173: }
174: }
175: }
176: indentLevel--;
177: println("</Service>", indentLevel);
178: }
179: indentLevel--;
180: println("</Components>", indentLevel);
181: }
182:
183: public void println(String s, int indentLevel) {
184: final String indent = StringUtils.repeat(' ', indentLevel
185: * XML_INDENT_SIZE);
186: println(indent + s);
187: }
188:
189: protected void printProviderStatsXml(String stats, int indentLevel) {
190: if (StringUtils.isBlank(stats)) {
191: return;
192: }
193:
194: StringTokenizer st = new StringTokenizer(stats, ";");
195:
196: if (st.countTokens() == 0) {
197: StringBuffer buf = new StringBuffer();
198: buf.append("<Provider name=\"");
199: int i = stats.indexOf("=");
200: buf.append(stats.substring(0, i));
201: buf.append("\" value=\"");
202: buf.append(stats.substring(i + 1));
203: buf.append("\"/>");
204: println(buf.toString(), indentLevel);
205: } else {
206: String token;
207: while (st.hasMoreTokens()) {
208: StringBuffer buf = new StringBuffer();
209: token = st.nextToken();
210: buf.append("<Provider name=\"");
211: int i = token.indexOf("=");
212: buf.append(token.substring(0, i));
213: buf.append("\" value=\"");
214: buf.append(token.substring(i + 1));
215: buf.append("\"/>");
216: println(buf.toString(), indentLevel);
217: }
218: }
219: }
220: }
|