001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Sam
027: */
028:
029: package com.caucho.management.j2ee;
030:
031: import com.caucho.util.Alarm;
032:
033: import javax.management.j2ee.statistics.*;
034: import java.lang.reflect.InvocationTargetException;
035: import java.lang.reflect.Method;
036: import java.util.ArrayList;
037: import java.util.TreeSet;
038:
039: public class StatsSupport implements Stats {
040: private final J2EEManagedObject _j2eeManagedObject;
041:
042: public StatsSupport(J2EEManagedObject j2eeManagedObject) {
043: _j2eeManagedObject = j2eeManagedObject;
044: }
045:
046: public String[] getStatisticNames() {
047: TreeSet<String> names = new TreeSet<String>();
048:
049: for (Method method : getClass().getMethods()) {
050: if (Statistic.class
051: .isAssignableFrom(method.getReturnType())) {
052: String name = method.getName();
053:
054: if (name.startsWith("get"))
055: names.add(name.substring(3));
056: }
057: }
058:
059: return names.toArray(new String[names.size()]);
060: }
061:
062: public Statistic getStatistic(String name) {
063: try {
064: Method method = getClass().getMethod("get" + name);
065:
066: return (Statistic) method.invoke(this , (Object[]) null);
067: } catch (NoSuchMethodException e) {
068: return null;
069: } catch (IllegalAccessException e) {
070: return null;
071: } catch (InvocationTargetException e) {
072: return null;
073: }
074: }
075:
076: public Statistic[] getStatistics() {
077: ArrayList<Statistic> statistics = new ArrayList<Statistic>();
078:
079: for (Method method : getClass().getMethods()) {
080: if (Statistic.class
081: .isAssignableFrom(method.getReturnType())) {
082: try {
083: statistics.add((Statistic) method.invoke(this ,
084: (Object[]) null));
085: } catch (IllegalAccessException e) {
086: continue;
087: } catch (InvocationTargetException e) {
088: continue;
089: }
090: }
091: }
092:
093: return statistics.toArray(new Statistic[statistics.size()]);
094: }
095:
096: class StatisticSupport implements Statistic {
097: public final String _name;
098:
099: public StatisticSupport(String name) {
100: _name = name;
101: }
102:
103: public String getName() {
104: return _name;
105: }
106:
107: public String getUnit() {
108: return "UNKNOWN";
109: }
110:
111: public String getDescription() {
112: return "";
113: }
114:
115: public long getStartTime() {
116: return _j2eeManagedObject.getStartTime();
117: }
118:
119: public long getLastSampleTime() {
120: return Alarm.getCurrentTime();
121: }
122: }
123:
124: class TimeStatisticImpl extends StatisticSupport implements
125: TimeStatistic {
126: private long _count;
127: private long _maxTime;
128: private long _minTime;
129: private long _totalTime;
130:
131: public TimeStatisticImpl(String name, long count, long maxTime,
132: long minTime, long totalTime) {
133: super (name);
134:
135: _count = count;
136: _maxTime = maxTime;
137: _minTime = minTime;
138: _totalTime = totalTime;
139: }
140:
141: public String getUnit() {
142: return "MILLISECOND";
143: }
144:
145: public long getCount() {
146: return _count;
147: }
148:
149: public long getMaxTime() {
150: return _maxTime;
151: }
152:
153: public long getMinTime() {
154: return _minTime;
155: }
156:
157: public long getTotalTime() {
158: return _totalTime;
159: }
160: }
161:
162: class RangeStatisticImpl extends StatisticSupport implements
163: RangeStatistic {
164: private long _highWaterMark;
165: private long _lowWaterMark;
166: private long _current;
167:
168: public RangeStatisticImpl(String name, long highWaterMark,
169: long lowWaterMark, long current) {
170: super (name);
171:
172: _highWaterMark = highWaterMark;
173: _lowWaterMark = lowWaterMark;
174: _current = current;
175: }
176:
177: public long getHighWaterMark() {
178: return _highWaterMark;
179: }
180:
181: public long getLowWaterMark() {
182: return _lowWaterMark;
183: }
184:
185: public long getCurrent() {
186: return _current;
187: }
188: }
189:
190: class BoundaryStatisticImpl extends StatisticSupport implements
191: BoundaryStatistic {
192: private long _upperBound;
193: private long _lowerBound;
194:
195: public BoundaryStatisticImpl(String name, long upperBound,
196: long lowerBound) {
197: super (name);
198: _upperBound = upperBound;
199: _lowerBound = lowerBound;
200: }
201:
202: public long getUpperBound() {
203: return _upperBound;
204: }
205:
206: public long getLowerBound() {
207: return _lowerBound;
208: }
209: }
210:
211: class CountStatisticImpl extends StatisticSupport implements
212: CountStatistic {
213: private long _count;
214:
215: public CountStatisticImpl(String name, long count) {
216: super (name);
217: _count = count;
218: }
219:
220: public long getCount() {
221: return _count;
222: }
223: }
224:
225: class BoundedRangeStatisticImpl extends StatisticSupport implements
226: BoundedRangeStatistic {
227: private long _upperBound;
228: private long _lowerBound;
229: private long _highWaterMark;
230: private long _lowWaterMark;
231: private long _current;
232:
233: public BoundedRangeStatisticImpl(String name, long upperBound,
234: long lowerBound, long highWaterMark, long lowWaterMark,
235: long current) {
236: super (name);
237:
238: _upperBound = upperBound;
239: _lowerBound = lowerBound;
240: _highWaterMark = highWaterMark;
241: _lowWaterMark = lowWaterMark;
242: _current = current;
243: }
244:
245: public long getUpperBound() {
246: return _upperBound;
247: }
248:
249: public long getLowerBound() {
250: return _lowerBound;
251: }
252:
253: public long getHighWaterMark() {
254: return _highWaterMark;
255: }
256:
257: public long getLowWaterMark() {
258: return _lowWaterMark;
259: }
260:
261: public long getCurrent() {
262: return _current;
263: }
264: }
265:
266: class UnimplementedTimeStatistic extends TimeStatisticImpl {
267: public UnimplementedTimeStatistic(String name) {
268: super (name, -1, -1, -1, -1);
269: }
270: }
271:
272: class UnimplementedRangeStatistic extends RangeStatisticImpl {
273: public UnimplementedRangeStatistic(String name) {
274: super (name, -1, -1, -1);
275: }
276: }
277:
278: class UnimplementedBoundaryStatistic extends BoundaryStatisticImpl {
279: public UnimplementedBoundaryStatistic(String name) {
280: super (name, -1, -1);
281: }
282: }
283:
284: class UnimplementedCountStatistic extends CountStatisticImpl {
285: public UnimplementedCountStatistic(String name) {
286: super (name, -1);
287: }
288: }
289:
290: class UnimplementedBoundedRangeStatistic extends
291: BoundedRangeStatisticImpl {
292: public UnimplementedBoundedRangeStatistic(String name) {
293: super (name, -1, -1, -1, -1, -1);
294: }
295: }
296: }
|