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.builder.xml.XPathBuilder;
029 import org.apache.camel.model.ModelCamelContext;
030 import org.apache.camel.model.language.HeaderExpression;
031 import org.apache.camel.model.language.MethodCallExpression;
032 import org.apache.camel.model.language.PropertyExpression;
033 import org.apache.camel.util.ObjectHelper;
034 import org.slf4j.Logger;
035 import org.slf4j.LoggerFactory;
036
037 /**
038 * Base class for implementation inheritance for different clauses in the <a
039 * href="http://camel.apache.org/dsl.html">Java DSL</a>
040 *
041 * @version
042 */
043 public abstract class BuilderSupport {
044 private ModelCamelContext context;
045 private ErrorHandlerBuilder errorHandlerBuilder;
046
047 protected BuilderSupport(CamelContext context) {
048 this.context = (ModelCamelContext)context;
049 }
050
051 // Builder methods
052 // -------------------------------------------------------------------------
053
054 /**
055 * Returns a value builder for the given header
056 */
057 public ValueBuilder header(String name) {
058 HeaderExpression expression = new HeaderExpression(name);
059 return new ValueBuilder(expression);
060 }
061
062 /**
063 * Returns a value builder for the given property
064 */
065 public ValueBuilder property(String name) {
066 PropertyExpression expression = new PropertyExpression(name);
067 return new ValueBuilder(expression);
068 }
069
070 /**
071 * Returns a predicate and value builder for the inbound body on an exchange
072 */
073 public ValueBuilder body() {
074 return Builder.body();
075 }
076
077 /**
078 * Returns a predicate and value builder for the inbound message body as a
079 * specific type
080 */
081 public <T> ValueBuilder body(Class<T> type) {
082 return Builder.bodyAs(type);
083 }
084
085 /**
086 * Returns a predicate and value builder for the outbound body on an
087 * exchange
088 */
089 public ValueBuilder outBody() {
090 return Builder.outBody();
091 }
092
093 /**
094 * Returns a predicate and value builder for the outbound message body as a
095 * specific type
096 */
097 public <T> ValueBuilder outBody(Class<T> type) {
098 return Builder.outBodyAs(type);
099 }
100
101 /**
102 * Returns a predicate and value builder for the fault body on an
103 * exchange
104 */
105 public ValueBuilder faultBody() {
106 return Builder.faultBody();
107 }
108
109 /**
110 * Returns a predicate and value builder for the fault message body as a
111 * specific type
112 */
113 public <T> ValueBuilder faultBodyAs(Class<T> type) {
114 return Builder.faultBodyAs(type);
115 }
116
117 /**
118 * Returns a value builder for the given system property
119 */
120 public ValueBuilder systemProperty(String name) {
121 return Builder.systemProperty(name);
122 }
123
124 /**
125 * Returns a value builder for the given system property
126 */
127 public ValueBuilder systemProperty(String name, String defaultValue) {
128 return Builder.systemProperty(name, defaultValue);
129 }
130
131 /**
132 * Returns a constant expression value builder
133 */
134 public ValueBuilder constant(Object value) {
135 return Builder.constant(value);
136 }
137
138 /**
139 * Returns a simple expression value builder
140 */
141 public SimpleBuilder simple(String value) {
142 return SimpleBuilder.simple(value);
143 }
144
145 /**
146 * Returns a simple expression value builder
147 */
148 public SimpleBuilder simple(String value, Class<?> resultType) {
149 return SimpleBuilder.simple(value, resultType);
150 }
151
152 /**
153 * Returns a xpath expression value builder
154 */
155 public XPathBuilder xpath(String value) {
156 return XPathBuilder.xpath(value);
157 }
158
159 /**
160 * Returns a <a href="http://camel.apache.org/bean-language.html">method call expression</a>
161 * value builder
162 * <p/>
163 * This method accepts dual parameters. Either an bean instance or a reference to a bean (String).
164 *
165 * @param beanOrBeanRef either an instanceof a bean or a reference to bean to lookup in the Registry
166 * @return the builder
167 * @deprecated use {@link #method(Object)} instead
168 */
169 @Deprecated
170 public ValueBuilder bean(Object beanOrBeanRef) {
171 return bean(beanOrBeanRef, null);
172 }
173
174 /**
175 * Returns a <a href="http://camel.apache.org/bean-language.html">method call expression</a>
176 * value builder
177 * <p/>
178 * This method accepts dual parameters. Either an bean instance or a reference to a bean (String).
179 *
180 * @param beanOrBeanRef either an instanceof a bean or a reference to bean to lookup in the Registry
181 * @param method name of method to invoke
182 * @return the builder
183 * @deprecated use {@link #method(Object, String)} instead
184 */
185 @Deprecated
186 public ValueBuilder bean(Object beanOrBeanRef, String method) {
187 MethodCallExpression expression;
188 if (beanOrBeanRef instanceof String) {
189 expression = new MethodCallExpression((String) beanOrBeanRef, method);
190 } else {
191 expression = new MethodCallExpression(beanOrBeanRef, method);
192 }
193 return new ValueBuilder(expression);
194 }
195
196 /**
197 * Returns a <a href="http://camel.apache.org/bean-language.html">method call expression</a>
198 * value builder
199 *
200 * @param beanType the Class of the bean which we want to invoke
201 * @return the builder
202 * @deprecated use {@link #method(Class)} instead
203 */
204 @Deprecated
205 public ValueBuilder bean(Class<?> beanType) {
206 MethodCallExpression expression = new MethodCallExpression(beanType);
207 return new ValueBuilder(expression);
208 }
209
210 /**
211 * Returns a <a href="http://camel.apache.org/bean-language.html">method call expression</a>
212 * value builder
213 *
214 * @param beanType the Class of the bean which we want to invoke
215 * @param method name of method to invoke
216 * @return the builder
217 * @deprecated use {@link #method(Class, String)} instead
218 */
219 @Deprecated
220 public ValueBuilder bean(Class<?> beanType, String method) {
221 MethodCallExpression expression = new MethodCallExpression(beanType, method);
222 return new ValueBuilder(expression);
223 }
224
225 /**
226 * Returns a <a href="http://camel.apache.org/bean-language.html">method call expression</a>
227 * value builder
228 * <p/>
229 * This method accepts dual parameters. Either an bean instance or a reference to a bean (String).
230 *
231 * @param beanOrBeanRef either an instanceof a bean or a reference to bean to lookup in the Registry
232 * @return the builder
233 */
234 public ValueBuilder method(Object beanOrBeanRef) {
235 return method(beanOrBeanRef, null);
236 }
237
238 /**
239 * Returns a <a href="http://camel.apache.org/bean-language.html">method call expression</a>
240 * value builder
241 * <p/>
242 * This method accepts dual parameters. Either an bean instance or a reference to a bean (String).
243 *
244 * @param beanOrBeanRef either an instanceof a bean or a reference to bean to lookup in the Registry
245 * @param method name of method to invoke
246 * @return the builder
247 */
248 public ValueBuilder method(Object beanOrBeanRef, String method) {
249 MethodCallExpression expression;
250 if (beanOrBeanRef instanceof String) {
251 expression = new MethodCallExpression((String) beanOrBeanRef, method);
252 } else {
253 expression = new MethodCallExpression(beanOrBeanRef, method);
254 }
255 return new ValueBuilder(expression);
256 }
257
258 /**
259 * Returns a <a href="http://camel.apache.org/bean-language.html">method call expression</a>
260 * value builder
261 *
262 * @param beanType the Class of the bean which we want to invoke
263 * @return the builder
264 */
265 public ValueBuilder method(Class<?> beanType) {
266 MethodCallExpression expression = new MethodCallExpression(beanType);
267 return new ValueBuilder(expression);
268 }
269
270 /**
271 * Returns a <a href="http://camel.apache.org/bean-language.html">method call expression</a>
272 * value builder
273 *
274 * @param beanType the Class of the bean which we want to invoke
275 * @param method name of method to invoke
276 * @return the builder
277 */
278 public ValueBuilder method(Class<?> beanType, String method) {
279 MethodCallExpression expression = new MethodCallExpression(beanType, method);
280 return new ValueBuilder(expression);
281 }
282
283 /**
284 * Returns an expression processing the exchange to the given endpoint uri
285 *
286 * @param uri endpoint uri to send the exchange to
287 * @return the builder
288 */
289 public ValueBuilder sendTo(String uri) {
290 return Builder.sendTo(uri);
291 }
292
293 /**
294 * Returns an expression value builder that replaces all occurrences of the
295 * regular expression with the given replacement
296 */
297 public ValueBuilder regexReplaceAll(Expression content, String regex, String replacement) {
298 return Builder.regexReplaceAll(content, regex, replacement);
299 }
300
301 /**
302 * Returns an expression value builder that replaces all occurrences of the
303 * regular expression with the given replacement
304 */
305 public ValueBuilder regexReplaceAll(Expression content, String regex, Expression replacement) {
306 return Builder.regexReplaceAll(content, regex, replacement);
307 }
308
309 /**
310 * Returns a exception expression value builder
311 */
312 public ValueBuilder exceptionMessage() {
313 return Builder.exceptionMessage();
314 }
315
316 /**
317 * Resolves the given URI to an endpoint
318 *
319 * @param uri the uri to resolve
320 * @throws NoSuchEndpointException if the endpoint URI could not be resolved
321 * @return the endpoint
322 */
323 public Endpoint endpoint(String uri) throws NoSuchEndpointException {
324 ObjectHelper.notNull(uri, "uri");
325 Endpoint endpoint = getContext().getEndpoint(uri);
326 if (endpoint == null) {
327 throw new NoSuchEndpointException(uri);
328 }
329 return endpoint;
330 }
331
332 /**
333 * Resolves the given URI to an endpoint of the specified type
334 *
335 * @param uri the uri to resolve
336 * @param type the excepted type of the endpoint
337 * @throws NoSuchEndpointException if the endpoint URI could not be resolved
338 * @return the endpoint
339 */
340 public <T extends Endpoint> T endpoint(String uri, Class<T> type) throws NoSuchEndpointException {
341 ObjectHelper.notNull(uri, "uri");
342 T endpoint = getContext().getEndpoint(uri, type);
343 if (endpoint == null) {
344 throw new NoSuchEndpointException(uri);
345 }
346 return endpoint;
347 }
348
349 /**
350 * Resolves the list of URIs into a list of {@link Endpoint} instances
351 *
352 * @param uris list of endpoints to resolve
353 * @throws NoSuchEndpointException if an endpoint URI could not be resolved
354 * @return list of endpoints
355 */
356 public List<Endpoint> endpoints(String... uris) throws NoSuchEndpointException {
357 List<Endpoint> endpoints = new ArrayList<Endpoint>();
358 for (String uri : uris) {
359 endpoints.add(endpoint(uri));
360 }
361 return endpoints;
362 }
363
364 /**
365 * Helper method to create a list of {@link Endpoint} instances
366 *
367 * @param endpoints endpoints
368 * @return list of the given endpoints
369 */
370 public List<Endpoint> endpoints(Endpoint... endpoints) {
371 List<Endpoint> answer = new ArrayList<Endpoint>();
372 answer.addAll(Arrays.asList(endpoints));
373 return answer;
374 }
375
376 /**
377 * Creates a default <a href="http://camel.apache.org/error-handler.html">error handler</a>.
378 *
379 * @return the builder
380 */
381 public DefaultErrorHandlerBuilder defaultErrorHandler() {
382 return new DefaultErrorHandlerBuilder();
383 }
384
385 /**
386 * Creates a disabled <a href="http://camel.apache.org/error-handler.html">error handler</a>
387 * for removing the default error handler
388 *
389 * @return the builder
390 */
391 public NoErrorHandlerBuilder noErrorHandler() {
392 return new NoErrorHandlerBuilder();
393 }
394
395 /**
396 * Creates an <a href="http://camel.apache.org/error-handler.html">error handler</a>
397 * which just logs errors
398 *
399 * @return the builder
400 */
401 public LoggingErrorHandlerBuilder loggingErrorHandler() {
402 return new LoggingErrorHandlerBuilder();
403 }
404
405 /**
406 * Creates an <a href="http://camel.apache.org/error-handler.html">error handler</a>
407 * which just logs errors
408 *
409 * @return the builder
410 */
411 public LoggingErrorHandlerBuilder loggingErrorHandler(String log) {
412 return loggingErrorHandler(LoggerFactory.getLogger(log));
413 }
414
415 /**
416 * Creates an <a href="http://camel.apache.org/error-handler.html">error handler</a>
417 * which just logs errors
418 *
419 * @return the builder
420 */
421 public LoggingErrorHandlerBuilder loggingErrorHandler(Logger log) {
422 return new LoggingErrorHandlerBuilder(log);
423 }
424
425 /**
426 * Creates an <a href="http://camel.apache.org/error-handler.html">error handler</a>
427 * which just logs errors
428 *
429 * @return the builder
430 */
431 public LoggingErrorHandlerBuilder loggingErrorHandler(Logger log, LoggingLevel level) {
432 return new LoggingErrorHandlerBuilder(log, level);
433 }
434
435 /**
436 * <a href="http://camel.apache.org/dead-letter-channel.html">Dead Letter Channel EIP:</a>
437 * is a error handler for handling messages that could not be delivered to it's intended destination.
438 *
439 * @param deadLetterUri uri to the dead letter endpoint storing dead messages
440 * @return the builder
441 */
442 public DeadLetterChannelBuilder deadLetterChannel(String deadLetterUri) {
443 return deadLetterChannel(endpoint(deadLetterUri));
444 }
445
446 /**
447 * <a href="http://camel.apache.org/dead-letter-channel.html">Dead Letter Channel EIP:</a>
448 * is a error handler for handling messages that could not be delivered to it's intended destination.
449 *
450 * @param deadLetterEndpoint dead letter endpoint storing dead messages
451 * @return the builder
452 */
453 public DeadLetterChannelBuilder deadLetterChannel(Endpoint deadLetterEndpoint) {
454 return new DeadLetterChannelBuilder(deadLetterEndpoint);
455 }
456
457 // Properties
458 // -------------------------------------------------------------------------
459
460 public ModelCamelContext getContext() {
461 return context;
462 }
463
464 @Deprecated
465 public void setContext(CamelContext context) {
466 this.context = (ModelCamelContext)context;
467 }
468
469 public void setContext(ModelCamelContext context) {
470 this.context = context;
471 }
472
473 public ErrorHandlerBuilder getErrorHandlerBuilder() {
474 if (errorHandlerBuilder == null) {
475 errorHandlerBuilder = createErrorHandlerBuilder();
476 }
477 return errorHandlerBuilder;
478 }
479
480 protected ErrorHandlerBuilder createErrorHandlerBuilder() {
481 return new DefaultErrorHandlerBuilder();
482 }
483
484 /**
485 * Sets the error handler to use with processors created by this builder
486 */
487 public void setErrorHandlerBuilder(ErrorHandlerBuilder errorHandlerBuilder) {
488 this.errorHandlerBuilder = errorHandlerBuilder;
489 }
490
491 }