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.builder.EndpointConsumerBuilder;
027import org.apache.camel.spi.Metadata;
028
029/**
030 * Act as a message source as input to a route
031 */
032@Metadata(label = "eip,endpoint,routing")
033@XmlRootElement(name = "from")
034@XmlAccessorType(XmlAccessType.FIELD)
035public class FromDefinition extends OptionalIdentifiedDefinition<FromDefinition> implements EndpointRequiredDefinition {
036    @XmlAttribute
037    @Metadata(required = true)
038    private String uri;
039    @XmlTransient
040    private Endpoint endpoint;
041    @XmlTransient
042    private EndpointConsumerBuilder endpointConsumerBuilder;
043
044    public FromDefinition() {
045    }
046
047    public FromDefinition(String uri) {
048        this();
049        setUri(uri);
050    }
051
052    public FromDefinition(Endpoint endpoint) {
053        this();
054        setEndpoint(endpoint);
055    }
056
057    public FromDefinition(EndpointConsumerBuilder endpointConsumerBuilder) {
058        this();
059        setEndpointConsumerBuilder(endpointConsumerBuilder);
060    }
061
062    @Override
063    public String toString() {
064        return "From[" + getLabel() + "]";
065    }
066
067    @Override
068    public String getShortName() {
069        return "from";
070    }
071
072    @Override
073    public String getLabel() {
074        String uri = getEndpointUri();
075        return uri != null ? uri : "no uri supplied";
076    }
077
078    @Override
079    public String getEndpointUri() {
080        if (uri != null) {
081            return uri;
082        } else if (endpoint != null) {
083            return endpoint.getEndpointUri();
084        } else if (endpointConsumerBuilder != null) {
085            return endpointConsumerBuilder.getUri();
086        } else {
087            return null;
088        }
089    }
090
091    // Properties
092    // -----------------------------------------------------------------------
093
094    public String getUri() {
095        return uri;
096    }
097
098    /**
099     * Sets the URI of the endpoint to use
100     *
101     * @param uri the endpoint URI to use
102     */
103    public void setUri(String uri) {
104        clear();
105        this.uri = uri;
106    }
107
108    /**
109     * Gets tne endpoint if an {@link Endpoint} instance was set.
110     * <p/>
111     * This implementation may return <tt>null</tt> which means you need to use
112     * {@link #getEndpointUri()} to get information about the endpoint.
113     *
114     * @return the endpoint instance, or <tt>null</tt>
115     */
116    public Endpoint getEndpoint() {
117        return endpoint;
118    }
119
120    public void setEndpoint(Endpoint endpoint) {
121        clear();
122        this.endpoint = endpoint;
123    }
124
125    public EndpointConsumerBuilder getEndpointConsumerBuilder() {
126        return endpointConsumerBuilder;
127    }
128
129    public void setEndpointConsumerBuilder(EndpointConsumerBuilder endpointConsumerBuilder) {
130        clear();
131        this.endpointConsumerBuilder = endpointConsumerBuilder;
132    }
133
134    // Implementation methods
135    // -----------------------------------------------------------------------
136    protected void clear() {
137        this.endpointConsumerBuilder = null;
138        this.endpoint = null;
139        this.uri = null;
140    }
141
142}