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.Endpoint;
027    import org.apache.camel.model.IdentifiedType;
028    
029    import org.springframework.beans.factory.FactoryBean;
030    
031    import static org.apache.camel.util.ObjectHelper.notNull;
032    
033    /**
034     * A {@link FactoryBean} which instantiates {@link Endpoint} objects
035     *
036     * @version $Revision: 1.1 $
037     */
038    @XmlRootElement(name = "endpoint")
039    @XmlAccessorType(XmlAccessType.FIELD)
040    public class EndpointFactoryBean extends IdentifiedType implements FactoryBean {
041        @XmlAttribute
042        private String uri;
043        @XmlTransient
044        private CamelContext context;
045        @XmlTransient
046        private Endpoint endpoint;
047        @XmlTransient
048        private boolean singleton;
049    
050        public Object getObject() throws Exception {
051            if (endpoint == null) {
052                endpoint = createEndpoint();
053            }
054            return endpoint;
055        }
056    
057        public Class getObjectType() {
058            return Endpoint.class;
059        }
060    
061        public boolean isSingleton() {
062            return singleton;
063        }
064    
065        public CamelContext getContext() {
066            return context;
067        }
068    
069        /**
070         * Sets the context to use to resolve endpoints
071         *
072         * @param context the context used to resolve endpoints
073         */
074        public void setContext(CamelContext context) {
075            this.context = context;
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 void setSingleton(boolean singleton) {
087            this.singleton = singleton;
088        }
089    
090        public String getUri() {
091            return uri;
092        }
093    
094        /**
095         * Sets the URI to use to resolve the endpoint
096         *
097         * @param uri the URI used to set the endpoint
098         */
099        public void setUri(String uri) {
100            this.uri = uri;
101        }
102    
103        protected Endpoint createEndpoint() {
104            notNull(context, "context");
105            notNull(uri, "uri");
106            return context.getEndpoint(uri);
107        }
108    }