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;
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.camel.CamelContext;
026    import org.apache.camel.CamelContextAware;
027    import org.apache.camel.Endpoint;
028    import org.apache.camel.NoSuchEndpointException;
029    import org.apache.camel.model.IdentifiedType;
030    import org.apache.camel.spring.util.CamelContextResolverHelper;
031    import org.springframework.beans.BeansException;
032    import org.springframework.beans.factory.FactoryBean;
033    import org.springframework.context.ApplicationContext;
034    import org.springframework.context.ApplicationContextAware;
035    
036    import static org.apache.camel.util.ObjectHelper.notNull;
037    
038    /**
039     * A {@link FactoryBean} which instantiates {@link Endpoint} objects
040     *
041     * @version $Revision: 941822 $
042     */
043    @XmlRootElement(name = "endpoint")
044    @XmlAccessorType(XmlAccessType.FIELD)
045    public class CamelEndpointFactoryBean extends IdentifiedType implements FactoryBean, CamelContextAware, ApplicationContextAware {
046        @XmlAttribute
047        private String uri;
048        @XmlAttribute
049        private String camelContextId;
050        @XmlTransient
051        private CamelContext context;    
052        @XmlTransient
053        private Endpoint endpoint;
054        @XmlTransient
055        private ApplicationContext applicationContext;
056    
057        public Object getObject() throws Exception {
058            if (endpoint == null) {
059                endpoint = createEndpoint();
060            }
061            return endpoint;
062        }
063    
064        public Class getObjectType() {
065            return Endpoint.class;
066        }
067        
068        public boolean isSingleton() {
069            return true;
070        }
071        
072        public CamelContext getCamelContext() {
073            return context;
074        }
075        
076    
077        /**
078         * Sets the context to use to resolve endpoints
079         *
080         * @param context the context used to resolve endpoints
081         */
082        public void setCamelContext(CamelContext context) {
083            this.context = context;
084        }
085    
086        public String getUri() {
087            return uri;
088        }
089    
090        /**
091         * Sets the URI to use to resolve the endpoint
092         *
093         * @param uri the URI used to set the endpoint
094         */
095        public void setUri(String uri) {
096            this.uri = uri;
097        }
098    
099        protected Endpoint createEndpoint() {
100            if (context == null && camelContextId != null) {
101                context = CamelContextResolverHelper.getCamelContextWithId(applicationContext, camelContextId);
102            }
103            notNull(context, "context");
104            notNull(uri, "uri");
105            
106            Endpoint endpoint = context.getEndpoint(uri);
107            if (endpoint == null) {
108                throw new NoSuchEndpointException(uri);
109            }
110            return endpoint;
111        }
112    
113        public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
114            this.applicationContext = applicationContext;
115        }
116    
117        public void setCamelContextId(String camelContextId) {
118            this.camelContextId = camelContextId;
119        }
120       
121    }