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.XmlTransient;
023
024import org.apache.camel.Endpoint;
025import org.apache.camel.ExchangePattern;
026import org.apache.camel.Processor;
027import org.apache.camel.processor.SendProcessor;
028import org.apache.camel.spi.Required;
029import org.apache.camel.spi.RouteContext;
030import org.apache.camel.util.ObjectHelper;
031
032/**
033 * Sends the message to an endpoint
034 *
035 * @version 
036 */
037@XmlAccessorType(XmlAccessType.FIELD)
038public abstract class SendDefinition<Type extends ProcessorDefinition<Type>> extends NoOutputDefinition<Type> implements EndpointRequiredDefinition {
039    @XmlAttribute
040    protected String uri;
041    @XmlAttribute
042    @Deprecated
043    protected String ref;
044    @XmlTransient
045    protected Endpoint endpoint;
046
047    public SendDefinition() {
048    }
049
050    public SendDefinition(String uri) {
051        this.uri = uri;
052    }
053
054    @Override
055    public Processor createProcessor(RouteContext routeContext) throws Exception {
056        Endpoint endpoint = resolveEndpoint(routeContext);
057        return new SendProcessor(endpoint, getPattern());
058    }
059
060    public Endpoint resolveEndpoint(RouteContext context) {
061        if (endpoint == null) {
062            return context.resolveEndpoint(getUri(), getRef());
063        } else {
064            return endpoint;
065        }
066    }
067
068    @Override
069    public String getEndpointUri() {
070        if (uri != null) {
071            return uri;
072        }
073        return null;
074    }
075
076    // Properties
077    // -----------------------------------------------------------------------
078    public String getRef() {
079        return ref;
080    }
081
082    /**
083     * Sets the reference of the endpoint to send to.
084     *
085     * @param ref the reference of the endpoint
086     * @deprecated use uri with ref:uri instead
087     */
088    @Deprecated
089    public void setRef(String ref) {
090        this.ref = ref;
091    }
092
093    public String getUri() {
094        return uri;
095    }
096
097    /**
098     * Sets the uri of the endpoint to send to.
099     *
100     * @param uri the uri of the endpoint
101     */
102    @Required
103    public void setUri(String uri) {
104        this.uri = uri;
105    }
106
107    /**
108     * Gets tne endpoint if an {@link Endpoint} instance was set.
109     * <p/>
110     * This implementation may return <tt>null</tt> which means you need to use
111     * {@link #getRef()} or {@link #getUri()} to get information about the endpoint.
112     *
113     * @return the endpoint instance, or <tt>null</tt>
114     */
115    public Endpoint getEndpoint() {
116        return endpoint;
117    }
118
119    public void setEndpoint(Endpoint endpoint) {
120        this.endpoint = endpoint;
121        this.uri = null;
122        if (endpoint != null) {
123            this.uri = endpoint.getEndpointUri();
124        }
125    }
126
127    public ExchangePattern getPattern() {
128        return null;
129    }
130
131    /**
132     * Returns the endpoint URI or the name of the reference to it
133     */
134    public String getUriOrRef() {
135        String uri = getUri();
136        if (ObjectHelper.isNotEmpty(uri)) {
137            return uri;
138        } else if (endpoint != null) {
139            return endpoint.getEndpointUri();
140        }
141        return getRef();
142    }
143
144    @Override
145    public String getLabel() {
146        return FromDefinition.description(getUri(), getRef(), getEndpoint());
147    }
148}