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