diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/xml/BatchParser.java b/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/xml/BatchParser.java new file mode 100644 index 000000000..1cb20ffa2 --- /dev/null +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/xml/BatchParser.java @@ -0,0 +1,78 @@ +/* + * Copyright 2013 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.jsr.configuration.xml; + +import java.util.List; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.support.AbstractBeanDefinition; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser; +import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.util.xml.DomUtils; +import org.w3c.dom.Element; + +/** + * Parser used to parse the batch.xml file as defined in JSR-352. It is not + * recommended to use the batch.xml approach with Spring to manage bean instantiation. + * It is recommended that standard Spring bean configurations (via XML or Java Config) + * be used. + * + * @author Michael Minella + * @since 3.0 + */ +public class BatchParser extends AbstractBeanDefinitionParser { + + private static final Log logger = LogFactory.getLog(BatchParser.class); + + @Override + protected boolean shouldGenerateIdAsFallback() { + return true; + } + + @Override + protected AbstractBeanDefinition parseInternal(Element element, + ParserContext parserContext) { + BeanDefinitionRegistry registry = parserContext.getRegistry(); + + parseRefElements(element, registry); + + return null; + } + + private void parseRefElements(Element element, + BeanDefinitionRegistry registry) { + List beanElements = DomUtils.getChildElementsByTagName(element, "ref"); + + if(beanElements.size() > 0) { + for (Element curElement : beanElements) { + AbstractBeanDefinition beanDefintion = BeanDefinitionBuilder.genericBeanDefinition(curElement.getAttribute("class")) + .getBeanDefinition(); + + String beanName = curElement.getAttribute("id"); + + if(!registry.containsBeanDefinition(beanName)) { + registry.registerBeanDefinition(beanName, beanDefintion); + } else { + logger.info("Ignoring batch.xml bean defintion for " + beanName + " because another bean of the same name has been registered"); + } + } + } + + } +} diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/xml/JsrNamespaceHandler.java b/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/xml/JsrNamespaceHandler.java index 999281e75..6ea579671 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/xml/JsrNamespaceHandler.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/xml/JsrNamespaceHandler.java @@ -27,5 +27,6 @@ public class JsrNamespaceHandler extends NamespaceHandlerSupport { @Override public void init() { this.registerBeanDefinitionParser("job", new JobParser()); + this.registerBeanDefinitionParser("batch-artifacts", new BatchParser()); } } diff --git a/spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/StepBuilder.java b/spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/StepBuilder.java index d8a889c60..500ae1722 100644 --- a/spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/StepBuilder.java +++ b/spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/StepBuilder.java @@ -62,8 +62,8 @@ public class StepBuilder extends StepBuilderHelper { * * @param chunkSize the chunk size (commit interval) * @return a {@link SimpleStepBuilder} - * @param I the type of item to be processed as input - * @param O the type of item to be output + * @param the type of item to be processed as input + * @param the type of item to be output */ public SimpleStepBuilder chunk(int chunkSize) { return new SimpleStepBuilder(this).chunk(chunkSize); @@ -81,8 +81,8 @@ public class StepBuilder extends StepBuilderHelper { * * @param completionPolicy the completion policy to use to control chunk processing * @return a {@link SimpleStepBuilder} - * @param I the type of item to be processed as input - * @param O the type of item to be output * + * @param the type of item to be processed as input + * @param the type of item to be output * */ public SimpleStepBuilder chunk(CompletionPolicy completionPolicy) { return new SimpleStepBuilder(this).chunk(completionPolicy); diff --git a/spring-batch-core/src/main/resources/META-INF/spring.schemas b/spring-batch-core/src/main/resources/META-INF/spring.schemas index 6fc4d4247..375d61f6b 100644 --- a/spring-batch-core/src/main/resources/META-INF/spring.schemas +++ b/spring-batch-core/src/main/resources/META-INF/spring.schemas @@ -2,4 +2,5 @@ http\://www.springframework.org/schema/batch/spring-batch.xsd=/org/springframewo http\://www.springframework.org/schema/batch/spring-batch-2.2.xsd=/org/springframework/batch/core/configuration/xml/spring-batch-2.2.xsd http\://www.springframework.org/schema/batch/spring-batch-2.1.xsd=/org/springframework/batch/core/configuration/xml/spring-batch-2.1.xsd http\://www.springframework.org/schema/batch/spring-batch-2.0.xsd=/org/springframework/batch/core/configuration/xml/spring-batch-2.0.xsd -http\://xmlns.jcp.org/xml/ns/javaee=/org/springframework/batch/core/jsr/configuration/xml/jobXML_1_0.xsd \ No newline at end of file +http\://xmlns.jcp.org/xml/ns/javaee=/org/springframework/batch/core/jsr/configuration/xml/jobXML_1_0.xsd +http\://xmlns.jcp.org/xml/ns/javaee=/org/springframework/batch/core/jsr/configuration/xml/batchXML_1_0.xsd diff --git a/spring-batch-core/src/main/resources/org/springframework/batch/core/jsr/configuration/xml/batchXML_1_0.xsd b/spring-batch-core/src/main/resources/org/springframework/batch/core/jsr/configuration/xml/batchXML_1_0.xsd new file mode 100644 index 000000000..7982a88f8 --- /dev/null +++ b/spring-batch-core/src/main/resources/org/springframework/batch/core/jsr/configuration/xml/batchXML_1_0.xsd @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/configuration/xml/BatchParserTests.java b/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/configuration/xml/BatchParserTests.java new file mode 100644 index 000000000..ad0a9b46b --- /dev/null +++ b/spring-batch-core/src/test/java/org/springframework/batch/core/jsr/configuration/xml/BatchParserTests.java @@ -0,0 +1,56 @@ +package org.springframework.batch.core.jsr.configuration.xml; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.batch.core.configuration.xml.DummyItemProcessor; +import org.springframework.batch.item.ItemProcessor; +import org.springframework.batch.item.support.PassThroughItemProcessor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@ContextConfiguration(value="batch.xml") +@RunWith(SpringJUnit4ClassRunner.class) +public class BatchParserTests { + + @Autowired + @Qualifier("itemProcessor") + @SuppressWarnings("rawtypes") + private ItemProcessor itemProcessor; + + @Test + public void testRoseyScenario() { + assertNotNull(itemProcessor); + assertTrue(itemProcessor instanceof PassThroughItemProcessor); + } + + @Test + @SuppressWarnings({"resource", "rawtypes"}) + public void testOverrideBeansFirst() { + AbstractApplicationContext context = new ClassPathXmlApplicationContext("/org/springframework/batch/core/jsr/configuration/xml/override_batch.xml", + "/org/springframework/batch/core/jsr/configuration/xml/batch.xml"); + + ItemProcessor processor = (ItemProcessor) context.getBean("itemProcessor"); + + assertNotNull(processor); + assertTrue(processor instanceof DummyItemProcessor); + } + + @Test + @SuppressWarnings({"resource", "rawtypes"}) + public void testOverrideBeansLast() { + AbstractApplicationContext context = new ClassPathXmlApplicationContext("/org/springframework/batch/core/jsr/configuration/xml/batch.xml", + "/org/springframework/batch/core/jsr/configuration/xml/override_batch.xml"); + + ItemProcessor processor = (ItemProcessor) context.getBean("itemProcessor"); + + assertNotNull(processor); + assertTrue(processor instanceof DummyItemProcessor); + } +} diff --git a/spring-batch-core/src/test/resources/org/springframework/batch/core/jsr/configuration/xml/batch.xml b/spring-batch-core/src/test/resources/org/springframework/batch/core/jsr/configuration/xml/batch.xml new file mode 100644 index 000000000..7477deaa7 --- /dev/null +++ b/spring-batch-core/src/test/resources/org/springframework/batch/core/jsr/configuration/xml/batch.xml @@ -0,0 +1,5 @@ + + + \ No newline at end of file diff --git a/spring-batch-core/src/test/resources/org/springframework/batch/core/jsr/configuration/xml/override_batch.xml b/spring-batch-core/src/test/resources/org/springframework/batch/core/jsr/configuration/xml/override_batch.xml new file mode 100644 index 000000000..fa826cd06 --- /dev/null +++ b/spring-batch-core/src/test/resources/org/springframework/batch/core/jsr/configuration/xml/override_batch.xml @@ -0,0 +1,8 @@ + + + + + + \ No newline at end of file