001/* 002 * Copyright 2006-2018 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.springframework.batch.core.step.tasklet; 017 018import org.springframework.batch.core.ExitStatus; 019import org.springframework.batch.core.StepContribution; 020import org.springframework.batch.core.scope.context.ChunkContext; 021import org.springframework.batch.item.adapter.AbstractMethodInvokingDelegator; 022import org.springframework.batch.repeat.RepeatStatus; 023 024/** 025 * A {@link Tasklet} that wraps a method in a POJO. By default the return 026 * value is {@link ExitStatus#COMPLETED} unless the delegate POJO itself returns 027 * an {@link ExitStatus}. The POJO method is usually going to have no arguments, 028 * but a static argument or array of arguments can be used by setting the 029 * arguments property. 030 * 031 * @see AbstractMethodInvokingDelegator 032 * 033 * @author Dave Syer 034 * @author Mahmoud Ben Hassine 035 * 036 */ 037public class MethodInvokingTaskletAdapter extends AbstractMethodInvokingDelegator<Object> implements Tasklet { 038 039 /** 040 * Delegate execution to the target object and translate the return value to 041 * an {@link ExitStatus} by invoking a method in the delegate POJO. Ignores 042 * the {@link StepContribution} and the attributes. 043 * 044 * @see Tasklet#execute(StepContribution, ChunkContext) 045 */ 046 @Override 047 public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { 048 if (getArguments() == null) { 049 setArguments(new Object[]{contribution, chunkContext}); 050 } 051 contribution.setExitStatus(mapResult(invokeDelegateMethod())); 052 return RepeatStatus.FINISHED; 053 } 054 055 /** 056 * If the result is an {@link ExitStatus} already just return that, 057 * otherwise return {@link ExitStatus#COMPLETED}. 058 * 059 * @param result the value returned by the delegate method 060 * @return an {@link ExitStatus} consistent with the result 061 */ 062 protected ExitStatus mapResult(Object result) { 063 if (result instanceof ExitStatus) { 064 return (ExitStatus) result; 065 } 066 return ExitStatus.COMPLETED; 067 } 068 069}