View Javadoc

1   /*
2    * $Id: XMLPrinter.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.RouterStatistics;
14  import org.mule.util.StringUtils;
15  
16  import java.io.OutputStream;
17  import java.io.Writer;
18  import java.util.Collection;
19  import java.util.Iterator;
20  import java.util.Map;
21  import java.util.StringTokenizer;
22  
23  
24  /**
25   * <code>XMLPrinter</code> prints event processing stats as a XML document
26   * 
27   */
28  public class XMLPrinter extends AbstractTablePrinter
29  {
30      /**
31       * Indentation step for XML pretty-printing.
32       */
33      protected static final int XML_INDENT_SIZE = 2;
34  
35      public XMLPrinter(Writer out)
36      {
37          super(out);
38      }
39  
40      public XMLPrinter(OutputStream out)
41      {
42          super(out);
43      }
44  
45      public String[] getHeaders()
46      {
47          String[] column = new String[42];
48          column[0] = "Service Name";
49          column[1] = "Service Pool Max Size";
50          column[2] = "Service Pool Size";
51          column[3] = "Thread Pool Size";
52          column[4] = "Current Queue Size";
53          column[5] = "Max Queue Size";
54          column[6] = "Avg Queue Size";
55          column[7] = "Sync Events Received";
56          column[8] = "Async Events Received";
57          column[9] = "Total Events Received";
58          column[10] = "Sync Events Sent";
59          column[11] = "Async Events Sent";
60          column[12] = "ReplyTo Events Sent";
61          column[13] = "Total Events Sent";
62          column[14] = "Executed Events";
63          column[15] = "Execution Messages";
64          column[16] = "Fatal Messages";
65          column[17] = "Min Execution Time";
66          column[18] = "Max Execution Time";
67          column[19] = "Avg Execution Time";
68          column[20] = "Total Execution Time";
69          column[21] = "Router";
70          column[22] = "Type";
71          column[23] = "Total Received";
72          column[24] = "Total Routed";
73          column[25] = "Not Routed";
74          column[26] = "Caught Events";
75          column[27] = "Providers";
76          column[28] = "";
77          column[29] = "Providers";
78          column[30] = "Router";
79          column[31] = "Router";
80          column[32] = "Type";
81          column[33] = "Total Received";
82          column[34] = "Total Routed";
83          column[35] = "Not Routed";
84          column[36] = "Caught Events";
85          column[37] = "Providers";
86          column[38] = "";
87          column[39] = "Providers";
88          column[40] = "Router";
89          column[41] = "Sample Period";
90          return column;
91      }
92      
93      protected int getRouterInfo(RouterStatistics stats, String[] col, int index)
94      {
95          index++;
96          if (stats.isInbound())
97          {
98              col[index++] = "Inbound";
99          }
100         else
101         {
102             col[index++] = "Outbound";
103         }
104 
105         col[index++] = String.valueOf(stats.getTotalReceived());
106         col[index++] = String.valueOf(stats.getTotalRouted());
107         col[index++] = String.valueOf(stats.getNotRouted());
108         col[index++] = String.valueOf(stats.getCaughtMessages());
109 
110         index++;
111         Map routed = stats.getRouted();
112         if (!routed.isEmpty())
113         {
114             Iterator it = routed.entrySet().iterator();
115 
116             StringBuffer buf = new StringBuffer(40);
117             while (it.hasNext())
118             {
119                 Map.Entry e = (Map.Entry) it.next();
120                 buf.append(e.getKey()).append('=').append(e.getValue());
121                 if (it.hasNext())
122                 {
123                     buf.append(';');
124                 }
125             }
126             col[index++] = buf.toString();
127         }
128         else
129         {
130             col[index++] = "";
131         }
132         index += 2;
133 
134         return index;
135     }
136 
137     public void print(Collection stats)
138     {
139         println("<?xml version=\"1.0\" encoding=\"US-ASCII\"?>");
140         println("<Components>");
141         String[][] table = getTable(stats);
142         boolean router = false;
143         boolean providers = false;
144 
145         int indentLevel = 1;
146 
147         for (int i = 1; i < table.length; i++)
148         {
149             println("<Service name=\"" + table[i][0] + "\">", indentLevel);
150             indentLevel++;
151             for (int j = 1; j < table[i].length; j++)
152             {
153                 if (StringUtils.equals(table[0][j], "Router"))
154                 {
155                     if (!router)
156                     {
157                         println("<Router type=\"" + table[i][++j] + "\">", indentLevel);
158                         indentLevel++;
159                         router = true;
160                     }
161                     else
162                     {
163                         indentLevel--;
164                         println("</Router>", indentLevel);
165                         router = false;
166                     }
167                 }
168                 else if (StringUtils.equals(table[0][j], "Providers"))
169                 {
170                     if (StringUtils.isEmpty(table[i][j + 1]) && StringUtils.equals(table[0][j + 2], "Providers"))
171                     {
172                         println("<Providers/>", indentLevel);
173                         j += 2;
174                     }
175                     else
176                     {
177                         if (!providers)
178                         {
179                             println("<Providers>", indentLevel);
180                             indentLevel++;
181                             providers = true;
182                         }
183                         else
184                         {
185                             indentLevel--;
186                             println("</Providers>", indentLevel);
187                             providers = false;
188                         }
189                     }
190                 }
191                 else
192                 {
193                     if (providers)
194                     {
195                         printProviderStatsXml(table[i][j], indentLevel);
196                     }
197                     else
198                     {
199                         println("<Statistic name=\"" + table[0][j] + "\" value=\"" + table[i][j] + "\"/>",
200                                 indentLevel);
201                     }
202                 }
203             }
204             indentLevel--;
205             println("</Service>", indentLevel);
206         }
207         indentLevel--;
208         println("</Components>", indentLevel);
209     }
210 
211     public void println(String s, int indentLevel)
212     {
213         final String indent = StringUtils.repeat(' ', indentLevel * XML_INDENT_SIZE);
214         println(indent + s);
215     }
216 
217     protected void printProviderStatsXml(String stats, int indentLevel)
218     {
219         if (StringUtils.isBlank(stats))
220         {
221             return;
222         }
223 
224         StringTokenizer st = new StringTokenizer(stats, ";");
225 
226         if (st.countTokens() == 0)
227         {
228             StringBuffer buf = new StringBuffer();
229             buf.append("<Provider name=\"");
230             int i = stats.indexOf("=");
231             buf.append(stats.substring(0, i));
232             buf.append("\" value=\"");
233             buf.append(stats.substring(i + 1));
234             buf.append("\"/>");
235             println(buf.toString(), indentLevel);
236         }
237         else
238         {
239             String token;
240             while (st.hasMoreTokens())
241             {
242                 StringBuffer buf = new StringBuffer();
243                 token = st.nextToken();
244                 buf.append("<Provider name=\"");
245                 int i = token.indexOf("=");
246                 buf.append(token.substring(0, i));
247                 buf.append("\" value=\"");
248                 buf.append(token.substring(i + 1));
249                 buf.append("\"/>");
250                 println(buf.toString(), indentLevel);
251             }
252         }
253     }
254 }