| 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 | @Override |
| 40 | protected String getBeanClassName(Element element) { |
| 41 | return "org.springframework.batch.core.repository.support.JobRepositoryFactoryBean"; |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) |
| 46 | throws BeanDefinitionStoreException { |
| 47 | |
| 48 | String id = element.getAttribute(ID_ATTRIBUTE); |
| 49 | if (!StringUtils.hasText(id)) { |
| 50 | id = "jobRepository"; |
| 51 | } |
| 52 | |
| 53 | return id; |
| 54 | |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Parse and create a bean definition for a |
| 59 | * {@link org.springframework.batch.core.repository.support.JobRepositoryFactoryBean} |
| 60 | * . |
| 61 | */ |
| 62 | @Override |
| 63 | protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { |
| 64 | |
| 65 | CoreNamespaceUtils.autoregisterBeansForNamespace(parserContext, element); |
| 66 | |
| 67 | String dataSource = element.getAttribute("data-source"); |
| 68 | |
| 69 | String transactionManager = element.getAttribute("transaction-manager"); |
| 70 | |
| 71 | String isolationLevelForCreate = element.getAttribute("isolation-level-for-create"); |
| 72 | |
| 73 | String tablePrefix = element.getAttribute("table-prefix"); |
| 74 | |
| 75 | String maxVarCharLength = element.getAttribute("max-varchar-length"); |
| 76 | |
| 77 | String lobHandler = element.getAttribute("lob-handler"); |
| 78 | |
| 79 | String serializer = element.getAttribute("serializer"); |
| 80 | |
| 81 | RuntimeBeanReference ds = new RuntimeBeanReference(dataSource); |
| 82 | builder.addPropertyValue("dataSource", ds); |
| 83 | RuntimeBeanReference tx = new RuntimeBeanReference(transactionManager); |
| 84 | builder.addPropertyValue("transactionManager", tx); |
| 85 | if (StringUtils.hasText(isolationLevelForCreate)) { |
| 86 | builder.addPropertyValue("isolationLevelForCreate", DefaultTransactionDefinition.PREFIX_ISOLATION |
| 87 | + isolationLevelForCreate); |
| 88 | } |
| 89 | if (StringUtils.hasText(tablePrefix)) { |
| 90 | builder.addPropertyValue("tablePrefix", tablePrefix); |
| 91 | } |
| 92 | if (StringUtils.hasText(lobHandler)) { |
| 93 | builder.addPropertyReference("lobHandler", lobHandler); |
| 94 | } |
| 95 | if (StringUtils.hasText(maxVarCharLength)) { |
| 96 | builder.addPropertyValue("maxVarCharLength", maxVarCharLength); |
| 97 | } |
| 98 | if (StringUtils.hasText(serializer)) { |
| 99 | builder.addPropertyReference("serializer", serializer); |
| 100 | } |
| 101 | |
| 102 | builder.setRole(BeanDefinition.ROLE_SUPPORT); |
| 103 | |
| 104 | } |
| 105 | } |