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.spring.remoting;
018    
019    import org.apache.camel.CamelContext;
020    import org.apache.camel.CamelContextAware;
021    import org.apache.camel.Consumer;
022    import org.apache.camel.Endpoint;
023    import org.apache.camel.component.bean.BeanProcessor;
024    import org.apache.camel.spring.util.CamelContextResolverHelper;
025    import org.apache.camel.util.CamelContextHelper;
026    import org.springframework.beans.BeansException;
027    import org.springframework.beans.factory.DisposableBean;
028    import org.springframework.beans.factory.FactoryBean;
029    import org.springframework.beans.factory.InitializingBean;
030    import org.springframework.context.ApplicationContext;
031    import org.springframework.context.ApplicationContextAware;
032    import org.springframework.remoting.support.RemoteExporter;
033    
034    import static org.apache.camel.util.ObjectHelper.notNull;
035    
036    /**
037     * A {@link FactoryBean} to create a proxy to a service exposing a given {@link #getServiceInterface()}
038     */
039    public class CamelServiceExporter extends RemoteExporter implements InitializingBean, DisposableBean, ApplicationContextAware, CamelContextAware {
040        private String uri;
041        private CamelContext camelContext;
042        private String camelContextId;
043        private Consumer consumer;
044        private String serviceRef;
045        private ApplicationContext applicationContext;
046    
047        public String getUri() {
048            return uri;
049        }
050    
051        public void setUri(String uri) {
052            this.uri = uri;
053        }
054    
055        public CamelContext getCamelContext() {
056            return camelContext;
057        }
058    
059        public void setCamelContext(CamelContext camelContext) {
060            this.camelContext = camelContext;
061        }
062        
063        public void setCamelContextId(String camelContextId) {
064            this.camelContextId = camelContextId;
065        }
066    
067        public String getServiceRef() {
068            return serviceRef;
069        }
070    
071        public void setServiceRef(String serviceRef) {
072            this.serviceRef = serviceRef;
073        }
074    
075        public ApplicationContext getApplicationContext() {
076            return applicationContext;
077        }
078    
079        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
080            this.applicationContext = applicationContext;
081        }
082    
083        public void afterPropertiesSet() throws Exception {
084            // lets bind the URI to a pojo
085            notNull(uri, "uri");
086            if (camelContext == null && camelContextId != null) {
087                camelContext = CamelContextResolverHelper.getCamelContextWithId(applicationContext, camelContextId);
088            }
089            notNull(camelContext, "camelContext");
090            if (serviceRef != null && getService() == null && applicationContext != null) {
091                setService(applicationContext.getBean(serviceRef));
092            }
093    
094            Endpoint endpoint = CamelContextHelper.getMandatoryEndpoint(camelContext, uri);
095            notNull(getService(), "service");
096            Object proxy = getProxyForService();
097    
098            consumer = endpoint.createConsumer(new BeanProcessor(proxy, camelContext));
099            consumer.start();
100        }
101    
102        public void destroy() throws Exception {
103            if (consumer != null) {
104                consumer.stop();
105            }
106        }
107    
108    }