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