View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.module.management.mbean;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.management.stats.AllStatistics;
11  import org.mule.management.stats.FlowConstructStatistics;
12  import org.mule.management.stats.printers.CSVPrinter;
13  import org.mule.management.stats.printers.HtmlTablePrinter;
14  import org.mule.management.stats.printers.XMLPrinter;
15  
16  import java.io.StringWriter;
17  import java.util.Collection;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  
22  /**
23   * <code>StatisicsService</code> exposes Mule processing statistics
24   */
25  public class StatisticsService implements StatisticsServiceMBean
26  {
27      /**
28       * Serial version
29       */
30      private static final long serialVersionUID = -4949499389883146363L;
31  
32      /**
33       * logger used by this class
34       */
35      protected static final Log logger = LogFactory.getLog(StatisticsService.class);
36  
37      private AllStatistics stats = new AllStatistics();
38      private MuleContext muleContext;
39  
40      public void setMuleContext(MuleContext context)
41      {
42          this.muleContext = context;
43          if (muleContext == null)
44          {
45              stats = new AllStatistics();
46          }
47          else
48          {
49              stats = this.muleContext.getStatistics();
50          }
51  
52      }
53  
54      public void clear()
55      {
56          stats.clear();
57      }
58  
59      public boolean isEnabled()
60      {
61          return stats.isEnabled();
62      }
63  
64      public void setEnabled(boolean b)
65      {
66          stats.setEnabled(b);
67  
68      }
69  
70      /**
71       * @deprecated use #getServiceStatistics
72       */
73      @Deprecated
74      public Collection<?> getComponentStatistics()
75      {
76          return stats.getServiceStatistics();
77      }
78  
79      public Collection<FlowConstructStatistics> getServiceStatistics()
80      {
81          return stats.getServiceStatistics();
82      }
83  
84      public void logSummary()
85      {
86          stats.logSummary();
87      }
88  
89      public String printCSVSummary ()
90      {
91          StringWriter w = new StringWriter(2048);
92          CSVPrinter printer = new CSVPrinter(w);
93          printer.setPrintHeaders(true);
94          stats.logSummary(printer);
95          return w.toString();
96      }
97  
98      public String printHtmlSummary ()
99      {
100         StringWriter w = new StringWriter(8192);
101         HtmlTablePrinter printer = new HtmlTablePrinter(w);
102         stats.logSummary(printer);
103         return w.toString();
104     }
105 
106     public String printXmlSummary()
107     {
108         StringWriter w = new StringWriter(8192);
109         XMLPrinter printer = new XMLPrinter(w);
110         stats.logSummary(printer);
111         return w.toString();
112     }
113 }