|
19 | 19 | package org.jooby.camel; |
20 | 20 |
|
21 | 21 | import static java.util.Objects.requireNonNull; |
| 22 | +import static javaslang.API.$; |
| 23 | +import static javaslang.API.Case; |
| 24 | +import static javaslang.API.Match; |
22 | 25 |
|
23 | 26 | import java.io.File; |
24 | 27 | import java.lang.reflect.Method; |
|
50 | 53 | import com.typesafe.config.Config; |
51 | 54 | import com.typesafe.config.ConfigFactory; |
52 | 55 |
|
| 56 | +import javaslang.control.Try; |
| 57 | + |
53 | 58 | /** |
54 | 59 | * Camel for Jooby. Exposes a {@link CamelContext}, {@link ProducerTemplate} and |
55 | 60 | * {@link ConsumerTemplate}. |
@@ -272,6 +277,12 @@ public Camel routes(final Class<?> routeClass) { |
272 | 277 | return this; |
273 | 278 | } |
274 | 279 |
|
| 280 | + /** |
| 281 | + * Hook to customize a {@link CamelContext}. |
| 282 | + * |
| 283 | + * @param configurer A configurer callback. |
| 284 | + * @return This instance. |
| 285 | + */ |
275 | 286 | public Camel doWith(final Configurer<CamelContext> configurer) { |
276 | 287 | this.configurer = requireNonNull(configurer, "A configurer is required."); |
277 | 288 | return this; |
@@ -311,8 +322,7 @@ public void configure(final Env env, final Config config, final Binder binder) { |
311 | 322 | .withoutPath("shutdown") |
312 | 323 | .withoutPath("threads") |
313 | 324 | .withoutPath("jmx") |
314 | | - .withoutPath("streamCaching") |
315 | | - ); |
| 325 | + .withoutPath("streamCaching")); |
316 | 326 |
|
317 | 327 | if (!$camel.getBoolean("jmx")) { |
318 | 328 | ctx.disableJMX(); |
@@ -356,7 +366,7 @@ public void configure(final Env env, final Config config, final Binder binder) { |
356 | 366 | try { |
357 | 367 | routes(routes, ctx, config); |
358 | 368 | } catch (Exception ex) { |
359 | | - throw new IllegalStateException("Route builder resulted in error", ex); |
| 369 | + throw new IllegalStateException("context.addRoutes(RouteBuilder) resulted in error", ex); |
360 | 370 | } |
361 | 371 | } |
362 | 372 |
|
@@ -426,29 +436,26 @@ private <T> T configure(final T source, final Config config) { |
426 | 436 | config.entrySet().forEach(o -> { |
427 | 437 | String key = o.getKey(); |
428 | 438 | String setter = "set" + CaseFormat.LOWER_CAMEL.to(CaseFormat.UPPER_CAMEL, key); |
429 | | - Object value = o.getValue().unwrapped(); |
| 439 | + Object raw = o.getValue().unwrapped(); |
430 | 440 | Optional<Method> result = methods.stream() |
431 | 441 | .filter(m -> m.getName().equals(setter)) |
432 | 442 | .findFirst(); |
433 | 443 | if (result.isPresent()) { |
434 | 444 | Method method = result.get(); |
435 | 445 | Class type = method.getParameterTypes()[0]; |
436 | | - if (Enum.class.isAssignableFrom(type)) { |
437 | | - value = Enum.valueOf(type, value.toString()); |
438 | | - } |
439 | | - if (Long.class.isAssignableFrom(type)) { |
440 | | - value = ((Number) value).longValue(); |
441 | | - } |
442 | | - if (File.class.isAssignableFrom(type)) { |
443 | | - value = new File(value.toString()); |
444 | | - } |
445 | | - try { |
446 | | - method.invoke(source, value); |
447 | | - } catch (Exception ex) { |
448 | | - throw new IllegalArgumentException("Bad option: <" + value + "> for: " + method, ex); |
449 | | - } |
| 446 | + Object value = Match(type).of( |
| 447 | + Case(Enum.class::isAssignableFrom, () -> { |
| 448 | + Object e = Enum.valueOf(type, raw.toString()); |
| 449 | + return e; |
| 450 | + }), |
| 451 | + Case(Long.class::isAssignableFrom, () -> ((Number) raw).longValue()), |
| 452 | + Case(File.class::isAssignableFrom, () -> new File(raw.toString())), |
| 453 | + Case($(), raw)); |
| 454 | + Try.of(() -> method.invoke(source, value)).onFailure(ex -> { |
| 455 | + throw new IllegalArgumentException("Bad option: <" + raw + "> for: " + method, ex); |
| 456 | + }); |
450 | 457 | } else { |
451 | | - log.error("Unknown option camel.{} = {}", key, value); |
| 458 | + log.error("Unknown option camel.{} = {}", key, raw); |
452 | 459 | } |
453 | 460 | }); |
454 | 461 | return source; |
|
0 commit comments