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    
018    package org.apache.camel.component.cxf.spring;
019    
020    import java.util.List;
021    import java.util.Map;
022    
023    import org.w3c.dom.Element;
024    
025    import org.apache.camel.component.cxf.jaxrs.BeanIdAware;
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.jaxrs.JAXRSServerFactoryBean;
031    import org.apache.cxf.jaxrs.JAXRSServiceFactoryBean;
032    import org.apache.cxf.jaxrs.model.UserResource;
033    import org.apache.cxf.jaxrs.utils.ResourceUtils;
034    import org.springframework.beans.BeansException;
035    import org.springframework.beans.factory.support.BeanDefinitionBuilder;
036    import org.springframework.beans.factory.xml.ParserContext;
037    import org.springframework.context.ApplicationContext;
038    import org.springframework.context.ApplicationContextAware;
039    
040    
041    
042    
043    /**
044     * 
045     */
046    public class CxfRsServerFactoryBeanDefinitionParser extends AbstractCxfBeanDefinitionParser  {
047        public CxfRsServerFactoryBeanDefinitionParser() {
048            super();
049            setBeanClass(SpringJAXRSServerFactoryBean.class);
050        }
051        
052        @Override
053        protected void doParse(Element element, ParserContext ctx, BeanDefinitionBuilder bean) {
054            super.doParse(element, ctx, bean);
055            bean.addPropertyValue("beanId", resolveId(element, bean.getBeanDefinition(), ctx));            
056        }
057        
058        @Override
059        protected void mapElement(ParserContext ctx, BeanDefinitionBuilder bean, Element el, String name) {
060            if ("properties".equals(name) 
061                || "extensionMappings".equals(name)
062                || "languageMappings".equals(name)) {
063                Map map = ctx.getDelegate().parseMapElement(el, bean.getBeanDefinition());
064                bean.addPropertyValue(name, map);
065            } else if ("executor".equals(name)) {
066                setFirstChildAsProperty(el, ctx, bean, "serviceFactory.executor");
067            } else if ("invoker".equals(name)) {
068                setFirstChildAsProperty(el, ctx, bean, "serviceFactory.invoker");
069            } else if ("binding".equals(name)) {
070                setFirstChildAsProperty(el, ctx, bean, "bindingConfig");
071            } else if ("inInterceptors".equals(name) || "inFaultInterceptors".equals(name)
072                || "outInterceptors".equals(name) || "outFaultInterceptors".equals(name)) {
073                List list = ctx.getDelegate().parseListElement(el, bean.getBeanDefinition());
074                bean.addPropertyValue(name, list);
075            } else if ("features".equals(name) || "schemaLocations".equals(name) 
076                || "providers".equals(name) || "serviceBeans".equals(name)
077                || "modelBeans".equals(name)) {
078                List list = ctx.getDelegate().parseListElement(el, bean.getBeanDefinition());
079                bean.addPropertyValue(name, list);
080            } else if ("model".equals(name)) {
081                List<UserResource> resources = ResourceUtils.getResourcesFromElement(el);
082                bean.addPropertyValue("modelBeans", resources);
083            } else {
084                setFirstChildAsProperty(el, ctx, bean, name);            
085            }        
086        }
087        
088        public static class SpringJAXRSServerFactoryBean extends JAXRSServerFactoryBean implements
089            ApplicationContextAware, BeanIdAware {
090            private String beanId;
091            
092            public SpringJAXRSServerFactoryBean() {
093                super();
094            }
095    
096            public SpringJAXRSServerFactoryBean(JAXRSServiceFactoryBean sf) {
097                super(sf);
098            }
099    
100            public void setApplicationContext(ApplicationContext ctx) throws BeansException {
101                if (getBus() == null) {
102                    // Don't relate on the DefaultBus
103                    BusFactory factory = new SpringBusFactory(ctx);
104                    Bus bus = factory.createBus();  
105                    BusWiringBeanFactoryPostProcessor.updateBusReferencesInContext(bus, ctx);
106                    setBus(bus);
107                }
108            }
109    
110            public String getBeanId() {
111                return beanId;
112            }
113    
114            public void setBeanId(String id) {
115                beanId = id;
116            }
117            
118            // to walk round the issue of setting the serviceClass in CXF
119            public void setServiceClass(Class clazz) {
120                setResourceClasses(clazz);
121            }
122        }
123        
124       
125    }