1 | /* |
2 | * Copyright 2006-2007 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.step.tasklet; |
17 | |
18 | import org.springframework.batch.core.ExitStatus; |
19 | import org.springframework.batch.core.StepContribution; |
20 | import org.springframework.batch.core.scope.context.ChunkContext; |
21 | import org.springframework.batch.item.adapter.AbstractMethodInvokingDelegator; |
22 | import org.springframework.batch.repeat.RepeatStatus; |
23 | |
24 | /** |
25 | * A {@link Tasklet} that wraps a method in a POJO. By default the return |
26 | * value is {@link ExitStatus#COMPLETED} unless the delegate POJO itself returns |
27 | * an {@link ExitStatus}. The POJO method is usually going to have no arguments, |
28 | * but a static argument or array of arguments can be used by setting the |
29 | * arguments property. |
30 | * |
31 | * @see AbstractMethodInvokingDelegator |
32 | * |
33 | * @author Dave Syer |
34 | * |
35 | */ |
36 | public class MethodInvokingTaskletAdapter extends AbstractMethodInvokingDelegator<Object> implements Tasklet { |
37 | |
38 | /** |
39 | * Delegate execution to the target object and translate the return value to |
40 | * an {@link ExitStatus} by invoking a method in the delegate POJO. Ignores |
41 | * the {@link StepContribution} and the attributes. |
42 | * |
43 | * @see Tasklet#execute(StepContribution, ChunkContext) |
44 | */ |
45 | public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { |
46 | contribution.setExitStatus(mapResult(invokeDelegateMethod())); |
47 | return RepeatStatus.FINISHED; |
48 | } |
49 | |
50 | /** |
51 | * If the result is an {@link ExitStatus} already just return that, |
52 | * otherwise return {@link ExitStatus#COMPLETED}. |
53 | * |
54 | * @param result the value returned by the delegate method |
55 | * @return an {@link ExitStatus} consistent with the result |
56 | */ |
57 | protected ExitStatus mapResult(Object result) { |
58 | if (result instanceof ExitStatus) { |
59 | return (ExitStatus) result; |
60 | } |
61 | return ExitStatus.COMPLETED; |
62 | } |
63 | |
64 | } |