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 */
017package org.apache.camel.model;
018
019import javax.xml.bind.annotation.XmlAccessType;
020import javax.xml.bind.annotation.XmlAccessorType;
021import javax.xml.bind.annotation.XmlAttribute;
022import javax.xml.bind.annotation.XmlRootElement;
023import javax.xml.bind.annotation.XmlTransient;
024
025import org.apache.camel.Endpoint;
026import org.apache.camel.spi.Metadata;
027import org.apache.camel.spi.Required;
028import org.apache.camel.spi.RouteContext;
029import org.apache.camel.util.ObjectHelper;
030
031/**
032 * Act as a message source as input to a route
033 *
034 * @version 
035 */
036@Metadata(label = "eip,endpoint,routing")
037@XmlRootElement(name = "from")
038@XmlAccessorType(XmlAccessType.FIELD)
039public class FromDefinition extends OptionalIdentifiedDefinition<FromDefinition> implements EndpointRequiredDefinition {
040    @XmlAttribute
041    private String uri;
042    @XmlAttribute
043    @Deprecated
044    private String ref;
045    @XmlTransient
046    private Endpoint endpoint;
047
048    public FromDefinition() {
049    }
050
051    public FromDefinition(String uri) {
052        setUri(uri);
053    }
054
055    public FromDefinition(Endpoint endpoint) {
056        setEndpoint(endpoint);
057    }
058
059    @Override
060    public String toString() {
061        return "From[" + getLabel() + "]";
062    }
063
064    public String getLabel() {
065        return description(getUri(), getRef(), getEndpoint());
066    }
067
068    public Endpoint resolveEndpoint(RouteContext context) {
069        if (endpoint == null) {
070            return context.resolveEndpoint(getUri(), getRef());
071        } else {
072            return endpoint;
073        }
074    }
075
076    @Override
077    public String getEndpointUri() {
078        return getUri();
079    }
080
081    // Properties
082    // -----------------------------------------------------------------------
083
084    public String getUri() {
085        if (uri != null) {
086            return uri;
087        } else if (endpoint != null) {
088            return endpoint.getEndpointUri();
089        } else {
090            return null;
091        }
092    }
093
094    /**
095     * Sets the URI of the endpoint to use
096     *
097     * @param uri the endpoint URI to use
098     */
099    @Required
100    public void setUri(String uri) {
101        clear();
102        this.uri = uri;
103    }
104
105    public String getRef() {
106        return ref;
107    }
108
109    /**
110     * Sets the name of the endpoint within the registry (such as the Spring
111     * ApplicationContext or JNDI) to use
112     *
113     * @param ref the reference name to use
114     * @deprecated use uri with ref:uri instead
115     */
116    @Deprecated
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}