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