001/*
002 * Copyright 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.integration.config.xml;
017
018import org.springframework.batch.integration.chunk.ChunkMessageChannelItemWriter;
019import org.springframework.batch.integration.chunk.RemoteChunkHandlerFactoryBean;
020import org.springframework.beans.factory.config.BeanDefinition;
021import org.springframework.beans.factory.support.AbstractBeanDefinition;
022import org.springframework.beans.factory.support.BeanDefinitionBuilder;
023import org.springframework.beans.factory.support.BeanDefinitionRegistry;
024import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
025import org.springframework.beans.factory.xml.ParserContext;
026import org.springframework.util.Assert;
027import org.w3c.dom.Element;
028
029/**
030 * <p>
031 * Parser for the remote-chunking-master namespace element.
032 * </p>
033 *
034 * @author Chris Schaefer
035 * @since 3.1
036 */
037public class RemoteChunkingMasterParser extends AbstractBeanDefinitionParser {
038        private static final String MESSAGE_TEMPLATE_ATTRIBUTE = "message-template";
039        private static final String STEP_ATTRIBUTE = "step";
040        private static final String REPLY_CHANNEL_ATTRIBUTE = "reply-channel";
041        private static final String MESSAGING_OPERATIONS_PROPERTY = "messagingOperations";
042        private static final String REPLY_CHANNEL_PROPERTY = "replyChannel";
043        private static final String CHUNK_WRITER_PROPERTY = "chunkWriter";
044        private static final String STEP_PROPERTY = "step";
045        private static final String CHUNK_HANDLER_BEAN_NAME_PREFIX = "remoteChunkHandlerFactoryBean_";
046
047        @Override
048        public AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
049                String id = element.getAttribute(ID_ATTRIBUTE);
050                Assert.hasText(id, "The id attribute must be specified");
051
052                String messageTemplate = element.getAttribute(MESSAGE_TEMPLATE_ATTRIBUTE);
053                Assert.hasText(messageTemplate, "The message-template attribute must be specified");
054
055                String step = element.getAttribute(STEP_ATTRIBUTE);
056                Assert.hasText(step, "The step attribute must be specified");
057
058                String replyChannel = element.getAttribute(REPLY_CHANNEL_ATTRIBUTE);
059                Assert.hasText(replyChannel, "The reply-channel attribute must be specified");
060
061                BeanDefinitionRegistry beanDefinitionRegistry = parserContext.getRegistry();
062
063                BeanDefinition chunkMessageChannelItemWriter =
064                                BeanDefinitionBuilder
065                                                .genericBeanDefinition(ChunkMessageChannelItemWriter.class)
066                                                .addPropertyReference(MESSAGING_OPERATIONS_PROPERTY, messageTemplate)
067                                                .addPropertyReference(REPLY_CHANNEL_PROPERTY, replyChannel)
068                                                .getBeanDefinition();
069
070                beanDefinitionRegistry.registerBeanDefinition(id, chunkMessageChannelItemWriter);
071
072                BeanDefinition remoteChunkHandlerFactoryBean =
073                                BeanDefinitionBuilder
074                                                .genericBeanDefinition(RemoteChunkHandlerFactoryBean.class)
075                                                .addPropertyValue(CHUNK_WRITER_PROPERTY, chunkMessageChannelItemWriter)
076                                                .addPropertyValue(STEP_PROPERTY, step)
077                                                .getBeanDefinition();
078
079                beanDefinitionRegistry.registerBeanDefinition(CHUNK_HANDLER_BEAN_NAME_PREFIX + step, remoteChunkHandlerFactoryBean);
080
081                return null;
082        }
083}