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.jsr.configuration.support.BatchArtifactType;
019import org.springframework.batch.core.step.tasklet.Tasklet;
020import org.springframework.beans.factory.config.BeanDefinition;
021import org.springframework.beans.factory.config.RuntimeBeanReference;
022import org.springframework.beans.factory.support.AbstractBeanDefinition;
023import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
024import org.springframework.beans.factory.xml.ParserContext;
025import org.springframework.util.StringUtils;
026import org.w3c.dom.Element;
027
028/**
029 * Parser for the <batchlet /> tag defined in JSR-352.  The current state
030 * of this parser parses a batchlet element into a {@link Tasklet} (the ref
031 * attribute is expected to point to an implementation of Tasklet).
032 * 
033 * @author Michael Minella
034 * @author Chris Schaefer
035 * @since 3.0
036 */
037public class BatchletParser extends AbstractSingleBeanDefinitionParser {
038        private static final String REF = "ref";
039
040        public void parseBatchlet(Element batchletElement, AbstractBeanDefinition bd, ParserContext parserContext, String stepName) {
041                bd.setBeanClass(StepFactoryBean.class);
042                bd.setAttribute("isNamespaceStep", false);
043
044                String taskletRef = batchletElement.getAttribute(REF);
045
046                if (StringUtils.hasText(taskletRef)) {
047                        bd.getPropertyValues().addPropertyValue("stepTasklet", new RuntimeBeanReference(taskletRef));
048                }
049
050                bd.setRole(BeanDefinition.ROLE_SUPPORT);
051                bd.setSource(parserContext.extractSource(batchletElement));
052
053                new PropertyParser(taskletRef, parserContext, BatchArtifactType.STEP_ARTIFACT, stepName).parseProperties(batchletElement);
054        }
055}