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    
020    import org.apache.camel.CamelContext;
021    import org.apache.camel.CamelContextAware;
022    import org.apache.camel.Consumer;
023    import org.apache.camel.Endpoint;
024    import org.apache.camel.component.bean.BeanProcessor;
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     * @author chirino
040     */
041    public class CamelServiceExporter extends RemoteExporter implements InitializingBean, DisposableBean, ApplicationContextAware, CamelContextAware {
042        private String uri;
043        private CamelContext camelContext;
044        private Consumer consumer;
045        private String serviceRef;
046        private ApplicationContext applicationContext;
047    
048        public String getUri() {
049            return uri;
050        }
051    
052        public void setUri(String uri) {
053            this.uri = uri;
054        }
055    
056        public CamelContext getCamelContext() {
057            return camelContext;
058        }
059    
060        public void setCamelContext(CamelContext camelContext) {
061            this.camelContext = camelContext;
062        }
063    
064        public String getServiceRef() {
065            return serviceRef;
066        }
067    
068        public void setServiceRef(String serviceRef) {
069            this.serviceRef = serviceRef;
070        }
071    
072        public ApplicationContext getApplicationContext() {
073            return applicationContext;
074        }
075    
076        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
077            this.applicationContext = applicationContext;
078        }
079    
080        public void afterPropertiesSet() throws Exception {
081            // lets bind the URI to a pojo
082            notNull(uri, "uri");
083            notNull(camelContext, "camelContext");
084            if (serviceRef != null && getService() == null && applicationContext != null) {
085                setService(applicationContext.getBean(serviceRef));
086            }
087    
088            Endpoint endpoint = CamelContextHelper.getMandatoryEndpoint(camelContext, uri);
089            Object proxy = getProxyForService();
090    
091            consumer = endpoint.createConsumer(new BeanProcessor(proxy, camelContext));
092            consumer.start();
093        }
094    
095        public void destroy() throws Exception {
096            if (consumer != null) {
097                consumer.stop();
098            }
099        }
100    
101    }