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