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.impl.RouteContext;
031    import org.apache.camel.processor.SendProcessor;
032    import org.apache.camel.util.ObjectHelper;
033    
034    /**
035     * Represents an XML <to/> element
036     *
037     * @version $Revision: 36321 $
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 getLabel() {
067            return FromType.description(getUri(), getRef(), getEndpoint());
068        }
069    
070        @Override
071        public Processor createProcessor(RouteContext routeContext) throws Exception {
072            Endpoint endpoint = resolveEndpoint(routeContext);
073            return new SendProcessor(endpoint);
074        }
075    
076        public Endpoint resolveEndpoint(RouteContext context) {
077            if (endpoint == null) {
078                endpoint = context.resolveEndpoint(getUri(), getRef());
079            }
080            return endpoint;
081        }
082    
083        // Properties
084        // -----------------------------------------------------------------------
085        public String getUri() {
086            return uri;
087        }
088    
089        /**
090         * Sets the URI of the endpoint to use
091         *
092         * @param uri the endpoint URI to use
093         */
094        public void setUri(String uri) {
095            this.uri = uri;
096        }
097    
098        public String getRef() {
099            return ref;
100        }
101    
102        /**
103         * Sets the name of the endpoint within the registry (such as the Spring
104         * ApplicationContext or JNDI) to use
105         *
106         * @param ref the reference name to use
107         */
108        public void setRef(String ref) {
109            this.ref = ref;
110        }
111    
112        public Endpoint getEndpoint() {
113            return endpoint;
114        }
115    
116        public void setEndpoint(Endpoint endpoint) {
117            this.endpoint = endpoint;
118        }
119    
120        public List<ProcessorType<?>> getOutputs() {
121            return Collections.EMPTY_LIST;
122        }
123    
124        /**
125         * Returns the endpoint URI or the name of the reference to it
126         */
127        public Object getUriOrRef() {
128            if (ObjectHelper.isNullOrBlank(uri)) {
129                return uri;
130            } else if (endpoint != null) {
131                return endpoint.getEndpointUri();
132            }
133            return ref;
134        }
135    }