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.FailedToCreateConsumerException;
024import org.apache.camel.component.bean.BeanProcessor;
025import org.apache.camel.spring.util.CamelContextResolverHelper;
026import org.apache.camel.support.CamelContextHelper;
027import org.apache.camel.support.service.ServiceHelper;
028import org.apache.camel.util.ObjectHelper;
029import org.springframework.beans.BeansException;
030import org.springframework.beans.factory.DisposableBean;
031import org.springframework.beans.factory.FactoryBean;
032import org.springframework.beans.factory.InitializingBean;
033import org.springframework.context.ApplicationContext;
034import org.springframework.context.ApplicationContextAware;
035import org.springframework.remoting.support.RemoteExporter;
036
037import static org.apache.camel.util.ObjectHelper.notNull;
038
039/**
040 * A {@link FactoryBean} to create a proxy to a service exposing a given {@link #getServiceInterface()}
041 */
042public class CamelServiceExporter extends RemoteExporter implements InitializingBean, DisposableBean, ApplicationContextAware, CamelContextAware {
043    private String uri;
044    private CamelContext camelContext;
045    private String camelContextId;
046    private Consumer consumer;
047    private String serviceRef;
048    private String method;
049    private ApplicationContext applicationContext;
050
051    public String getUri() {
052        return uri;
053    }
054
055    public void setUri(String uri) {
056        this.uri = uri;
057    }
058
059    @Override
060    public CamelContext getCamelContext() {
061        return camelContext;
062    }
063
064    @Override
065    public void setCamelContext(CamelContext camelContext) {
066        this.camelContext = camelContext;
067    }
068    
069    public void setCamelContextId(String camelContextId) {
070        this.camelContextId = camelContextId;
071    }
072
073    public String getServiceRef() {
074        return serviceRef;
075    }
076
077    public void setServiceRef(String serviceRef) {
078        this.serviceRef = serviceRef;
079    }
080
081    public String getMethod() {
082        return method;
083    }
084
085    public void setMethod(String method) {
086        this.method = method;
087    }
088
089    public ApplicationContext getApplicationContext() {
090        return applicationContext;
091    }
092
093    @Override
094    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
095        this.applicationContext = applicationContext;
096    }
097
098    @Override
099    public void afterPropertiesSet() throws Exception {
100        // lets bind the URI to a pojo
101        notNull(uri, "uri");
102        // Always resolve the camel context by using the camelContextID
103        if (ObjectHelper.isNotEmpty(camelContextId)) {
104            camelContext = CamelContextResolverHelper.getCamelContextWithId(applicationContext, camelContextId);
105        }
106        notNull(camelContext, "camelContext");
107        if (serviceRef != null && getService() == null && applicationContext != null) {
108            setService(applicationContext.getBean(serviceRef));
109        }
110
111        Endpoint endpoint = CamelContextHelper.getMandatoryEndpoint(camelContext, uri);
112        notNull(getService(), "service");
113        Object proxy = getProxyForService();
114
115        try {
116            // need to start endpoint before we create consumer
117            ServiceHelper.initService(endpoint);
118            BeanProcessor processor = new BeanProcessor(proxy, camelContext);
119            processor.setMethod(method);
120            consumer = endpoint.createConsumer(processor);
121            // add and start consumer
122            camelContext.addService(consumer, true, false);
123        } catch (Exception e) {
124            throw new FailedToCreateConsumerException(endpoint, e);
125        }
126    }
127
128    @Override
129    public void destroy() throws Exception {
130        // we let CamelContext manage the lifecycle of the consumer and shut it down when Camel stops
131    }
132
133}