KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > tools > stats > AbstractStatisticsRecorder


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.tools.stats;
10
11 import java.util.Date JavaDoc;
12 import java.util.SortedMap JavaDoc;
13 import java.util.TreeMap JavaDoc;
14 import javax.management.MBeanRegistration JavaDoc;
15 import javax.management.MBeanServer JavaDoc;
16 import javax.management.ObjectName JavaDoc;
17
18 import mx4j.log.Log;
19 import mx4j.log.Logger;
20
21 /**
22  * Class AbstractStatisticsRecorder. Abstract Parent of the Stats collector
23  * classes. It implements some basic services
24  *
25  * @version $Revision: 1.6 $
26  * @see StatisticsRecorderMBean
27  */

28 public abstract class AbstractStatisticsRecorder implements StatisticsRecorderMBean, MBeanRegistration JavaDoc
29 {
30    /* Indicates whether the Monitor is active */
31    protected boolean isActive = false;
32
33    /* MBeanServer reference */
34    protected MBeanServer JavaDoc server;
35
36    /* Maximum amount of entries */
37    protected int maxEntries = 256;
38
39    /* Holds the entries */
40    protected SortedMap JavaDoc entries = new TreeMap JavaDoc();
41
42    /* Initial recording date */
43    protected Date JavaDoc recordingStart;
44
45    /* Indicates if the type of the recorded value is double */
46    protected boolean isDouble = false;
47
48    /* Statistical values */
49    protected double minimumValue, maximumValue, averageValue;
50
51    /* Count of recorded values */
52    protected long count = 0;
53
54    protected Logger getLogger()
55    {
56       return Log.getLogger(getClass().getName());
57    }
58
59    public void start()
60    {
61       Logger logger = getLogger();
62       if (!isActive)
63       {
64          if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Starting statistics recorder " + this);
65          this.isActive = true;
66          recordingStart = new Date JavaDoc();
67          entries.clear();
68          minimumValue = maximumValue = averageValue = 0;
69          count = 0;
70          isDouble = false;
71          try
72          {
73             doStart();
74          }
75          catch (Exception JavaDoc e)
76          {
77             logger.error("Exception while starting recorder " + this, e);
78          }
79       }
80    }
81
82    public void stop()
83    {
84       Logger logger = getLogger();
85       if (isActive)
86       {
87          if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Starting statistics recorder " + this);
88          this.isActive = false;
89          try
90          {
91             doStop();
92          }
93          catch (Exception JavaDoc e)
94          {
95             logger.error("Exception starting recorder " + this, e);
96          }
97       }
98    }
99
100    public Number JavaDoc getAverage()
101    {
102       return createValue(averageValue);
103    }
104
105    public Number JavaDoc getMin()
106    {
107       return createValue(minimumValue);
108    }
109
110    public Number JavaDoc getMax()
111    {
112       return createValue(maximumValue);
113    }
114
115    public synchronized boolean isActive()
116    {
117       return isActive;
118    }
119
120    public int getMaxEntries()
121    {
122       return maxEntries;
123    }
124
125    public void setMaxEntries(int maxEntries)
126    {
127       if (maxEntries <= 0)
128       {
129          throw new IllegalArgumentException JavaDoc("Max entries has to be bigger than 0");
130       }
131       this.maxEntries = maxEntries;
132    }
133
134    public SortedMap JavaDoc getEntries()
135    {
136       return (SortedMap JavaDoc)((TreeMap JavaDoc)entries).clone();
137    }
138
139    public Date JavaDoc getRecordingStart()
140    {
141       return recordingStart;
142    }
143
144    public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server, ObjectName JavaDoc name) throws Exception JavaDoc
145    {
146       this.server = server;
147       return name;
148    }
149
150    public void postRegister(Boolean JavaDoc registrationDone)
151    {
152    }
153
154    public void preDeregister() throws Exception JavaDoc
155    {
156       this.stop();
157    }
158
159    public void postDeregister()
160    {
161    }
162
163    /**
164     * Subclasses may override this to offer a custom startup procedure
165     */

166    protected void doStart() throws Exception JavaDoc
167    {
168    }
169
170    /**
171     * Subclasses may override this to offer a custom stop procedure
172     */

173    protected void doStop() throws Exception JavaDoc
174    {
175    }
176
177    /**
178     * Adds an entry to the collection. It also reduces the size if too big
179     * and updates the statics
180     */

181    protected synchronized void addEntry(Date JavaDoc key, Number JavaDoc value)
182    {
183       if (isActive)
184       {
185          entries.put(new PointTime(key, count++), value);
186          if (entries.size() > maxEntries)
187          {
188             while (entries.size() > maxEntries)
189             {
190                entries.remove(entries.firstKey());
191             }
192          }
193          calculateStats(value);
194       }
195    }
196
197    /**
198     * Updates the statistics
199     */

200    private void calculateStats(Number JavaDoc value)
201    {
202       if (!isDouble && (value instanceof Double JavaDoc || value instanceof Float JavaDoc))
203       {
204          isDouble = true;
205       }
206       double newValue = value.doubleValue();
207
208       if (count == 1)
209       {
210          maximumValue = minimumValue = averageValue = newValue;
211          return;
212       }
213       if (newValue > maximumValue)
214       {
215          maximumValue = newValue;
216       }
217       if (newValue < minimumValue)
218       {
219          minimumValue = newValue;
220       }
221       averageValue = (1 - 1 / (double)count) * averageValue + 1 / (double)count * newValue;
222    }
223
224    private Number JavaDoc createValue(double targetValue)
225    {
226       if (isDouble)
227       {
228          return new Double JavaDoc(targetValue);
229       }
230       else
231       {
232          return new Long JavaDoc((long)targetValue);
233       }
234    }
235
236 }
237
Popular Tags