Coverage Report - org.apache.camel.builder.RouteBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
RouteBuilder
78% 
100% 
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.builder;
 18  
 
 19  
 import org.apache.camel.CamelContext;
 20  
 import org.apache.camel.Endpoint;
 21  
 import org.apache.camel.Predicate;
 22  
 import org.apache.camel.Route;
 23  
 import org.apache.camel.impl.DefaultCamelContext;
 24  
 import org.apache.camel.model.InterceptType;
 25  
 import org.apache.camel.model.OtherwiseType;
 26  
 import org.apache.camel.model.ProcessorType;
 27  
 import org.apache.camel.model.RouteType;
 28  
 import org.apache.camel.model.RoutesType;
 29  
 import org.apache.camel.model.ExceptionType;
 30  
 import org.apache.camel.processor.DelegateProcessor;
 31  
 
 32  
 import java.util.ArrayList;
 33  
 import java.util.List;
 34  
 import java.util.concurrent.atomic.AtomicBoolean;
 35  
 
 36  
 /**
 37  
  * A <a href="http://activemq.apache.org/camel/dsl.html">Java DSL</a> which is
 38  
  * used to build {@link Route} instances in a
 39  
  * 
 40  
  * @{link CamelContext} for smart routing.
 41  
  * 
 42  
  * @version $Revision: 564295 $
 43  
  */
 44  
 public abstract class RouteBuilder extends BuilderSupport {
 45  267
     private AtomicBoolean initalized = new AtomicBoolean(false);
 46  267
     private RoutesType routeCollection = new RoutesType();
 47  267
     private List<Route> routes = new ArrayList<Route>();
 48  
 
 49  
     protected RouteBuilder() {
 50  267
         this(null);
 51  267
     }
 52  
 
 53  
     protected RouteBuilder(CamelContext context) {
 54  267
         super(context);
 55  267
     }
 56  
 
 57  
     /**
 58  
      * Called on initialization to to build the required destinationBuilders
 59  
      */
 60  
     public abstract void configure() throws Exception;
 61  
 
 62  
     /**
 63  
      * Creates a new route from the given URI input
 64  
      */
 65  
     public RouteType from(String uri) {
 66  267
         return routeCollection.from(uri);
 67  
     }
 68  
 
 69  
     /**
 70  
      * Creates a new route from the given endpoint
 71  
      */
 72  
     public RouteType from(Endpoint endpoint) {
 73  0
         return routeCollection.from(endpoint);
 74  
     }
 75  
 
 76  
     /**
 77  
      * Installs the given error handler builder
 78  
      * 
 79  
      * @param errorHandlerBuilder the error handler to be used by default for
 80  
      *                all child routes
 81  
      * @return the current builder with the error handler configured
 82  
      */
 83  
     public RouteBuilder errorHandler(ErrorHandlerBuilder errorHandlerBuilder) {
 84  0
         setErrorHandlerBuilder(errorHandlerBuilder);
 85  0
         return this;
 86  
     }
 87  
 
 88  
     /**
 89  
      * Configures whether or not the error handler is inherited by every
 90  
      * processing node (or just the top most one)
 91  
      * 
 92  
      * @param value the flag as to whether error handlers should be inherited or
 93  
      *                not
 94  
      * @return the current builder
 95  
      */
 96  
     public RouteBuilder inheritErrorHandler(boolean value) {
 97  0
         routeCollection.setInheritErrorHandlerFlag(value);
 98  0
         return this;
 99  
     }
 100  
 
 101  
     /**
 102  
      * Adds the given interceptor to this route
 103  
      */
 104  
     public RouteBuilder intercept(DelegateProcessor interceptor) {
 105  0
         routeCollection.intercept(interceptor);
 106  0
         return this;
 107  
     }
 108  
 
 109  
     /**
 110  
      * Adds a route for an interceptor; use the {@link ProcessorType#proceed()} method
 111  
      * to continue processing the underying route being intercepted.
 112  
      *
 113  
      * @return
 114  
      */
 115  
     public InterceptType intercept() {
 116  12
         return routeCollection.intercept();
 117  
     }
 118  
 
 119  
     /**
 120  
      * Applies a route for an interceptor if the given predicate is true
 121  
      * otherwise the interceptor route is not applied
 122  
      */
 123  
     public OtherwiseType intercept(Predicate predicate) {
 124  12
         return routeCollection.intercept(predicate);
 125  
     }
 126  
 
 127  
     /**
 128  
      * Adds an exception handler route for the given exception type
 129  
      */
 130  
     public ExceptionType exception(Class exceptionType) {
 131  18
         return routeCollection.exception(exceptionType);
 132  
     }
 133  
 
 134  
     // Properties
 135  
     // -----------------------------------------------------------------------
 136  
     public CamelContext getContext() {
 137  273
         CamelContext context = super.getContext();
 138  273
         if (context == null) {
 139  36
             context = createContainer();
 140  36
             setContext(context);
 141  
         }
 142  273
         return context;
 143  
     }
 144  
 
 145  
     /**
 146  
      * Returns the routing map from inbound endpoints to processors
 147  
      */
 148  
     public List<Route> getRouteList() throws Exception {
 149  267
         checkInitialized();
 150  264
         return routes;
 151  
     }
 152  
 
 153  
     // Implementation methods
 154  
     // -----------------------------------------------------------------------
 155  
     protected void checkInitialized() throws Exception {
 156  267
         if (initalized.compareAndSet(false, true)) {
 157  267
             configure();
 158  267
             populateRoutes(routes);
 159  
         }
 160  264
     }
 161  
 
 162  
     protected void populateRoutes(List<Route> routes) throws Exception {
 163  267
         CamelContext camelContext = getContext();
 164  267
         if (camelContext == null) {
 165  0
             throw new IllegalArgumentException("No CamelContext has been injected!");
 166  
         }
 167  267
         routeCollection.setCamelContext(camelContext);
 168  267
         routeCollection.populateRoutes(routes);
 169  264
     }
 170  
 
 171  
     /**
 172  
      * Factory method
 173  
      */
 174  
     protected CamelContext createContainer() {
 175  36
         return new DefaultCamelContext();
 176  
     }
 177  
 }