View Javadoc

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