View Javadoc

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