01: /*
02: * Copyright 2004-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.compass.sample.petclinic.jmx;
18:
19: import org.aopalliance.intercept.MethodInterceptor;
20: import org.aopalliance.intercept.MethodInvocation;
21: import org.springframework.util.StopWatch;
22:
23: /**
24: * Simple interceptor that monitors call count and call invocation time.
25: * Implements the CallMonitor management interface.
26: *
27: * @author Rob Harrop
28: * @author Juergen Hoeller
29: * @since 1.2
30: */
31: public class CallMonitoringInterceptor implements CallMonitor,
32: MethodInterceptor {
33:
34: private boolean isEnabled = true;
35:
36: private int callCount = 0;
37:
38: private long accumulatedCallTime = 0;
39:
40: public void setEnabled(boolean enabled) {
41: isEnabled = enabled;
42: }
43:
44: public boolean isEnabled() {
45: return isEnabled;
46: }
47:
48: public void reset() {
49: this .callCount = 0;
50: this .accumulatedCallTime = 0;
51: }
52:
53: public int getCallCount() {
54: return callCount;
55: }
56:
57: public long getCallTime() {
58: return (this .callCount > 0 ? this .accumulatedCallTime
59: / this .callCount : 0);
60: }
61:
62: public Object invoke(MethodInvocation invocation) throws Throwable {
63: if (this .isEnabled) {
64: this .callCount++;
65:
66: StopWatch sw = new StopWatch(invocation.getMethod()
67: .getName());
68:
69: sw.start("invoke");
70: Object retVal = invocation.proceed();
71: sw.stop();
72:
73: this.accumulatedCallTime += sw.getTotalTimeMillis();
74: return retVal;
75: }
76:
77: else {
78: return invocation.proceed();
79: }
80: }
81:
82: }
|