View Javadoc

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