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.blueprint;
018    
019    import javax.xml.bind.annotation.XmlAccessType;
020    import javax.xml.bind.annotation.XmlAccessorType;
021    import javax.xml.bind.annotation.XmlAttribute;
022    import javax.xml.bind.annotation.XmlRootElement;
023    import javax.xml.bind.annotation.XmlTransient;
024    
025    import org.apache.aries.blueprint.ExtendedBlueprintContainer;
026    import org.apache.camel.CamelContext;
027    import org.apache.camel.Endpoint;
028    import org.apache.camel.FailedToCreateProducerException;
029    import org.apache.camel.Producer;
030    import org.apache.camel.component.bean.ProxyHelper;
031    import org.apache.camel.core.xml.AbstractCamelFactoryBean;
032    import org.apache.camel.util.ServiceHelper;
033    
034    /**
035     * A factory to create a Proxy to a a Camel Pojo Endpoint.
036     */
037    @XmlRootElement(name = "proxy")
038    @XmlAccessorType(XmlAccessType.FIELD)
039    public class CamelProxyFactoryBean extends AbstractCamelFactoryBean<Object> {
040    
041        @XmlAttribute
042        private String serviceUrl;
043        @XmlAttribute
044        private String serviceRef;
045        @XmlAttribute
046        private String serviceInterface;
047        @XmlTransient
048        private Endpoint endpoint;
049        @XmlTransient
050        private Object serviceProxy;
051        @XmlTransient
052        private Producer producer;
053        @XmlTransient
054        private ExtendedBlueprintContainer blueprintContainer;
055    
056        public Object getObject() {
057            return serviceProxy;
058        }
059    
060        public Class<Object> getObjectType() {
061            return Object.class;
062        }
063    
064        protected CamelContext getCamelContextWithId(String camelContextId) {
065            if (blueprintContainer != null) {
066                return (CamelContext) blueprintContainer.getComponentInstance(camelContextId);
067            }
068            return null;
069        }
070    
071        public void afterPropertiesSet() throws Exception {
072            if (endpoint == null) {
073                getCamelContext();
074                if (getServiceUrl() == null && getServiceRef() == null) {
075                    throw new IllegalArgumentException("serviceUrl or serviceRef must be specified.");
076                }
077                if (getServiceInterface() == null) {
078                    throw new IllegalArgumentException("serviceInterface must be specified.");
079                }
080    
081                // lookup endpoint or we have the url for it
082                if (getServiceRef() != null) {
083                    endpoint = getCamelContext().getRegistry().lookup(getServiceRef(), Endpoint.class);
084                } else {
085                    endpoint = getCamelContext().getEndpoint(getServiceUrl());
086                }
087    
088                if (endpoint == null) {
089                    throw new IllegalArgumentException("Could not resolve endpoint: " + getServiceUrl());
090                }
091            }
092    
093            try {
094                producer = endpoint.createProducer();
095                ServiceHelper.startService(producer);
096                Class clazz = blueprintContainer.loadClass(getServiceInterface());
097                serviceProxy = ProxyHelper.createProxy(endpoint, producer, clazz);
098            } catch (Exception e) {
099                throw new FailedToCreateProducerException(endpoint, e);
100            }
101        }
102    
103        public void destroy() throws Exception {
104            ServiceHelper.stopService(producer);
105        }
106    
107        public String getServiceUrl() {
108            return serviceUrl;
109        }
110    
111        public void setServiceUrl(String serviceUrl) {
112            this.serviceUrl = serviceUrl;
113        }
114    
115        public String getServiceRef() {
116            return serviceRef;
117        }
118    
119        public void setServiceRef(String serviceRef) {
120            this.serviceRef = serviceRef;
121        }
122    
123        public String getServiceInterface() {
124            return serviceInterface;
125        }
126    
127        public void setServiceInterface(String serviceInterface) {
128            this.serviceInterface = serviceInterface;
129        }
130    
131        public Endpoint getEndpoint() {
132            return endpoint;
133        }
134    
135        public void setEndpoint(Endpoint endpoint) {
136            this.endpoint = endpoint;
137        }
138    
139        public Producer getProducer() {
140            return producer;
141        }
142    
143        public void setProducer(Producer producer) {
144            this.producer = producer;
145        }
146    
147        public ExtendedBlueprintContainer getBlueprintContainer() {
148            return blueprintContainer;
149        }
150    
151        public void setBlueprintContainer(ExtendedBlueprintContainer blueprintContainer) {
152            this.blueprintContainer = blueprintContainer;
153        }
154    
155    }