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.jaxrs;
019    
020    import org.apache.camel.CamelContext;
021    import org.apache.camel.spring.SpringCamelContext;
022    import org.apache.cxf.configuration.spring.ConfigurerImpl;
023    import org.apache.cxf.jaxrs.AbstractJAXRSFactoryBean;
024    import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
025    import org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean;
026    import org.springframework.context.ApplicationContext;
027    import org.springframework.context.support.AbstractApplicationContext;
028    
029    /**
030     * 
031     */
032    public class CxfRsSpringEndpoint extends CxfRsEndpoint implements BeanIdAware {
033        private AbstractJAXRSFactoryBean bean;
034        private ApplicationContext applicationContext;
035        private String beanId;
036        private ConfigurerImpl configurer;
037        
038        public CxfRsSpringEndpoint(CamelContext context, AbstractJAXRSFactoryBean bean) throws Exception {
039            super(bean.getAddress(), context);        
040            init(bean);
041        }
042        
043        private void init(AbstractJAXRSFactoryBean bean) {
044            this.bean = bean;
045            if (bean instanceof BeanIdAware) {
046                setBeanId(((BeanIdAware)bean).getBeanId());
047            }
048            applicationContext = ((SpringCamelContext)getCamelContext()).getApplicationContext();
049            // create configurer
050            configurer = new ConfigurerImpl(applicationContext);
051        }
052        
053        void configure(Object beanInstance) {
054            // check the ApplicationContext states first , and call the refresh if necessary
055            if (applicationContext instanceof AbstractApplicationContext) {
056                AbstractApplicationContext context = (AbstractApplicationContext) applicationContext;
057                if (!context.isActive()) {
058                    context.refresh();
059                }
060            }
061            configurer.configureBean(beanId, beanInstance);
062        }
063        
064        void checkBeanType(Class<?> clazz) {
065            if (!clazz.isAssignableFrom(bean.getClass())) {
066                throw new IllegalArgumentException("The configure bean is not the instance of " + clazz.getName());
067            }
068        }
069        
070        protected void setupJAXRSServerFactoryBean(JAXRSServerFactoryBean sfb) {
071            checkBeanType(JAXRSServerFactoryBean.class);
072            configure(sfb);
073            
074        }
075        
076        protected void setupJAXRSClientFactoryBean(JAXRSClientFactoryBean cfb) {
077            checkBeanType(JAXRSClientFactoryBean.class);
078            configure(cfb);        
079        }
080        
081        public String getBeanId() {
082            return beanId;
083        }
084        
085        public void setBeanId(String id) {        
086            this.beanId = id;
087        }
088    }