KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > management > j2ee > StatsSupport


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Sam
27  */

28
29
30 package com.caucho.management.j2ee;
31
32 import com.caucho.util.Alarm;
33
34 import javax.management.j2ee.statistics.*;
35 import java.lang.reflect.InvocationTargetException JavaDoc;
36 import java.lang.reflect.Method JavaDoc;
37 import java.util.ArrayList JavaDoc;
38 import java.util.TreeSet JavaDoc;
39
40 public class StatsSupport
41   implements Stats
42 {
43   private final J2EEManagedObject _j2eeManagedObject;
44
45   public StatsSupport(J2EEManagedObject j2eeManagedObject)
46   {
47     _j2eeManagedObject = j2eeManagedObject;
48   }
49
50   public String JavaDoc []getStatisticNames()
51   {
52     TreeSet JavaDoc<String JavaDoc> names = new TreeSet JavaDoc<String JavaDoc>();
53
54     for (Method JavaDoc method : getClass().getMethods()) {
55       if (Statistic.class.isAssignableFrom(method.getReturnType())) {
56         String JavaDoc name = method.getName();
57
58         if (name.startsWith("get"))
59           names.add(name.substring(3));
60       }
61     }
62
63     return names.toArray(new String JavaDoc[names.size()]);
64   }
65
66   public Statistic getStatistic(String JavaDoc name)
67   {
68     try {
69       Method JavaDoc method = getClass().getMethod("get" + name);
70
71       return (Statistic) method.invoke(this, (Object JavaDoc[]) null);
72     }
73     catch (NoSuchMethodException JavaDoc e) {
74       return null;
75     }
76     catch (IllegalAccessException JavaDoc e) {
77       return null;
78     }
79     catch (InvocationTargetException JavaDoc e) {
80       return null;
81     }
82   }
83
84   public Statistic []getStatistics()
85   {
86     ArrayList JavaDoc<Statistic> statistics = new ArrayList JavaDoc<Statistic>();
87
88     for (Method JavaDoc method : getClass().getMethods()) {
89       if (Statistic.class.isAssignableFrom(method.getReturnType())) {
90         try {
91           statistics.add((Statistic) method.invoke(this, (Object JavaDoc[]) null));
92         }
93         catch (IllegalAccessException JavaDoc e) {
94           continue;
95         }
96         catch (InvocationTargetException JavaDoc e) {
97           continue;
98         }
99       }
100     }
101
102     return statistics.toArray(new Statistic[statistics.size()]);
103   }
104
105   class StatisticSupport
106     implements Statistic
107   {
108     public final String JavaDoc _name;
109
110     public StatisticSupport(String JavaDoc name)
111     {
112       _name = name;
113     }
114
115     public String JavaDoc getName()
116     {
117       return _name;
118     }
119
120     public String JavaDoc getUnit()
121     {
122       return "UNKNOWN";
123     }
124
125     public String JavaDoc getDescription()
126     {
127       return "";
128     }
129
130     public long getStartTime()
131     {
132       return _j2eeManagedObject.getStartTime();
133     }
134
135     public long getLastSampleTime()
136     {
137       return Alarm.getCurrentTime();
138     }
139   }
140
141   class TimeStatisticImpl
142     extends StatisticSupport
143     implements TimeStatistic
144   {
145     private long _count;
146     private long _maxTime;
147     private long _minTime;
148     private long _totalTime;
149
150     public TimeStatisticImpl(String JavaDoc name,
151                              long count,
152                              long maxTime,
153                              long minTime,
154                              long totalTime)
155     {
156       super(name);
157
158       _count = count;
159       _maxTime = maxTime;
160       _minTime = minTime;
161       _totalTime = totalTime;
162     }
163
164     public String JavaDoc getUnit()
165     {
166       return "MILLISECOND";
167     }
168
169     public long getCount()
170     {
171       return _count;
172     }
173
174     public long getMaxTime()
175     {
176       return _maxTime;
177     }
178
179     public long getMinTime()
180     {
181       return _minTime;
182     }
183
184     public long getTotalTime()
185     {
186       return _totalTime;
187     }
188   }
189
190   class RangeStatisticImpl
191     extends StatisticSupport
192     implements RangeStatistic
193   {
194     private long _highWaterMark;
195     private long _lowWaterMark;
196     private long _current;
197
198     public RangeStatisticImpl(String JavaDoc name,
199                               long highWaterMark,
200                               long lowWaterMark,
201                               long current)
202     {
203       super(name);
204
205       _highWaterMark = highWaterMark;
206       _lowWaterMark = lowWaterMark;
207       _current = current;
208     }
209
210     public long getHighWaterMark()
211     {
212       return _highWaterMark;
213     }
214
215     public long getLowWaterMark()
216     {
217       return _lowWaterMark;
218     }
219
220     public long getCurrent()
221     {
222       return _current;
223     }
224   }
225
226   class BoundaryStatisticImpl
227     extends StatisticSupport
228     implements BoundaryStatistic
229   {
230     private long _upperBound;
231     private long _lowerBound;
232
233     public BoundaryStatisticImpl(String JavaDoc name, long upperBound, long lowerBound)
234     {
235       super(name);
236       _upperBound = upperBound;
237       _lowerBound = lowerBound;
238     }
239
240     public long getUpperBound()
241     {
242       return _upperBound;
243     }
244
245     public long getLowerBound()
246     {
247       return _lowerBound;
248     }
249   }
250
251   class CountStatisticImpl
252     extends StatisticSupport
253     implements CountStatistic
254   {
255     private long _count;
256
257     public CountStatisticImpl(String JavaDoc name, long count)
258     {
259       super(name);
260       _count = count;
261     }
262
263     public long getCount()
264     {
265       return _count;
266     }
267   }
268
269   class BoundedRangeStatisticImpl
270     extends StatisticSupport
271     implements BoundedRangeStatistic
272   {
273     private long _upperBound;
274     private long _lowerBound;
275     private long _highWaterMark;
276     private long _lowWaterMark;
277     private long _current;
278
279     public BoundedRangeStatisticImpl(String JavaDoc name,
280                                      long upperBound,
281                                      long lowerBound,
282                                      long highWaterMark,
283                                      long lowWaterMark,
284                                      long current)
285     {
286       super(name);
287
288       _upperBound = upperBound;
289       _lowerBound = lowerBound;
290       _highWaterMark = highWaterMark;
291       _lowWaterMark = lowWaterMark;
292       _current = current;
293     }
294
295     public long getUpperBound()
296     {
297       return _upperBound;
298     }
299
300     public long getLowerBound()
301     {
302       return _lowerBound;
303     }
304
305     public long getHighWaterMark()
306     {
307       return _highWaterMark;
308     }
309
310     public long getLowWaterMark()
311     {
312       return _lowWaterMark;
313     }
314
315     public long getCurrent()
316     {
317       return _current;
318     }
319   }
320
321   class UnimplementedTimeStatistic
322     extends TimeStatisticImpl
323   {
324     public UnimplementedTimeStatistic(String JavaDoc name)
325     {
326       super(name, -1, -1, -1, -1);
327     }
328   }
329
330   class UnimplementedRangeStatistic
331     extends RangeStatisticImpl
332   {
333     public UnimplementedRangeStatistic(String JavaDoc name)
334     {
335       super(name, -1, -1, -1);
336     }
337   }
338
339   class UnimplementedBoundaryStatistic
340     extends BoundaryStatisticImpl
341   {
342     public UnimplementedBoundaryStatistic(String JavaDoc name)
343     {
344       super(name, -1, -1);
345     }
346   }
347
348   class UnimplementedCountStatistic
349     extends CountStatisticImpl
350   {
351     public UnimplementedCountStatistic(String JavaDoc name)
352     {
353       super(name, -1);
354     }
355   }
356
357   class UnimplementedBoundedRangeStatistic
358     extends BoundedRangeStatisticImpl
359   {
360     public UnimplementedBoundedRangeStatistic(String JavaDoc name)
361     {
362       super(name, -1, -1, -1, -1, -1);
363     }
364   }
365 }
366
Popular Tags