KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > util > perf > PerformanceMonitorAdvice


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.util.perf;
18
19 import org.aopalliance.intercept.MethodInterceptor;
20 import org.aopalliance.intercept.MethodInvocation;
21
22 import com.vladium.utils.timing.ITimer;
23 import com.vladium.utils.timing.TimerFactory;
24
25 /**
26  * An instance of this class keeps track of timings of method calls on a bean
27  *
28  * @author Derek Hulley
29  */

30 public class PerformanceMonitorAdvice extends AbstractPerformanceMonitor implements MethodInterceptor
31 {
32     public PerformanceMonitorAdvice(String JavaDoc beanName)
33     {
34         super(beanName);
35     }
36     
37     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc
38     {
39         // bypass all recording if performance logging is not required
40
if (AbstractPerformanceMonitor.isDebugEnabled())
41         {
42             return invokeWithLogging(invocation);
43         }
44         else
45         {
46             // no logging required
47
return invocation.proceed();
48         }
49     }
50     
51     private Object JavaDoc invokeWithLogging(MethodInvocation invocation) throws Throwable JavaDoc
52     {
53         // get the time prior to call
54
ITimer timer = TimerFactory.newTimer ();
55         
56         timer.start ();
57
58         //long start = System.currentTimeMillis();
59
// execute - do not record exceptions
60
Object JavaDoc ret = invocation.proceed();
61         // get time after call
62
//long end = System.currentTimeMillis();
63
// record the stats
64
timer.stop ();
65        
66         recordStats(invocation.getMethod().getName(), timer.getDuration ());
67         // done
68
return ret;
69     }
70 }
71
Popular Tags