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 org.w3c.dom.Element;
023    
024    import org.apache.camel.component.cxf.jaxrs.BeanIdAware;
025    import org.apache.cxf.Bus;
026    import org.apache.cxf.BusFactory;
027    import org.apache.cxf.bus.spring.BusWiringBeanFactoryPostProcessor;
028    import org.apache.cxf.bus.spring.SpringBusFactory;
029    import org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean;
030    import org.apache.cxf.jaxrs.model.UserResource;
031    import org.apache.cxf.jaxrs.utils.ResourceUtils;
032    import org.springframework.beans.BeansException;
033    import org.springframework.beans.factory.support.BeanDefinitionBuilder;
034    import org.springframework.beans.factory.xml.ParserContext;
035    import org.springframework.context.ApplicationContext;
036    import org.springframework.context.ApplicationContextAware;
037    
038    
039    public class CxfRsClientFactoryBeanDefinitionParser extends AbstractCxfBeanDefinitionParser {
040        public CxfRsClientFactoryBeanDefinitionParser() {
041            super();
042            setBeanClass(SpringJAXRSClientFactoryBean.class);
043        }
044        
045        
046        @Override
047        protected void doParse(Element element, ParserContext ctx, BeanDefinitionBuilder bean) {
048            super.doParse(element, ctx, bean);
049            bean.addPropertyValue("beanId", resolveId(element, bean.getBeanDefinition(), ctx));            
050        }
051       
052        @Override
053        protected void mapElement(ParserContext ctx, BeanDefinitionBuilder bean, Element el, String name) {
054            if ("properties".equals(name) || "headers".equals(name)) {
055                Map map = ctx.getDelegate().parseMapElement(el, bean.getBeanDefinition());
056                bean.addPropertyValue(name, map);         
057            } else if ("binding".equals(name)) {
058                setFirstChildAsProperty(el, ctx, bean, "bindingConfig");
059            } else if ("inInterceptors".equals(name) || "inFaultInterceptors".equals(name)
060                || "outInterceptors".equals(name) || "outFaultInterceptors".equals(name)) {
061                List list = ctx.getDelegate().parseListElement(el, bean.getBeanDefinition());
062                bean.addPropertyValue(name, list);
063            } else if ("features".equals(name) || "providers".equals(name)
064                       || "schemaLocations".equals(name) || "modelBeans".equals(name)) {
065                List list = ctx.getDelegate().parseListElement(el, bean.getBeanDefinition());
066                bean.addPropertyValue(name, list);
067            } else if ("model".equals(name)) {
068                List<UserResource> resources = ResourceUtils.getResourcesFromElement(el);
069                bean.addPropertyValue("modelBeans", resources);
070            } else {
071                setFirstChildAsProperty(el, ctx, bean, name);            
072            }        
073        }
074    
075        public static class SpringJAXRSClientFactoryBean extends JAXRSClientFactoryBean
076            implements ApplicationContextAware, BeanIdAware {
077            private String beanId;
078        
079            public SpringJAXRSClientFactoryBean() {
080                super();
081            }
082            
083            public void setApplicationContext(ApplicationContext ctx) throws BeansException {
084                if (getBus() == null) {
085                    // Don't relate on the DefaultBus
086                    BusFactory factory = new SpringBusFactory();
087                    Bus bus = factory.createBus();    
088                    BusWiringBeanFactoryPostProcessor.updateBusReferencesInContext(bus, ctx);
089                    setBus(bus);
090                }
091            }
092    
093            public String getBeanId() {            
094                return beanId;
095            }
096    
097            public void setBeanId(String id) {            
098                beanId = id;            
099            }
100        }    
101    }