Coverage Report - org.apache.camel.model.RouteType
 
Classes in this File Line Coverage Branch Coverage Complexity
RouteType
66% 
71% 
0
 
 1  
 /**
 2  
  * Licensed to the Apache Software Foundation (ASF) under one or more
 3  
  * contributor license agreements.  See the NOTICE file distributed with
 4  
  * this work for additional information regarding copyright ownership.
 5  
  * The ASF licenses this file to You under the Apache License, Version 2.0
 6  
  * (the "License"); you may not use this file except in compliance with
 7  
  * the License.  You may obtain a copy of the License at
 8  
  *
 9  
  *      http://www.apache.org/licenses/LICENSE-2.0
 10  
  *
 11  
  * Unless required by applicable law or agreed to in writing, software
 12  
  * distributed under the License is distributed on an "AS IS" BASIS,
 13  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 14  
  * See the License for the specific language governing permissions and
 15  
  * limitations under the License.
 16  
  */
 17  
 package org.apache.camel.model;
 18  
 
 19  
 import java.util.ArrayList;
 20  
 import java.util.Collection;
 21  
 import java.util.List;
 22  
 
 23  
 import javax.xml.bind.annotation.XmlAccessType;
 24  
 import javax.xml.bind.annotation.XmlAccessorType;
 25  
 import javax.xml.bind.annotation.XmlElement;
 26  
 import javax.xml.bind.annotation.XmlElementRef;
 27  
 import javax.xml.bind.annotation.XmlRootElement;
 28  
 import javax.xml.bind.annotation.XmlTransient;
 29  
 import javax.xml.bind.annotation.XmlType;
 30  
 
 31  
 import org.apache.camel.CamelContext;
 32  
 import org.apache.camel.CamelContextAware;
 33  
 import org.apache.camel.Endpoint;
 34  
 import org.apache.camel.NoSuchEndpointException;
 35  
 import org.apache.camel.Route;
 36  
 import org.apache.camel.impl.RouteContext;
 37  
 import org.apache.camel.util.CamelContextHelper;
 38  
 import org.apache.commons.logging.Log;
 39  
 import org.apache.commons.logging.LogFactory;
 40  
 
 41  
 /**
 42  
  * Represents an XML <route/> element
 43  
  * 
 44  
  * @version $Revision: $
 45  
  */
 46  
 @XmlRootElement(name = "route")
 47  
 @XmlType(propOrder = {"interceptors", "inputs", "outputs" })
 48  
 @XmlAccessorType(XmlAccessType.FIELD)
 49  
 public class RouteType extends ProcessorType implements CamelContextAware {
 50  3
     private static final transient Log LOG = LogFactory.getLog(RouteType.class);
 51  
     @XmlElementRef
 52  306
     private List<InterceptorType> interceptors = new ArrayList<InterceptorType>();
 53  
     @XmlElementRef
 54  306
     private List<FromType> inputs = new ArrayList<FromType>();
 55  
     @XmlElementRef
 56  306
     private List<ProcessorType> outputs = new ArrayList<ProcessorType>();
 57  
     @XmlTransient
 58  
     private CamelContext camelContext;
 59  
 
 60  39
     public RouteType() {
 61  39
     }
 62  
 
 63  267
     public RouteType(String uri) {
 64  267
         getInputs().add(new FromType(uri));
 65  267
     }
 66  
 
 67  0
     public RouteType(Endpoint endpoint) {
 68  0
         getInputs().add(new FromType(endpoint));
 69  0
     }
 70  
 
 71  
     @Override
 72  
     public String toString() {
 73  99
         return "Route[ " + inputs + " -> " + outputs + "]";
 74  
     }
 75  
 
 76  
     // TODO should we zap this and replace with next method?
 77  
     public void addRoutes(CamelContext context) throws Exception {
 78  0
         Collection<Route> routes = new ArrayList<Route>();
 79  
 
 80  0
         addRoutes(context, routes);
 81  
 
 82  0
         context.addRoutes(routes);
 83  0
     }
 84  
 
 85  
     public void addRoutes(CamelContext context, Collection<Route> routes) throws Exception {
 86  267
         setCamelContext(context);
 87  
 
 88  267
         for (FromType fromType : inputs) {
 89  267
             addRoutes(routes, fromType);
 90  264
         }
 91  264
     }
 92  
 
 93  
     public Endpoint resolveEndpoint(String uri) throws NoSuchEndpointException {
 94  579
         CamelContext context = getCamelContext();
 95  579
         if (context == null) {
 96  0
             throw new IllegalArgumentException("No CamelContext has been injected!");
 97  
         }
 98  579
         return CamelContextHelper.getMandatoryEndpoint(context, uri);
 99  
     }
 100  
 
 101  
     // Fluent API
 102  
     // -----------------------------------------------------------------------
 103  
 
 104  
     /**
 105  
      * Creates an input to the route
 106  
      */
 107  
     public RouteType from(String uri) {
 108  6
         getInputs().add(new FromType(uri));
 109  6
         return this;
 110  
     }
 111  
 
 112  
     // Properties
 113  
     // -----------------------------------------------------------------------
 114  
 
 115  
     public List<InterceptorType> getInterceptors() {
 116  921
         return interceptors;
 117  
     }
 118  
 
 119  
     public void setInterceptors(List<InterceptorType> interceptors) {
 120  0
         this.interceptors = interceptors;
 121  0
     }
 122  
 
 123  
     public List<FromType> getInputs() {
 124  306
         return inputs;
 125  
     }
 126  
 
 127  
     public void setInputs(List<FromType> inputs) {
 128  0
         this.inputs = inputs;
 129  0
     }
 130  
 
 131  
     public List<ProcessorType> getOutputs() {
 132  891
         return outputs;
 133  
     }
 134  
 
 135  
     public void setOutputs(List<ProcessorType> outputs) {
 136  0
         this.outputs = outputs;
 137  
 
 138  0
         if (outputs != null) {
 139  0
             for (ProcessorType output : outputs) {
 140  0
                 configureChild(output);
 141  0
             }
 142  
         }
 143  0
     }
 144  
 
 145  
     public CamelContext getCamelContext() {
 146  579
         return camelContext;
 147  
     }
 148  
 
 149  
     public void setCamelContext(CamelContext camelContext) {
 150  540
         this.camelContext = camelContext;
 151  540
     }
 152  
 
 153  
     // Implementation methods
 154  
     // -------------------------------------------------------------------------
 155  
 
 156  
     protected void addRoutes(Collection<Route> routes, FromType fromType) throws Exception {
 157  267
         RouteContext routeContext = new RouteContext(this, fromType, routes);
 158  267
         Endpoint endpoint = routeContext.getEndpoint();
 159  
 
 160  267
         for (ProcessorType output : outputs) {
 161  345
             output.addRoutes(routeContext, routes);
 162  342
         }
 163  
 
 164  264
         routeContext.commit();
 165  264
     }
 166  
 
 167  
     @Override
 168  
     protected void configureChild(ProcessorType output) {
 169  309
         if (isInheritErrorHandler()) {
 170  309
             output.setErrorHandlerBuilder(getErrorHandlerBuilder());
 171  
         }
 172  309
         List<InterceptorType> list = output.getInterceptors();
 173  309
         if (list == null) {
 174  0
             LOG.warn("No interceptor collection: " + output);
 175  0
         } else {
 176  309
             list.addAll(getInterceptors());
 177  
         }
 178  309
     }
 179  
 }