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