1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.springframework.batch.core.listener;
17
18 import java.util.Map;
19 import java.util.Set;
20
21 import org.aopalliance.intercept.MethodInterceptor;
22 import org.aopalliance.intercept.MethodInvocation;
23 import org.springframework.batch.core.ExitStatus;
24 import org.springframework.batch.core.StepExecutionListener;
25 import org.springframework.batch.support.MethodInvoker;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 public class MethodInvokerMethodInterceptor implements MethodInterceptor {
41
42 private final Map<String, Set<MethodInvoker>> invokerMap;
43 private final boolean ordered;
44
45 public MethodInvokerMethodInterceptor(Map<String, Set<MethodInvoker>> invokerMap) {
46 this(invokerMap, false);
47 }
48
49 public MethodInvokerMethodInterceptor(Map<String, Set<MethodInvoker>> invokerMap, boolean ordered) {
50 this.ordered = ordered;
51 this.invokerMap = invokerMap;
52 }
53
54 @Override
55 public Object invoke(MethodInvocation invocation) throws Throwable {
56
57 String methodName = invocation.getMethod().getName();
58 if (ordered && methodName.equals("getOrder")) {
59 return invocation.proceed();
60 }
61
62 Set<MethodInvoker> invokers = invokerMap.get(methodName);
63
64 if (invokers == null) {
65 return null;
66 }
67 ExitStatus status = null;
68 for (MethodInvoker invoker : invokers) {
69 Object retVal = invoker.invokeMethod(invocation.getArguments());
70 if (retVal instanceof ExitStatus) {
71 if (status != null) {
72 status = status.and((ExitStatus) retVal);
73 }
74 else {
75 status = (ExitStatus) retVal;
76 }
77 }
78 }
79
80
81 return status;
82 }
83
84
85
86
87 @Override
88 public boolean equals(Object obj) {
89 if (!(obj instanceof MethodInvokerMethodInterceptor)) {
90 return false;
91 }
92 MethodInvokerMethodInterceptor other = (MethodInvokerMethodInterceptor) obj;
93 return invokerMap.equals(other.invokerMap);
94 }
95
96
97
98
99 @Override
100 public int hashCode() {
101 return invokerMap.hashCode();
102 }
103
104 }