001/*
002   * Copyright 2002-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.integration.config.xml;
017
018import org.apache.commons.logging.Log;
019import org.apache.commons.logging.LogFactory;
020import org.springframework.batch.core.launch.JobLauncher;
021import org.springframework.batch.integration.launch.JobLaunchingGateway;
022import org.springframework.beans.factory.support.BeanDefinitionBuilder;
023import org.springframework.beans.factory.xml.ParserContext;
024import org.springframework.integration.config.xml.AbstractConsumerEndpointParser;
025import org.springframework.integration.config.xml.IntegrationNamespaceUtils;
026import org.springframework.util.StringUtils;
027import org.w3c.dom.Element;
028
029/**
030 * The parser for the Job-Launching Gateway, which will instantiate a
031 * {@link JobLaunchingGatewayParser}. If no {@link JobLauncher} reference has
032 * been provided, this parse will use the use the globally registered bean
033 * 'jobLauncher'.
034 *
035 * @author Gunnar Hillert
036 * @since 1.3
037 *
038 */
039public class JobLaunchingGatewayParser extends AbstractConsumerEndpointParser {
040
041        private static final Log logger = LogFactory.getLog(JobLaunchingGatewayParser.class);
042
043        @Override
044        protected String getInputChannelAttributeName() {
045                return "request-channel";
046        }
047
048        @Override
049        protected BeanDefinitionBuilder parseHandler(Element element, ParserContext parserContext) {
050
051                final BeanDefinitionBuilder jobLaunchingGatewayBuilder =
052                        BeanDefinitionBuilder.genericBeanDefinition(JobLaunchingGateway.class);
053
054                final String jobLauncher = element.getAttribute("job-launcher");
055
056                if (StringUtils.hasText(jobLauncher)) {
057                        jobLaunchingGatewayBuilder.addConstructorArgReference(jobLauncher);
058                }
059                else {
060                        if (logger.isDebugEnabled()) {
061                                logger.debug("No jobLauncher specified, using default 'jobLauncher' reference instead.");
062                        }
063                        jobLaunchingGatewayBuilder.addConstructorArgReference("jobLauncher");
064                }
065
066                IntegrationNamespaceUtils.setValueIfAttributeDefined(jobLaunchingGatewayBuilder, element, "reply-timeout", "sendTimeout");
067
068                final String replyChannel = element.getAttribute("reply-channel");
069
070                if (StringUtils.hasText(replyChannel)) {
071                        jobLaunchingGatewayBuilder.addPropertyReference("outputChannel", replyChannel);
072                }
073
074                return jobLaunchingGatewayBuilder;
075
076        }
077
078}