001: //========================================================================
002: //$Id: Jsr77ServletHolderMBean.java 1259 2006-11-19 16:00:48Z janb $
003: //Copyright 2000-2004 Mort Bay Consulting Pty. Ltd.
004: //------------------------------------------------------------------------
005: //Licensed under the Apache License, Version 2.0 (the "License");
006: //you may not use this file except in compliance with the License.
007: //You may obtain a copy of the License at
008: //http://www.apache.org/licenses/LICENSE-2.0
009: //Unless required by applicable law or agreed to in writing, software
010: //distributed under the License is distributed on an "AS IS" BASIS,
011: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: //See the License for the specific language governing permissions and
013: //limitations under the License.
014: //========================================================================
015:
016: package org.mortbay.jetty.servlet.jsr77.management;
017:
018: import javax.management.MBeanException;
019: import javax.management.ObjectName;
020: import javax.management.j2ee.statistics.ServletStats;
021:
022: import org.jboss.jetty.JBossWebAppContext;
023: import org.mortbay.jetty.servlet.jsr77.Jsr77ServletHolder;
024: import org.mortbay.management.ObjectMBean;
025: import org.mortbay.log.Log;
026:
027: /**
028: *
029: * Jsr77ServletHolderMBean
030: *
031: * @author janb
032: * @version $Revision: 1259 $ $Date: 2006-11-19 17:00:48 +0100 (Sun, 19 Nov 2006) $
033: *
034: */
035: public class Jsr77ServletHolderMBean extends ObjectMBean {
036: private Jsr77ServletHolder _servletHolder = null;
037: private ServletStats _stats;
038:
039: public Jsr77ServletHolderMBean(Object managedObject)
040: throws MBeanException {
041: super (managedObject);
042: _servletHolder = (Jsr77ServletHolder) managedObject;
043: }
044:
045: /**StatisticsProvider
046: * As per the jsr77 spec, we are providing statistics for a
047: * servlet
048: * @return true
049: */
050: public boolean getStatisticsProvider() {
051: return true;
052: }
053:
054: /**ServletStats
055: * @return the JSR77 servlet stats for the servlet we represent
056: */
057: public ServletStats getStats() {
058: getJsr77Stats();
059: return _stats;
060: }
061:
062: /**MaxTime
063: * Necessary for JBoss's JSR77 impl.
064: * @return the max service time statistic
065: */
066: public Long getMaxTime() {
067: getJsr77Stats();
068:
069: if (null == _stats)
070: return new Long(0L);
071:
072: return new Long(_stats.getServiceTime().getMaxTime());
073: }
074:
075: /**MinTime
076: * Necessary for JBoss's JSR77 impl.
077: * @return the min service time statistic
078: */
079: public Long getMinTime() {
080: getJsr77Stats();
081: if (null == _stats)
082: return new Long(0L);
083:
084: return new Long(_stats.getServiceTime().getMinTime());
085: }
086:
087: /**Satisfying JBoss's JSR77 impl
088: * @return
089: */
090: public Long getProcessingTime() {
091: return new Long(getTotalTime());
092: }
093:
094: /**Satisfying JBoss's JSR77 impl
095: * @return
096: */
097: public Integer getRequestCount() {
098: return new Integer((int) getCount());
099: }
100:
101: /**Count
102: * Convenience method. Also helpful for JBoss's JSR77 impl.
103: * @return the number of times the servlet service() method has been called.
104: */
105: private long getCount() {
106: getJsr77Stats();
107: if (null == _stats)
108: return 0L;
109:
110: return _stats.getServiceTime().getCount();
111: }
112:
113: /**TotalTime
114: * Convenience method. Also helpful for JBoss's JSR77 impl.
115: * @return the total time spent in the servlet's service() method.
116: */
117: private long getTotalTime() {
118: getJsr77Stats();
119: if (null == _stats)
120: return 0L;
121:
122: return _stats.getServiceTime().getTotalTime();
123: }
124:
125: /** Jsr77Stats
126: * Lookup the statistic object for the servlet we represent.
127: * Statistics are captured by a filter placed in front of each servlet.
128: */
129: private void getJsr77Stats() {
130: if (null == _stats) {
131: if (null == _servletHolder)
132: return;
133:
134: _stats = _servletHolder.getServletStats();
135: }
136: }
137:
138: public ObjectName getObjectName() {
139:
140: if (getMBeanContainer() == null)
141: return null; //not possible to make a name
142:
143: String name = _servletHolder.getName();
144: ObjectName oname = null;
145: try {
146: oname = new ObjectName(
147: getMBeanContainer().getDomain()
148: + ":J2EEServer=none,J2EEApplication=none,J2EEWebModule="
149: + ((JBossWebAppContext) _servletHolder
150: .getWebAppContext())
151: .getUniqueName()
152: + ",j2eeType=Servlet,name=" + name);
153: } catch (Exception e) {
154: Log.warn(e);
155: }
156: return oname;
157:
158: }
159: }
|