Skip to content

Commit a83070b

Browse files
committed
Merge pull request #183 from mminella/BATCH-2000
* BATCH-2000: BATCH-2000: Adding support for JSR-352's batch.xml file
2 parents a39447f + e2b41fd commit a83070b

File tree

8 files changed

+188
-5
lines changed

8 files changed

+188
-5
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.batch.core.jsr.configuration.xml;
17+
18+
import java.util.List;
19+
20+
import org.apache.commons.logging.Log;
21+
import org.apache.commons.logging.LogFactory;
22+
import org.springframework.beans.factory.support.AbstractBeanDefinition;
23+
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
24+
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
25+
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
26+
import org.springframework.beans.factory.xml.ParserContext;
27+
import org.springframework.util.xml.DomUtils;
28+
import org.w3c.dom.Element;
29+
30+
/**
31+
* Parser used to parse the batch.xml file as defined in JSR-352. It is <em>not</em>
32+
* recommended to use the batch.xml approach with Spring to manage bean instantiation.
33+
* It is recommended that standard Spring bean configurations (via XML or Java Config)
34+
* be used.
35+
*
36+
* @author Michael Minella
37+
* @since 3.0
38+
*/
39+
public class BatchParser extends AbstractBeanDefinitionParser {
40+
41+
private static final Log logger = LogFactory.getLog(BatchParser.class);
42+
43+
@Override
44+
protected boolean shouldGenerateIdAsFallback() {
45+
return true;
46+
}
47+
48+
@Override
49+
protected AbstractBeanDefinition parseInternal(Element element,
50+
ParserContext parserContext) {
51+
BeanDefinitionRegistry registry = parserContext.getRegistry();
52+
53+
parseRefElements(element, registry);
54+
55+
return null;
56+
}
57+
58+
private void parseRefElements(Element element,
59+
BeanDefinitionRegistry registry) {
60+
List<Element> beanElements = DomUtils.getChildElementsByTagName(element, "ref");
61+
62+
if(beanElements.size() > 0) {
63+
for (Element curElement : beanElements) {
64+
AbstractBeanDefinition beanDefintion = BeanDefinitionBuilder.genericBeanDefinition(curElement.getAttribute("class"))
65+
.getBeanDefinition();
66+
67+
String beanName = curElement.getAttribute("id");
68+
69+
if(!registry.containsBeanDefinition(beanName)) {
70+
registry.registerBeanDefinition(beanName, beanDefintion);
71+
} else {
72+
logger.info("Ignoring batch.xml bean defintion for " + beanName + " because another bean of the same name has been registered");
73+
}
74+
}
75+
}
76+
77+
}
78+
}

spring-batch-core/src/main/java/org/springframework/batch/core/jsr/configuration/xml/JsrNamespaceHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ public class JsrNamespaceHandler extends NamespaceHandlerSupport {
2727
@Override
2828
public void init() {
2929
this.registerBeanDefinitionParser("job", new JobParser());
30+
this.registerBeanDefinitionParser("batch-artifacts", new BatchParser());
3031
}
3132
}

spring-batch-core/src/main/java/org/springframework/batch/core/step/builder/StepBuilder.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ public TaskletStepBuilder tasklet(Tasklet tasklet) {
6262
*
6363
* @param chunkSize the chunk size (commit interval)
6464
* @return a {@link SimpleStepBuilder}
65-
* @param I the type of item to be processed as input
66-
* @param O the type of item to be output
65+
* @param <I> the type of item to be processed as input
66+
* @param <O> the type of item to be output
6767
*/
6868
public <I, O> SimpleStepBuilder<I, O> chunk(int chunkSize) {
6969
return new SimpleStepBuilder<I, O>(this).chunk(chunkSize);
@@ -81,8 +81,8 @@ public <I, O> SimpleStepBuilder<I, O> chunk(int chunkSize) {
8181
*
8282
* @param completionPolicy the completion policy to use to control chunk processing
8383
* @return a {@link SimpleStepBuilder}
84-
* @param I the type of item to be processed as input
85-
* @param O the type of item to be output *
84+
* @param <I> the type of item to be processed as input
85+
* @param <O> the type of item to be output *
8686
*/
8787
public <I, O> SimpleStepBuilder<I, O> chunk(CompletionPolicy completionPolicy) {
8888
return new SimpleStepBuilder<I, O>(this).chunk(completionPolicy);

spring-batch-core/src/main/resources/META-INF/spring.schemas

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ http\://www.springframework.org/schema/batch/spring-batch.xsd=/org/springframewo
22
http\://www.springframework.org/schema/batch/spring-batch-2.2.xsd=/org/springframework/batch/core/configuration/xml/spring-batch-2.2.xsd
33
http\://www.springframework.org/schema/batch/spring-batch-2.1.xsd=/org/springframework/batch/core/configuration/xml/spring-batch-2.1.xsd
44
http\://www.springframework.org/schema/batch/spring-batch-2.0.xsd=/org/springframework/batch/core/configuration/xml/spring-batch-2.0.xsd
5-
http\://xmlns.jcp.org/xml/ns/javaee=/org/springframework/batch/core/jsr/configuration/xml/jobXML_1_0.xsd
5+
http\://xmlns.jcp.org/xml/ns/javaee=/org/springframework/batch/core/jsr/configuration/xml/jobXML_1_0.xsd
6+
http\://xmlns.jcp.org/xml/ns/javaee=/org/springframework/batch/core/jsr/configuration/xml/batchXML_1_0.xsd
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2012 International Business Machines Corp.
4+
5+
See the NOTICE file distributed with this work for additional information
6+
regarding copyright ownership. Licensed under the Apache License,
7+
Version 2.0 (the "License"); you may not use this file except in compliance
8+
with the License. You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-->
18+
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
19+
targetNamespace="http://xmlns.jcp.org/xml/ns/javaee" xmlns:jbatch="http://xmlns.jcp.org/xml/ns/javaee" version="1.0">
20+
21+
<xs:element name="batch-artifacts" type="jbatch:BatchArtifacts" />
22+
23+
<xs:complexType name="BatchArtifacts">
24+
<xs:sequence>
25+
<xs:element name="ref" type="jbatch:BatchArtifactRef" minOccurs="0" maxOccurs="unbounded" />
26+
</xs:sequence>
27+
</xs:complexType>
28+
29+
<xs:complexType name="BatchArtifactRef">
30+
<xs:attribute name="id" use="required" type="xs:string" />
31+
<xs:attribute name="class" use="required" type="xs:string" />
32+
</xs:complexType>
33+
34+
</xs:schema>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.springframework.batch.core.jsr.configuration.xml;
2+
3+
import static org.junit.Assert.assertNotNull;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.springframework.batch.core.configuration.xml.DummyItemProcessor;
9+
import org.springframework.batch.item.ItemProcessor;
10+
import org.springframework.batch.item.support.PassThroughItemProcessor;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.beans.factory.annotation.Qualifier;
13+
import org.springframework.context.support.AbstractApplicationContext;
14+
import org.springframework.context.support.ClassPathXmlApplicationContext;
15+
import org.springframework.test.context.ContextConfiguration;
16+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
17+
18+
@ContextConfiguration(value="batch.xml")
19+
@RunWith(SpringJUnit4ClassRunner.class)
20+
public class BatchParserTests {
21+
22+
@Autowired
23+
@Qualifier("itemProcessor")
24+
@SuppressWarnings("rawtypes")
25+
private ItemProcessor itemProcessor;
26+
27+
@Test
28+
public void testRoseyScenario() {
29+
assertNotNull(itemProcessor);
30+
assertTrue(itemProcessor instanceof PassThroughItemProcessor);
31+
}
32+
33+
@Test
34+
@SuppressWarnings({"resource", "rawtypes"})
35+
public void testOverrideBeansFirst() {
36+
AbstractApplicationContext context = new ClassPathXmlApplicationContext("/org/springframework/batch/core/jsr/configuration/xml/override_batch.xml",
37+
"/org/springframework/batch/core/jsr/configuration/xml/batch.xml");
38+
39+
ItemProcessor processor = (ItemProcessor) context.getBean("itemProcessor");
40+
41+
assertNotNull(processor);
42+
assertTrue(processor instanceof DummyItemProcessor);
43+
}
44+
45+
@Test
46+
@SuppressWarnings({"resource", "rawtypes"})
47+
public void testOverrideBeansLast() {
48+
AbstractApplicationContext context = new ClassPathXmlApplicationContext("/org/springframework/batch/core/jsr/configuration/xml/batch.xml",
49+
"/org/springframework/batch/core/jsr/configuration/xml/override_batch.xml");
50+
51+
ItemProcessor processor = (ItemProcessor) context.getBean("itemProcessor");
52+
53+
assertNotNull(processor);
54+
assertTrue(processor instanceof DummyItemProcessor);
55+
}
56+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<batch-artifacts xmlns="http://xmlns.jcp.org/xml/ns/javaee"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/batchXML_1_0.xsd">
4+
<ref id="itemProcessor" class="org.springframework.batch.item.support.PassThroughItemProcessor" />
5+
</batch-artifacts>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
5+
6+
<bean id="itemProcessor" class="org.springframework.batch.core.configuration.xml.DummyItemProcessor"/>
7+
8+
</beans>

0 commit comments

Comments
 (0)