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.  Not sure when you'd want to do that, but hey.
030     *
031     * @version $Rev: 503407 $ $Date: 2007-02-04 08:57:40 -0500 (Sun, 04 Feb 2007) $
032     */
033    public class StatsImpl implements Stats, Serializable {
034        private final Map <String,StatisticImpl> stats = new HashMap<String,StatisticImpl>();
035    
036        public StatsImpl() {
037        }
038    
039        protected void addStat(String name, StatisticImpl 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 void setStartTime() {
054            long now = System.currentTimeMillis();
055            for (StatisticImpl item : stats.values()) {
056                item.setStartTime(now);
057            }            
058        }
059        
060        public void setLastSampleTime() {
061            long now = System.currentTimeMillis();
062            for (StatisticImpl item : stats.values()) {
063                item.setLastSampleTime(now);
064            }            
065        }
066    
067        public Statistic getStatistic(String statisticName) {
068            return (Statistic)stats.get(statisticName);
069        }
070    
071        public String[] getStatisticNames() {
072            return (String[]) stats.keySet().toArray(new String[stats.size()]);
073        }
074    
075        public Statistic[] getStatistics() {
076            String[] names = getStatisticNames();
077            Statistic[] result = new Statistic[names.length];
078            for (int i = 0; i < names.length; i++) {
079                result[i] = (Statistic) stats.get(names[i]);
080            }
081            return result;
082        }
083    }