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