Skip to content

Commit ac6493b

Browse files
author
trisberg
committed
BATCH-63: added first cut of <job-repository> parser
1 parent 1723b26 commit ac6493b

File tree

5 files changed

+172
-0
lines changed

5 files changed

+172
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,6 @@ public class CoreNamespaceHandler extends NamespaceHandlerSupport {
3131
*/
3232
public void init() {
3333
this.registerBeanDefinitionParser("job", new JobParser());
34+
this.registerBeanDefinitionParser("job-repository", new JobRepositoryParser());
3435
}
3536
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2006-2007 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.configuration.xml;
17+
18+
import org.apache.commons.logging.Log;
19+
import org.apache.commons.logging.LogFactory;
20+
import org.springframework.beans.factory.config.BeanDefinition;
21+
import org.springframework.beans.factory.config.RuntimeBeanReference;
22+
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
23+
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
24+
import org.w3c.dom.Element;
25+
26+
/**
27+
* Parser for the lt;job-repository/gt; element in the Batch namespace. Sets up and returns
28+
* a JobRepositoryFactoryBean.
29+
*
30+
* @author Thomas Risberg
31+
* @since 2.0
32+
*
33+
*/
34+
public class JobRepositoryParser extends AbstractSingleBeanDefinitionParser {
35+
36+
private final Log logger = LogFactory.getLog(getClass());
37+
38+
protected String getBeanClassName(Element element) {
39+
return "org.springframework.batch.core.repository.support.JobRepositoryFactoryBean";
40+
}
41+
42+
/**
43+
* Parse and create a bean definition for a
44+
* {@link org.springframework.batch.core.repository.support.JobRepositoryFactoryBean}.
45+
*/
46+
protected void doParse(Element element, BeanDefinitionBuilder builder) {
47+
48+
String dataSource = element.getAttribute("data-source");
49+
50+
String transactionManager = element.getAttribute("transaction-manager");
51+
52+
if (logger.isDebugEnabled()) {
53+
logger.debug("Using data-source: " + dataSource);
54+
logger.debug("Using transaction-manager: " + transactionManager);
55+
}
56+
57+
RuntimeBeanReference ds = new RuntimeBeanReference(dataSource);
58+
builder.addPropertyValue("dataSource", ds);
59+
RuntimeBeanReference tx = new RuntimeBeanReference(transactionManager);
60+
builder.addPropertyValue("transactionManager", tx);
61+
62+
builder.setRole(BeanDefinition.ROLE_SUPPORT);
63+
64+
}
65+
66+
}

spring-batch-core/src/main/resources/org/springframework/batch/core/configuration/xml/spring-batch-2.0.xsd

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,48 @@ The decider is a reference to a JobExecutionDecider that can produce a status to
177177
</xsd:complexType>
178178
</xsd:element>
179179

180+
<xsd:element name="job-repository">
181+
<xsd:annotation>
182+
<xsd:documentation><![CDATA[
183+
Configures a SimplJobRepository using a JobRepositoryFactoryBean.
184+
]]></xsd:documentation>
185+
</xsd:annotation>
186+
<xsd:complexType>
187+
<xsd:complexContent>
188+
<xsd:extension base="beans:identifiedType">
189+
<xsd:attribute name="data-source" type="xsd:string" default="dataSource">
190+
<xsd:annotation>
191+
<xsd:documentation source="java:org.springframework.transaction.PlatformTransactionManager"><![CDATA[
192+
The bean name of the DataSource that is to be used. This attribute
193+
is not required, and only needs to be specified explicitly
194+
if the bean name of the desired DataSource is not 'dataSource'.
195+
]]></xsd:documentation>
196+
<xsd:appinfo>
197+
<tool:annotation kind="ref">
198+
<tool:expected-type type="javax.sql.DataSource"/>
199+
</tool:annotation>
200+
</xsd:appinfo>
201+
</xsd:annotation>
202+
</xsd:attribute>
203+
<xsd:attribute name="transaction-manager" type="xsd:string" default="transactionManager">
204+
<xsd:annotation>
205+
<xsd:documentation source="java:org.springframework.transaction.PlatformTransactionManager"><![CDATA[
206+
The bean name of the TransactionManager that is to be used. This attribute
207+
is not required, and only needs to be specified explicitly
208+
if the bean name of the desired TransactionManager is not 'transactionManager'.
209+
]]></xsd:documentation>
210+
<xsd:appinfo>
211+
<tool:annotation kind="ref">
212+
<tool:expected-type type="org.springframework.transaction.PlatformTransactionManager"/>
213+
</tool:annotation>
214+
</xsd:appinfo>
215+
</xsd:annotation>
216+
</xsd:attribute>
217+
</xsd:extension>
218+
</xsd:complexContent>
219+
</xsd:complexType>
220+
</xsd:element>
221+
180222
<xsd:complexType name="flowType">
181223
<xsd:sequence>
182224
<xsd:choice minOccurs="1" maxOccurs="unbounded" >
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2006-2007 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.configuration.xml;
17+
18+
import static org.junit.Assert.assertNotNull;
19+
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
import org.springframework.batch.core.repository.JobRepository;
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.beans.factory.annotation.Qualifier;
25+
import org.springframework.test.context.ContextConfiguration;
26+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
27+
28+
29+
/**
30+
* @author Thomas Risberg
31+
*
32+
*/
33+
@ContextConfiguration
34+
@RunWith(SpringJUnit4ClassRunner.class)
35+
public class JobRepositoryParserTests {
36+
37+
@Autowired
38+
@Qualifier("jobRepo1")
39+
private JobRepository jobRepository;
40+
41+
@Test
42+
public void testOneStep() throws Exception {
43+
assertNotNull(jobRepository);
44+
}
45+
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans:beans xmlns="http://www.springframework.org/schema/batch" xmlns:beans="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.0.xsd
5+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
6+
7+
<beans:bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" lazy-init="true">
8+
<beans:property name="driverClassName" value="org.hsqldb.jdbcDriver" />
9+
<beans:property name="url" value="jdbc:hsqldb:mem:testdb;sql.enforce_strict_size=true" />
10+
</beans:bean>
11+
<beans:bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
12+
<beans:property name="dataSource" ref="dataSource"/>
13+
</beans:bean>
14+
15+
<job-repository id="jobRepo1"/>
16+
17+
</beans:beans>

0 commit comments

Comments
 (0)