1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.apache.onami.autobind.aop; |
17 | |
|
18 | |
import java.lang.reflect.Method; |
19 | |
import java.util.logging.Level; |
20 | |
import java.util.logging.Logger; |
21 | |
|
22 | |
import javax.interceptor.Interceptor; |
23 | |
|
24 | |
import org.aopalliance.intercept.MethodInvocation; |
25 | |
|
26 | |
import com.google.inject.matcher.Matcher; |
27 | |
import com.google.inject.matcher.Matchers; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
@Interceptor |
36 | 0 | public class MethodCallingInterceptor { |
37 | 0 | private Logger _logger = Logger.getLogger(MethodCallingInterceptor.class.getName()); |
38 | |
|
39 | |
@Invoke |
40 | |
public Object invoke(MethodInvocation invocation) throws Throwable { |
41 | 0 | Object destination = invocation.getThis(); |
42 | 0 | StringBuilder logMessageBuilder = new StringBuilder(250); |
43 | |
|
44 | 0 | logMessageBuilder.append("Invoking Method \""); |
45 | 0 | logMessageBuilder.append(invocation.getMethod().getName()); |
46 | 0 | logMessageBuilder.append("\" on "); |
47 | 0 | logMessageBuilder.append(destination.getClass().getName()); |
48 | 0 | logMessageBuilder.append(" with Arguments: "); |
49 | |
|
50 | 0 | Class<?>[] types = invocation.getMethod().getParameterTypes(); |
51 | 0 | Object[] parameters = invocation.getArguments(); |
52 | |
|
53 | 0 | for (int i = 0; i < types.length; i++) { |
54 | 0 | Object parameter = parameters[i]; |
55 | 0 | Class<?> type = types[i]; |
56 | |
|
57 | 0 | logMessageBuilder.append(" \""); |
58 | 0 | logMessageBuilder.append(type.getSimpleName()); |
59 | 0 | logMessageBuilder.append("\": "); |
60 | 0 | logMessageBuilder.append(parameter); |
61 | |
} |
62 | 0 | _logger.log(Level.SEVERE, logMessageBuilder.toString()); |
63 | |
|
64 | 0 | return invocation.proceed(); |
65 | |
} |
66 | |
|
67 | |
@ClassMatcher |
68 | |
public Matcher<? super Class<?>> getClassMatcher() { |
69 | 0 | return Matchers.any(); |
70 | |
} |
71 | |
|
72 | |
@MethodMatcher |
73 | |
public Matcher<? super Method> getMethodMatcher() { |
74 | 0 | return Matchers.annotatedWith(Intercept.class); |
75 | |
} |
76 | |
|
77 | |
} |