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.springframework.beans.factory.FactoryBean; 031 032 import static org.apache.camel.util.ObjectHelper.notNull; 033 034 /** 035 * A {@link FactoryBean} which instantiates {@link Endpoint} objects 036 * 037 * @version $Revision: 817970 $ 038 */ 039 @XmlRootElement(name = "endpoint") 040 @XmlAccessorType(XmlAccessType.FIELD) 041 public class CamelEndpointFactoryBean extends IdentifiedType implements FactoryBean, CamelContextAware { 042 @XmlAttribute 043 private String uri; 044 @XmlTransient 045 private CamelContext context; 046 @XmlTransient 047 private Endpoint endpoint; 048 049 public Object getObject() throws Exception { 050 if (endpoint == null) { 051 endpoint = createEndpoint(); 052 } 053 return endpoint; 054 } 055 056 public Class getObjectType() { 057 return Endpoint.class; 058 } 059 060 public boolean isSingleton() { 061 if (endpoint == null) { 062 this.endpoint = createEndpoint(); 063 } 064 return endpoint.isSingleton(); 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 String getUri() { 081 return uri; 082 } 083 084 /** 085 * Sets the URI to use to resolve the endpoint 086 * 087 * @param uri the URI used to set the endpoint 088 */ 089 public void setUri(String uri) { 090 this.uri = uri; 091 } 092 093 protected Endpoint createEndpoint() { 094 notNull(context, "context"); 095 notNull(uri, "uri"); 096 Endpoint endpoint = context.getEndpoint(uri); 097 if (endpoint == null) { 098 throw new NoSuchEndpointException(uri); 099 } 100 return endpoint; 101 } 102 }