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.builder; 018 019 import java.util.ArrayList; 020 import java.util.Arrays; 021 import java.util.List; 022 023 import org.apache.camel.CamelContext; 024 import org.apache.camel.Endpoint; 025 import org.apache.camel.Expression; 026 import org.apache.camel.LoggingLevel; 027 import org.apache.camel.NoSuchEndpointException; 028 import org.apache.camel.util.ObjectHelper; 029 import org.apache.commons.logging.Log; 030 import org.apache.commons.logging.LogFactory; 031 032 /** 033 * Base class for implementation inheritance for different clauses in the <a 034 * href="http://camel.apache.org/dsl.html">Java DSL</a> 035 * 036 * @version $Revision: 784652 $ 037 */ 038 public abstract class BuilderSupport { 039 private CamelContext context; 040 private ErrorHandlerBuilder errorHandlerBuilder; 041 042 protected BuilderSupport(CamelContext context) { 043 this.context = context; 044 } 045 046 // Builder methods 047 // ------------------------------------------------------------------------- 048 049 /** 050 * Returns a value builder for the given header 051 */ 052 public ValueBuilder header(String name) { 053 return Builder.header(name); 054 } 055 056 /** 057 * Returns a value builder for the given property 058 */ 059 public ValueBuilder property(String name) { 060 return Builder.property(name); 061 } 062 063 /** 064 * Returns a predicate and value builder for the inbound body on an exchange 065 */ 066 public ValueBuilder body() { 067 return Builder.body(); 068 } 069 070 /** 071 * Returns a predicate and value builder for the inbound message body as a 072 * specific type 073 */ 074 public <T> ValueBuilder body(Class<T> type) { 075 return Builder.bodyAs(type); 076 } 077 078 /** 079 * Returns a predicate and value builder for the outbound body on an 080 * exchange 081 */ 082 public ValueBuilder outBody() { 083 return Builder.outBody(); 084 } 085 086 /** 087 * Returns a predicate and value builder for the outbound message body as a 088 * specific type 089 */ 090 public <T> ValueBuilder outBody(Class<T> type) { 091 return Builder.outBodyAs(type); 092 } 093 094 /** 095 * Returns a predicate and value builder for the fault body on an 096 * exchange 097 */ 098 public ValueBuilder faultBody() { 099 return Builder.faultBody(); 100 } 101 102 /** 103 * Returns a predicate and value builder for the fault message body as a 104 * specific type 105 */ 106 public <T> ValueBuilder faultBodyAs(Class<T> type) { 107 return Builder.faultBodyAs(type); 108 } 109 110 /** 111 * Returns a value builder for the given system property 112 */ 113 public ValueBuilder systemProperty(String name) { 114 return Builder.systemProperty(name); 115 } 116 117 /** 118 * Returns a value builder for the given system property 119 */ 120 public ValueBuilder systemProperty(String name, String defaultValue) { 121 return Builder.systemProperty(name, defaultValue); 122 } 123 124 /** 125 * Returns a constant expression value builder 126 */ 127 public ValueBuilder constant(Object value) { 128 return Builder.constant(value); 129 } 130 131 /** 132 * Returns a <a href="http://camel.apache.org/bean-language.html">bean expression</a> 133 * value builder 134 * 135 * @param beanRef reference to bean to lookup in the Registry 136 * @return the builder 137 */ 138 public ValueBuilder bean(String beanRef) { 139 return Builder.bean(beanRef, null); 140 } 141 142 /** 143 * Returns a <a href="http://camel.apache.org/bean-language.html">bean expression</a> 144 * value builder 145 * 146 * @param beanRef reference to bean to lookup in the Registry 147 * @param method name of method to invoke 148 * @return the builder 149 */ 150 public ValueBuilder bean(String beanRef, String method) { 151 return Builder.bean(beanRef, method); 152 } 153 154 /** 155 * Returns a <a href="http://camel.apache.org/bean-language.html">bean expression</a> 156 * value builder 157 * 158 * @param beanType the Class of the bean which we want to invoke 159 * @return the builder 160 */ 161 public ValueBuilder bean(Class beanType) { 162 return Builder.bean(beanType, null); 163 } 164 165 /** 166 * Returns a <a href="http://camel.apache.org/bean-language.html">bean expression</a> 167 * value builder 168 * 169 * @param beanType the Class of the bean which we want to invoke 170 * @param method name of method to invoke 171 * @return the builder 172 */ 173 public ValueBuilder bean(Class beanType, String method) { 174 return Builder.bean(beanType, method); 175 } 176 177 /** 178 * Returns an expression processing the exchange to the given endpoint uri 179 * 180 * @param uri endpoint uri to send the exchange to 181 * @return the builder 182 */ 183 public ValueBuilder sendTo(String uri) { 184 return Builder.sendTo(uri); 185 } 186 187 /** 188 * Returns an expression value builder that replaces all occurrences of the 189 * regular expression with the given replacement 190 */ 191 public ValueBuilder regexReplaceAll(Expression content, String regex, String replacement) { 192 return Builder.regexReplaceAll(content, regex, replacement); 193 } 194 195 /** 196 * Returns an expression value builder that replaces all occurrences of the 197 * regular expression with the given replacement 198 */ 199 public ValueBuilder regexReplaceAll(Expression content, String regex, Expression replacement) { 200 return Builder.regexReplaceAll(content, regex, replacement); 201 } 202 203 /** 204 * Returns a exception expression value builder 205 */ 206 public ValueBuilder exceptionMessage() { 207 return Builder.exceptionMessage(); 208 } 209 210 /** 211 * Resolves the given URI to an endpoint 212 * 213 * @param uri the uri to resolve 214 * @throws NoSuchEndpointException if the endpoint URI could not be resolved 215 * @return the endpoint 216 */ 217 public Endpoint endpoint(String uri) throws NoSuchEndpointException { 218 ObjectHelper.notNull(uri, "uri"); 219 Endpoint endpoint = getContext().getEndpoint(uri); 220 if (endpoint == null) { 221 throw new NoSuchEndpointException(uri); 222 } 223 return endpoint; 224 } 225 226 /** 227 * Resolves the given URI to an endpoint of the specified type 228 * 229 * @param uri the uri to resolve 230 * @param type the excepted type of the endpoint 231 * @throws NoSuchEndpointException if the endpoint URI could not be resolved 232 * @return the endpoint 233 */ 234 public <T extends Endpoint> T endpoint(String uri, Class<T> type) throws NoSuchEndpointException { 235 ObjectHelper.notNull(uri, "uri"); 236 T endpoint = getContext().getEndpoint(uri, type); 237 if (endpoint == null) { 238 throw new NoSuchEndpointException(uri); 239 } 240 return endpoint; 241 } 242 243 /** 244 * Resolves the list of URIs into a list of {@link Endpoint} instances 245 * 246 * @param uris list of endpoints to resolve 247 * @throws NoSuchEndpointException if an endpoint URI could not be resolved 248 * @return list of endpoints 249 */ 250 public List<Endpoint> endpoints(String... uris) throws NoSuchEndpointException { 251 List<Endpoint> endpoints = new ArrayList<Endpoint>(); 252 for (String uri : uris) { 253 endpoints.add(endpoint(uri)); 254 } 255 return endpoints; 256 } 257 258 /** 259 * Helper method to create a list of {@link Endpoint} instances 260 * 261 * @param endpoints endpoints 262 * @return list of the given endpoints 263 */ 264 public List<Endpoint> endpoints(Endpoint... endpoints) { 265 List<Endpoint> answer = new ArrayList<Endpoint>(); 266 answer.addAll(Arrays.asList(endpoints)); 267 return answer; 268 } 269 270 /** 271 * Creates a default <a href="http://camel.apache.org/error-handler.html">error handler</a>. 272 * 273 * @return the builder 274 */ 275 public DefaultErrorHandlerBuilder defaultErrorHandler() { 276 return new DefaultErrorHandlerBuilder(); 277 } 278 279 /** 280 * Creates a disabled <a href="http://camel.apache.org/error-handler.html">error handler</a> 281 * for removing the default error handler 282 * 283 * @return the builder 284 */ 285 public NoErrorHandlerBuilder noErrorHandler() { 286 return new NoErrorHandlerBuilder(); 287 } 288 289 /** 290 * Creates an <a href="http://camel.apache.org/error-handler.html">error handler</a> 291 * which just logs errors 292 * 293 * @return the builder 294 */ 295 public LoggingErrorHandlerBuilder loggingErrorHandler() { 296 return new LoggingErrorHandlerBuilder(); 297 } 298 299 /** 300 * Creates an <a href="http://camel.apache.org/error-handler.html">error handler</a> 301 * which just logs errors 302 * 303 * @return the builder 304 */ 305 public LoggingErrorHandlerBuilder loggingErrorHandler(String log) { 306 return loggingErrorHandler(LogFactory.getLog(log)); 307 } 308 309 /** 310 * Creates an <a href="http://camel.apache.org/error-handler.html">error handler</a> 311 * which just logs errors 312 * 313 * @return the builder 314 */ 315 public LoggingErrorHandlerBuilder loggingErrorHandler(Log log) { 316 return new LoggingErrorHandlerBuilder(log); 317 } 318 319 /** 320 * Creates an <a href="http://camel.apache.org/error-handler.html">error handler</a> 321 * which just logs errors 322 * 323 * @return the builder 324 */ 325 public LoggingErrorHandlerBuilder loggingErrorHandler(Log log, LoggingLevel level) { 326 return new LoggingErrorHandlerBuilder(log, level); 327 } 328 329 /** 330 * <a href="http://camel.apache.org/dead-letter-channel.html">Dead Letter Channel EIP:</a> 331 * is a error handler for handling messages that could not be delivered to it's intented destination. 332 * 333 * @param deadLetterUri uri to the dead letter endpoint storing dead messages 334 * @return the builder 335 */ 336 public DeadLetterChannelBuilder deadLetterChannel(String deadLetterUri) { 337 return deadLetterChannel(endpoint(deadLetterUri)); 338 } 339 340 /** 341 * <a href="http://camel.apache.org/dead-letter-channel.html">Dead Letter Channel EIP:</a> 342 * is a error handler for handling messages that could not be delivered to it's intented destination. 343 * 344 * @param deadLetterEndpoint dead letter endpoint storing dead messages 345 * @return the builder 346 */ 347 public DeadLetterChannelBuilder deadLetterChannel(Endpoint deadLetterEndpoint) { 348 return new DeadLetterChannelBuilder(deadLetterEndpoint); 349 } 350 351 // Properties 352 // ------------------------------------------------------------------------- 353 354 public CamelContext getContext() { 355 return context; 356 } 357 358 public void setContext(CamelContext context) { 359 this.context = context; 360 } 361 362 public ErrorHandlerBuilder getErrorHandlerBuilder() { 363 if (errorHandlerBuilder == null) { 364 errorHandlerBuilder = createErrorHandlerBuilder(); 365 } 366 return errorHandlerBuilder; 367 } 368 369 protected ErrorHandlerBuilder createErrorHandlerBuilder() { 370 return new DefaultErrorHandlerBuilder(); 371 } 372 373 /** 374 * Sets the error handler to use with processors created by this builder 375 */ 376 public void setErrorHandlerBuilder(ErrorHandlerBuilder errorHandlerBuilder) { 377 this.errorHandlerBuilder = errorHandlerBuilder; 378 } 379 380 }