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.util.CamelContextHelper; 027import org.apache.camel.util.ObjectHelper; 028import org.apache.camel.util.ServiceHelper; 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 ApplicationContext applicationContext; 049 050 public String getUri() { 051 return uri; 052 } 053 054 public void setUri(String uri) { 055 this.uri = uri; 056 } 057 058 public CamelContext getCamelContext() { 059 return camelContext; 060 } 061 062 public void setCamelContext(CamelContext camelContext) { 063 this.camelContext = camelContext; 064 } 065 066 public void setCamelContextId(String camelContextId) { 067 this.camelContextId = camelContextId; 068 } 069 070 public String getServiceRef() { 071 return serviceRef; 072 } 073 074 public void setServiceRef(String serviceRef) { 075 this.serviceRef = serviceRef; 076 } 077 078 public ApplicationContext getApplicationContext() { 079 return applicationContext; 080 } 081 082 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 083 this.applicationContext = applicationContext; 084 } 085 086 public void afterPropertiesSet() throws Exception { 087 // lets bind the URI to a pojo 088 notNull(uri, "uri"); 089 // Always resolve the camel context by using the camelContextID 090 if (ObjectHelper.isNotEmpty(camelContextId)) { 091 camelContext = CamelContextResolverHelper.getCamelContextWithId(applicationContext, camelContextId); 092 } 093 notNull(camelContext, "camelContext"); 094 if (serviceRef != null && getService() == null && applicationContext != null) { 095 setService(applicationContext.getBean(serviceRef)); 096 } 097 098 Endpoint endpoint = CamelContextHelper.getMandatoryEndpoint(camelContext, uri); 099 notNull(getService(), "service"); 100 Object proxy = getProxyForService(); 101 102 try { 103 // need to start endpoint before we create consumer 104 ServiceHelper.startService(endpoint); 105 consumer = endpoint.createConsumer(new BeanProcessor(proxy, camelContext)); 106 // add and start consumer 107 camelContext.addService(consumer, true, true); 108 } catch (Exception e) { 109 throw new FailedToCreateConsumerException(endpoint, e); 110 } 111 } 112 113 public void destroy() throws Exception { 114 // we let CamelContext manage the lifecycle of the consumer and shut it down when Camel stops 115 } 116 117}