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        public void afterPropertiesSet() {
039            super.afterPropertiesSet();
040            try {
041                if (endpoint == null) {
042                    if (getServiceUrl() == null || camelContext == null) {
043                        throw new IllegalArgumentException("If endpoint is not specified, the serviceUrl and camelContext must be specified.");
044                    }
045    
046                    endpoint = camelContext.getEndpoint(getServiceUrl());
047                    if (endpoint == null) {
048                        throw new IllegalArgumentException("Could not resolve endpoint: " + getServiceUrl());
049                    }
050                }
051    
052                this.producer = endpoint.createProducer();
053                this.producer.start();
054                this.serviceProxy = ProxyHelper.createProxy(endpoint, producer, getServiceInterface());
055            } catch (Exception e) {
056                throw new IllegalArgumentException(e);
057            }
058        }
059    
060        public void destroy() throws Exception {
061            this.producer.stop();
062            this.producer = null;
063            this.serviceProxy = null;
064        }
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    }