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