001    /**
002     *
003     *  Licensed to the Apache Software Foundation (ASF) under one or more
004     *  contributor license agreements.  See the NOTICE file distributed with
005     *  this work for additional information regarding copyright ownership.
006     *  The ASF licenses this file to You under the Apache License, Version 2.0
007     *  (the "License"); you may not use this file except in compliance with
008     *  the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *  Unless required by applicable law or agreed to in writing, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    
019    package org.apache.geronimo.tomcat.stats;
020    
021    import java.util.Iterator;
022    import java.util.Set;
023    
024    import javax.management.MBeanServer;
025    import javax.management.ObjectInstance;
026    import javax.management.ObjectName;
027    import javax.management.j2ee.statistics.Stats;
028    
029    import org.apache.catalina.core.StandardContext;
030    import org.apache.commons.logging.Log;
031    import org.apache.commons.logging.LogFactory;
032    import org.apache.tomcat.util.modeler.Registry;
033    import org.apache.geronimo.management.stats.WebModuleStatsImpl;
034    
035    /**
036     * Query MBeanServer and provide jsr77 Stats for module, i.e. a webapp
037     * 
038     * @version $Revision: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
039     */
040    
041    public class ModuleStats {
042    
043        private static final Log log = LogFactory.getLog(ModuleStats.class);
044    
045        private MBeanServer mBeanServer = null;
046    
047        private ObjectName mgrName;
048    
049        private WebModuleStatsImpl stats = new WebModuleStatsImpl();
050    
051        public ModuleStats(StandardContext context) {
052            assert context != null;
053            // Retrieve the MBean server
054            mBeanServer = Registry.getRegistry(null, null).getMBeanServer();
055    
056            try {
057                // org.apache.commons.modeler.BaseModelMBean@Geronimo:type=Manager,path=/,host=localhost
058                mgrName = new ObjectName("*:type=Manager,*");
059            } catch (Exception ex) {
060                log.error("Error - " + ex.toString());
061            }
062            // Query Session Managers
063            Set set = mBeanServer.queryMBeans(mgrName, null);
064            Iterator iterator = set.iterator();
065            ObjectName objectName;
066            while (iterator.hasNext()) {
067                ObjectInstance oi = (ObjectInstance) iterator.next();
068                objectName = oi.getObjectName();
069                if (objectName.getKeyProperty("path").indexOf(context.getPath()) > -1) {
070                    mgrName = objectName;
071                    break;
072                    
073                }
074            }
075    
076    //      initialize static values
077            stats.setProcessingTime(context.getProcessingTime());
078            stats.setStartupTime(context.getStartupTime());
079            stats.setTldScanTime(context.getTldScanTime());
080        }
081    
082        public Stats getStats() {
083            // Initialize startTime for all statistics 
084            stats.setStartTime();
085            // get transient statistics
086            updateStats(stats);
087            return stats;
088        }
089        
090        public Stats updateStats() {
091            // get transient statistics
092            updateStats(stats);
093            return stats;
094        }
095    
096        /*
097         * return updated value of all trainsient statistics
098         * 
099         */
100        private void updateStats(WebModuleStatsImpl stats) {
101            stats.setLastSampleTime();
102    
103            // transient data
104            try {
105                int maxActive = ((Integer) (mBeanServer.getAttribute(mgrName,
106                        "maxActive"))).intValue();
107                int sessionMaxAliveTime = ((Integer) (mBeanServer.getAttribute(
108                        mgrName, "sessionMaxAliveTime"))).intValue();
109                int sessionAverageAliveTime = ((Integer) (mBeanServer.getAttribute(
110                        mgrName, "sessionAverageAliveTime"))).intValue();
111                int activeSessions = ((Integer) (mBeanServer.getAttribute(mgrName,
112                        "activeSessions"))).intValue();
113                int rejectedSessions = ((Integer) (mBeanServer.getAttribute(
114                        mgrName, "rejectedSessions"))).intValue();
115                int expiredSessions = ((Integer) (mBeanServer.getAttribute(mgrName,
116                        "expiredSessions"))).intValue();
117                int sessionCounter = ((Integer) (mBeanServer.getAttribute(mgrName,
118                        "sessionCounter"))).intValue();
119    
120                stats.setSessionAliveTime(maxActive, -1, sessionMaxAliveTime,
121                        sessionAverageAliveTime * maxActive);
122                stats.setRejectedSessionCount(rejectedSessions);
123                stats.setExpiredSessionCount(expiredSessions);
124                stats.setActiveSessionCount(activeSessions);
125                stats.setSessionCount(sessionCounter);
126    
127            } catch (Exception ex) {
128                log.error("Error getting attribute " + mgrName + " " + ex.toString());
129            }
130    
131        }
132    
133    }