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.spring.model;
018    
019    import org.apache.camel.CamelContext;
020    import org.apache.camel.CamelContextAware;
021    import org.apache.camel.Endpoint;
022    import org.apache.camel.Processor;
023    import org.apache.camel.builder.ProcessorFactory;
024    
025    import javax.xml.bind.annotation.XmlRootElement;
026    import javax.xml.bind.annotation.XmlTransient;
027    import javax.xml.bind.annotation.XmlElementRef;
028    import java.util.ArrayList;
029    import java.util.List;
030    
031    /**
032     * Represents an XML <route/> element
033     *
034     * @version $Revision: $
035     */
036    @XmlRootElement(name = "root")
037    public class RouteType extends OutputType implements CamelContextAware, ProcessorFactory {
038        private CamelContext camelContext;
039        private List<FromType> inputs = new ArrayList<FromType>();
040    
041    /*
042        public Route createRoute() throws Exception {
043            return new EventDrivenConsumerRoute(getEndpoint(), createProcessor());
044        }
045    */
046    
047        @Override
048        public String toString() {
049            return "Route[ " + inputs + " -> " + outputs + "]";
050        }
051    
052        // Properties
053        //-----------------------------------------------------------------------
054    
055        @XmlElementRef
056        public List<FromType> getInputs() {
057            return inputs;
058        }
059    
060        public void setInputs(List<FromType> inputs) {
061            this.inputs = inputs;
062        }
063    
064        @XmlTransient
065        public CamelContext getCamelContext() {
066            return camelContext;
067        }
068    
069        public void setCamelContext(CamelContext camelContext) {
070            this.camelContext = camelContext;
071        }
072    
073        public Processor createProcessor() throws Exception {
074            return null;  // TODO
075        }
076    
077        protected Endpoint resolveEndpoint(String uri) {
078            CamelContext context = getCamelContext();
079            if (context == null) {
080                throw new IllegalArgumentException("No CamelContext has been injected!");
081            }
082            Endpoint answer = context.getEndpoint(uri);
083            if (answer == null) {
084                throw new IllegalArgumentException("No Endpoint found for uri: " + uri);
085            }
086            return answer;
087        }
088    
089        // Fluent API
090        //-----------------------------------------------------------------------
091        public RouteType from(String uri) {
092            getInputs().add(new FromType(uri));
093            return this;
094        }
095    
096        public RouteType interceptor(String ref) {
097            getInterceptors().add(new InterceptorRef(ref));
098            return this;
099        }
100    
101        public RouteType interceptors(String... refs) {
102            for (String ref : refs) {
103                interceptor(ref);
104            }
105            return this;
106        }
107    }