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