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 @SuppressWarnings("unchecked") 048 public void afterPropertiesSet() { 049 if (endpoint == null) { 050 if (camelContext == null && camelContextId != null) { 051 camelContext = CamelContextResolverHelper.getCamelContextWithId(applicationContext, camelContextId); 052 } 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().lookup(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 try { 075 producer = endpoint.createProducer(); 076 ServiceHelper.startService(producer); 077 serviceProxy = ProxyHelper.createProxy(endpoint, producer, getServiceInterface()); 078 } catch (Exception e) { 079 throw new FailedToCreateProducerException(endpoint, e); 080 } 081 } 082 083 public void destroy() throws Exception { 084 ServiceHelper.stopService(producer); 085 } 086 087 public Class getServiceInterface() { 088 return super.getServiceInterface(); 089 } 090 091 public String getServiceUrl() { 092 return super.getServiceUrl(); 093 } 094 095 public Object getObject() throws Exception { 096 return serviceProxy; 097 } 098 099 public Class getObjectType() { 100 return getServiceInterface(); 101 } 102 103 public boolean isSingleton() { 104 return true; 105 } 106 107 public String getServiceRef() { 108 return serviceRef; 109 } 110 111 public void setServiceRef(String serviceRef) { 112 this.serviceRef = serviceRef; 113 } 114 115 public Endpoint getEndpoint() { 116 return endpoint; 117 } 118 119 public void setEndpoint(Endpoint endpoint) { 120 this.endpoint = endpoint; 121 } 122 123 public CamelContext getCamelContext() { 124 return camelContext; 125 } 126 127 public void setCamelContext(CamelContext camelContext) { 128 this.camelContext = camelContext; 129 } 130 131 public void setCamelContextId(String contextId) { 132 this.camelContextId = contextId; 133 } 134 135 public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 136 this.applicationContext = applicationContext; 137 } 138 139 }