View Javadoc

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