001/* 002 * Copyright 2006-2008 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.springframework.batch.core.configuration.xml; 017 018import org.springframework.beans.factory.BeanDefinitionStoreException; 019import org.springframework.beans.factory.config.BeanDefinition; 020import org.springframework.beans.factory.config.RuntimeBeanReference; 021import org.springframework.beans.factory.support.AbstractBeanDefinition; 022import org.springframework.beans.factory.support.BeanDefinitionBuilder; 023import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser; 024import org.springframework.beans.factory.xml.ParserContext; 025import org.springframework.transaction.support.DefaultTransactionDefinition; 026import org.springframework.util.StringUtils; 027import org.w3c.dom.Element; 028 029/** 030 * Parser for the <job-repository/> element in the Batch namespace. Sets up 031 * and returns a JobRepositoryFactoryBean. 032 * 033 * @author Thomas Risberg 034 * @since 2.0 035 * 036 */ 037public class JobRepositoryParser extends AbstractSingleBeanDefinitionParser { 038 039 @Override 040 protected String getBeanClassName(Element element) { 041 return "org.springframework.batch.core.repository.support.JobRepositoryFactoryBean"; 042 } 043 044 @Override 045 protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext) 046 throws BeanDefinitionStoreException { 047 048 String id = element.getAttribute(ID_ATTRIBUTE); 049 if (!StringUtils.hasText(id)) { 050 id = "jobRepository"; 051 } 052 053 return id; 054 055 } 056 057 /** 058 * Parse and create a bean definition for a 059 * {@link org.springframework.batch.core.repository.support.JobRepositoryFactoryBean} 060 * . 061 */ 062 @Override 063 protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) { 064 065 CoreNamespaceUtils.autoregisterBeansForNamespace(parserContext, element); 066 067 String dataSource = element.getAttribute("data-source"); 068 069 String jdbcOperations = element.getAttribute("jdbc-operations"); 070 071 String transactionManager = element.getAttribute("transaction-manager"); 072 073 String isolationLevelForCreate = element.getAttribute("isolation-level-for-create"); 074 075 String tablePrefix = element.getAttribute("table-prefix"); 076 077 String maxVarCharLength = element.getAttribute("max-varchar-length"); 078 079 String lobHandler = element.getAttribute("lob-handler"); 080 081 String serializer = element.getAttribute("serializer"); 082 083 RuntimeBeanReference ds = new RuntimeBeanReference(dataSource); 084 builder.addPropertyValue("dataSource", ds); 085 RuntimeBeanReference tx = new RuntimeBeanReference(transactionManager); 086 builder.addPropertyValue("transactionManager", tx); 087 if (StringUtils.hasText(jdbcOperations)) { 088 builder.addPropertyReference("jdbcOperations", jdbcOperations); 089 } 090 if (StringUtils.hasText(isolationLevelForCreate)) { 091 builder.addPropertyValue("isolationLevelForCreate", DefaultTransactionDefinition.PREFIX_ISOLATION 092 + isolationLevelForCreate); 093 } 094 if (StringUtils.hasText(tablePrefix)) { 095 builder.addPropertyValue("tablePrefix", tablePrefix); 096 } 097 if (StringUtils.hasText(lobHandler)) { 098 builder.addPropertyReference("lobHandler", lobHandler); 099 } 100 if (StringUtils.hasText(maxVarCharLength)) { 101 builder.addPropertyValue("maxVarCharLength", maxVarCharLength); 102 } 103 if (StringUtils.hasText(serializer)) { 104 builder.addPropertyReference("serializer", serializer); 105 } 106 107 builder.setRole(BeanDefinition.ROLE_SUPPORT); 108 109 } 110}