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.component.bean.ProxyHelper;
023    
024    import org.springframework.beans.factory.FactoryBean;
025    import org.springframework.remoting.support.UrlBasedRemoteAccessor;
026    
027    /**
028     * A {@link FactoryBean} to create a Proxy to a a Camel Pojo Endpoint.
029     * 
030     * @author chirino
031     */
032    public class CamelProxyFactoryBean extends UrlBasedRemoteAccessor implements FactoryBean, CamelContextAware {
033        private CamelContext camelContext;
034        private Endpoint endpoint;
035        private Object serviceProxy;
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.serviceProxy = ProxyHelper.createProxy(endpoint, getServiceInterface());
053            } catch (Exception e) {
054                throw new IllegalArgumentException(e);
055            }
056        }
057    
058        public Class getServiceInterface() {
059            return super.getServiceInterface();
060        }
061    
062        public String getServiceUrl() {
063            return super.getServiceUrl();
064        }
065    
066        public Object getObject() throws Exception {
067            return serviceProxy;
068        }
069    
070        public Class getObjectType() {
071            return getServiceInterface();
072        }
073    
074        public boolean isSingleton() {
075            return true;
076        }
077    
078        public Endpoint getEndpoint() {
079            return endpoint;
080        }
081    
082        public void setEndpoint(Endpoint endpoint) {
083            this.endpoint = endpoint;
084        }
085    
086        public CamelContext getCamelContext() {
087            return camelContext;
088        }
089    
090        public void setCamelContext(CamelContext camelContext) {
091            this.camelContext = camelContext;
092        }
093    }