diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/StepParser.java b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/StepParser.java index 50f09e3bb..8a1643e1b 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/StepParser.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/configuration/xml/StepParser.java @@ -30,6 +30,7 @@ import org.springframework.beans.factory.config.RuntimeBeanReference; import org.springframework.beans.factory.parsing.BeanComponentDefinition; import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.beans.factory.xml.ParserContext; import org.springframework.util.StringUtils; import org.springframework.util.xml.DomUtils; @@ -44,6 +45,7 @@ import org.w3c.dom.Element; * @see JobParser * * @author Dave Syer + * @author Thomas Risberg * */ public class StepParser { @@ -61,9 +63,27 @@ public class StepParser { */ public Collection parse(Element element, ParserContext parserContext) { - RuntimeBeanReference stateDef = new RuntimeBeanReference(element.getAttribute("name")); BeanDefinitionBuilder stateBuilder = BeanDefinitionBuilder.genericBeanDefinition(StepState.class); - stateBuilder.addConstructorArgValue(stateDef); + String stepRef = element.getAttribute("name"); + + @SuppressWarnings("unchecked") + List taskElements = (List) DomUtils.getChildElementsByTagName(element, "task"); + if (taskElements.size() > 0) { + //TaskParser taskParser = new TaskParser(); +// Object task = taskParser.parse(taskElements.get(0), parserContext); + Object task = parseTask(taskElements.get(0), parserContext); + stateBuilder.addConstructorArgValue(stepRef); + stateBuilder.addConstructorArgValue(task); + } + else { + if (StringUtils.hasText(stepRef)) { + RuntimeBeanReference stateDef = new RuntimeBeanReference(stepRef); + stateBuilder.addConstructorArgValue(stateDef); + } + else { + throw new BeanCreationException("Error creating Step for " + element); + } + } return getNextElements(parserContext, stateBuilder.getBeanDefinition(), element); } @@ -165,4 +185,32 @@ public class StepParser { } + /** + * @param element + * @param parserContext + * @return the TaskletStep bean + */ + protected RootBeanDefinition parseTask(Element element, ParserContext parserContext) { + + RootBeanDefinition bd = new RootBeanDefinition("org.springframework.batch.core.step.tasklet.TaskletStep", null, null); + + String taskletBeanId = element.getAttribute("tasklet"); + if (StringUtils.hasText(taskletBeanId)) { + RuntimeBeanReference taskletRef = new RuntimeBeanReference(taskletBeanId); + bd.getPropertyValues().addPropertyValue("tasklet", taskletRef); + } + String jobRepository = element.getAttribute("job-repository"); + RuntimeBeanReference jobRepositoryRef = new RuntimeBeanReference(jobRepository); + bd.getPropertyValues().addPropertyValue("jobRepository", jobRepositoryRef); + + String transactionManager = element.getAttribute("transaction-manager"); + RuntimeBeanReference tx = new RuntimeBeanReference(transactionManager); + bd.getPropertyValues().addPropertyValue("transactionManager", tx); + + bd.setRole(BeanDefinition.ROLE_SUPPORT); + + return bd; + + } + } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/support/state/StepState.java b/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/support/state/StepState.java index e888ce188..9e6678552 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/support/state/StepState.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/job/flow/support/state/StepState.java @@ -23,6 +23,15 @@ public class StepState extends AbstractState { this.step = step; } + /** + * @param name for the step that will be executed + * @param step the step that will be executed + */ + public StepState(String name, Step step) { + super(name); + this.step = step; + } + @Override public String handle(FlowExecutor executor) throws Exception { return executor.executeStep(step); diff --git a/spring-batch-core/src/main/resources/org/springframework/batch/core/configuration/xml/spring-batch-2.0.xsd b/spring-batch-core/src/main/resources/org/springframework/batch/core/configuration/xml/spring-batch-2.0.xsd index 038912e5f..db84db732 100644 --- a/spring-batch-core/src/main/resources/org/springframework/batch/core/configuration/xml/spring-batch-2.0.xsd +++ b/spring-batch-core/src/main/resources/org/springframework/batch/core/configuration/xml/spring-batch-2.0.xsd @@ -128,7 +128,7 @@ - + @@ -187,22 +187,89 @@ - + - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -282,6 +349,6 @@ - + \ No newline at end of file diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/DummyTasklet.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/DummyTasklet.java new file mode 100644 index 000000000..abeb34114 --- /dev/null +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/DummyTasklet.java @@ -0,0 +1,15 @@ +package org.springframework.batch.core.configuration.xml; + +import org.springframework.batch.core.StepContribution; +import org.springframework.batch.core.step.tasklet.Tasklet; +import org.springframework.batch.repeat.ExitStatus; +import org.springframework.core.AttributeAccessor; + +public class DummyTasklet implements Tasklet { + + public ExitStatus execute(StepContribution contribution, + AttributeAccessor attributes) throws Exception { + return ExitStatus.FINISHED; + } + +} diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/StepWithTaskJobParserTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/StepWithTaskJobParserTests.java new file mode 100644 index 000000000..133659667 --- /dev/null +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/configuration/xml/StepWithTaskJobParserTests.java @@ -0,0 +1,62 @@ +/* + * Copyright 2006-2007 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.core.configuration.xml; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.batch.core.BatchStatus; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.JobExecution; +import org.springframework.batch.core.JobParameters; +import org.springframework.batch.core.repository.JobRepository; +import org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + + +/** + * @author Dave Syer + * + */ +@ContextConfiguration +@RunWith(SpringJUnit4ClassRunner.class) +public class StepWithTaskJobParserTests { + + @Autowired + private Job job; + + @Autowired + private JobRepository jobRepository; + + @Before + public void setUp() { + MapJobRepositoryFactoryBean.clear(); + } + + @Test + public void testStepWithTask() throws Exception { + assertNotNull(job); + JobExecution jobExecution = jobRepository.createJobExecution(job.getName(), new JobParameters()); + job.execute(jobExecution); + assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus()); + assertEquals(2, jobExecution.getStepExecutions().size()); + } +} diff --git a/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/StepWithTaskJobParserTests-context.xml b/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/StepWithTaskJobParserTests-context.xml new file mode 100644 index 000000000..f4b1ac8cd --- /dev/null +++ b/spring-batch-core/src/test/resources/org/springframework/batch/core/configuration/xml/StepWithTaskJobParserTests-context.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file