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.cxf.transport.spring;
018    
019    import org.w3c.dom.Element;
020    import org.w3c.dom.Node;
021    import org.w3c.dom.NodeList;
022    
023    import org.apache.camel.util.ObjectHelper;
024    import org.apache.cxf.configuration.spring.AbstractBeanDefinitionParser;
025    import org.springframework.beans.factory.config.BeanDefinition;
026    import org.springframework.beans.factory.support.BeanDefinitionBuilder;
027    import org.springframework.beans.factory.xml.ParserContext;
028    
029    public class AbstractCamelContextBeanDefinitionParser extends AbstractBeanDefinitionParser {
030        private static final String DEFAULT_CAMEL_CONTEXT_NAME = "camelContext";
031    
032        private String getContextId(String contextId) {
033            if (ObjectHelper.isEmpty(contextId)) {
034                //Set the contextId default value here
035                return DEFAULT_CAMEL_CONTEXT_NAME;
036            } else {
037                return contextId;
038            }
039        }
040    
041        protected void wireCamelContext(BeanDefinitionBuilder bean, String camelContextId) {
042            bean.addPropertyReference("camelContext", camelContextId);
043        }
044    
045        protected void doParse(Element element, ParserContext ctx, BeanDefinitionBuilder bean) {
046            // Parser the id attribute
047            bean.setAbstract(true);
048            NodeList children = element.getChildNodes();
049            for (int i = 0; i < children.getLength(); i++) {
050                Node n = children.item(i);
051                if (n.getNodeType() == Node.ELEMENT_NODE) {
052                    String name = n.getLocalName();
053                    if ("camelContext".equals(name)) {
054                        // Parser the camel context
055                        BeanDefinition bd = ctx.getDelegate().parseCustomElement((Element)n);
056                        // Get the inner camel context id
057                        String contextId = (String)bd.getPropertyValues().getPropertyValue("id").getValue();
058                        wireCamelContext(bean, getContextId(contextId));
059                    } else if ("camelContextRef".equals(name)) {
060                        String contextId = n.getTextContent();
061                        wireCamelContext(bean, getContextId(contextId));
062                    }
063                }
064            }
065        }
066    
067    }