001    /**
002     *
003     * Copyright 2003-2004 The Apache Software Foundation
004     *
005     *  Licensed under the Apache License, Version 2.0 (the "License");
006     *  you may not use this file except in compliance with the License.
007     *  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.  Not sure when you'd want to do that, but hey.
030     *
031     * @version $Rev: 392847 $ $Date: 2006-04-09 15:58:39 -0700 (Sun, 09 Apr 2006) $
032     */
033    public class StatsImpl implements Stats, Serializable {
034        private final Map stats = new HashMap();
035    
036        public StatsImpl() {
037        }
038    
039        protected void addStat(String name, Statistic value) {
040            stats.put(name, value);
041        }
042    
043        /**
044         * Used when the available statistics are dynamic (e.g. depend on the
045         * current clients of the service, etc.).
046         * 
047         * @param name The statistic to remove
048         */
049        protected void removeStat(String name) {
050            stats.remove(name);
051        }
052    
053        public Statistic getStatistic(String statisticName) {
054            return (Statistic)stats.get(statisticName);
055        }
056    
057        public String[] getStatisticNames() {
058            return (String[]) stats.keySet().toArray(new String[stats.size()]);
059        }
060    
061        public Statistic[] getStatistics() {
062            String[] names = getStatisticNames();
063            Statistic[] result = new Statistic[names.length];
064            for (int i = 0; i < names.length; i++) {
065                result[i] = (Statistic) stats.get(names[i]);
066            }
067            return result;
068        }
069    }