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.Producer;
023 import org.apache.camel.component.bean.ProxyHelper;
024 import org.springframework.beans.factory.DisposableBean;
025 import org.springframework.beans.factory.FactoryBean;
026 import org.springframework.remoting.support.UrlBasedRemoteAccessor;
027
028 /**
029 * A {@link FactoryBean} to create a Proxy to a a Camel Pojo Endpoint.
030 */
031 public class CamelProxyFactoryBean extends UrlBasedRemoteAccessor implements FactoryBean, CamelContextAware, DisposableBean {
032 private CamelContext camelContext;
033 private Endpoint endpoint;
034 private Object serviceProxy;
035 private Producer producer;
036
037 @Override
038 @SuppressWarnings("unchecked")
039 public void afterPropertiesSet() {
040 super.afterPropertiesSet();
041 try {
042 if (endpoint == null) {
043 if (getServiceUrl() == null || camelContext == null) {
044 throw new IllegalArgumentException("If endpoint is not specified, the serviceUrl and camelContext must be specified.");
045 }
046
047 endpoint = camelContext.getEndpoint(getServiceUrl());
048 if (endpoint == null) {
049 throw new IllegalArgumentException("Could not resolve endpoint: " + getServiceUrl());
050 }
051 }
052
053 this.producer = endpoint.createProducer();
054 this.producer.start();
055 this.serviceProxy = ProxyHelper.createProxy(endpoint, producer, getServiceInterface());
056 } catch (Exception e) {
057 throw new IllegalArgumentException(e);
058 }
059 }
060
061 public void destroy() throws Exception {
062 this.producer.stop();
063 this.producer = null;
064 this.serviceProxy = null;
065 }
066
067 public Class getServiceInterface() {
068 return super.getServiceInterface();
069 }
070
071 public String getServiceUrl() {
072 return super.getServiceUrl();
073 }
074
075 public Object getObject() throws Exception {
076 return serviceProxy;
077 }
078
079 public Class getObjectType() {
080 return getServiceInterface();
081 }
082
083 public boolean isSingleton() {
084 return true;
085 }
086
087 public Endpoint getEndpoint() {
088 return endpoint;
089 }
090
091 public void setEndpoint(Endpoint endpoint) {
092 this.endpoint = endpoint;
093 }
094
095 public CamelContext getCamelContext() {
096 return camelContext;
097 }
098
099 public void setCamelContext(CamelContext camelContext) {
100 this.camelContext = camelContext;
101 }
102
103 }