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.spring;
018    
019    import java.util.List;
020    import java.util.Map;
021    
022    import javax.xml.namespace.QName;
023    
024    import org.w3c.dom.Element;
025    
026    import org.apache.cxf.Bus;
027    import org.apache.cxf.BusFactory;
028    import org.apache.cxf.bus.spring.BusWiringBeanFactoryPostProcessor;
029    import org.apache.cxf.bus.spring.SpringBusFactory;
030    import org.apache.cxf.service.factory.ReflectionServiceFactoryBean;
031    import org.springframework.beans.BeansException;
032    import org.springframework.beans.factory.support.BeanDefinitionBuilder;
033    import org.springframework.beans.factory.xml.ParserContext;
034    import org.springframework.context.ApplicationContext;
035    import org.springframework.context.ApplicationContextAware;
036    
037    public class CxfEndpointBeanDefinitionParser extends AbstractCxfBeanDefinitionParser {
038    
039        @Override
040        protected Class<?> getBeanClass(Element arg0) {
041            return CxfSpringEndpointBean.class;
042        }
043    
044        @Override
045        protected void mapAttribute(BeanDefinitionBuilder bean, Element e, String name, String val) {
046                    
047            if ("endpointName".equals(name) || "serviceName".equals(name)) {           
048                QName q = parseQName(e, val);
049                bean.addPropertyValue(name, q);
050            } else {
051                mapToProperty(bean, name, val);
052            }
053        }
054    
055        @SuppressWarnings("unchecked")
056        @Override
057        protected void mapElement(ParserContext ctx, BeanDefinitionBuilder bean, Element el, String name) {
058            if ("properties".equals(name)) {
059                Map map = ctx.getDelegate().parseMapElement(el, bean.getBeanDefinition());
060                Map props = getPropertyMap(bean, false);
061                if (props != null) {
062                    map.putAll(props);
063                }
064                bean.addPropertyValue("properties", map);
065                
066            } else if ("binding".equals(name)) {
067                setFirstChildAsProperty(el, ctx, bean, "bindingConfig");
068            } else if ("inInterceptors".equals(name) || "inFaultInterceptors".equals(name)
069                || "outInterceptors".equals(name) || "outFaultInterceptors".equals(name)
070                || "features".equals(name) || "schemaLocations".equals(name)
071                || "handlers".equals(name)) {
072                List<?> list = (List<?>)ctx.getDelegate().parseListElement(el, bean.getBeanDefinition());
073                bean.addPropertyValue(name, list);
074            } else {
075                setFirstChildAsProperty(el, ctx, bean, name);
076            }
077        }
078        
079        // To make the CxfEndpointBean clear without touching any Spring relates class 
080        // , we implements the ApplicationContextAware here
081        public static class CxfSpringEndpointBean extends CxfEndpointBean implements ApplicationContextAware {
082            private ApplicationContext applicationContext;
083            
084            public CxfSpringEndpointBean() {
085                super();
086            }
087            
088            public CxfSpringEndpointBean(ReflectionServiceFactoryBean factory) {
089                super(factory);
090            }
091            
092            public void setApplicationContext(ApplicationContext ctx) throws BeansException {
093                applicationContext = ctx;
094                if (getBus() == null) {
095                    // Don't relate on the DefaultBus
096                    BusFactory factory = new SpringBusFactory(ctx);
097                    Bus bus = factory.createBus();               
098                    setBus(bus);
099                }
100                BusWiringBeanFactoryPostProcessor.updateBusReferencesInContext(getBus(), ctx);
101            }
102            
103            public ApplicationContext getApplicationContext() {
104                return applicationContext;
105            }
106            
107        }
108    
109    
110    }