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> implements EndpointRequiredDefinition {
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            setEndpoint(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        @Override
079        public String getEndpointUri() {
080            return getUri();
081        }
082    
083        // Properties
084        // -----------------------------------------------------------------------
085    
086        public String getUri() {
087            if (uri != null) {
088                return uri;
089            } else if (endpoint != null) {
090                return endpoint.getEndpointUri();
091            } else {
092                return null;
093            }
094        }
095    
096        /**
097         * Sets the URI of the endpoint to use
098         *
099         * @param uri the endpoint URI to use
100         */
101        @Required
102        public void setUri(String uri) {
103            clear();
104            this.uri = uri;
105        }
106    
107        public String getRef() {
108            return ref;
109        }
110    
111        /**
112         * Sets the name of the endpoint within the registry (such as the Spring
113         * ApplicationContext or JNDI) to use
114         *
115         * @param ref the reference name to use
116         */
117        public void setRef(String ref) {
118            clear();
119            this.ref = ref;
120        }
121    
122        /**
123         * Gets tne endpoint if an {@link Endpoint} instance was set.
124         * <p/>
125         * This implementation may return <tt>null</tt> which means you need to use
126         * {@link #getRef()} or {@link #getUri()} to get information about the endpoint.
127         *
128         * @return the endpoint instance, or <tt>null</tt>
129         */
130        public Endpoint getEndpoint() {
131            return endpoint;
132        }
133    
134        public void setEndpoint(Endpoint endpoint) {
135            this.endpoint = endpoint;
136            this.uri = null;
137            if (endpoint != null) {
138                this.uri = endpoint.getEndpointUri();
139            }
140        }
141    
142        /**
143         * Returns the endpoint URI or the name of the reference to it
144         */
145        public Object getUriOrRef() {
146            if (ObjectHelper.isNotEmpty(uri)) {
147                return uri;
148            } else if (endpoint != null) {
149                return endpoint.getEndpointUri();
150            }
151            return ref;
152        }
153    
154        // Implementation methods
155        // -----------------------------------------------------------------------
156        protected static String description(String uri, String ref, Endpoint endpoint) {
157            if (ref != null) {
158                return "ref:" + ref;
159            } else if (endpoint != null) {
160                return endpoint.getEndpointUri();
161            } else if (uri != null) {
162                return uri;
163            } else {
164                return "no uri or ref supplied!";
165            }
166        }
167    
168        protected void clear() {
169            this.endpoint = null;
170            this.ref = null;
171            this.uri = null;
172        }
173    
174    }