From fe5828d3e56b9774cc88116f8ca90ee35858b13b Mon Sep 17 00:00:00 2001 From: dsyer Date: Wed, 10 Dec 2008 12:03:09 +0000 Subject: [PATCH] RESOLVED - issue BATCH-945: Add support for step and job name in late binding Also added some null checks in SimpleJobExplorer --- spring-batch-core/.classpath | 1 + spring-batch-core/.project | 13 +++++++ .../org.eclipse.wst.common.component | 9 +++++ ....eclipse.wst.common.project.facet.core.xml | 7 ++++ .../explore/support/SimpleJobExplorer.java | 6 +++ .../batch/core/scope/context/StepContext.java | 38 ++++++++++++++++++- .../support/SimpleJobExplorerTests.java | 8 ++++ .../core/scope/context/StepContextTests.java | 18 ++++++++- spring-batch-infrastructure/.classpath | 1 + spring-batch-infrastructure/.project | 7 ++++ .../org.eclipse.wst.common.component | 9 +++++ ....eclipse.wst.common.project.facet.core.xml | 10 +++-- spring-batch-samples/foo.txt | 1 - 13 files changed, 122 insertions(+), 6 deletions(-) create mode 100644 spring-batch-core/.settings/org.eclipse.wst.common.component create mode 100644 spring-batch-core/.settings/org.eclipse.wst.common.project.facet.core.xml create mode 100644 spring-batch-infrastructure/.settings/org.eclipse.wst.common.component delete mode 100644 spring-batch-samples/foo.txt diff --git a/spring-batch-core/.classpath b/spring-batch-core/.classpath index b6d9ce7c7..6be6dfa2c 100644 --- a/spring-batch-core/.classpath +++ b/spring-batch-core/.classpath @@ -6,5 +6,6 @@ + diff --git a/spring-batch-core/.project b/spring-batch-core/.project index aee145583..9b4496e64 100644 --- a/spring-batch-core/.project +++ b/spring-batch-core/.project @@ -6,6 +6,11 @@ spring-batch-infrastructure + + org.eclipse.wst.common.project.facet.core.builder + + + org.eclipse.jdt.core.javabuilder @@ -21,10 +26,18 @@ + + org.eclipse.wst.validation.validationbuilder + + + + org.eclipse.jem.workbench.JavaEMFNature + org.eclipse.wst.common.modulecore.ModuleCoreNature org.springframework.ide.eclipse.core.springnature org.eclipse.jdt.core.javanature org.devzuz.q.maven.jdt.core.mavenNature + org.eclipse.wst.common.project.facet.core.nature diff --git a/spring-batch-core/.settings/org.eclipse.wst.common.component b/spring-batch-core/.settings/org.eclipse.wst.common.component new file mode 100644 index 000000000..7ac42d5f1 --- /dev/null +++ b/spring-batch-core/.settings/org.eclipse.wst.common.component @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/spring-batch-core/.settings/org.eclipse.wst.common.project.facet.core.xml b/spring-batch-core/.settings/org.eclipse.wst.common.project.facet.core.xml new file mode 100644 index 000000000..8de16aa85 --- /dev/null +++ b/spring-batch-core/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/explore/support/SimpleJobExplorer.java b/spring-batch-core/src/main/java/org/springframework/batch/core/explore/support/SimpleJobExplorer.java index d39b0ab3e..9a9fc87ec 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/explore/support/SimpleJobExplorer.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/explore/support/SimpleJobExplorer.java @@ -91,6 +91,9 @@ public class SimpleJobExplorer implements JobExplorer { */ public JobExecution getJobExecution(Long executionId) { JobExecution jobExecution = jobExecutionDao.getJobExecution(executionId); + if (jobExecution==null) { + return null; + } getJobExecutionDependencies(jobExecution); return jobExecution; } @@ -100,6 +103,9 @@ public class SimpleJobExplorer implements JobExplorer { */ public StepExecution getStepExecution(Long executionId, String stepName) { JobExecution jobExecution = getJobExecution(executionId); + if (jobExecution==null) { + return null; + } return stepExecutionDao.getStepExecution(jobExecution, stepName); } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/scope/context/StepContext.java b/spring-batch-core/src/main/java/org/springframework/batch/core/scope/context/StepContext.java index 067be85ee..cef8949ac 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/scope/context/StepContext.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/scope/context/StepContext.java @@ -21,9 +21,11 @@ import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Properties; import java.util.Set; import java.util.Map.Entry; +import org.springframework.batch.core.JobInstance; import org.springframework.batch.core.JobParameter; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.StepExecution; @@ -57,6 +59,40 @@ public class StepContext extends SynchronizedAttributeAccessor { this.stepExecution = stepExecution; } + /** + * Convenient accessor for current step name identifier. Usually this is the + * same as the bean name of the step that is executing (but might not be + * e.g. in a partition). + * + * @return the step name identifier of the current {@link StepExecution} + */ + public String getStepName() { + return stepExecution.getStepName(); + } + + /** + * Convenient accessor for current job name identifier. + * + * @return the job name identifier of the enclosing {@link JobInstance} + * associated with the current {@link StepExecution} + */ + public String getJobName() { + Assert.state(stepExecution.getJobExecution() != null, "StepExecution does not have a JobExecution"); + Assert.state(stepExecution.getJobExecution().getJobInstance() != null, + "StepExecution does not have a JobInstance"); + return stepExecution.getJobExecution().getJobInstance().getJobName(); + } + + /** + * Convenient accessor for System properties to make it easy to access them + * from placeholder expressions. + * + * @return the current System properties + */ + public Properties getSystemProperties() { + return System.getProperties(); + } + /** * @return a map containing the items from the step {@link ExecutionContext} */ @@ -136,7 +172,7 @@ public class StepContext extends SynchronizedAttributeAccessor { Map> copy = Collections.unmodifiableMap(callbacks); - for(Entry> entry : copy.entrySet()) { + for (Entry> entry : copy.entrySet()) { Set set = entry.getValue(); for (Runnable callback : set) { if (callback != null) { diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/exlore/support/SimpleJobExplorerTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/exlore/support/SimpleJobExplorerTests.java index e5b788ccf..6900a2209 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/exlore/support/SimpleJobExplorerTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/exlore/support/SimpleJobExplorerTests.java @@ -72,6 +72,14 @@ public class SimpleJobExplorerTests extends TestCase { verify(jobExecutionDao, jobInstanceDao, stepExecutionDao); } + @Test + public void testMissingGetJobExecution() throws Exception { + expect(jobExecutionDao.getJobExecution(123L)).andReturn(null); + replay(jobExecutionDao); + assertNull(jobExplorer.getJobExecution(123L)); + verify(jobExecutionDao); + } + @Test public void testGetStepExecution() throws Exception { expect(jobExecutionDao.getJobExecution(123L)).andReturn(jobExecution); diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/scope/context/StepContextTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/scope/context/StepContextTests.java index d1286340e..caf755340 100644 --- a/spring-batch-core/src/test/java/org/springframework/batch/core/scope/context/StepContextTests.java +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/scope/context/StepContextTests.java @@ -41,7 +41,7 @@ public class StepContextTests { private List list = new ArrayList(); - private StepExecution stepExecution = new StepExecution("step", new JobExecution(0L), 1L); + private StepExecution stepExecution = new StepExecution("step", new JobExecution(new JobInstance(2L, null, "job"), 0L), 1L); private StepContext context = new StepContext(stepExecution); @@ -133,6 +133,16 @@ public class StepContextTests { assertTrue(list.contains("spam")); } + @Test + public void testStepName() throws Exception { + assertEquals("step", context.getStepName()); + } + + @Test + public void testJobName() throws Exception { + assertEquals("job", context.getJobName()); + } + @Test public void testStepExecutionContext() throws Exception { ExecutionContext executionContext = stepExecution.getExecutionContext(); @@ -140,6 +150,12 @@ public class StepContextTests { assertEquals("bar", context.getStepExecutionContext().get("foo")); } + @Test + public void testSystemProperties() throws Exception { + System.setProperty("foo", "bar"); + assertEquals("bar", context.getSystemProperties().getProperty("foo")); + } + @Test public void testJobExecutionContext() throws Exception { ExecutionContext executionContext = stepExecution.getJobExecution().getExecutionContext(); diff --git a/spring-batch-infrastructure/.classpath b/spring-batch-infrastructure/.classpath index 9dc3052d5..0d0c8ed26 100644 --- a/spring-batch-infrastructure/.classpath +++ b/spring-batch-infrastructure/.classpath @@ -6,5 +6,6 @@ + diff --git a/spring-batch-infrastructure/.project b/spring-batch-infrastructure/.project index 079c0065c..2ffefcc81 100644 --- a/spring-batch-infrastructure/.project +++ b/spring-batch-infrastructure/.project @@ -25,8 +25,15 @@ + + org.eclipse.wst.validation.validationbuilder + + + + org.eclipse.jem.workbench.JavaEMFNature + org.eclipse.wst.common.modulecore.ModuleCoreNature org.springframework.ide.eclipse.core.springnature org.eclipse.jdt.core.javanature org.devzuz.q.maven.jdt.core.mavenNature diff --git a/spring-batch-infrastructure/.settings/org.eclipse.wst.common.component b/spring-batch-infrastructure/.settings/org.eclipse.wst.common.component new file mode 100644 index 000000000..403cf4ec2 --- /dev/null +++ b/spring-batch-infrastructure/.settings/org.eclipse.wst.common.component @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/spring-batch-infrastructure/.settings/org.eclipse.wst.common.project.facet.core.xml b/spring-batch-infrastructure/.settings/org.eclipse.wst.common.project.facet.core.xml index 88ceb05cc..8de16aa85 100644 --- a/spring-batch-infrastructure/.settings/org.eclipse.wst.common.project.facet.core.xml +++ b/spring-batch-infrastructure/.settings/org.eclipse.wst.common.project.facet.core.xml @@ -1,3 +1,7 @@ - - - + + + + + + + diff --git a/spring-batch-samples/foo.txt b/spring-batch-samples/foo.txt deleted file mode 100644 index e965047ad..000000000 --- a/spring-batch-samples/foo.txt +++ /dev/null @@ -1 +0,0 @@ -Hello