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 */
017package org.apache.camel.spring.remoting;
018
019import org.apache.camel.CamelContext;
020import org.apache.camel.CamelContextAware;
021import org.apache.camel.Endpoint;
022import org.apache.camel.FailedToCreateProducerException;
023import org.apache.camel.Producer;
024import org.apache.camel.component.bean.ProxyHelper;
025import org.apache.camel.spring.util.CamelContextResolverHelper;
026import org.apache.camel.util.ObjectHelper;
027import org.apache.camel.util.ServiceHelper;
028import org.springframework.beans.BeansException;
029import org.springframework.beans.factory.DisposableBean;
030import org.springframework.beans.factory.FactoryBean;
031import org.springframework.context.ApplicationContext;
032import org.springframework.context.ApplicationContextAware;
033import org.springframework.remoting.support.UrlBasedRemoteAccessor;
034
035/**
036 * A {@link FactoryBean} to create a Proxy to a a Camel Pojo Endpoint.
037 */
038public class CamelProxyFactoryBean extends UrlBasedRemoteAccessor implements FactoryBean<Object>, CamelContextAware, DisposableBean, ApplicationContextAware {
039    private String serviceRef;
040    private CamelContext camelContext;
041    private String camelContextId;
042    private Boolean binding;
043    private ApplicationContext applicationContext;
044    private Endpoint endpoint;
045    private Object serviceProxy;
046    private Producer producer;
047
048    @Override
049    public void afterPropertiesSet() {
050        if (endpoint == null) {
051            if (ObjectHelper.isNotEmpty(camelContextId)) {
052                camelContext = CamelContextResolverHelper.getCamelContextWithId(applicationContext, camelContextId);
053            }
054            if (camelContext == null) {
055                throw new IllegalArgumentException("camelContext or camelContextId must be specified");
056            }
057            
058            if (getServiceUrl() == null && getServiceRef() == null) {
059                throw new IllegalArgumentException("serviceUrl or serviceRef must be specified.");
060            }
061
062            // lookup endpoint or we have the url for it
063            if (getServiceRef() != null) {
064                endpoint = camelContext.getRegistry().lookupByNameAndType(getServiceRef(), Endpoint.class);
065            } else {
066                endpoint = camelContext.getEndpoint(getServiceUrl());
067            }
068
069            if (endpoint == null) {
070                throw new IllegalArgumentException("Could not resolve endpoint: " + getServiceUrl());
071            }
072        }
073
074        // binding is enabled by default
075        boolean bind = getBinding() != null ? getBinding() : true;
076
077        try {
078            producer = endpoint.createProducer();
079            ServiceHelper.startService(producer);
080            serviceProxy = ProxyHelper.createProxy(endpoint, bind, producer, getServiceInterface());
081        } catch (Exception e) {
082            throw new FailedToCreateProducerException(endpoint, e);
083        }
084    }
085
086    public void destroy() throws Exception {
087        ServiceHelper.stopService(producer);
088    }
089
090    public Class<?> getServiceInterface() {
091        return super.getServiceInterface();
092    }
093
094    public String getServiceUrl() {
095        return super.getServiceUrl();
096    }
097
098    public Object getObject() throws Exception {
099        return serviceProxy;
100    }
101
102    public Class<?> getObjectType() {
103        return getServiceInterface();
104    }
105
106    public boolean isSingleton() {
107        return true;
108    }
109
110    public String getServiceRef() {
111        return serviceRef;
112    }
113
114    public void setServiceRef(String serviceRef) {
115        this.serviceRef = serviceRef;
116    }
117
118    public Boolean getBinding() {
119        return binding;
120    }
121
122    public void setBinding(Boolean binding) {
123        this.binding = binding;
124    }
125
126    public Endpoint getEndpoint() {
127        return endpoint;
128    }
129
130    public void setEndpoint(Endpoint endpoint) {
131        this.endpoint = endpoint;
132    }
133
134    public CamelContext getCamelContext() {
135        return camelContext;
136    }
137
138    public void setCamelContext(CamelContext camelContext) {
139        this.camelContext = camelContext;
140    }
141    
142    public void setCamelContextId(String contextId) {
143        this.camelContextId = contextId;
144    }
145
146    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
147        this.applicationContext = applicationContext;
148    }
149
150}