View Javadoc

1   /**
2    *
3    * Copyright 2003-2004 The Apache Software Foundation
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  package org.apache.geronimo.management.stats;
18  
19  import java.io.Serializable;
20  import java.util.HashMap;
21  import java.util.Map;
22  import javax.management.j2ee.statistics.Statistic;
23  import javax.management.j2ee.statistics.Stats;
24  
25  /**
26   * Geronimo implementation of the JSR-77 Stats interface.  Dynamically tracks
27   * available statistics for its subclasses, to make it easy to iterate
28   * available statistics without knowing exactly what kind of class you're
29   * looking at.  Not sure when you'd want to do that, but hey.
30   *
31   * @version $Rev: 392847 $ $Date: 2006-04-09 15:58:39 -0700 (Sun, 09 Apr 2006) $
32   */
33  public class StatsImpl implements Stats, Serializable {
34      private final Map stats = new HashMap();
35  
36      public StatsImpl() {
37      }
38  
39      protected void addStat(String name, Statistic value) {
40          stats.put(name, value);
41      }
42  
43      /**
44       * Used when the available statistics are dynamic (e.g. depend on the
45       * current clients of the service, etc.).
46       * 
47       * @param name The statistic to remove
48       */
49      protected void removeStat(String name) {
50          stats.remove(name);
51      }
52  
53      public Statistic getStatistic(String statisticName) {
54          return (Statistic)stats.get(statisticName);
55      }
56  
57      public String[] getStatisticNames() {
58          return (String[]) stats.keySet().toArray(new String[stats.size()]);
59      }
60  
61      public Statistic[] getStatistics() {
62          String[] names = getStatisticNames();
63          Statistic[] result = new Statistic[names.length];
64          for (int i = 0; i < names.length; i++) {
65              result[i] = (Statistic) stats.get(names[i]);
66          }
67          return result;
68      }
69  }