Save This Page
Home » geronimo-2.2-source-release » org.apache.geronimo.tomcat.stats » [javadoc | source]
    1   /**
    2    *
    3    *  Licensed to the Apache Software Foundation (ASF) under one or more
    4    *  contributor license agreements.  See the NOTICE file distributed with
    5    *  this work for additional information regarding copyright ownership.
    6    *  The ASF licenses this file to You under the Apache License, Version 2.0
    7    *  (the "License"); you may not use this file except in compliance with
    8    *  the License.  You may obtain a copy of the License at
    9    *
   10    *     http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    *  Unless required by applicable law or agreed to in writing, software
   13    *  distributed under the License is distributed on an "AS IS" BASIS,
   14    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   15    *  See the License for the specific language governing permissions and
   16    *  limitations under the License.
   17    */
   18   
   19   package org.apache.geronimo.tomcat.stats;
   20   
   21   import java.util.Iterator;
   22   import java.util.Set;
   23   
   24   import javax.management.MBeanServer;
   25   import javax.management.ObjectInstance;
   26   import javax.management.ObjectName;
   27   import javax.management.j2ee.statistics.Stats;
   28   
   29   import org.apache.catalina.core.StandardContext;
   30   import org.slf4j.Logger;
   31   import org.slf4j.LoggerFactory;
   32   import org.apache.tomcat.util.modeler.Registry;
   33   import org.apache.geronimo.management.stats.WebModuleStatsImpl;
   34   
   35   /**
   36    * Query MBeanServer and provide jsr77 Stats for module, i.e. a webapp
   37    * 
   38    * @version $Revision: 653782 $ $Date: 2008-05-06 07:10:14 -0700 (Tue, 06 May 2008) $
   39    */
   40   
   41   public class ModuleStats {
   42   
   43       private static final Logger log = LoggerFactory.getLogger(ModuleStats.class);
   44   
   45       private MBeanServer mBeanServer = null;
   46   
   47       private ObjectName mgrName;
   48   
   49       private WebModuleStatsImpl stats = new WebModuleStatsImpl();
   50   
   51       public ModuleStats(StandardContext context) {
   52           assert context != null;
   53           // Retrieve the MBean server
   54           mBeanServer = Registry.getRegistry(null, null).getMBeanServer();
   55   
   56           try {
   57               // org.apache.commons.modeler.BaseModelMBean@Geronimo:type=Manager,path=/,host=localhost
   58               mgrName = new ObjectName("*:type=Manager,*");
   59           } catch (Exception ex) {
   60               log.error("Error - " + ex.toString());
   61           }
   62           // Query Session Managers
   63           Set set = mBeanServer.queryMBeans(mgrName, null);
   64           Iterator iterator = set.iterator();
   65           ObjectName objectName;
   66           while (iterator.hasNext()) {
   67               ObjectInstance oi = (ObjectInstance) iterator.next();
   68               objectName = oi.getObjectName();
   69               if (objectName.getKeyProperty("path").indexOf(context.getPath()) > -1) {
   70                   mgrName = objectName;
   71                   break;
   72                   
   73               }
   74           }
   75   
   76   //      initialize static values
   77           stats.setProcessingTime(context.getProcessingTime());
   78           stats.setStartupTime(context.getStartupTime());
   79           stats.setTldScanTime(context.getTldScanTime());
   80       }
   81   
   82       public Stats getStats() {
   83           // Initialize startTime for all statistics 
   84           stats.setStartTime();
   85           // get transient statistics
   86           updateStats(stats);
   87           return stats;
   88       }
   89       
   90       public Stats updateStats() {
   91           // get transient statistics
   92           updateStats(stats);
   93           return stats;
   94       }
   95   
   96       /*
   97        * return updated value of all trainsient statistics
   98        * 
   99        */
  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   }

Save This Page
Home » geronimo-2.2-source-release » org.apache.geronimo.tomcat.stats » [javadoc | source]