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.Collection;
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.XmlElement;
026    import javax.xml.bind.annotation.XmlElementRef;
027    import javax.xml.bind.annotation.XmlRootElement;
028    import javax.xml.bind.annotation.XmlTransient;
029    import javax.xml.bind.annotation.XmlType;
030    
031    import org.apache.camel.CamelContext;
032    import org.apache.camel.CamelContextAware;
033    import org.apache.camel.Endpoint;
034    import org.apache.camel.NoSuchEndpointException;
035    import org.apache.camel.Route;
036    import org.apache.camel.impl.RouteContext;
037    import org.apache.camel.util.CamelContextHelper;
038    import org.apache.commons.logging.Log;
039    import org.apache.commons.logging.LogFactory;
040    
041    /**
042     * Represents an XML <route/> element
043     * 
044     * @version $Revision: $
045     */
046    @XmlRootElement(name = "route")
047    @XmlType(propOrder = {"interceptors", "inputs", "outputs" })
048    @XmlAccessorType(XmlAccessType.FIELD)
049    public class RouteType extends ProcessorType implements CamelContextAware {
050        private static final transient Log LOG = LogFactory.getLog(RouteType.class);
051        @XmlElementRef
052        private List<InterceptorType> interceptors = new ArrayList<InterceptorType>();
053        @XmlElementRef
054        private List<FromType> inputs = new ArrayList<FromType>();
055        @XmlElementRef
056        private List<ProcessorType> outputs = new ArrayList<ProcessorType>();
057        @XmlTransient
058        private CamelContext camelContext;
059    
060        public RouteType() {
061        }
062    
063        public RouteType(String uri) {
064            getInputs().add(new FromType(uri));
065        }
066    
067        public RouteType(Endpoint endpoint) {
068            getInputs().add(new FromType(endpoint));
069        }
070    
071        @Override
072        public String toString() {
073            return "Route[ " + inputs + " -> " + outputs + "]";
074        }
075    
076        // TODO should we zap this and replace with next method?
077        public void addRoutes(CamelContext context) throws Exception {
078            Collection<Route> routes = new ArrayList<Route>();
079    
080            addRoutes(context, routes);
081    
082            context.addRoutes(routes);
083        }
084    
085        public void addRoutes(CamelContext context, Collection<Route> routes) throws Exception {
086            setCamelContext(context);
087    
088            for (FromType fromType : inputs) {
089                addRoutes(routes, fromType);
090            }
091        }
092    
093        public Endpoint resolveEndpoint(String uri) throws NoSuchEndpointException {
094            CamelContext context = getCamelContext();
095            if (context == null) {
096                throw new IllegalArgumentException("No CamelContext has been injected!");
097            }
098            return CamelContextHelper.getMandatoryEndpoint(context, uri);
099        }
100    
101        // Fluent API
102        // -----------------------------------------------------------------------
103    
104        /**
105         * Creates an input to the route
106         */
107        public RouteType from(String uri) {
108            getInputs().add(new FromType(uri));
109            return this;
110        }
111    
112        // Properties
113        // -----------------------------------------------------------------------
114    
115        public List<InterceptorType> getInterceptors() {
116            return interceptors;
117        }
118    
119        public void setInterceptors(List<InterceptorType> interceptors) {
120            this.interceptors = interceptors;
121        }
122    
123        public List<FromType> getInputs() {
124            return inputs;
125        }
126    
127        public void setInputs(List<FromType> inputs) {
128            this.inputs = inputs;
129        }
130    
131        public List<ProcessorType> getOutputs() {
132            return outputs;
133        }
134    
135        public void setOutputs(List<ProcessorType> outputs) {
136            this.outputs = outputs;
137    
138            if (outputs != null) {
139                for (ProcessorType output : outputs) {
140                    configureChild(output);
141                }
142            }
143        }
144    
145        public CamelContext getCamelContext() {
146            return camelContext;
147        }
148    
149        public void setCamelContext(CamelContext camelContext) {
150            this.camelContext = camelContext;
151        }
152    
153        // Implementation methods
154        // -------------------------------------------------------------------------
155    
156        protected void addRoutes(Collection<Route> routes, FromType fromType) throws Exception {
157            RouteContext routeContext = new RouteContext(this, fromType, routes);
158            Endpoint endpoint = routeContext.getEndpoint();
159    
160            for (ProcessorType output : outputs) {
161                output.addRoutes(routeContext, routes);
162            }
163    
164            routeContext.commit();
165        }
166    
167        @Override
168        protected void configureChild(ProcessorType output) {
169            if (isInheritErrorHandler()) {
170                output.setErrorHandlerBuilder(getErrorHandlerBuilder());
171            }
172            List<InterceptorType> list = output.getInterceptors();
173            if (list == null) {
174                LOG.warn("No interceptor collection: " + output);
175            } else {
176                list.addAll(getInterceptors());
177            }
178        }
179    }