Coverage Report - org.apache.camel.builder.RouteBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
RouteBuilder
85% 
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 java.util.ArrayList;
 20  
 import java.util.List;
 21  
 import java.util.concurrent.atomic.AtomicBoolean;
 22  
 
 23  
 import org.apache.camel.CamelContext;
 24  
 import org.apache.camel.Endpoint;
 25  
 import org.apache.camel.Exchange;
 26  
 import org.apache.camel.Processor;
 27  
 import org.apache.camel.Route;
 28  
 import org.apache.camel.impl.DefaultCamelContext;
 29  
 
 30  
 /**
 31  
  * A <a href="http://activemq.apache.org/camel/dsl.html">Java DSL</a>
 32  
  * which is used to build {@link Route} instances in a @{link CamelContext} for smart routing.
 33  
  *
 34  
  * @version $Revision: 541693 $
 35  
  */
 36  
 public abstract class RouteBuilder extends BuilderSupport {
 37  43
     private List<FromBuilder> fromBuilders = new ArrayList<FromBuilder>();
 38  43
     private AtomicBoolean initalized = new AtomicBoolean(false);
 39  43
     private List<Route> routes = new ArrayList<Route>();
 40  
 
 41  
     protected RouteBuilder() {
 42  43
         this(null);
 43  43
     }
 44  
 
 45  
     protected RouteBuilder(CamelContext context) {
 46  43
         super(context);
 47  43
     }
 48  
 
 49  
     /**
 50  
      * Called on initialization to to build the required destinationBuilders
 51  
      */
 52  
     public abstract void configure() throws Exception;
 53  
 
 54  
     @Fluent
 55  
     public FromBuilder from( @FluentArg("uri") String uri) {
 56  54
             if( uri == null ) {
 57  0
                     throw new IllegalArgumentException("uri parameter cannot be null");
 58  
             }
 59  54
             Endpoint endpoint = endpoint(uri);
 60  54
             if( endpoint == null ) {
 61  0
                     throw new IllegalArgumentException("uri '"+uri+"' could not be resolved.");
 62  
             }
 63  54
         return from(endpoint);
 64  
     }
 65  
 
 66  
     @Fluent
 67  
     public FromBuilder from( @FluentArg("ref") Endpoint endpoint) {
 68  54
         FromBuilder answer = new FromBuilder(this, endpoint);
 69  54
         addFromBuilder(answer);
 70  54
         return answer;
 71  
     }
 72  
 
 73  
     /**
 74  
      * Installs the given error handler builder
 75  
      *
 76  
      * @param errorHandlerBuilder the error handler to be used by default for all child routes
 77  
      * @return the current builder with the error handler configured
 78  
      */
 79  
     public RouteBuilder errorHandler(ErrorHandlerBuilder errorHandlerBuilder) {
 80  0
         setErrorHandlerBuilder(errorHandlerBuilder);
 81  0
         return this;
 82  
     }
 83  
 
 84  
     /**
 85  
      * Configures whether or not the error handler is inherited by every processing node (or just the top most one)
 86  
      *
 87  
      * @param value the flag as to whether error handlers should be inherited or not
 88  
      * @return the current builder
 89  
      */
 90  
     public RouteBuilder inheritErrorHandler(boolean value) {
 91  1
         setInheritErrorHandler(value);
 92  1
         return this;
 93  
     }
 94  
 
 95  
     // Properties
 96  
     //-----------------------------------------------------------------------
 97  
     public CamelContext getContext() {
 98  110
         CamelContext context = super.getContext();
 99  110
         if (context == null) {
 100  12
             context = createContainer();
 101  12
             setContext(context);
 102  
         }
 103  110
         return context;
 104  
     }
 105  
 
 106  
     /**
 107  
      * Returns the routing map from inbound endpoints to processors
 108  
      */
 109  
     public List<Route> getRouteList() throws Exception {
 110  43
         checkInitialized();
 111  43
         return routes;
 112  
     }
 113  
 
 114  
     /**
 115  
      * Returns the builders which have been created
 116  
      */
 117  
     public List<FromBuilder> getFromBuilders() throws Exception {
 118  0
         checkInitialized();
 119  0
         return fromBuilders;
 120  
     }
 121  
 
 122  
     // Implementation methods
 123  
     //-----------------------------------------------------------------------
 124  
     public void addFromBuilder(FromBuilder answer) {
 125  54
         fromBuilders.add(answer);
 126  54
     }
 127  
 
 128  
     protected void checkInitialized() throws Exception {
 129  43
         if (initalized.compareAndSet(false, true)) {
 130  43
             configure();
 131  43
             populateRoutes(routes);
 132  
         }
 133  43
     }
 134  
 
 135  
     protected void populateRoutes(List<Route> routes) throws Exception {
 136  43
         for (FromBuilder builder : fromBuilders) {
 137  54
             Route route = builder.createRoute();
 138  54
             routes.add(route);
 139  54
         }
 140  43
     }
 141  
 
 142  
     /**
 143  
      * Factory method
 144  
      */
 145  
     protected CamelContext createContainer() {
 146  12
         return new DefaultCamelContext();
 147  
     }
 148  
 }