001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.spring.remoting;
019    
020    import org.apache.camel.CamelContext;
021    import org.apache.camel.Endpoint;
022    import org.apache.camel.component.pojo.PojoComponent;
023    import org.springframework.beans.factory.FactoryBean;
024    import org.springframework.remoting.support.UrlBasedRemoteAccessor;
025    
026    /**
027     * Creates a Proxy to Camel Pojo Endpoint.
028     *  
029     * @author chirino
030     */
031    public class CamelProxyFactoryBean extends UrlBasedRemoteAccessor implements FactoryBean {
032    
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 = PojoComponent.createProxy(endpoint, getServiceInterface());
053                    } catch (Exception e) {
054                            throw new IllegalArgumentException(e);
055                    }
056            }
057            
058            public Object getObject() throws Exception {
059                    return serviceProxy;
060            }
061    
062            public Class getObjectType() {
063                    return getServiceInterface();
064            }
065    
066            public boolean isSingleton() {
067                    return true;
068            }
069    
070            public Endpoint getEndpoint() {
071                    return endpoint;
072            }
073    
074            public void setEndpoint(Endpoint endpoint) {
075                    this.endpoint = endpoint;
076            }
077    
078            public CamelContext getCamelContext() {
079                    return camelContext;
080            }
081    
082            public void setCamelContext(CamelContext camelContext) {
083                    this.camelContext = camelContext;
084            }
085            
086    }