001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.geronimo.management.stats;
018
019 import java.io.Serializable;
020 import java.util.HashMap;
021 import java.util.Map;
022 import javax.management.j2ee.statistics.Statistic;
023 import javax.management.j2ee.statistics.Stats;
024
025 /**
026 * Geronimo implementation of the JSR-77 Stats interface. Dynamically tracks
027 * available statistics for its subclasses, to make it easy to iterate
028 * available statistics without knowing exactly what kind of class you're
029 * looking at.
030 *
031 * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
032 */
033
034 public class StatsImpl implements Stats, Serializable {
035 private final Map <String,StatisticImpl> stats = new HashMap<String,StatisticImpl>();
036
037 public StatsImpl() {
038 }
039
040 protected void addStat(String name, StatisticImpl value) {
041 stats.put(name, value);
042 }
043
044 /**
045 * Used when the available statistics are dynamic (e.g. depend on the
046 * current clients of the service, etc.).
047 *
048 * @param name The statistic to remove
049 */
050 protected void removeStat(String name) {
051 stats.remove(name);
052 }
053
054 /**
055 * Set the startTime for all statistics to System.currentTimeMillis()
056 */
057 public void setStartTime() {
058 long now = System.currentTimeMillis();
059 for (StatisticImpl item : stats.values()) {
060 item.setStartTime(now);
061 }
062 }
063
064 /**
065 * Set the startTime for all statistics to the given value
066 * @param time
067 */
068 public void setStartTime(long time) {
069 for (StatisticImpl item : stats.values()) {
070 item.setStartTime(time);
071 }
072 }
073
074 /**
075 * Set the lastSampleTime for all statistics to System.currentTimeMillis()
076 */
077 public void setLastSampleTime() {
078 long now = System.currentTimeMillis();
079 for (StatisticImpl item : stats.values()) {
080 item.setLastSampleTime(now);
081 }
082 }
083
084 /*
085 * Gets a Statistic by name
086 * @see javax.management.j2ee.statistics.Stats#getStatistic(java.lang.String)
087 */
088 public Statistic getStatistic(String statisticName) {
089 return (Statistic)stats.get(statisticName);
090 }
091
092 /*
093 * Returns a list of names of statistics
094 * @see javax.management.j2ee.statistics.Stats#getStatisticNames()
095 */
096 public String[] getStatisticNames() {
097 return (String[]) stats.keySet().toArray(new String[stats.size()]);
098 }
099
100 /*
101 * Returns a list of all the Statistic objects supported by this Stats object
102 * @see javax.management.j2ee.statistics.Stats#getStatistics()
103 */
104 public Statistic[] getStatistics() {
105 String[] names = getStatisticNames();
106 Statistic[] result = new Statistic[names.length];
107 for (int i = 0; i < names.length; i++) {
108 result[i] = (Statistic) stats.get(names[i]);
109 }
110 return result;
111 }
112 }