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