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.util.ObjectHelper; 027import org.apache.camel.util.ServiceHelper; 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 public void destroy() throws Exception { 090 // we let CamelContext manage the lifecycle of the producer and shut it down when Camel stops 091 } 092 093 public Class<?> getServiceInterface() { 094 return super.getServiceInterface(); 095 } 096 097 public String getServiceUrl() { 098 return super.getServiceUrl(); 099 } 100 101 public Object getObject() throws Exception { 102 return serviceProxy; 103 } 104 105 public Class<?> getObjectType() { 106 return getServiceInterface(); 107 } 108 109 public boolean isSingleton() { 110 return true; 111 } 112 113 public String getServiceRef() { 114 return serviceRef; 115 } 116 117 public void setServiceRef(String serviceRef) { 118 this.serviceRef = serviceRef; 119 } 120 121 public Boolean getBinding() { 122 return binding; 123 } 124 125 public void setBinding(Boolean binding) { 126 this.binding = binding; 127 } 128 129 public Endpoint getEndpoint() { 130 return endpoint; 131 } 132 133 public void setEndpoint(Endpoint endpoint) { 134 this.endpoint = endpoint; 135 } 136 137 public CamelContext getCamelContext() { 138 return camelContext; 139 } 140 141 public void setCamelContext(CamelContext camelContext) { 142 this.camelContext = camelContext; 143 } 144 145 public void setCamelContextId(String contextId) { 146 this.camelContextId = contextId; 147 } 148 149 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 150 this.applicationContext = applicationContext; 151 } 152 153}