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