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.util.CamelContextHelper;
025    
026    import org.springframework.beans.BeansException;
027    import org.springframework.beans.factory.DisposableBean;
028    import org.springframework.beans.factory.InitializingBean;
029    import org.springframework.context.ApplicationContext;
030    import org.springframework.context.ApplicationContextAware;
031    import org.springframework.remoting.support.RemoteExporter;
032    
033    import static org.apache.camel.util.ObjectHelper.notNull;
034    
035    /**
036     * A {@link FactoryBean} to create a proxy to a service exposing a given {@link #getServiceInterface()}
037     *
038     * @author chirino
039     */
040    public class CamelServiceExporter extends RemoteExporter implements InitializingBean, DisposableBean, ApplicationContextAware, CamelContextAware {
041        private String uri;
042        private CamelContext camelContext;
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 String getServiceRef() {
064            return serviceRef;
065        }
066    
067        public void setServiceRef(String serviceRef) {
068            this.serviceRef = serviceRef;
069        }
070    
071        public ApplicationContext getApplicationContext() {
072            return applicationContext;
073        }
074    
075        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
076            this.applicationContext = applicationContext;
077        }
078    
079        public void afterPropertiesSet() throws Exception {
080            // lets bind the URI to a pojo
081            notNull(uri, "uri");
082            notNull(camelContext, "camelContext");
083            if (serviceRef != null && getService() == null && applicationContext != null) {
084                setService(applicationContext.getBean(serviceRef));
085            }
086    
087            Endpoint endpoint = CamelContextHelper.getMandatoryEndpoint(camelContext, uri);
088            Object proxy = getProxyForService();
089    
090            consumer = endpoint.createConsumer(new BeanProcessor(proxy, camelContext));
091            consumer.start();
092        }
093    
094        public void destroy() throws Exception {
095            if (consumer != null) {
096                consumer.stop();
097            }
098        }
099    
100    }