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