001/* 002 * Copyright 2006-2014 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 */ 016 017package org.springframework.batch.core.job.flow.support.state; 018 019import java.util.ArrayList; 020import java.util.Collection; 021import java.util.List; 022 023import org.springframework.batch.core.Step; 024import org.springframework.batch.core.job.flow.FlowExecutionStatus; 025import org.springframework.batch.core.job.flow.FlowExecutor; 026import org.springframework.batch.core.job.flow.State; 027import org.springframework.batch.core.step.NoSuchStepException; 028import org.springframework.batch.core.step.StepHolder; 029import org.springframework.batch.core.step.StepLocator; 030 031/** 032 * {@link State} implementation that delegates to a {@link FlowExecutor} to 033 * execute the specified {@link Step}. 034 * 035 * @author Dave Syer 036 * @author Michael Minella 037 * @since 2.0 038 */ 039public class StepState extends AbstractState implements StepLocator, StepHolder { 040 041 private final Step step; 042 043 /** 044 * @param step the step that will be executed 045 */ 046 public StepState(Step step) { 047 super(step.getName()); 048 this.step = step; 049 } 050 051 /** 052 * @param name for the step that will be executed 053 * @param step the step that will be executed 054 */ 055 public StepState(String name, Step step) { 056 super(name); 057 this.step = step; 058 } 059 060 @Override 061 public FlowExecutionStatus handle(FlowExecutor executor) throws Exception { 062 /* 063 * On starting a new step, possibly upgrade the last execution to make 064 * sure it is abandoned on restart if it failed. 065 */ 066 executor.abandonStepExecution(); 067 return new FlowExecutionStatus(executor.executeStep(step)); 068 } 069 070 @Override 071 public Step getStep() { 072 return step; 073 } 074 075 /* (non-Javadoc) 076 * @see org.springframework.batch.core.job.flow.State#isEndState() 077 */ 078 @Override 079 public boolean isEndState() { 080 return false; 081 } 082 083 /* (non-Javadoc) 084 * @see org.springframework.batch.core.step.StepLocator#getStepNames() 085 */ 086 @Override 087 public Collection<String> getStepNames() { 088 List<String> names = new ArrayList<String>(); 089 090 names.add(step.getName()); 091 092 if(step instanceof StepLocator) { 093 names.addAll(((StepLocator)step).getStepNames()); 094 } 095 096 return names; 097 } 098 099 /* (non-Javadoc) 100 * @see org.springframework.batch.core.step.StepLocator#getStep(java.lang.String) 101 */ 102 @Override 103 public Step getStep(String stepName) throws NoSuchStepException { 104 Step result = null; 105 106 if(step.getName().equals(stepName)) { 107 result = step; 108 } else if(step instanceof StepLocator) { 109 result = ((StepLocator) step).getStep(stepName); 110 } 111 112 return result; 113 } 114}