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
020 import javax.xml.bind.annotation.XmlAccessType;
021 import javax.xml.bind.annotation.XmlAccessorType;
022 import javax.xml.bind.annotation.XmlAttribute;
023 import javax.xml.bind.annotation.XmlRootElement;
024 import javax.xml.bind.annotation.XmlTransient;
025
026 import org.apache.camel.CamelContext;
027 import org.apache.camel.CamelContextAware;
028 import org.apache.camel.Endpoint;
029 import org.apache.camel.NoSuchEndpointException;
030 import org.apache.camel.model.IdentifiedType;
031 import org.springframework.beans.factory.FactoryBean;
032
033 import static org.apache.camel.util.ObjectHelper.notNull;
034
035 /**
036 * A {@link FactoryBean} which instantiates {@link Endpoint} objects
037 *
038 * @version $Revision: 641676 $
039 */
040 @XmlRootElement(name = "endpoint")
041 @XmlAccessorType(XmlAccessType.FIELD)
042 public class EndpointFactoryBean extends IdentifiedType implements FactoryBean, CamelContextAware {
043 @XmlAttribute
044 private String uri;
045 @XmlTransient
046 private CamelContext context;
047 @XmlTransient
048 private Endpoint endpoint;
049 @XmlTransient
050 private boolean singleton;
051
052 public Object getObject() throws Exception {
053 if (endpoint == null) {
054 endpoint = createEndpoint();
055 }
056 return endpoint;
057 }
058
059 public Class getObjectType() {
060 return Endpoint.class;
061 }
062
063 public boolean isSingleton() {
064 return singleton;
065 }
066
067 public CamelContext getCamelContext() {
068 return context;
069 }
070
071 /**
072 * Sets the context to use to resolve endpoints
073 *
074 * @param context the context used to resolve endpoints
075 */
076 public void setCamelContext(CamelContext context) {
077 this.context = context;
078 }
079
080 public Endpoint getEndpoint() {
081 return endpoint;
082 }
083
084 public void setEndpoint(Endpoint endpoint) {
085 this.endpoint = endpoint;
086 }
087
088 public void setSingleton(boolean singleton) {
089 this.singleton = singleton;
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 protected Endpoint createEndpoint() {
106 notNull(context, "context");
107 notNull(uri, "uri");
108 Endpoint endpoint = context.getEndpoint(uri);
109 if (endpoint == null) {
110 throw new NoSuchEndpointException(uri);
111 }
112 return endpoint;
113 }
114 }