diff --git a/org.springframework.integration/src/main/java/org/springframework/integration/config/xml/PayloadTypeRouterParser.java b/org.springframework.integration/src/main/java/org/springframework/integration/config/xml/PayloadTypeRouterParser.java new file mode 100644 index 0000000000..33446c481a --- /dev/null +++ b/org.springframework.integration/src/main/java/org/springframework/integration/config/xml/PayloadTypeRouterParser.java @@ -0,0 +1,59 @@ +/* + * Copyright 2002-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.integration.config.xml; + +import java.util.List; + +import org.springframework.beans.factory.config.RuntimeBeanReference; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.ManagedMap; +import org.springframework.beans.factory.xml.ParserContext; +import org.springframework.integration.router.PayloadTypeRouter; +import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; +import org.springframework.util.xml.DomUtils; +import org.w3c.dom.Element; + +/** + * Parser for the <payload-type-router/> element. + * + * @author Oleg Zhurakousky + */ +public class PayloadTypeRouterParser extends RouterParser { + + @Override + protected BeanDefinitionBuilder parseHandler(Element element, ParserContext parserContext) { + BeanDefinitionBuilder payloadTypeRouterBuilder = BeanDefinitionBuilder.genericBeanDefinition(PayloadTypeRouter.class); + + List childElements = DomUtils.getChildElementsByTagName(element, "mapping"); + Assert.notEmpty(childElements, "Type mapping must be provided (e.g., )"); + + ManagedMap channelMap = new ManagedMap(); + for (Element childElement : childElements) { + String typeName = childElement.getAttribute("type"); + Assert.isTrue(ClassUtils.isPresent(typeName, ClassUtils.getDefaultClassLoader()), typeName + " can not be loaded"); + channelMap.put(typeName, new RuntimeBeanReference(childElement.getAttribute("channel"))); + } + + payloadTypeRouterBuilder.addPropertyValue("payloadTypeChannelMap", channelMap); + + BeanDefinitionBuilder rootBuilder = this.createBuilder(); + rootBuilder.addPropertyValue("targetObject", payloadTypeRouterBuilder.getBeanDefinition()); + return this.doParse(element, parserContext, rootBuilder); + } + +}