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: */package org.apache.geronimo.tomcat.stats;
018:
019: import java.util.Iterator;
020: import java.util.Set;
021:
022: import javax.management.MBeanServer;
023: import javax.management.ObjectInstance;
024: import javax.management.ObjectName;
025: import javax.management.j2ee.statistics.Stats;
026:
027: import org.apache.catalina.core.StandardContext;
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030: import org.apache.tomcat.util.modeler.Registry;
031: import org.apache.geronimo.management.stats.WebModuleStatsImpl;
032:
033: /**
034: * Query MBeanServer and provide jsr77 Stats for module, i.e. a webapp
035: *
036: * @version $Revision: 511136 $ $Date: 2007-02-23 14:12:09 -0800 (Fri, 23 Feb 2007) $
037: */
038:
039: public class ModuleStats {
040:
041: private static final Log log = LogFactory.getLog(ModuleStats.class);
042:
043: private MBeanServer mBeanServer = null;
044:
045: private ObjectName mgrName;
046:
047: private WebModuleStatsImpl stats = new WebModuleStatsImpl();
048:
049: public ModuleStats(StandardContext context) {
050: assert context != null;
051: // Retrieve the MBean server
052: mBeanServer = Registry.getRegistry(null, null).getMBeanServer();
053:
054: try {
055: // org.apache.commons.modeler.BaseModelMBean@Geronimo:type=Manager,path=/,host=localhost
056: mgrName = new ObjectName("*:type=Manager,*");
057: } catch (Exception ex) {
058: log.error("Error - " + ex.toString());
059: }
060: // Query Session Managers
061: Set set = mBeanServer.queryMBeans(mgrName, null);
062: Iterator iterator = set.iterator();
063: ObjectName objectName;
064: while (iterator.hasNext()) {
065: ObjectInstance oi = (ObjectInstance) iterator.next();
066: objectName = oi.getObjectName();
067: if (objectName.getKeyProperty("path").indexOf(
068: context.getPath()) > -1) {
069: mgrName = objectName;
070: break;
071:
072: }
073: }
074:
075: // initialize static values
076: stats.setProcessingTime(context.getProcessingTime());
077: stats.setStartupTime(context.getStartupTime());
078: stats.setTldScanTime(context.getTldScanTime());
079: }
080:
081: public Stats getStats() {
082: // Initialize startTime for all statistics
083: stats.setStartTime();
084: // get transient statistics
085: updateStats(stats);
086: return stats;
087: }
088:
089: public Stats updateStats() {
090: // get transient statistics
091: updateStats(stats);
092: return stats;
093: }
094:
095: /*
096: * return updated value of all trainsient statistics
097: *
098: */
099: private void updateStats(WebModuleStatsImpl stats) {
100: stats.setLastSampleTime();
101:
102: // transient data
103: try {
104: int maxActive = ((Integer) (mBeanServer.getAttribute(
105: mgrName, "maxActive"))).intValue();
106: int sessionMaxAliveTime = ((Integer) (mBeanServer
107: .getAttribute(mgrName, "sessionMaxAliveTime")))
108: .intValue();
109: int sessionAverageAliveTime = ((Integer) (mBeanServer
110: .getAttribute(mgrName, "sessionAverageAliveTime")))
111: .intValue();
112: int activeSessions = ((Integer) (mBeanServer.getAttribute(
113: mgrName, "activeSessions"))).intValue();
114: int rejectedSessions = ((Integer) (mBeanServer
115: .getAttribute(mgrName, "rejectedSessions")))
116: .intValue();
117: int expiredSessions = ((Integer) (mBeanServer.getAttribute(
118: mgrName, "expiredSessions"))).intValue();
119: int sessionCounter = ((Integer) (mBeanServer.getAttribute(
120: mgrName, "sessionCounter"))).intValue();
121:
122: stats.setSessionAliveTime(maxActive, -1,
123: sessionMaxAliveTime, sessionAverageAliveTime
124: * maxActive);
125: stats.setRejectedSessionCount(rejectedSessions);
126: stats.setExpiredSessionCount(expiredSessions);
127: stats.setActiveSessionCount(activeSessions);
128: stats.setSessionCount(sessionCounter);
129:
130: } catch (Exception ex) {
131: log.error("Error getting attribute " + mgrName + " "
132: + ex.toString());
133: }
134:
135: }
136:
137: }
|