KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > interceptor > JamonPerformanceMonitorInterceptor


1 /*
2  * Copyright 2002-2006 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.springframework.aop.interceptor;
18
19 import com.jamonapi.Monitor;
20 import com.jamonapi.MonitorFactory;
21 import org.aopalliance.intercept.MethodInvocation;
22 import org.apache.commons.logging.Log;
23
24 /**
25  * Performance monitor interceptor that uses <b>JAMon</b> library
26  * to perform the performance measurement on the intercepted method
27  * and output the stats.
28  *
29  * <p>This code is inspired by Thierry Templier's blog.
30  *
31  * @author Dmitriy Kopylenko
32  * @author Juergen Hoeller
33  * @author Rob Harrop
34  * @since 1.1.3
35  * @see com.jamonapi.MonitorFactory
36  * @see PerformanceMonitorInterceptor
37  */

38 public class JamonPerformanceMonitorInterceptor extends AbstractMonitoringInterceptor {
39
40     private boolean trackAllInvocations = false;
41
42
43     /**
44      * Create a new JamonPerformanceMonitorInterceptor with a static logger.
45      */

46     public JamonPerformanceMonitorInterceptor() {
47     }
48
49     /**
50      * Create a new JamonPerformanceMonitorInterceptor with a dynamic or static logger,
51      * according to the given flag.
52      * @param useDynamicLogger whether to use a dynamic logger or a static logger
53      * @see #setUseDynamicLogger
54      */

55     public JamonPerformanceMonitorInterceptor(boolean useDynamicLogger) {
56         setUseDynamicLogger(useDynamicLogger);
57     }
58
59     /**
60      * Create a new JamonPerformanceMonitorInterceptor with a dynamic or static logger,
61      * according to the given flag.
62      * @param useDynamicLogger whether to use a dynamic logger or a static logger
63      * @param trackAllInvocations whether to track all invocations that go through
64      * this interceptor, or just invocations with trace logging enabled
65      * @see #setUseDynamicLogger
66      */

67     public JamonPerformanceMonitorInterceptor(boolean useDynamicLogger, boolean trackAllInvocations) {
68         setUseDynamicLogger(useDynamicLogger);
69         setTrackAllInvocations(trackAllInvocations);
70     }
71
72
73     /**
74      * Set whether to track all invocations that go through this interceptor,
75      * or just invocations with trace logging enabled.
76      * <p>Default is "false": Only invocations with trace logging enabled will
77      * be monitored. Specify "true" to let JAMon track all invocations,
78      * gathering statistics even when trace logging is disabled.
79      */

80     public void setTrackAllInvocations(boolean trackAllInvocations) {
81         this.trackAllInvocations = trackAllInvocations;
82     }
83
84
85     /**
86      * Always applies the interceptor if the "trackAllInvocations" flag has been set;
87      * else just kicks in if the log is enabled.
88      * @see #setTrackAllInvocations
89      * @see #isLogEnabled
90      */

91     protected boolean isInterceptorEnabled(MethodInvocation invocation, Log logger) {
92         return (this.trackAllInvocations || isLogEnabled(logger));
93     }
94
95     /**
96      * Wraps the invocation with a JAMon Monitor and writes the current
97      * performance statistics to the log (if enabled).
98      * @see com.jamonapi.MonitorFactory#start
99      * @see com.jamonapi.Monitor#stop
100      */

101     protected Object JavaDoc invokeUnderTrace(MethodInvocation invocation, Log logger) throws Throwable JavaDoc {
102         String JavaDoc name = createInvocationTraceName(invocation);
103         Monitor monitor = MonitorFactory.start(name);
104         try {
105             return invocation.proceed();
106         }
107         finally {
108             monitor.stop();
109             if (!this.trackAllInvocations || isLogEnabled(logger)) {
110                 logger.trace("JAMon performance statistics for method [" + name + "]:\n" + monitor);
111             }
112         }
113     }
114
115 }
116
Popular Tags