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.XmlRootElement;
026    import javax.xml.bind.annotation.XmlTransient;
027    
028    import org.apache.camel.Endpoint;
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     * Represents an XML <to/> element
036     *
037     * @version $Revision: 43540 $
038     */
039    @XmlRootElement(name = "to")
040    @XmlAccessorType(XmlAccessType.FIELD)
041    public class ToType extends ProcessorType<ProcessorType> {
042        @XmlAttribute
043        private String uri;
044        @XmlAttribute
045        private String ref;
046        @XmlTransient
047        private Endpoint endpoint;
048    
049        public ToType() {
050        }
051    
052        public ToType(String uri) {
053            setUri(uri);
054        }
055    
056        public ToType(Endpoint endpoint) {
057            setEndpoint(endpoint);
058        }
059    
060        @Override
061        public String toString() {
062            return "To[" + getLabel() + "]";
063        }
064    
065        @Override
066        public String getShortName() {
067            return "to";
068        }
069    
070        @Override
071        public String getLabel() {
072            return FromType.description(getUri(), getRef(), getEndpoint());
073        }
074    
075        @Override
076        public Processor createProcessor(RouteContext routeContext) throws Exception {
077            Endpoint endpoint = resolveEndpoint(routeContext);
078            return new SendProcessor(endpoint);
079        }
080    
081        public Endpoint resolveEndpoint(RouteContext context) {
082            if (endpoint == null) {
083                endpoint = context.resolveEndpoint(getUri(), getRef());
084            }
085            return endpoint;
086        }
087    
088        // Properties
089        // -----------------------------------------------------------------------
090        public String getUri() {
091            return uri;
092        }
093    
094        /**
095         * Sets the URI of the endpoint to use
096         *
097         * @param uri the endpoint URI to use
098         */
099        public void setUri(String uri) {
100            this.uri = uri;
101        }
102    
103        public String getRef() {
104            return ref;
105        }
106    
107        /**
108         * Sets the name of the endpoint within the registry (such as the Spring
109         * ApplicationContext or JNDI) to use
110         *
111         * @param ref the reference name to use
112         */
113        public void setRef(String ref) {
114            this.ref = ref;
115        }
116    
117        public Endpoint getEndpoint() {
118            return endpoint;
119        }
120    
121        public void setEndpoint(Endpoint endpoint) {
122            this.endpoint = endpoint;
123        }
124    
125        public List<ProcessorType<?>> getOutputs() {
126            return Collections.EMPTY_LIST;
127        }
128    
129        /**
130         * Returns the endpoint URI or the name of the reference to it
131         */
132        public Object getUriOrRef() {
133            if (ObjectHelper.isNullOrBlank(uri)) {
134                return uri;
135            } else if (endpoint != null) {
136                return endpoint.getEndpointUri();
137            }
138            return ref;
139        }
140    }