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.core.xml;
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.XmlTransient;
023    import javax.xml.bind.annotation.XmlType;
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    
031    import static org.apache.camel.util.ObjectHelper.notNull;
032    
033    
034    @XmlAccessorType(XmlAccessType.FIELD)
035    public abstract class AbstractCamelEndpointFactoryBean extends IdentifiedType implements CamelContextAware {
036        @XmlAttribute
037        @Deprecated
038        private Boolean singleton = Boolean.FALSE;
039        @XmlAttribute
040        private String uri;
041        @XmlAttribute
042        private String camelContextId;
043        @XmlTransient
044        private CamelContext context;
045        @XmlTransient
046        private Endpoint endpoint;
047    
048        public Object getObject() throws Exception {
049            if (endpoint == null || !endpoint.isSingleton()) {
050                if (context == null && camelContextId != null) {
051                    context = getCamelContextWithId(camelContextId);
052                }
053    
054                notNull(context, "context");
055                notNull(uri, "uri");
056    
057                endpoint = context.getEndpoint(uri);
058                if (endpoint == null) {
059                    throw new NoSuchEndpointException(uri);
060                }
061            }
062            return endpoint;
063        }
064    
065        protected abstract CamelContext getCamelContextWithId(String camelContextId);
066    
067        public Class getObjectType() {
068            return Endpoint.class;
069        }
070    
071        public boolean isSingleton() {
072            return false;
073        }
074    
075        public void setSingleton(boolean singleton) {
076        }
077    
078        public CamelContext getCamelContext() {
079            return context;
080        }
081    
082    
083        /**
084         * Sets the context to use to resolve endpoints
085         *
086         * @param context the context used to resolve endpoints
087         */
088        public void setCamelContext(CamelContext context) {
089            this.context = context;
090        }
091    
092        public String getUri() {
093            return uri;
094        }
095    
096        /**
097         * Sets the URI to use to resolve the endpoint
098         *
099         * @param uri the URI used to set the endpoint
100         */
101        public void setUri(String uri) {
102            this.uri = uri;
103        }
104    
105        public String getCamelContextId() {
106            return camelContextId;
107        }
108    
109        public void setCamelContextId(String camelContextId) {
110            this.camelContextId = camelContextId;
111        }
112    
113    }