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.springframework.beans.BeansException; 027 import org.springframework.beans.factory.DisposableBean; 028 import org.springframework.beans.factory.FactoryBean; 029 import org.springframework.context.ApplicationContext; 030 import org.springframework.context.ApplicationContextAware; 031 import org.springframework.remoting.support.UrlBasedRemoteAccessor; 032 033 /** 034 * A {@link FactoryBean} to create a Proxy to a a Camel Pojo Endpoint. 035 */ 036 public class CamelProxyFactoryBean extends UrlBasedRemoteAccessor implements FactoryBean, CamelContextAware, DisposableBean, ApplicationContextAware { 037 private String serviceRef; 038 private CamelContext camelContext; 039 private String camelContextId; 040 private ApplicationContext applicationContext; 041 private Endpoint endpoint; 042 private Object serviceProxy; 043 private Producer producer; 044 045 @Override 046 @SuppressWarnings("unchecked") 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 this.producer = endpoint.createProducer(); 075 this.producer.start(); 076 this.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 this.producer.stop(); 084 this.producer = null; 085 this.serviceProxy = null; 086 } 087 088 public Class getServiceInterface() { 089 return super.getServiceInterface(); 090 } 091 092 public String getServiceUrl() { 093 return super.getServiceUrl(); 094 } 095 096 public Object getObject() throws Exception { 097 return serviceProxy; 098 } 099 100 public Class getObjectType() { 101 return getServiceInterface(); 102 } 103 104 public boolean isSingleton() { 105 return true; 106 } 107 108 public String getServiceRef() { 109 return serviceRef; 110 } 111 112 public void setServiceRef(String serviceRef) { 113 this.serviceRef = serviceRef; 114 } 115 116 public Endpoint getEndpoint() { 117 return endpoint; 118 } 119 120 public void setEndpoint(Endpoint endpoint) { 121 this.endpoint = endpoint; 122 } 123 124 public CamelContext getCamelContext() { 125 return camelContext; 126 } 127 128 public void setCamelContext(CamelContext camelContext) { 129 this.camelContext = camelContext; 130 } 131 132 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 133 this.applicationContext = applicationContext; 134 } 135 136 }