1 | /* |
2 | * Copyright 2006-2008 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.springframework.beans.factory.BeanDefinitionStoreException; |
19 | import org.springframework.beans.factory.config.BeanDefinition; |
20 | import org.springframework.beans.factory.config.RuntimeBeanReference; |
21 | import org.springframework.beans.factory.support.AbstractBeanDefinition; |
22 | import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
23 | import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; |
24 | import org.springframework.beans.factory.xml.ParserContext; |
25 | import org.springframework.transaction.support.DefaultTransactionDefinition; |
26 | import org.springframework.util.StringUtils; |
27 | import org.w3c.dom.Element; |
28 | |
29 | /** |
30 | * Parser for the lt;job-repository/gt; element in the Batch namespace. Sets up |
31 | * and returns a JobRepositoryFactoryBean. |
32 | * |
33 | * @author Thomas Risberg |
34 | * @since 2.0 |
35 | * |
36 | */ |
37 | public class JobRepositoryParser extends AbstractSingleBeanDefinitionParser { |
38 | |
39 | protected String getBeanClassName(Element element) { |
40 | return "org.springframework.batch.core.repository.support.JobRepositoryFactoryBean"; |
41 | } |
42 | |
43 | @Override |
44 | protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) |
45 | throws BeanDefinitionStoreException { |
46 | |
47 | String id = element.getAttribute(ID_ATTRIBUTE); |
48 | if (!StringUtils.hasText(id)) { |
49 | id = "jobRepository"; |
50 | } |
51 | |
52 | return id; |
53 | |
54 | } |
55 | |
56 | /** |
57 | * Parse and create a bean definition for a |
58 | * {@link org.springframework.batch.core.repository.support.JobRepositoryFactoryBean} |
59 | * . |
60 | */ |
61 | @Override |
62 | protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { |
63 | |
64 | CoreNamespaceUtils.autoregisterBeansForNamespace(parserContext, element); |
65 | |
66 | String dataSource = element.getAttribute("data-source"); |
67 | |
68 | String transactionManager = element.getAttribute("transaction-manager"); |
69 | |
70 | String isolationLevelForCreate = element.getAttribute("isolation-level-for-create"); |
71 | |
72 | String tablePrefix = element.getAttribute("table-prefix"); |
73 | |
74 | String maxVarCharLength = element.getAttribute("max-varchar-length"); |
75 | |
76 | String lobHandler = element.getAttribute("lob-handler"); |
77 | |
78 | RuntimeBeanReference ds = new RuntimeBeanReference(dataSource); |
79 | builder.addPropertyValue("dataSource", ds); |
80 | RuntimeBeanReference tx = new RuntimeBeanReference(transactionManager); |
81 | builder.addPropertyValue("transactionManager", tx); |
82 | if (StringUtils.hasText(isolationLevelForCreate)) { |
83 | builder.addPropertyValue("isolationLevelForCreate", DefaultTransactionDefinition.PREFIX_ISOLATION |
84 | + isolationLevelForCreate); |
85 | } |
86 | if (StringUtils.hasText(tablePrefix)) { |
87 | builder.addPropertyValue("tablePrefix", tablePrefix); |
88 | } |
89 | if (StringUtils.hasText(lobHandler)) { |
90 | builder.addPropertyReference("lobHandler", lobHandler); |
91 | } |
92 | if (StringUtils.hasText(maxVarCharLength)) { |
93 | builder.addPropertyValue("maxVarCharLength", maxVarCharLength); |
94 | } |
95 | |
96 | builder.setRole(BeanDefinition.ROLE_SUPPORT); |
97 | |
98 | } |
99 | } |