diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/job/AbstractJob.java b/spring-batch-core/src/main/java/org/springframework/batch/core/job/AbstractJob.java index 8a4d34872..75b4e8a89 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/job/AbstractJob.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/job/AbstractJob.java @@ -16,6 +16,7 @@ package org.springframework.batch.core.job; +import java.util.Collection; import java.util.Date; import org.apache.commons.logging.Log; @@ -136,6 +137,13 @@ public abstract class AbstractJob implements Job, StepLocator, BeanNameAware, In */ public abstract Step getStep(String stepName); + /** + * Retrieve the step names. + * + * @return the step names + */ + public abstract Collection getStepNames(); + /** * Boolean flag to prevent categorically a job from restarting, even if it * has failed previously. diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/job/SimpleJob.java b/spring-batch-core/src/main/java/org/springframework/batch/core/job/SimpleJob.java index 0b61ad259..afb42279a 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/job/SimpleJob.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/job/SimpleJob.java @@ -17,7 +17,7 @@ package org.springframework.batch.core.job; import java.util.ArrayList; -import java.util.Collections; +import java.util.Collection; import java.util.List; import org.springframework.batch.core.BatchStatus; @@ -69,10 +69,14 @@ public class SimpleJob extends AbstractJob { /** * Convenience method for clients to inspect the steps for this job. * - * @return an unmodifiable copy of the steps for this job + * @return the step names for this job */ - public List getSteps() { - return Collections.unmodifiableList(steps); + public Collection getStepNames() { + List names = new ArrayList(); + for (Step step : steps) { + names.add(step.getName()); + } + return names; } /** diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/Flow.java b/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/Flow.java index 8221cc917..28c620208 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/Flow.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/Flow.java @@ -15,10 +15,11 @@ */ package org.springframework.batch.core.job.flow; +import java.util.Collection; /** * @author Dave Syer - * + * */ public interface Flow { @@ -34,8 +35,8 @@ public interface Flow { * @param stateName * @return the State */ - public State getState(String stateName); - + State getState(String stateName); + /** * @throws FlowExecutionException */ @@ -49,4 +50,11 @@ public interface Flow { */ FlowExecution resume(String stateName, FlowExecutor executor) throws FlowExecutionException; + /** + * Convenient accessor for clients needing to explore the states of this + * flow. + * @return the states + */ + Collection getStates(); + } \ No newline at end of file diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/FlowJob.java b/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/FlowJob.java index a94ffbc94..f6c0cb6f3 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/FlowJob.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/FlowJob.java @@ -15,6 +15,9 @@ */ package org.springframework.batch.core.job.flow; +import java.util.Collection; +import java.util.HashSet; + import org.springframework.batch.core.BatchStatus; import org.springframework.batch.core.ExitStatus; import org.springframework.batch.core.JobExecution; @@ -59,12 +62,10 @@ public class FlowJob extends AbstractJob { this.flow = flow; } - /* - * (non-Javadoc) - * - * @see - * org.springframework.batch.core.job.AbstractJob#getStep(java.lang.String) + /** + * {@inheritDoc} */ + @Override public Step getStep(String stepName) { State state = this.flow.getState(stepName); if (state instanceof StepHolder) { @@ -72,6 +73,20 @@ public class FlowJob extends AbstractJob { } return null; } + + /** + * {@inheritDoc} + */ + @Override + public Collection getStepNames() { + Collection steps = new HashSet(); + for (State state: flow.getStates()) { + if (state instanceof StepHolder) { + steps.add(state.getName()); + } + } + return steps; + } /** * @see AbstractJob#doExecute(JobExecution) diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/support/SimpleFlow.java b/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/support/SimpleFlow.java index bddf6ef43..bb42f7bd6 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/support/SimpleFlow.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/support/SimpleFlow.java @@ -16,7 +16,9 @@ package org.springframework.batch.core.job.flow.support; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -83,13 +85,19 @@ public class SimpleFlow implements Flow, InitializingBean { this.stateTransitions = stateTransitions; } - /* - * (non-Javadoc) - * @see org.springframework.batch.core.job.flow.Flow#getState(java.lang.String) + /** + * {@inheritDoc} */ public State getState(String stateName) { return stateMap.get(stateName); } + + /** + * {@inheritDoc} + */ + public Collection getStates() { + return new HashSet(stateMap.values()); + } /** * Locate start state and pre-populate data structures needed for execution. diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/step/StepLocator.java b/spring-batch-core/src/main/java/org/springframework/batch/core/step/StepLocator.java index 6f78f2678..b977e2286 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/step/StepLocator.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/step/StepLocator.java @@ -1,5 +1,7 @@ package org.springframework.batch.core.step; +import java.util.Collection; + import org.springframework.batch.core.Step; /** @@ -10,6 +12,8 @@ import org.springframework.batch.core.Step; */ public interface StepLocator { + Collection getStepNames(); + Step getStep(String stepName); } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/job/AbstractJobTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/job/AbstractJobTests.java index 2926dd582..99766586e 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/job/AbstractJobTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/job/AbstractJobTests.java @@ -15,8 +15,14 @@ */ package org.springframework.batch.core.job; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import java.util.Collection; +import java.util.Collections; import java.util.Date; import org.junit.Test; @@ -174,6 +180,11 @@ public class AbstractJobTests { public Step getStep(String stepName) { return null; } + + @Override + public Collection getStepNames() { + return Collections. emptySet(); + } } diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/job/SimpleJobTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/job/SimpleJobTests.java index 58a05fd27..869b4f229 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/job/SimpleJobTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/job/SimpleJobTests.java @@ -147,6 +147,14 @@ public class SimpleJobTests { assertEquals(1, jobExecution.getStepExecutions().size()); } + /** + * Test method for {@link SimpleJob#setSteps(java.util.List)}. + */ + @Test + public void testGetSteps() { + assertEquals(2, job.getStepNames().size()); + } + /** * Test method for * {@link SimpleJob#addStep(org.springframework.batch.core.Step)}. diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/job/flow/FlowJobTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/job/flow/FlowJobTests.java index dab1e99e5..8f3875c9b 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/job/flow/FlowJobTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/job/flow/FlowJobTests.java @@ -67,6 +67,20 @@ public class FlowJobTests { jobExecution = jobRepository.createJobExecution("job", new JobParameters()); } + @Test + public void testGetSteps() throws Exception { + SimpleFlow flow = new SimpleFlow("job"); + List transitions = new ArrayList(); + transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step1")), "step2")); + transitions.add(StateTransition.createStateTransition(new StepState(new StubStep("step2")), "end0")); + transitions.add(StateTransition.createEndStateTransition(new EndState(FlowExecutionStatus.COMPLETED, "end0"))); + flow.setStateTransitions(transitions); + flow.afterPropertiesSet(); + job.setFlow(flow); + job.afterPropertiesSet(); + assertEquals(2, job.getStepNames().size()); + } + @Test public void testTwoSteps() throws Exception { SimpleFlow flow = new SimpleFlow("job");