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.Metadata;
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 @Metadata(required = "true")
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    public void setUri(String uri) {
103        this.uri = uri;
104    }
105
106    /**
107     * Gets tne endpoint if an {@link Endpoint} instance was set.
108     * <p/>
109     * This implementation may return <tt>null</tt> which means you need to use
110     * {@link #getRef()} or {@link #getUri()} to get information about the endpoint.
111     *
112     * @return the endpoint instance, or <tt>null</tt>
113     */
114    public Endpoint getEndpoint() {
115        return endpoint;
116    }
117
118    public void setEndpoint(Endpoint endpoint) {
119        this.endpoint = endpoint;
120        this.uri = null;
121        if (endpoint != null) {
122            this.uri = endpoint.getEndpointUri();
123        }
124    }
125
126    public ExchangePattern getPattern() {
127        return null;
128    }
129
130    /**
131     * Returns the endpoint URI or the name of the reference to it
132     */
133    public String getUriOrRef() {
134        String uri = getUri();
135        if (ObjectHelper.isNotEmpty(uri)) {
136            return uri;
137        } else if (endpoint != null) {
138            return endpoint.getEndpointUri();
139        }
140        return getRef();
141    }
142
143    @Override
144    public String getLabel() {
145        return FromDefinition.description(getUri(), getRef(), getEndpoint());
146    }
147}