001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.camel.component.spring.integration.adapter.config;
019    
020    import org.w3c.dom.Attr;
021    import org.w3c.dom.Element;
022    import org.w3c.dom.NamedNodeMap;
023    import org.w3c.dom.Node;
024    import org.w3c.dom.NodeList;
025    
026    import org.apache.camel.util.ObjectHelper;
027    import org.springframework.beans.factory.config.BeanDefinition;
028    import org.springframework.beans.factory.config.RuntimeBeanReference;
029    import org.springframework.beans.factory.support.BeanDefinitionBuilder;
030    import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
031    import org.springframework.beans.factory.xml.ParserContext;
032    import org.springframework.util.StringUtils;
033    
034    /**
035     * This BeanDefinition paraser help to inject the camel context into the beans
036     *
037     * @version $Revision: 792319 $
038     */
039    public class AbstractCamelContextBeanDefinitionParaser extends AbstractSingleBeanDefinitionParser {
040        private static final String DEFAULT_CAMEL_CONTEXT_NAME = "camelContext";
041    
042        private String getContextId(String contextId) {
043            if (ObjectHelper.isEmpty(contextId)) {
044                //Set the contextId default value here
045                return DEFAULT_CAMEL_CONTEXT_NAME;
046            } else {
047                return contextId;
048            }
049        }
050    
051        protected void mapToProperty(BeanDefinitionBuilder bean, String propertyName, String val) {
052            if (ID_ATTRIBUTE.equals(propertyName)) {
053                return;
054            }
055    
056            if (StringUtils.hasText(val)) {
057                if (val.startsWith("#")) {
058                    bean.addPropertyReference(propertyName, val.substring(1));
059                } else {
060                    bean.addPropertyValue(propertyName, val);
061                }
062            }
063        }
064    
065        protected void wireCamelContext(BeanDefinitionBuilder bean, String camelContextId) {
066            bean.addPropertyReference("camelContext", camelContextId);
067        }
068    
069        protected void parseAttributes(Element element, ParserContext ctx, BeanDefinitionBuilder bean) {
070            NamedNodeMap atts = element.getAttributes();
071    
072            for (int i = 0; i < atts.getLength(); i++) {
073                Attr node = (Attr) atts.item(i);
074                String val = node.getValue();
075                String name = node.getLocalName();
076    
077                if (name.equals("requestChannel") || name.equals("replyChannel")) {
078                    bean.addPropertyReference(name, val);
079                } else {
080                    mapToProperty(bean, name, val);
081                }
082            }
083        }
084    
085        protected void parseCamelContext(Element element, ParserContext ctx, BeanDefinitionBuilder bean) {
086            NodeList children = element.getChildNodes();
087            for (int i = 0; i < children.getLength(); i++) {
088                Node n = children.item(i);
089                if (n.getNodeType() == Node.ELEMENT_NODE) {
090                    String name = n.getLocalName();
091                    if ("camelContext".equals(name)) {
092                        // Parser the camel context
093                        BeanDefinition bd = ctx.getDelegate().parseCustomElement((Element)n);
094                        // Get the inner camel context id
095                        String contextId = (String)bd.getPropertyValues().getPropertyValue("id").getValue();
096                        wireCamelContext(bean, getContextId(contextId));
097                    } else if ("camelContextRef".equals(name)) {
098                        String contextId = n.getTextContent();
099                        wireCamelContext(bean, getContextId(contextId));
100                    }
101                }
102            }
103        }
104    
105        protected void doParse(Element element, ParserContext ctx, BeanDefinitionBuilder bean) {
106            parseAttributes(element, ctx, bean);
107            parseCamelContext(element, ctx, bean);
108    
109        }
110    
111    }