001/*
002 * Copyright 2013-2014 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 org.springframework.batch.core.configuration.xml.CoreNamespaceUtils;
019import org.springframework.batch.core.jsr.JsrStepContextFactoryBean;
020import org.springframework.batch.core.jsr.configuration.support.BatchArtifactType;
021import org.springframework.beans.factory.config.BeanDefinition;
022import org.springframework.beans.factory.support.AbstractBeanDefinition;
023import org.springframework.beans.factory.support.BeanDefinitionBuilder;
024import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
025import org.springframework.beans.factory.xml.ParserContext;
026import org.springframework.util.StringUtils;
027import org.w3c.dom.Element;
028
029/**
030 * Parses a <job /> tag as defined in JSR-352.  Current state parses into
031 * the standard Spring Batch artifacts.
032 *
033 * @author Michael Minella
034 * @author Chris Schaefer
035 * @since 3.0
036 */
037public class JsrJobParser extends AbstractSingleBeanDefinitionParser {
038        private static final String ID_ATTRIBUTE = "id";
039        private static final String RESTARTABLE_ATTRIBUTE = "restartable";
040
041        @Override
042        protected Class<JobFactoryBean> getBeanClass(Element element) {
043                return JobFactoryBean.class;
044        }
045
046        @Override
047        protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
048                CoreNamespaceUtils.autoregisterBeansForNamespace(parserContext, parserContext.extractSource(element));
049                JsrNamespaceUtils.autoregisterJsrBeansForNamespace(parserContext);
050
051                String jobName = element.getAttribute(ID_ATTRIBUTE);
052
053                builder.setLazyInit(true);
054
055                builder.addConstructorArgValue(jobName);
056
057                builder.addPropertyReference("jobExplorer", "jobExplorer");
058
059                String restartableAttribute = element.getAttribute(RESTARTABLE_ATTRIBUTE);
060                if (StringUtils.hasText(restartableAttribute)) {
061                        builder.addPropertyValue("restartable", restartableAttribute);
062                }
063
064                new PropertyParser(jobName, parserContext, BatchArtifactType.JOB).parseProperties(element);
065
066                BeanDefinition flowDef = new FlowParser(jobName, jobName).parse(element, parserContext);
067                builder.addPropertyValue("flow", flowDef);
068
069                AbstractBeanDefinition stepContextBeanDefinition = BeanDefinitionBuilder.genericBeanDefinition(JsrStepContextFactoryBean.class)
070                                .getBeanDefinition();
071
072                stepContextBeanDefinition.setScope("step");
073
074                parserContext.getRegistry().registerBeanDefinition("stepContextFactory", stepContextBeanDefinition);
075
076                new ListenerParser(JsrJobListenerFactoryBean.class, "jobExecutionListeners").parseListeners(element, parserContext, builder);
077        }
078}