1 | /* |
2 | * Copyright 2002-2013 the original author or authors. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
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 | * {@link MethodInterceptor} that, given a map of method names and |
29 | * {@link MethodInvoker}s, will execute all methods tied to a particular method |
30 | * name, with the provided arguments. The only possible return value that is |
31 | * handled is of type ExitStatus, since the only StepListener implementation |
32 | * that isn't void is |
33 | * {@link StepExecutionListener#afterStep(org.springframework.batch.core.StepExecution)} |
34 | * , which returns ExitStatus. |
35 | * |
36 | * @author Lucas Ward |
37 | * @since 2.0 |
38 | * @see MethodInvoker |
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 | // The only possible return values are ExitStatus or int (from Ordered) |
81 | return status; |
82 | } |
83 | |
84 | /** |
85 | * {@inheritDoc} |
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 | * {@inheritDoc} |
98 | */ |
99 | @Override |
100 | public int hashCode() { |
101 | return invokerMap.hashCode(); |
102 | } |
103 | |
104 | } |