View Javadoc

1   /*
2    * $Id: AbstractTablePrinter.java 10529 2008-01-25 05:58:36Z dfeist $
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.ServiceStatistics;
14  import org.mule.management.stats.RouterStatistics;
15  import org.mule.management.stats.SedaServiceStatistics;
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] = "Service Name";
46          column[1] = "Service Pool Max Size";
47          column[2] = "Service 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(ServiceStatistics stats, String[] col)
85      {
86          if (stats == null)
87          {
88              return;
89          }
90  
91  
92          col[0] = stats.getName();
93  
94          //TODO RM* Handling custom stats objects
95          if (stats instanceof SedaServiceStatistics)
96          {
97              col[1] = ((SedaServiceStatistics) stats).getComponentPoolMaxSize() + "/"
98                      + ((SedaServiceStatistics) stats).getComponentPoolAbsoluteMaxSize();
99              col[2] = String.valueOf(((SedaServiceStatistics) stats).getComponentPoolSize());
100         }
101         else
102         {
103             col[1] = "-";
104             col[2] = "-";
105         }
106         col[3] = String.valueOf(stats.getThreadPoolSize());
107         col[4] = String.valueOf(stats.getQueuedEvents());
108         col[5] = String.valueOf(stats.getMaxQueueSize());
109         col[6] = String.valueOf(stats.getAverageQueueSize());
110         col[7] = String.valueOf(stats.getSyncEventsReceived());
111         col[8] = String.valueOf(stats.getAsyncEventsReceived());
112         col[9] = String.valueOf(stats.getTotalEventsReceived());
113         col[10] = String.valueOf(stats.getSyncEventsSent());
114         col[11] = String.valueOf(stats.getAsyncEventsSent());
115         col[12] = String.valueOf(stats.getReplyToEventsSent());
116         col[13] = String.valueOf(stats.getTotalEventsSent());
117         col[14] = String.valueOf(stats.getExecutedEvents());
118         col[15] = String.valueOf(stats.getExecutionErrors());
119         col[16] = String.valueOf(stats.getFatalErrors());
120         col[17] = String.valueOf(stats.getMinExecutionTime());
121         col[18] = String.valueOf(stats.getMaxExecutionTime());
122         col[19] = String.valueOf(stats.getAverageExecutionTime());
123         col[20] = String.valueOf(stats.getTotalExecutionTime());
124 
125         int i = getRouterInfo(stats.getInboundRouterStat(), col, 21);
126         i = getRouterInfo(stats.getOutboundRouterStat(), col, i);
127         col[i] = String.valueOf(stats.getSamplePeriod());
128     }
129 
130     protected int getRouterInfo(RouterStatistics stats, String[] col, int index)
131     {
132         // TODO what's the deal with the +/- signs?
133         if (stats.isInbound())
134         {
135             col[index++] = "-";
136         }
137         else
138         {
139             col[index++] = "-";
140         }
141 
142         col[index++] = String.valueOf(stats.getTotalReceived());
143         col[index++] = String.valueOf(stats.getTotalRouted());
144         col[index++] = String.valueOf(stats.getNotRouted());
145         col[index++] = String.valueOf(stats.getCaughtMessages());
146 
147         Map routed = stats.getRouted();
148 
149         col[index++] = "-";
150         if (!routed.isEmpty())
151         {
152             Iterator it = routed.entrySet().iterator();
153 
154             StringBuffer buf = new StringBuffer(40);
155             while (it.hasNext())
156             {
157                 Map.Entry e = (Map.Entry) it.next();
158                 buf.append(e.getKey()).append('=').append(e.getValue());
159                 if (it.hasNext())
160                 {
161                     buf.append(';');
162                 }
163             }
164             col[index++] = buf.toString();
165         }
166         else
167         {
168             col[index++] = "";
169         }
170         return index;
171     }
172 
173     protected String[][] getTable(Collection stats)
174     {
175         String[] cols = getHeaders();
176         String[][] table = new String[stats.size() + 1][cols.length];
177         for (int i = 0; i < cols.length; i++)
178         {
179             table[0][i] = cols[i];
180         }
181 
182         int i = 1;
183         for (Iterator iterator = stats.iterator(); iterator.hasNext(); i++)
184         {
185             getColumn((ServiceStatistics) iterator.next(), table[i]);
186         }
187 
188         return table;
189     }
190 
191     public void print(Object obj)
192     {
193         if (obj instanceof Collection)
194         {
195             print((Collection) obj);
196         }
197         else if (obj instanceof ServiceStatistics)
198         {
199             List l = new ArrayList();
200             l.add(obj);
201             print(l);
202         }
203         else
204         {
205             super.print(obj);
206         }
207     }
208 
209     public void println(Object obj)
210     {
211         print(obj);
212         println();
213     }
214 
215     public void print(Collection c)
216     {
217         throw new UnsupportedOperationException();
218     }
219 
220     // help IBM compiler, it complains helplessly about
221     // an abmiguously overloaded/overridden method.
222     public void println(String string)
223     {
224         this.println((Object) string);
225     }
226 
227     // help IBM compiler, it complains helplessly about
228     // an abmiguously overloaded/overridden method.
229     public void print(String string)
230     {
231         this.print((Object) string);
232     }
233 
234 }