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 java.util.Collection;
019
020import org.springframework.batch.core.job.flow.JobExecutionDecider;
021import org.springframework.batch.core.jsr.configuration.support.BatchArtifactType;
022import org.springframework.batch.core.jsr.job.flow.support.state.JsrStepState;
023import org.springframework.beans.factory.config.BeanDefinition;
024import org.springframework.beans.factory.config.RuntimeBeanReference;
025import org.springframework.beans.factory.parsing.BeanComponentDefinition;
026import org.springframework.beans.factory.support.AbstractBeanDefinition;
027import org.springframework.beans.factory.support.BeanDefinitionBuilder;
028import org.springframework.beans.factory.xml.ParserContext;
029import org.springframework.util.StringUtils;
030import org.w3c.dom.Element;
031
032/**
033 * Parser for the <decision /> element as specified in JSR-352.  The current state
034 * parses a decision element and assumes that it refers to a {@link JobExecutionDecider}
035 *
036 * @author Michael Minella
037 * @author Chris Schaefer
038 * @since 3.0
039 */
040public class JsrDecisionParser {
041
042        private static final String ID_ATTRIBUTE = "id";
043        private static final String REF_ATTRIBUTE = "ref";
044
045        public Collection<BeanDefinition> parse(Element element, ParserContext parserContext, String jobFactoryRef) {
046                BeanDefinitionBuilder factoryBuilder = BeanDefinitionBuilder.genericBeanDefinition();
047                AbstractBeanDefinition factoryDefinition = factoryBuilder.getRawBeanDefinition();
048                factoryDefinition.setBeanClass(DecisionStepFactoryBean.class);
049
050                BeanDefinitionBuilder stateBuilder = BeanDefinitionBuilder.genericBeanDefinition(JsrStepState.class);
051
052                String idAttribute = element.getAttribute(ID_ATTRIBUTE);
053
054                parserContext.registerBeanComponent(new BeanComponentDefinition(factoryDefinition, idAttribute));
055                stateBuilder.addConstructorArgReference(idAttribute);
056
057                String refAttribute = element.getAttribute(REF_ATTRIBUTE);
058                factoryDefinition.getPropertyValues().add("decider", new RuntimeBeanReference(refAttribute));
059                factoryDefinition.getPropertyValues().add("name", idAttribute);
060
061                if(StringUtils.hasText(jobFactoryRef)) {
062                        factoryDefinition.setAttribute("jobParserJobFactoryBeanRef", jobFactoryRef);
063                }
064
065                new PropertyParser(refAttribute, parserContext, BatchArtifactType.STEP_ARTIFACT, idAttribute).parseProperties(element);
066
067                return FlowParser.getNextElements(parserContext, stateBuilder.getBeanDefinition(), element);
068        }
069}