1 | /* |
2 | * Copyright 2002-2008 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 | public Object invoke(MethodInvocation invocation) throws Throwable { |
55 | |
56 | String methodName = invocation.getMethod().getName(); |
57 | if (ordered && methodName.equals("getOrder")) { |
58 | return invocation.proceed(); |
59 | } |
60 | |
61 | Set<MethodInvoker> invokers = invokerMap.get(methodName); |
62 | |
63 | if (invokers == null) { |
64 | return null; |
65 | } |
66 | ExitStatus status = null; |
67 | for (MethodInvoker invoker : invokers) { |
68 | Object retVal = invoker.invokeMethod(invocation.getArguments()); |
69 | if (retVal instanceof ExitStatus) { |
70 | if (status != null) { |
71 | status = status.and((ExitStatus) retVal); |
72 | } |
73 | else { |
74 | status = (ExitStatus) retVal; |
75 | } |
76 | } |
77 | } |
78 | |
79 | // The only possible return values are ExitStatus or int (from Ordered) |
80 | return status; |
81 | } |
82 | |
83 | /** |
84 | * {@inheritDoc} |
85 | */ |
86 | @Override |
87 | public boolean equals(Object obj) { |
88 | if (!(obj instanceof MethodInvokerMethodInterceptor)) { |
89 | return false; |
90 | } |
91 | MethodInvokerMethodInterceptor other = (MethodInvokerMethodInterceptor) obj; |
92 | return invokerMap.equals(other.invokerMap); |
93 | } |
94 | |
95 | /** |
96 | * {@inheritDoc} |
97 | */ |
98 | @Override |
99 | public int hashCode() { |
100 | return invokerMap.hashCode(); |
101 | } |
102 | |
103 | } |