Package org.apache.geronimo.tomcat.stats

Source Code of org.apache.geronimo.tomcat.stats.ModuleStats

/**
*
*  Licensed to the Apache Software Foundation (ASF) under one or more
*  contributor license agreements.  See the NOTICE file distributed with
*  this work for additional information regarding copyright ownership.
*  The ASF licenses this file to You under the Apache License, Version 2.0
*  (the "License"); you may not use this file except in compliance with
*  the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/

package org.apache.geronimo.tomcat.stats;

import java.util.Iterator;
import java.util.Set;

import javax.management.MBeanServer;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.j2ee.statistics.Stats;

import org.apache.catalina.core.StandardContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.tomcat.util.modeler.Registry;
import org.apache.geronimo.management.stats.WebModuleStatsImpl;

/**
* Query MBeanServer and provide jsr77 Stats for module, i.e. a webapp
*
* @version $Revision: 653782 $ $Date: 2008-05-06 22:10:14 +0800 (Tue, 06 May 2008) $
*/

public class ModuleStats {

    private static final Logger log = LoggerFactory.getLogger(ModuleStats.class);

    private MBeanServer mBeanServer = null;

    private ObjectName mgrName;

    private WebModuleStatsImpl stats = new WebModuleStatsImpl();

    public ModuleStats(StandardContext context) {
        assert context != null;
        // Retrieve the MBean server
        mBeanServer = Registry.getRegistry(null, null).getMBeanServer();

        try {
            // org.apache.commons.modeler.BaseModelMBean@Geronimo:type=Manager,path=/,host=localhost
            mgrName = new ObjectName("*:type=Manager,*");
        } catch (Exception ex) {
            log.error("Error - " + ex.toString());
        }
        // Query Session Managers
        Set set = mBeanServer.queryMBeans(mgrName, null);
        Iterator iterator = set.iterator();
        ObjectName objectName;
        while (iterator.hasNext()) {
            ObjectInstance oi = (ObjectInstance) iterator.next();
            objectName = oi.getObjectName();
            if (objectName.getKeyProperty("path").indexOf(context.getPath()) > -1) {
                mgrName = objectName;
                break;
               
            }
        }

//      initialize static values
        stats.setProcessingTime(context.getProcessingTime());
        stats.setStartupTime(context.getStartupTime());
        stats.setTldScanTime(context.getTldScanTime());
    }

    public Stats getStats() {
        // Initialize startTime for all statistics
        stats.setStartTime();
        // get transient statistics
        updateStats(stats);
        return stats;
    }
   
    public Stats updateStats() {
        // get transient statistics
        updateStats(stats);
        return stats;
    }

    /*
     * return updated value of all trainsient statistics
     *
     */
    private void updateStats(WebModuleStatsImpl stats) {
        stats.setLastSampleTime();

        // transient data
        try {
            int maxActive = ((Integer) (mBeanServer.getAttribute(mgrName,
                    "maxActive"))).intValue();
            int sessionMaxAliveTime = ((Integer) (mBeanServer.getAttribute(
                    mgrName, "sessionMaxAliveTime"))).intValue();
            int sessionAverageAliveTime = ((Integer) (mBeanServer.getAttribute(
                    mgrName, "sessionAverageAliveTime"))).intValue();
            int activeSessions = ((Integer) (mBeanServer.getAttribute(mgrName,
                    "activeSessions"))).intValue();
            int rejectedSessions = ((Integer) (mBeanServer.getAttribute(
                    mgrName, "rejectedSessions"))).intValue();
            int expiredSessions = ((Integer) (mBeanServer.getAttribute(mgrName,
                    "expiredSessions"))).intValue();
            int sessionCounter = ((Integer) (mBeanServer.getAttribute(mgrName,
                    "sessionCounter"))).intValue();

            stats.setSessionAliveTime(maxActive, -1, sessionMaxAliveTime,
                    sessionAverageAliveTime * maxActive);
            stats.setRejectedSessionCount(rejectedSessions);
            stats.setExpiredSessionCount(expiredSessions);
            stats.setActiveSessionCount(activeSessions);
            stats.setSessionCount(sessionCounter);

        } catch (Exception ex) {
            log.error("Error getting attribute " + mgrName + " " + ex.toString());
        }

    }

}
TOP

Related Classes of org.apache.geronimo.tomcat.stats.ModuleStats

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.