KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tirsen > nanning > profiler > ProfilerInterceptor


1 package com.tirsen.nanning.profiler;
2
3 import com.tirsen.nanning.Invocation;
4 import com.tirsen.nanning.MethodInterceptor;
5
6 import java.lang.reflect.Method JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.Map JavaDoc;
9
10
11 public class ProfilerInterceptor implements MethodInterceptor {
12     static ThreadLocal JavaDoc threadLocal = new ThreadLocal JavaDoc();
13
14     private static long minDuration = 0;
15
16     public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc {
17
18         Map JavaDoc methodsStartingTime = (Map JavaDoc) threadLocal.get();
19         if (methodsStartingTime == null) {
20             methodsStartingTime = new HashMap JavaDoc();
21             threadLocal.set(methodsStartingTime);
22         }
23         Method JavaDoc method = invocation.getMethod();
24         methodsStartingTime.put(method, new Long JavaDoc(System.currentTimeMillis()));
25         Object JavaDoc result = invocation.invokeNext();
26         long duration = System.currentTimeMillis() - ((Long JavaDoc) methodsStartingTime.get(method)).longValue();
27         if (duration >= minDuration) ProfilerLogger.getProfilerLogger().log(invocation, duration);
28         return result;
29     }
30
31     public static long getMinDuration() {
32         return minDuration;
33     }
34
35     public static void setMinDuration(long l) {
36         minDuration = l;
37     }
38
39 }
40
Popular Tags