Coverage Report - org.apache.camel.builder.BuilderSupport
 
Classes in this File Line Coverage Branch Coverage Complexity
BuilderSupport
73% 
80% 
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  
 
 22  
 import org.apache.camel.CamelContext;
 23  
 import org.apache.camel.Endpoint;
 24  
 import org.apache.camel.processor.LoggingLevel;
 25  
 import org.apache.camel.processor.SendProcessor;
 26  
 import org.apache.commons.logging.Log;
 27  
 import org.apache.commons.logging.LogFactory;
 28  
 
 29  
 /**
 30  
  * Base class for implementation inheritance for different clauses in the 
 31  
  * <a href="http://activemq.apache.org/camel/dsl.html">Java DSL</a>
 32  
  *
 33  
  * @version $Revision: $
 34  
  */
 35  
 public abstract class BuilderSupport {
 36  
     private CamelContext context;
 37  
     private ErrorHandlerBuilder errorHandlerBuilder;
 38  190
     private boolean inheritErrorHandler = true;
 39  
 
 40  43
     protected BuilderSupport(CamelContext context) {
 41  43
         this.context = context;
 42  43
     }
 43  
 
 44  147
     protected BuilderSupport(BuilderSupport parent) {
 45  147
         this.context = parent.getContext();
 46  147
         this.inheritErrorHandler = parent.inheritErrorHandler;
 47  147
         if (inheritErrorHandler && parent.errorHandlerBuilder != null) {
 48  0
             this.errorHandlerBuilder = parent.errorHandlerBuilder.copy();
 49  
         }
 50  147
     }
 51  
 
 52  
     // Builder methods
 53  
     //-------------------------------------------------------------------------
 54  
 
 55  
     /**
 56  
      * Returns a value builder for the given header
 57  
      */
 58  
     @Fluent
 59  
     public ValueBuilder header(@FluentArg("name")String name) {
 60  20
         return Builder.header(name);
 61  
     }
 62  
 
 63  
     /**
 64  
      * Returns a predicate and value builder for the inbound body on an exchange
 65  
      */
 66  
     @Fluent
 67  
     public ValueBuilder body() {
 68  4
         return Builder.body();
 69  
     }
 70  
 
 71  
     /**
 72  
      * Returns a predicate and value builder for the inbound message body as a specific type
 73  
      */
 74  
     @Fluent
 75  
     public <T> ValueBuilder bodyAs(@FluentArg("class")Class<T> type) {
 76  1
         return Builder.bodyAs(type);
 77  
     }
 78  
 
 79  
     /**
 80  
      * Returns a predicate and value builder for the outbound body on an exchange
 81  
      */
 82  
     @Fluent
 83  
     public ValueBuilder outBody() {
 84  0
         return Builder.outBody();
 85  
     }
 86  
 
 87  
     /**
 88  
      * Returns a predicate and value builder for the outbound message body as a specific type
 89  
      */
 90  
     @Fluent
 91  
     public <T> ValueBuilder outBody(@FluentArg("class")Class<T> type) {
 92  0
         return Builder.outBody(type);
 93  
     }
 94  
 
 95  
     /**
 96  
      * Returns a value builder for the given system property
 97  
      */
 98  
     @Fluent
 99  
     public ValueBuilder systemProperty(@FluentArg("name")String name) {
 100  0
         return Builder.systemProperty(name);
 101  
     }
 102  
 
 103  
     /**
 104  
      * Returns a value builder for the given system property
 105  
      */
 106  
     @Fluent
 107  
     public ValueBuilder systemProperty(
 108  
             @FluentArg("name")String name, @FluentArg("defaultValue")String defaultValue) {
 109  0
         return Builder.systemProperty(name, defaultValue);
 110  
     }
 111  
 
 112  
     /**
 113  
      * Resolves the given URI to an endpoint
 114  
      */
 115  
     @Fluent
 116  
     public Endpoint endpoint(@FluentArg("uri")String uri) {
 117  113
         return getContext().getEndpoint(uri);
 118  
     }
 119  
 
 120  
     /**
 121  
      * Resolves the list of URIs into a list of {@link Endpoint} instances
 122  
      */
 123  
     @Fluent
 124  
     public List<Endpoint> endpoints(@FluentArg("uris")String... uris) {
 125  3
         List<Endpoint> endpoints = new ArrayList<Endpoint>();
 126  12
         for (String uri : uris) {
 127  9
             endpoints.add(endpoint(uri));
 128  
         }
 129  3
         return endpoints;
 130  
     }
 131  
 
 132  
     /**
 133  
      * Helper method to create a list of {@link Endpoint} instances
 134  
      */
 135  
     @Fluent
 136  
     public List<Endpoint> endpoints(@FluentArg("endpoints")Endpoint... endpoints) {
 137  0
         List<Endpoint> answer = new ArrayList<Endpoint>();
 138  0
         for (Endpoint endpoint : endpoints) {
 139  0
             answer.add(endpoint);
 140  
         }
 141  0
         return answer;
 142  
     }
 143  
 
 144  
     /**
 145  
      * Creates a disabled error handler for removing the default error handler
 146  
      */
 147  
     @Fluent
 148  
     public NoErrorHandlerBuilder noErrorHandler() {
 149  0
         return new NoErrorHandlerBuilder();
 150  
     }
 151  
 
 152  
     /**
 153  
      * Creates an error handler which just logs errors
 154  
      */
 155  
     @Fluent
 156  
     public LoggingErrorHandlerBuilder loggingErrorHandler() {
 157  0
         return new LoggingErrorHandlerBuilder();
 158  
     }
 159  
 
 160  
     /**
 161  
      * Creates an error handler which just logs errors
 162  
      */
 163  
     @Fluent
 164  
     public LoggingErrorHandlerBuilder loggingErrorHandler(@FluentArg("log")String log) {
 165  1
         return loggingErrorHandler(LogFactory.getLog(log));
 166  
     }
 167  
 
 168  
     /**
 169  
      * Creates an error handler which just logs errors
 170  
      */
 171  
     @Fluent
 172  
     public LoggingErrorHandlerBuilder loggingErrorHandler(@FluentArg("log")Log log) {
 173  1
         return new LoggingErrorHandlerBuilder(log);
 174  
     }
 175  
 
 176  
     /**
 177  
      * Creates an error handler which just logs errors
 178  
      */
 179  
     @Fluent
 180  
     public LoggingErrorHandlerBuilder loggingErrorHandler(
 181  
             @FluentArg("log")Log log, @FluentArg("level")LoggingLevel level) {
 182  0
         return new LoggingErrorHandlerBuilder(log, level);
 183  
     }
 184  
 
 185  
     @Fluent
 186  
     public DeadLetterChannelBuilder deadLetterChannel() {
 187  0
         return new DeadLetterChannelBuilder();
 188  
     }
 189  
 
 190  
     @Fluent
 191  
     public DeadLetterChannelBuilder deadLetterChannel(@FluentArg("uri")String deadLetterUri) {
 192  2
         return deadLetterChannel(endpoint(deadLetterUri));
 193  
     }
 194  
 
 195  
     @Fluent
 196  
     public DeadLetterChannelBuilder deadLetterChannel(@FluentArg("ref")Endpoint deadLetterEndpoint) {
 197  2
         return new DeadLetterChannelBuilder(new SendProcessor(deadLetterEndpoint));
 198  
     }
 199  
 
 200  
     // Properties
 201  
     //-------------------------------------------------------------------------
 202  
     public CamelContext getContext() {
 203  260
         return context;
 204  
     }
 205  
 
 206  
     public void setContext(CamelContext context) {
 207  43
         this.context = context;
 208  43
     }
 209  
 
 210  
     public ErrorHandlerBuilder getErrorHandlerBuilder() {
 211  92
         if (errorHandlerBuilder == null) {
 212  83
             errorHandlerBuilder = createErrorHandlerBuilder();
 213  
         }
 214  92
         return errorHandlerBuilder;
 215  
     }
 216  
 
 217  
     protected ErrorHandlerBuilder createErrorHandlerBuilder() {
 218  83
         if (isInheritErrorHandler()) {
 219  82
             return new DeadLetterChannelBuilder();
 220  
         }
 221  
         else {
 222  1
             return new NoErrorHandlerBuilder();
 223  
         }
 224  
     }
 225  
 
 226  
     /**
 227  
      * Sets the error handler to use with processors created by this builder
 228  
      */
 229  
     public void setErrorHandlerBuilder(ErrorHandlerBuilder errorHandlerBuilder) {
 230  3
         this.errorHandlerBuilder = errorHandlerBuilder;
 231  3
     }
 232  
 
 233  
     public boolean isInheritErrorHandler() {
 234  83
         return inheritErrorHandler;
 235  
     }
 236  
 
 237  
     public void setInheritErrorHandler(boolean inheritErrorHandler) {
 238  1
         this.inheritErrorHandler = inheritErrorHandler;
 239  1
     }
 240  
 }