View Javadoc

1   /*
2    * $Id: AbstractTablePrinter.java 20288 2010-11-22 01:19:54Z mike.schilling $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.management.stats.printers;
12  
13  import org.mule.management.stats.FlowConstructStatistics;
14  import org.mule.management.stats.ServiceStatistics;
15  import org.mule.management.stats.RouterStatistics;
16  import org.mule.management.stats.SedaServiceStatistics;
17  
18  import java.io.OutputStream;
19  import java.io.PrintWriter;
20  import java.io.Writer;
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.Collection;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Map;
27  
28  /**
29   * <code>HtmlTablePrinter</code> prints event processing stats as a HTML table
30   */
31  public class AbstractTablePrinter extends PrintWriter
32  {
33      public AbstractTablePrinter(Writer out)
34      {
35          super(out, true);
36      }
37  
38      public AbstractTablePrinter(OutputStream out)
39      {
40          super(out, true);
41      }
42  
43      public String[] getHeaders()
44      {
45          String[] column = new String[41];
46          column[0] = "Name";
47          column[1] = "Service Pool Max Size";
48          column[2] = "Service Pool Size";
49          column[3] = "Thread Pool Size";
50          column[4] = "Current Queue Size";
51          column[5] = "Max Queue Size";
52          column[6] = "Avg Queue Size";
53          column[7] = "Sync Events Received";
54          column[8] = "Async Events Received";
55          column[9] = "Total Events Received";
56          column[10] = "Sync Events Sent";
57          column[11] = "Async Events Sent";
58          column[12] = "ReplyTo Events Sent";
59          column[13] = "Total Events Sent";
60          column[14] = "Executed Events";
61          column[15] = "Execution Messages";
62          column[16] = "Fatal Messages";
63          column[17] = "Min Execution Time";
64          column[18] = "Max Execution Time";
65          column[19] = "Avg Execution Time";
66          column[20] = "Total Execution Time";
67          column[21] = "Processed Events";
68          column[22] = "Min Processing Time";
69          column[23] = "Max Processing Time";
70          column[24] = "Avg Processing Time";
71          column[25] = "Total Processing Time";
72          column[26] = "In Router Statistics";
73          column[27] = "Total Received";
74          column[28] = "Total Routed";
75          column[29] = "Not Routed";
76          column[30] = "Caught Events";
77          column[31] = "By Provider";
78          column[32] = "";
79          column[33] = "Out Router Statistics";
80          column[34] = "Total Received";
81          column[35] = "Total Routed";
82          column[36] = "Not Routed";
83          column[37] = "Caught Events";
84          column[38] = "By Provider";
85          column[39] = "";
86          column[40] = "Sample Period";
87          return column;
88      }
89  
90      protected void getColumn(FlowConstructStatistics stats, String[] col)
91      {
92          if (stats == null)
93          {
94              return;
95          }
96          ServiceStatistics serviceStats = (stats instanceof ServiceStatistics) ? (ServiceStatistics) stats : null;
97  
98          Arrays.fill(col, "-");
99  
100         col[0] = stats.getName();
101 
102         //TODO RM* Handling custom stats objects
103         if (stats instanceof SedaServiceStatistics)
104         {
105             col[1] = ((SedaServiceStatistics) stats).getComponentPoolMaxSize() + "/"
106                     + ((SedaServiceStatistics) stats).getComponentPoolAbsoluteMaxSize();
107             col[2] = String.valueOf(((SedaServiceStatistics) stats).getComponentPoolSize());
108         }
109         else
110         {
111             col[1] = "-";
112             col[2] = "-";
113         }
114         col[3] = String.valueOf(stats.getThreadPoolSize());
115         if (serviceStats != null)
116         {
117             col[4] = String.valueOf(serviceStats.getQueuedEvents());
118             col[5] = String.valueOf(serviceStats.getMaxQueueSize());
119             col[6] = String.valueOf(serviceStats.getAverageQueueSize());
120         }
121         col[7] = String.valueOf(stats.getSyncEventsReceived());
122         col[8] = String.valueOf(stats.getAsyncEventsReceived());
123         col[9] = String.valueOf(stats.getTotalEventsReceived());
124         if (serviceStats != null)
125         {
126             col[10] = String.valueOf(serviceStats.getSyncEventsSent());
127             col[11] = String.valueOf(serviceStats.getAsyncEventsSent());
128             col[12] = String.valueOf(serviceStats.getReplyToEventsSent());
129             col[13] = String.valueOf(serviceStats.getTotalEventsSent());
130         }
131 
132         if (serviceStats != null)
133         {
134             col[14] = String.valueOf(serviceStats.getExecutedEvents());
135         }
136         col[15] = String.valueOf(stats.getExecutionErrors());
137         col[16] = String.valueOf(stats.getFatalErrors());
138         if (serviceStats != null)
139         {
140             col[17] = String.valueOf(serviceStats.getMinExecutionTime());
141             col[18] = String.valueOf(serviceStats.getMaxExecutionTime());
142             col[19] = String.valueOf(serviceStats.getAverageExecutionTime());
143             col[20] = String.valueOf(serviceStats.getTotalExecutionTime());
144         }
145 
146         col[21] = String.valueOf(stats.getProcessedEvents());
147         col[22] = String.valueOf(stats.getMinProcessingTime());
148         col[23] = String.valueOf(stats.getMaxProcessingTime());
149         col[24] = String.valueOf(stats.getAverageProcessingTime());
150         col[25] = String.valueOf(stats.getTotalProcessingTime());
151 
152         if (serviceStats != null)
153         {
154             int i = getRouterInfo(serviceStats.getInboundRouterStat(), col, 26);
155             i = getRouterInfo(serviceStats.getOutboundRouterStat(), col, i);
156         }
157 
158         col[40] = String.valueOf(stats.getSamplePeriod());
159     }
160 
161     protected int getRouterInfo(RouterStatistics stats, String[] col, int index)
162     {
163         // TODO what's the deal with the +/- signs?
164         if (stats.isInbound())
165         {
166             col[index++] = "-";
167         }
168         else
169         {
170             col[index++] = "-";
171         }
172 
173         col[index++] = String.valueOf(stats.getTotalReceived());
174         col[index++] = String.valueOf(stats.getTotalRouted());
175         col[index++] = String.valueOf(stats.getNotRouted());
176         col[index++] = String.valueOf(stats.getCaughtMessages());
177 
178         Map routed = stats.getRouted();
179 
180         col[index++] = "-";
181         if (!routed.isEmpty())
182         {
183             Iterator it = routed.entrySet().iterator();
184 
185             StringBuffer buf = new StringBuffer(40);
186             while (it.hasNext())
187             {
188                 Map.Entry e = (Map.Entry) it.next();
189                 buf.append(e.getKey()).append('=').append(e.getValue());
190                 if (it.hasNext())
191                 {
192                     buf.append(';');
193                 }
194             }
195             col[index++] = buf.toString();
196         }
197         else
198         {
199             col[index++] = "";
200         }
201         return index;
202     }
203 
204     protected String[][] getTable(Collection stats)
205     {
206         String[] cols = getHeaders();
207         String[][] table = new String[stats.size() + 1][cols.length];
208         for (int i = 0; i < cols.length; i++)
209         {
210             table[0][i] = cols[i];
211         }
212 
213         int i = 1;
214         for (Iterator iterator = stats.iterator(); iterator.hasNext(); i++)
215         {
216             getColumn((FlowConstructStatistics) iterator.next(), table[i]);
217         }
218 
219         return table;
220     }
221 
222     public void print(Object obj)
223     {
224         if (obj instanceof Collection)
225         {
226             print((Collection) obj);
227         }
228         else if (obj instanceof ServiceStatistics)
229         {
230             List l = new ArrayList();
231             l.add(obj);
232             print(l);
233         }
234         else
235         {
236             super.print(obj);
237         }
238     }
239 
240     public void println(Object obj)
241     {
242         print(obj);
243         println();
244     }
245 
246     public void print(Collection c)
247     {
248         throw new UnsupportedOperationException();
249     }
250 
251     // help IBM compiler, it complains helplessly about
252     // an abmiguously overloaded/overridden method.
253     public void println(String string)
254     {
255         this.println((Object) string);
256     }
257 
258     // help IBM compiler, it complains helplessly about
259     // an abmiguously overloaded/overridden method.
260     public void print(String string)
261     {
262         this.print((Object) string);
263     }
264 
265 }