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 */ 017package org.apache.camel.model; 018 019import javax.xml.bind.annotation.XmlAccessType; 020import javax.xml.bind.annotation.XmlAccessorType; 021import javax.xml.bind.annotation.XmlAttribute; 022import javax.xml.bind.annotation.XmlRootElement; 023import javax.xml.bind.annotation.XmlTransient; 024 025import org.apache.camel.Endpoint; 026import org.apache.camel.spi.Metadata; 027import org.apache.camel.spi.RouteContext; 028import org.apache.camel.util.ObjectHelper; 029 030/** 031 * Act as a message source as input to a route 032 * 033 * @version 034 */ 035@Metadata(label = "eip,endpoint,routing") 036@XmlRootElement(name = "from") 037@XmlAccessorType(XmlAccessType.FIELD) 038public class FromDefinition extends OptionalIdentifiedDefinition<FromDefinition> implements EndpointRequiredDefinition { 039 @XmlAttribute @Metadata(required = "true") 040 private String uri; 041 @XmlAttribute 042 @Deprecated 043 private String ref; 044 @XmlTransient 045 private Endpoint endpoint; 046 047 public FromDefinition() { 048 } 049 050 public FromDefinition(String uri) { 051 setUri(uri); 052 } 053 054 public FromDefinition(Endpoint endpoint) { 055 setEndpoint(endpoint); 056 } 057 058 @Override 059 public String toString() { 060 return "From[" + getLabel() + "]"; 061 } 062 063 public String getLabel() { 064 return description(getUri(), getRef(), getEndpoint()); 065 } 066 067 public Endpoint resolveEndpoint(RouteContext context) { 068 if (endpoint == null) { 069 return context.resolveEndpoint(getUri(), getRef()); 070 } else { 071 return endpoint; 072 } 073 } 074 075 @Override 076 public String getEndpointUri() { 077 return getUri(); 078 } 079 080 // Properties 081 // ----------------------------------------------------------------------- 082 083 public String getUri() { 084 if (uri != null) { 085 return uri; 086 } else if (endpoint != null) { 087 return endpoint.getEndpointUri(); 088 } else { 089 return null; 090 } 091 } 092 093 /** 094 * Sets the URI of the endpoint to use 095 * 096 * @param uri the endpoint URI to use 097 */ 098 public void setUri(String uri) { 099 clear(); 100 this.uri = uri; 101 } 102 103 public String getRef() { 104 return ref; 105 } 106 107 /** 108 * Sets the name of the endpoint within the registry (such as the Spring 109 * ApplicationContext or JNDI) to use 110 * 111 * @param ref the reference name to use 112 * @deprecated use uri with ref:uri instead 113 */ 114 @Deprecated 115 public void setRef(String ref) { 116 clear(); 117 this.ref = ref; 118 } 119 120 /** 121 * Gets tne endpoint if an {@link Endpoint} instance was set. 122 * <p/> 123 * This implementation may return <tt>null</tt> which means you need to use 124 * {@link #getRef()} or {@link #getUri()} to get information about the endpoint. 125 * 126 * @return the endpoint instance, or <tt>null</tt> 127 */ 128 public Endpoint getEndpoint() { 129 return endpoint; 130 } 131 132 public void setEndpoint(Endpoint endpoint) { 133 this.endpoint = endpoint; 134 this.uri = null; 135 if (endpoint != null) { 136 this.uri = endpoint.getEndpointUri(); 137 } 138 } 139 140 /** 141 * Returns the endpoint URI or the name of the reference to it 142 */ 143 public Object getUriOrRef() { 144 if (ObjectHelper.isNotEmpty(uri)) { 145 return uri; 146 } else if (endpoint != null) { 147 return endpoint.getEndpointUri(); 148 } 149 return ref; 150 } 151 152 // Implementation methods 153 // ----------------------------------------------------------------------- 154 protected static String description(String uri, String ref, Endpoint endpoint) { 155 if (ref != null) { 156 return "ref:" + ref; 157 } else if (endpoint != null) { 158 return endpoint.getEndpointUri(); 159 } else if (uri != null) { 160 return uri; 161 } else { 162 return "no uri or ref supplied!"; 163 } 164 } 165 166 protected void clear() { 167 this.endpoint = null; 168 this.ref = null; 169 this.uri = null; 170 } 171 172}