1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| package org.jboss.cache.pojo.interceptors; |
9 |
| |
10 |
| import org.apache.commons.logging.Log; |
11 |
| import org.apache.commons.logging.LogFactory; |
12 |
| import org.jboss.aop.advice.Interceptor; |
13 |
| import org.jboss.aop.joinpoint.Invocation; |
14 |
| import org.jboss.aop.joinpoint.MethodInvocation; |
15 |
| import org.jboss.cache.pojo.PojoCacheException; |
16 |
| |
17 |
| import java.lang.reflect.Method; |
18 |
| |
19 |
| |
20 |
| |
21 |
| |
22 |
| |
23 |
| public class MethodReentrancyStopperInterceptor implements Interceptor |
24 |
| { |
25 |
| private final Log log_ = LogFactory.getLog(MethodReentrancyStopperInterceptor.class); |
26 |
| private ThreadLocal<Boolean> done; |
27 |
| private String methodName; |
28 |
| |
29 |
48
| public MethodReentrancyStopperInterceptor()
|
30 |
| { |
31 |
48
| done = new ThreadLocal<Boolean>();
|
32 |
48
| done.set(false);
|
33 |
| } |
34 |
| |
35 |
48
| public void setMethodName(String mname)
|
36 |
| { |
37 |
48
| methodName = mname;
|
38 |
| } |
39 |
| |
40 |
0
| public String getName()
|
41 |
| { |
42 |
0
| return MethodReentrancyStopperInterceptor.class.getName() + "-" + methodName;
|
43 |
| } |
44 |
| |
45 |
41
| public Object invoke(Invocation invocation) throws Throwable
|
46 |
| { |
47 |
41
| boolean wasDone = done.get();
|
48 |
| |
49 |
41
| try
|
50 |
| { |
51 |
41
| if (!wasDone)
|
52 |
| { |
53 |
37
| done.set(true);
|
54 |
37
| return invocation.invokeNext();
|
55 |
| } |
56 |
| else |
57 |
| { |
58 |
| |
59 |
4
| if (log_.isDebugEnabled())
|
60 |
| { |
61 |
0
| Method method = ((MethodInvocation) invocation).getMethod();
|
62 |
0
| log_.debug("Detect recursive interception. Will call the target directly: " + method.getName());
|
63 |
| } |
64 |
| |
65 |
4
| if (methodName.equals("toString"))
|
66 |
| { |
67 |
4
| return invocation.getTargetObject().getClass().getName();
|
68 |
| } |
69 |
0
| else if (methodName.equals("hashCode"))
|
70 |
| { |
71 |
0
| return 0;
|
72 |
| } |
73 |
| else |
74 |
| { |
75 |
0
| throw new PojoCacheException("MethodReentrancyStopperInterceptor.invoke(): unknown method name"
|
76 |
| + methodName); |
77 |
| } |
78 |
| } |
79 |
| } |
80 |
| finally |
81 |
| { |
82 |
41
| if (!wasDone)
|
83 |
| { |
84 |
37
| done.set(false);
|
85 |
| } |
86 |
| } |
87 |
| } |
88 |
| } |