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    package org.apache.camel.component.spring.integration.adapter.config;
018    
019    import org.w3c.dom.Attr;
020    import org.w3c.dom.Element;
021    import org.w3c.dom.NamedNodeMap;
022    import org.w3c.dom.Node;
023    import org.w3c.dom.NodeList;
024    
025    import org.apache.camel.util.ObjectHelper;
026    import org.springframework.beans.factory.config.BeanDefinition;
027    import org.springframework.beans.factory.support.BeanDefinitionBuilder;
028    import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
029    import org.springframework.beans.factory.xml.ParserContext;
030    import org.springframework.util.StringUtils;
031    
032    /**
033     * This BeanDefinition parser help to inject the camel context into the beans
034     *
035     * @version 
036     */
037    public class AbstractCamelContextBeanDefinitionParaser extends AbstractSingleBeanDefinitionParser {
038        private static final String DEFAULT_CAMEL_CONTEXT_NAME = "camelContext";
039    
040        private String getContextId(String contextId) {
041            if (ObjectHelper.isEmpty(contextId)) {
042                //Set the contextId default value here
043                return DEFAULT_CAMEL_CONTEXT_NAME;
044            } else {
045                return contextId;
046            }
047        }
048    
049        protected void mapToProperty(BeanDefinitionBuilder bean, String propertyName, String val) {
050            if (ID_ATTRIBUTE.equals(propertyName)) {
051                return;
052            }
053    
054            if (StringUtils.hasText(val)) {
055                if (val.startsWith("#")) {
056                    bean.addPropertyReference(propertyName, val.substring(1));
057                } else {
058                    bean.addPropertyValue(propertyName, val);
059                }
060            }
061        }
062    
063        protected void wireCamelContext(BeanDefinitionBuilder bean, String camelContextId) {
064            bean.addPropertyReference("camelContext", camelContextId);
065        }
066    
067        protected void parseAttributes(Element element, ParserContext ctx, BeanDefinitionBuilder bean) {
068            NamedNodeMap atts = element.getAttributes();
069    
070            for (int i = 0; i < atts.getLength(); i++) {
071                Attr node = (Attr) atts.item(i);
072                String val = node.getValue();
073                String name = node.getLocalName();
074    
075                if (name.equals("requestChannel") || name.equals("replyChannel")) {
076                    bean.addPropertyReference(name, val);
077                } else {
078                    mapToProperty(bean, name, val);
079                }
080            }
081        }
082    
083        protected void parseCamelContext(Element element, ParserContext ctx, BeanDefinitionBuilder bean) {
084            NodeList children = element.getChildNodes();
085            for (int i = 0; i < children.getLength(); i++) {
086                Node n = children.item(i);
087                if (n.getNodeType() == Node.ELEMENT_NODE) {
088                    String name = n.getLocalName();
089                    if ("camelContext".equals(name)) {
090                        // Parser the camel context
091                        BeanDefinition bd = ctx.getDelegate().parseCustomElement((Element)n);
092                        // Get the inner camel context id
093                        String contextId = (String)bd.getPropertyValues().getPropertyValue("id").getValue();
094                        wireCamelContext(bean, getContextId(contextId));
095                    } else if ("camelContextRef".equals(name)) {
096                        String contextId = n.getTextContent();
097                        wireCamelContext(bean, getContextId(contextId));
098                    }
099                }
100            }
101        }
102    
103        protected void doParse(Element element, ParserContext ctx, BeanDefinitionBuilder bean) {
104            parseAttributes(element, ctx, bean);
105            parseCamelContext(element, ctx, bean);
106    
107        }
108    
109    }