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> {
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        // Properties
068        // -----------------------------------------------------------------------
069        public String getRef() {
070            return ref;
071        }
072    
073        public void setRef(String ref) {
074            this.ref = ref;
075        }
076    
077        public String getUri() {
078            return uri;
079        }
080    
081        @Required
082        public void setUri(String uri) {
083            this.uri = uri;
084        }
085    
086        /**
087         * Gets tne endpoint if an {@link Endpoint} instance was set.
088         * <p/>
089         * This implementation may return <tt>null</tt> which means you need to use
090         * {@link #getRef()} or {@link #getUri()} to get information about the endpoint.
091         *
092         * @return the endpoint instance, or <tt>null</tt>
093         */
094        public Endpoint getEndpoint() {
095            return endpoint;
096        }
097    
098        public void setEndpoint(Endpoint endpoint) {
099            this.endpoint = endpoint;
100        }
101    
102        public ExchangePattern getPattern() {
103            return null;
104        }
105    
106        /**
107         * Returns the endpoint URI or the name of the reference to it
108         */
109        public String getUriOrRef() {
110            String uri = getUri();
111            if (ObjectHelper.isNotEmpty(uri)) {
112                return uri;
113            } else if (endpoint != null) {
114                return endpoint.getEndpointUri();
115            }
116            return getRef();
117        }
118    
119        @Override
120        public String getLabel() {
121            return FromDefinition.description(getUri(), getRef(), getEndpoint());
122        }
123    }