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 @Override 064 public String getShortName() { 065 return "from"; 066 } 067 068 @Override 069 public String getLabel() { 070 return description(getUri(), getRef(), getEndpoint()); 071 } 072 073 public Endpoint resolveEndpoint(RouteContext context) { 074 if (endpoint == null) { 075 return context.resolveEndpoint(getUri(), getRef()); 076 } else { 077 return endpoint; 078 } 079 } 080 081 @Override 082 public String getEndpointUri() { 083 return getUri(); 084 } 085 086 // Properties 087 // ----------------------------------------------------------------------- 088 089 public String getUri() { 090 if (uri != null) { 091 return uri; 092 } else if (endpoint != null) { 093 return endpoint.getEndpointUri(); 094 } else { 095 return null; 096 } 097 } 098 099 /** 100 * Sets the URI of the endpoint to use 101 * 102 * @param uri the endpoint URI to use 103 */ 104 public void setUri(String uri) { 105 clear(); 106 this.uri = uri; 107 } 108 109 public String getRef() { 110 return ref; 111 } 112 113 /** 114 * Sets the name of the endpoint within the registry (such as the Spring 115 * ApplicationContext or JNDI) to use 116 * 117 * @param ref the reference name to use 118 * @deprecated use uri with ref:uri instead 119 */ 120 @Deprecated 121 public void setRef(String ref) { 122 clear(); 123 this.ref = ref; 124 } 125 126 /** 127 * Gets tne endpoint if an {@link Endpoint} instance was set. 128 * <p/> 129 * This implementation may return <tt>null</tt> which means you need to use 130 * {@link #getRef()} or {@link #getUri()} to get information about the endpoint. 131 * 132 * @return the endpoint instance, or <tt>null</tt> 133 */ 134 public Endpoint getEndpoint() { 135 return endpoint; 136 } 137 138 public void setEndpoint(Endpoint endpoint) { 139 this.endpoint = endpoint; 140 this.uri = null; 141 if (endpoint != null) { 142 this.uri = endpoint.getEndpointUri(); 143 } 144 } 145 146 /** 147 * Returns the endpoint URI or the name of the reference to it 148 */ 149 public Object getUriOrRef() { 150 if (ObjectHelper.isNotEmpty(uri)) { 151 return uri; 152 } else if (endpoint != null) { 153 return endpoint.getEndpointUri(); 154 } 155 return ref; 156 } 157 158 // Implementation methods 159 // ----------------------------------------------------------------------- 160 protected static String description(String uri, String ref, Endpoint endpoint) { 161 if (ref != null) { 162 return "ref:" + ref; 163 } else if (endpoint != null) { 164 return endpoint.getEndpointUri(); 165 } else if (uri != null) { 166 return uri; 167 } else { 168 return "no uri or ref supplied!"; 169 } 170 } 171 172 protected void clear() { 173 this.endpoint = null; 174 this.ref = null; 175 this.uri = null; 176 } 177 178}