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 java.util.Collections;
020    import java.util.List;
021    
022    import javax.xml.bind.annotation.XmlAccessType;
023    import javax.xml.bind.annotation.XmlAccessorType;
024    import javax.xml.bind.annotation.XmlAttribute;
025    import javax.xml.bind.annotation.XmlTransient;
026    
027    import org.apache.camel.Endpoint;
028    import org.apache.camel.ExchangePattern;
029    import org.apache.camel.Processor;
030    import org.apache.camel.processor.SendProcessor;
031    import org.apache.camel.spi.RouteContext;
032    import org.apache.camel.util.ObjectHelper;
033    
034    /**
035     * Base class for sending to an endpoint with an optional {@link ExchangePattern}
036     *
037     * @version $Revision: 881198 $
038     */
039    @XmlAccessorType(XmlAccessType.FIELD)
040    public abstract class SendDefinition<Type extends ProcessorDefinition> extends ProcessorDefinition<Type> {
041        @XmlAttribute(required = false)
042        protected String uri;
043        @XmlAttribute(required = false)
044        protected String ref;
045        @XmlTransient
046        protected Endpoint endpoint;
047    
048        public SendDefinition() {
049        }
050    
051        public SendDefinition(String uri) {
052            this.uri = uri;
053        }
054    
055        @Override
056        public Processor createProcessor(RouteContext routeContext) throws Exception {
057            Endpoint endpoint = resolveEndpoint(routeContext);
058            return new SendProcessor(endpoint, getPattern());
059        }
060    
061        public Endpoint resolveEndpoint(RouteContext context) {
062            if (endpoint == null) {
063                endpoint = context.resolveEndpoint(getUri(), getRef());
064            }
065            return endpoint;
066        }
067    
068        // Properties
069        // -----------------------------------------------------------------------
070        public String getRef() {
071            return ref;
072        }
073    
074        public void setRef(String ref) {
075            this.ref = ref;
076        }
077    
078        public String getUri() {
079            return uri;
080        }
081    
082        public void setUri(String uri) {
083            this.uri = uri;
084        }
085    
086        public Endpoint getEndpoint() {
087            return endpoint;
088        }
089    
090        public void setEndpoint(Endpoint endpoint) {
091            this.endpoint = endpoint;
092        }
093    
094        public ExchangePattern getPattern() {
095            return null;
096        }
097    
098        public List<ProcessorDefinition> getOutputs() {
099            return Collections.emptyList();
100        }
101    
102        /**
103         * Returns the endpoint URI or the name of the reference to it
104         */
105        public Object getUriOrRef() {
106            String uri = getUri();
107            if (ObjectHelper.isEmpty(uri)) {
108                return uri;
109            } else if (endpoint != null) {
110                return endpoint.getEndpointUri();
111            }
112            return getRef();
113        }
114    
115        @Override
116        public String getLabel() {
117            return FromDefinition.description(getUri(), getRef(), getEndpoint());
118        }
119    }