001: /*
002: * $Id: AbstractTablePrinter.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.ServiceStatistics;
014: import org.mule.management.stats.RouterStatistics;
015: import org.mule.management.stats.SedaServiceStatistics;
016:
017: import java.io.OutputStream;
018: import java.io.PrintWriter;
019: import java.io.Writer;
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025:
026: /**
027: * <code>HtmlTablePrinter</code> prints event processing stats as a HTML table
028: */
029: public class AbstractTablePrinter extends PrintWriter {
030:
031: public AbstractTablePrinter(Writer out) {
032: super (out, true);
033: }
034:
035: public AbstractTablePrinter(OutputStream out) {
036: super (out, true);
037: }
038:
039: public String[] getHeaders() {
040: String[] column = new String[36];
041: column[0] = "Service Name";
042: column[1] = "Service Pool Max Size";
043: column[2] = "Service Pool Size";
044: column[3] = "Thread Pool Size";
045: column[4] = "Current Queue Size";
046: column[5] = "Max Queue Size";
047: column[6] = "Avg Queue Size";
048: column[7] = "Sync Events Received";
049: column[8] = "Async Events Received";
050: column[9] = "Total Events Received";
051: column[10] = "Sync Events Sent";
052: column[11] = "Async Events Sent";
053: column[12] = "ReplyTo Events Sent";
054: column[13] = "Total Events Sent";
055: column[14] = "Executed Events";
056: column[15] = "Execution Messages";
057: column[16] = "Fatal Messages";
058: column[17] = "Min Execution Time";
059: column[18] = "Max Execution Time";
060: column[19] = "Avg Execution Time";
061: column[20] = "Total Execution Time";
062: column[21] = "In Router Statistics";
063: column[22] = "Total Received";
064: column[23] = "Total Routed";
065: column[24] = "Not Routed";
066: column[25] = "Caught Events";
067: column[26] = "By Provider";
068: column[27] = "";
069: column[28] = "Out Router Statistics";
070: column[29] = "Total Received";
071: column[30] = "Total Routed";
072: column[31] = "Not Routed";
073: column[32] = "Caught Events";
074: column[33] = "By Provider";
075: column[34] = "";
076: column[35] = "Sample Period";
077: return column;
078: }
079:
080: protected void getColumn(ServiceStatistics stats, String[] col) {
081: if (stats == null) {
082: return;
083: }
084:
085: col[0] = stats.getName();
086:
087: //TODO RM* Handling custom stats objects
088: if (stats instanceof SedaServiceStatistics) {
089: col[1] = ((SedaServiceStatistics) stats)
090: .getComponentPoolMaxSize()
091: + "/"
092: + ((SedaServiceStatistics) stats)
093: .getComponentPoolAbsoluteMaxSize();
094: col[2] = String.valueOf(((SedaServiceStatistics) stats)
095: .getComponentPoolSize());
096: } else {
097: col[1] = "-";
098: col[2] = "-";
099: }
100: col[3] = String.valueOf(stats.getThreadPoolSize());
101: col[4] = String.valueOf(stats.getQueuedEvents());
102: col[5] = String.valueOf(stats.getMaxQueueSize());
103: col[6] = String.valueOf(stats.getAverageQueueSize());
104: col[7] = String.valueOf(stats.getSyncEventsReceived());
105: col[8] = String.valueOf(stats.getAsyncEventsReceived());
106: col[9] = String.valueOf(stats.getTotalEventsReceived());
107: col[10] = String.valueOf(stats.getSyncEventsSent());
108: col[11] = String.valueOf(stats.getAsyncEventsSent());
109: col[12] = String.valueOf(stats.getReplyToEventsSent());
110: col[13] = String.valueOf(stats.getTotalEventsSent());
111: col[14] = String.valueOf(stats.getExecutedEvents());
112: col[15] = String.valueOf(stats.getExecutionErrors());
113: col[16] = String.valueOf(stats.getFatalErrors());
114: col[17] = String.valueOf(stats.getMinExecutionTime());
115: col[18] = String.valueOf(stats.getMaxExecutionTime());
116: col[19] = String.valueOf(stats.getAverageExecutionTime());
117: col[20] = String.valueOf(stats.getTotalExecutionTime());
118:
119: int i = getRouterInfo(stats.getInboundRouterStat(), col, 21);
120: i = getRouterInfo(stats.getOutboundRouterStat(), col, i);
121: col[i] = String.valueOf(stats.getSamplePeriod());
122: }
123:
124: protected int getRouterInfo(RouterStatistics stats, String[] col,
125: int index) {
126: // TODO what's the deal with the +/- signs?
127: if (stats.isInbound()) {
128: col[index++] = "-";
129: } else {
130: col[index++] = "-";
131: }
132:
133: col[index++] = String.valueOf(stats.getTotalReceived());
134: col[index++] = String.valueOf(stats.getTotalRouted());
135: col[index++] = String.valueOf(stats.getNotRouted());
136: col[index++] = String.valueOf(stats.getCaughtMessages());
137:
138: Map routed = stats.getRouted();
139:
140: col[index++] = "-";
141: if (!routed.isEmpty()) {
142: Iterator it = routed.entrySet().iterator();
143:
144: StringBuffer buf = new StringBuffer(40);
145: while (it.hasNext()) {
146: Map.Entry e = (Map.Entry) it.next();
147: buf.append(e.getKey()).append('=').append(e.getValue());
148: if (it.hasNext()) {
149: buf.append(';');
150: }
151: }
152: col[index++] = buf.toString();
153: } else {
154: col[index++] = "";
155: }
156: return index;
157: }
158:
159: protected String[][] getTable(Collection stats) {
160: String[] cols = getHeaders();
161: String[][] table = new String[stats.size() + 1][cols.length];
162: for (int i = 0; i < cols.length; i++) {
163: table[0][i] = cols[i];
164: }
165:
166: int i = 1;
167: for (Iterator iterator = stats.iterator(); iterator.hasNext(); i++) {
168: getColumn((ServiceStatistics) iterator.next(), table[i]);
169: }
170:
171: return table;
172: }
173:
174: public void print(Object obj) {
175: if (obj instanceof Collection) {
176: print((Collection) obj);
177: } else if (obj instanceof ServiceStatistics) {
178: List l = new ArrayList();
179: l.add(obj);
180: print(l);
181: } else {
182: super .print(obj);
183: }
184: }
185:
186: public void println(Object obj) {
187: print(obj);
188: println();
189: }
190:
191: public void print(Collection c) {
192: throw new UnsupportedOperationException();
193: }
194:
195: // help IBM compiler, it complains helplessly about
196: // an abmiguously overloaded/overridden method.
197: public void println(String string) {
198: this .println((Object) string);
199: }
200:
201: // help IBM compiler, it complains helplessly about
202: // an abmiguously overloaded/overridden method.
203: public void print(String string) {
204: this .print((Object) string);
205: }
206:
207: }
|