1 /***
2 *
3 * Copyright 2004 Protique Ltd
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.codehaus.activemq.management;
19
20 import javax.management.j2ee.statistics.Statistic;
21 import javax.management.j2ee.statistics.Stats;
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Set;
26
27 /***
28 * Base class for a Stats implementation
29 *
30 * @version $Revision: 1.2 $
31 */
32 public class StatsImpl implements Stats, Resettable {
33 private Map map = new HashMap();
34
35 public StatsImpl() {
36 }
37
38 public StatsImpl(Map map) {
39 this.map = map;
40 }
41
42 public void reset() {
43 Statistic[] stats = getStatistics();
44 for (int i = 0, size = stats.length; i < size; i++) {
45 Statistic stat = stats[i];
46 if (stat instanceof Resettable) {
47 Resettable r = (Resettable) stat;
48 r.reset();
49 }
50 }
51 }
52
53 public Statistic getStatistic(String name) {
54 return (Statistic) map.get(name);
55 }
56
57 public String[] getStatisticNames() {
58 Set keys = map.keySet();
59 String[] answer = new String[keys.size()];
60 keys.toArray(answer);
61 return answer;
62 }
63
64 public Statistic[] getStatistics() {
65 Collection values = map.values();
66 Statistic[] answer = new Statistic[values.size()];
67 values.toArray(answer);
68 return answer;
69 }
70
71 protected void addStatistic(String name, Statistic statistic) {
72 map.put(name, statistic);
73 }
74 }