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.Endpoint; 022import org.apache.camel.FailedToCreateProducerException; 023import org.apache.camel.Producer; 024import org.apache.camel.component.bean.ProxyHelper; 025import org.apache.camel.spring.util.CamelContextResolverHelper; 026import org.apache.camel.support.service.ServiceHelper; 027import org.apache.camel.util.ObjectHelper; 028import org.springframework.beans.BeansException; 029import org.springframework.beans.factory.DisposableBean; 030import org.springframework.beans.factory.FactoryBean; 031import org.springframework.context.ApplicationContext; 032import org.springframework.context.ApplicationContextAware; 033import org.springframework.remoting.support.UrlBasedRemoteAccessor; 034 035/** 036 * A {@link FactoryBean} to create a Proxy to a a Camel Pojo Endpoint. 037 */ 038public class CamelProxyFactoryBean extends UrlBasedRemoteAccessor 039 implements FactoryBean<Object>, CamelContextAware, DisposableBean, ApplicationContextAware { 040 private String serviceRef; 041 private CamelContext camelContext; 042 private String camelContextId; 043 private Boolean binding; 044 private ApplicationContext applicationContext; 045 private Endpoint endpoint; 046 private Object serviceProxy; 047 private Producer producer; 048 049 @Override 050 public void afterPropertiesSet() { 051 if (endpoint == null) { 052 if (ObjectHelper.isNotEmpty(camelContextId)) { 053 camelContext = CamelContextResolverHelper.getCamelContextWithId(applicationContext, camelContextId); 054 } 055 if (camelContext == null) { 056 throw new IllegalArgumentException("camelContext or camelContextId must be specified"); 057 } 058 059 if (getServiceUrl() == null && getServiceRef() == null) { 060 throw new IllegalArgumentException("serviceUrl or serviceRef must be specified."); 061 } 062 063 // lookup endpoint or we have the url for it 064 if (getServiceRef() != null) { 065 endpoint = camelContext.getRegistry().lookupByNameAndType(getServiceRef(), Endpoint.class); 066 } else { 067 endpoint = camelContext.getEndpoint(getServiceUrl()); 068 } 069 070 if (endpoint == null) { 071 throw new IllegalArgumentException("Could not resolve endpoint: " + getServiceUrl()); 072 } 073 } 074 075 // binding is enabled by default 076 boolean bind = getBinding() != null ? getBinding() : true; 077 078 try { 079 // need to start endpoint before we create producer 080 ServiceHelper.startService(endpoint); 081 producer = endpoint.createProducer(); 082 // add and start producer 083 camelContext.addService(producer, true, false); 084 serviceProxy = ProxyHelper.createProxy(endpoint, bind, producer, getServiceInterface()); 085 } catch (Exception e) { 086 throw new FailedToCreateProducerException(endpoint, e); 087 } 088 } 089 090 @Override 091 public void destroy() throws Exception { 092 // we let CamelContext manage the lifecycle of the producer and shut it down when Camel stops 093 } 094 095 @Override 096 public Class<?> getServiceInterface() { 097 return super.getServiceInterface(); 098 } 099 100 @Override 101 public String getServiceUrl() { 102 return super.getServiceUrl(); 103 } 104 105 @Override 106 public Object getObject() throws Exception { 107 return serviceProxy; 108 } 109 110 @Override 111 public Class<?> getObjectType() { 112 return getServiceInterface(); 113 } 114 115 @Override 116 public boolean isSingleton() { 117 return true; 118 } 119 120 public String getServiceRef() { 121 return serviceRef; 122 } 123 124 public void setServiceRef(String serviceRef) { 125 this.serviceRef = serviceRef; 126 } 127 128 public Boolean getBinding() { 129 return binding; 130 } 131 132 public void setBinding(Boolean binding) { 133 this.binding = binding; 134 } 135 136 public Endpoint getEndpoint() { 137 return endpoint; 138 } 139 140 public void setEndpoint(Endpoint endpoint) { 141 this.endpoint = endpoint; 142 } 143 144 @Override 145 public CamelContext getCamelContext() { 146 return camelContext; 147 } 148 149 @Override 150 public void setCamelContext(CamelContext camelContext) { 151 this.camelContext = camelContext; 152 } 153 154 public void setCamelContextId(String contextId) { 155 this.camelContextId = contextId; 156 } 157 158 @Override 159 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 160 this.applicationContext = applicationContext; 161 } 162 163}