001/*
002 * Copyright 2013 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.jsr.configuration.xml;
017
018import java.util.List;
019
020import org.apache.commons.logging.Log;
021import org.apache.commons.logging.LogFactory;
022import org.springframework.beans.factory.support.AbstractBeanDefinition;
023import org.springframework.beans.factory.support.BeanDefinitionBuilder;
024import org.springframework.beans.factory.support.BeanDefinitionRegistry;
025import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
026import org.springframework.beans.factory.xml.ParserContext;
027import org.springframework.util.xml.DomUtils;
028import org.w3c.dom.Element;
029
030/**
031 * Parser used to parse the batch.xml file as defined in JSR-352.  It is <em>not</em>
032 * recommended to use the batch.xml approach with Spring to manage bean instantiation.
033 * It is recommended that standard Spring bean configurations (via XML or Java Config)
034 * be used.
035 * 
036 * @author Michael Minella
037 * @since 3.0
038 */
039public class BatchParser extends AbstractBeanDefinitionParser {
040
041        private static final Log logger = LogFactory.getLog(BatchParser.class);
042
043        @Override
044        protected boolean shouldGenerateIdAsFallback() {
045                return true;
046        }
047
048        @Override
049        protected AbstractBeanDefinition parseInternal(Element element,
050                        ParserContext parserContext) {
051                BeanDefinitionRegistry registry = parserContext.getRegistry();
052
053                parseRefElements(element, registry);
054
055                return null;
056        }
057
058        private void parseRefElements(Element element,
059                        BeanDefinitionRegistry registry) {
060                List<Element> beanElements = DomUtils.getChildElementsByTagName(element, "ref");
061
062                if(beanElements.size() > 0) {
063                        for (Element curElement : beanElements) {
064                                AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder.genericBeanDefinition(curElement.getAttribute("class"))
065                                                .getBeanDefinition();
066
067                                beanDefinition.setScope("step");
068
069                                String beanName = curElement.getAttribute("id");
070
071                                if(!registry.containsBeanDefinition(beanName)) {
072                                        registry.registerBeanDefinition(beanName, beanDefinition);
073                                } else {
074                                        logger.info("Ignoring batch.xml bean definition for " + beanName + " because another bean of the same name has been registered");
075                                }
076                        }
077                }
078
079        }
080}