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