001 /**
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one or more
004 * contributor license agreements. See the NOTICE file distributed with
005 * this work for additional information regarding copyright ownership.
006 * The ASF licenses this file to You under the Apache License, Version 2.0
007 * (the "License"); you may not use this file except in compliance with
008 * the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018 package org.apache.camel.spring;
019
020 import org.apache.camel.CamelContext;
021 import org.apache.camel.Endpoint;
022 import static org.apache.camel.util.ObjectHelper.notNull;
023 import org.springframework.beans.factory.FactoryBean;
024
025 /**
026 * A {@link FactoryBean} which instantiates {@link Endpoint} objects
027 *
028 * @version $Revision: 1.1 $
029 */
030 public class EndpointFactoryBean implements FactoryBean {
031 private CamelContext context;
032 private String uri;
033 private Endpoint endpoint;
034 private boolean singleton;
035
036 public Object getObject() throws Exception {
037 if (endpoint == null) {
038 endpoint = createEndpoint();
039 }
040 return endpoint;
041 }
042
043 public Class getObjectType() {
044 return Endpoint.class;
045 }
046
047 public boolean isSingleton() {
048 return singleton;
049 }
050
051 public CamelContext getContext() {
052 return context;
053 }
054
055 /**
056 * Sets the context to use to resolve endpoints
057 *
058 * @param context the context used to resolve endpoints
059 */
060 public void setContext(CamelContext context) {
061 this.context = context;
062 }
063
064 public Endpoint getEndpoint() {
065 return endpoint;
066 }
067
068 public void setEndpoint(Endpoint endpoint) {
069 this.endpoint = endpoint;
070 }
071
072 public void setSingleton(boolean singleton) {
073 this.singleton = singleton;
074 }
075
076 public String getUri() {
077 return uri;
078 }
079
080 /**
081 * Sets the URI to use to resolve the endpoint
082 *
083 * @param uri the URI used to set the endpoint
084 */
085 public void setUri(String uri) {
086 this.uri = uri;
087 }
088
089 protected Endpoint createEndpoint() {
090 notNull(context, "context");
091 notNull(uri, "uri");
092 return context.getEndpoint(uri);
093 }
094
095 }