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