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 public class CamelServiceExporter extends RemoteExporter implements InitializingBean, DisposableBean, ApplicationContextAware, CamelContextAware {
040 private String uri;
041 private CamelContext camelContext;
042 private Consumer consumer;
043 private String serviceRef;
044 private ApplicationContext applicationContext;
045
046 public String getUri() {
047 return uri;
048 }
049
050 public void setUri(String uri) {
051 this.uri = uri;
052 }
053
054 public CamelContext getCamelContext() {
055 return camelContext;
056 }
057
058 public void setCamelContext(CamelContext camelContext) {
059 this.camelContext = camelContext;
060 }
061
062 public String getServiceRef() {
063 return serviceRef;
064 }
065
066 public void setServiceRef(String serviceRef) {
067 this.serviceRef = serviceRef;
068 }
069
070 public ApplicationContext getApplicationContext() {
071 return applicationContext;
072 }
073
074 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
075 this.applicationContext = applicationContext;
076 }
077
078 public void afterPropertiesSet() throws Exception {
079 // lets bind the URI to a pojo
080 notNull(uri, "uri");
081 notNull(camelContext, "camelContext");
082 if (serviceRef != null && getService() == null && applicationContext != null) {
083 setService(applicationContext.getBean(serviceRef));
084 }
085
086 Endpoint endpoint = CamelContextHelper.getMandatoryEndpoint(camelContext, uri);
087 notNull(camelContext, "service");
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 }