View Javadoc

1   /*
2    * $Id: AllStatistics.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;
12  
13  import org.mule.api.management.stats.Statistics;
14  import org.mule.management.stats.printers.AbstractTablePrinter;
15  import org.mule.management.stats.printers.SimplePrinter;
16  
17  import java.io.PrintWriter;
18  import java.util.Collection;
19  import java.util.HashMap;
20  import java.util.Iterator;
21  
22  /**
23   * <code>AllStatistics</code> TODO
24   */
25  public class AllStatistics
26  {
27      private boolean isStatisticsEnabled;
28      private long startTime;
29  
30      private HashMap componentStat = new HashMap();
31  
32      /**
33       * 
34       */
35      public AllStatistics()
36      {
37          clear();
38      }
39  
40      public void logSummary()
41      {
42          logSummary(new SimplePrinter(System.out));
43      }
44  
45      public void logSummary(PrintWriter printer)
46      {
47  
48          if (printer instanceof AbstractTablePrinter)
49          {
50              printer.print(componentStat.values());
51          }
52          else
53          {
54              Iterator it = componentStat.values().iterator();
55  
56              while (it.hasNext())
57              {
58                  printer.print(it.next());
59              }
60          }
61          // printer.println("-----------------------------");
62          // printer.println("duration (ms): " + (System.currentTimeMillis() -
63          // startTime));
64      }
65  
66      public synchronized void clear()
67      {
68  
69          Iterator it = getComponentStatistics().iterator();
70  
71          while (it.hasNext())
72          {
73              ((Statistics) it.next()).clear();
74          }
75          startTime = System.currentTimeMillis();
76      }
77  
78      /**
79       * Are statistics logged
80       */
81      public boolean isEnabled()
82      {
83          return isStatisticsEnabled;
84      }
85  
86      /**
87       * Enable statistics logs (this is a dynamic parameter)
88       */
89      public void setEnabled(boolean b)
90      {
91          isStatisticsEnabled = b;
92  
93          Iterator it = componentStat.values().iterator();
94  
95          while (it.hasNext())
96          {
97              ((ServiceStatistics) it.next()).setEnabled(b);
98          }
99      }
100 
101     public synchronized long getStartTime()
102     {
103         return startTime;
104     }
105 
106     public synchronized void setStartTime(long startTime)
107     {
108         this.startTime = startTime;
109     }
110 
111     public synchronized void add(ServiceStatistics stat)
112     {
113         if (stat != null)
114         {
115             componentStat.put(stat.getName(), stat);
116         }
117     }
118 
119     public synchronized void remove(ServiceStatistics stat)
120     {
121         if (stat != null)
122         {
123             componentStat.remove(stat.getName());
124         }
125     }
126 
127     public synchronized Collection getComponentStatistics()
128     {
129         return componentStat.values();
130     }
131 
132 }