View Javadoc

1   /*
2    * Copyright 2006-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.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  	@Override
46  	public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
47  		contribution.setExitStatus(mapResult(invokeDelegateMethod()));
48  		return RepeatStatus.FINISHED;
49  	}
50  
51  	/**
52  	 * If the result is an {@link ExitStatus} already just return that,
53  	 * otherwise return {@link ExitStatus#COMPLETED}.
54  	 *
55  	 * @param result the value returned by the delegate method
56  	 * @return an {@link ExitStatus} consistent with the result
57  	 */
58  	protected ExitStatus mapResult(Object result) {
59  		if (result instanceof ExitStatus) {
60  			return (ExitStatus) result;
61  		}
62  		return ExitStatus.COMPLETED;
63  	}
64  
65  }