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.RouteContext;
027    import org.apache.camel.util.ObjectHelper;
028    
029    /**
030     * Represents an XML <from/> element
031     *
032     * @version $Revision: 750806 $
033     */
034    @XmlRootElement(name = "from")
035    @XmlAccessorType(XmlAccessType.FIELD)
036    public class FromDefinition extends OptionalIdentifiedType<FromDefinition> {
037        @XmlAttribute
038        private String uri;
039        @XmlAttribute
040        private String ref;
041        @XmlTransient
042        private Endpoint endpoint;
043    
044        public FromDefinition() {
045        }
046    
047        public FromDefinition(String uri) {
048            setUri(uri);
049        }
050    
051        public FromDefinition(Endpoint endpoint) {
052            this.endpoint = endpoint;
053        }
054    
055        @Override
056        public String toString() {
057            return "From[" + getLabel() + "]";
058        }
059    
060        @Override
061        public String getShortName() {
062            return "from";
063        }
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                endpoint = context.resolveEndpoint(getUri(), getRef());
073            }
074            return endpoint;
075        }
076    
077        // Properties
078        // -----------------------------------------------------------------------
079        public String getUri() {
080            return uri;
081        }
082    
083        /**
084         * Sets the URI of the endpoint to use
085         *
086         * @param uri the endpoint URI to use
087         */
088        public void setUri(String uri) {
089            this.uri = uri;
090            clear();
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            clear();
106        }
107    
108        public Endpoint getEndpoint() {
109            return endpoint;
110        }
111    
112        public void setEndpoint(Endpoint endpoint) {
113            this.endpoint = endpoint;
114        }
115    
116        /**
117         * Returns the endpoint URI or the name of the reference to it
118         */
119        public Object getUriOrRef() {
120            if (ObjectHelper.isEmpty(uri)) {
121                return uri;
122            } else if (endpoint != null) {
123                return endpoint.getEndpointUri();
124            }
125            return ref;
126        }
127    
128        // Implementation methods
129        // -----------------------------------------------------------------------
130        protected static String description(String uri, String ref, Endpoint endpoint) {
131            if (ref != null) {
132                return "ref:" + ref;
133            } else if (endpoint != null) {
134                return endpoint.getEndpointUri();
135            } else if (uri != null) {
136                return uri;
137            } else {
138                return "no uri or ref supplied!";
139            }
140        }
141    
142        protected void clear() {
143            this.endpoint = null;
144        }
145    
146    }