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.impl.RouteContext;
027
028 /**
029 * Represents an XML <to/> element
030 *
031 * @version $Revision: $
032 */
033 @XmlRootElement(name = "from")
034 @XmlAccessorType(XmlAccessType.FIELD)
035 public class FromType {
036 @XmlAttribute
037 private String uri;
038 @XmlAttribute
039 private String ref;
040 @XmlTransient
041 private Endpoint endpoint;
042
043 public FromType() {
044 }
045
046 public FromType(String uri) {
047 setUri(uri);
048 }
049
050 public FromType(Endpoint endpoint) {
051 this.endpoint = endpoint;
052 }
053
054 @Override
055 public String toString() {
056 return "From[" + description(getUri(), getRef(), getEndpoint()) + "]";
057 }
058
059 public Endpoint resolveEndpoint(RouteContext context) {
060 if (endpoint == null) {
061 endpoint = context.resolveEndpoint(getUri(), getRef());
062 }
063 return endpoint;
064 }
065
066 // Properties
067 // -----------------------------------------------------------------------
068 public String getUri() {
069 return uri;
070 }
071
072 /**
073 * Sets the URI of the endpoint to use
074 *
075 * @param uri the endpoint URI to use
076 */
077 public void setUri(String uri) {
078 this.uri = uri;
079 }
080
081 public String getRef() {
082 return ref;
083 }
084
085 /**
086 * Sets the name of the endpoint within the registry (such as the Spring
087 * ApplicationContext or JNDI) to use
088 *
089 * @param ref the reference name to use
090 */
091 public void setRef(String ref) {
092 this.ref = ref;
093 }
094
095 public Endpoint getEndpoint() {
096 return endpoint;
097 }
098
099 public void setEndpoint(Endpoint endpoint) {
100 this.endpoint = endpoint;
101 }
102
103 // Implementation methods
104 // -----------------------------------------------------------------------
105 protected static String description(String uri, String ref, Endpoint endpoint) {
106 if (endpoint != null) {
107 return endpoint.getEndpointUri();
108 } else if (uri != null) {
109 return uri;
110 } else if (ref != null) {
111 return "ref:" + ref;
112 } else {
113 return "no uri or ref supplied!";
114 }
115 }
116 }