Repository: camel Updated Branches: refs/heads/master d032095f7 -> d8a47d4ea
CAMEL-11043: ServiceCall : allow to use placeholders for name, uri, etc Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/d8a47d4e Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/d8a47d4e Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/d8a47d4e Branch: refs/heads/master Commit: d8a47d4eafdd7d404e1aeaa257ed45675d0377ef Parents: d032095 Author: lburgazzoli <lburgazz...@gmail.com> Authored: Thu Mar 23 17:48:37 2017 +0100 Committer: lburgazzoli <lburgazz...@gmail.com> Committed: Thu Mar 23 17:48:37 2017 +0100 ---------------------------------------------------------------------- .../impl/cloud/DefaultServiceCallProcessor.java | 22 ++++++++- .../ServiceCallConfigurationDefinition.java | 2 +- .../camel/model/cloud/ServiceCallConstants.java | 1 + .../model/cloud/ServiceCallDefinition.java | 26 ++++++---- .../org/apache/camel/util/ObjectHelper.java | 33 ++++++++++++- .../cloud/ServiceCallConfigurationTest.java | 50 ++++++++++++++++++++ 6 files changed, 123 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/d8a47d4e/camel-core/src/main/java/org/apache/camel/impl/cloud/DefaultServiceCallProcessor.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/impl/cloud/DefaultServiceCallProcessor.java b/camel-core/src/main/java/org/apache/camel/impl/cloud/DefaultServiceCallProcessor.java index e75c8cb..be97182 100644 --- a/camel-core/src/main/java/org/apache/camel/impl/cloud/DefaultServiceCallProcessor.java +++ b/camel-core/src/main/java/org/apache/camel/impl/cloud/DefaultServiceCallProcessor.java @@ -83,6 +83,27 @@ public class DefaultServiceCallProcessor extends ServiceSupport implements Async // Properties // ************************************* + + public ExchangePattern getExchangePattern() { + return exchangePattern; + } + + public String getName() { + return name; + } + + public String getScheme() { + return scheme; + } + + public String getUri() { + return uri; + } + + public String getContextPath() { + return contextPath; + } + public LoadBalancer getLoadBalancer() { return loadBalancer; } @@ -91,7 +112,6 @@ public class DefaultServiceCallProcessor extends ServiceSupport implements Async return expression; } - // ************************************* // Lifecycle // ************************************* http://git-wip-us.apache.org/repos/asf/camel/blob/d8a47d4e/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallConfigurationDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallConfigurationDefinition.java b/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallConfigurationDefinition.java index e0b3818..6a910b9 100644 --- a/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallConfigurationDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallConfigurationDefinition.java @@ -42,7 +42,7 @@ import org.apache.camel.spi.Metadata; public class ServiceCallConfigurationDefinition extends IdentifiedType { @XmlAttribute private String uri; - @XmlAttribute @Metadata(defaultValue = "http") + @XmlAttribute @Metadata(defaultValue = ServiceCallConstants.DEFAULT_COMPONENT) private String component; @XmlAttribute private ExchangePattern pattern; http://git-wip-us.apache.org/repos/asf/camel/blob/d8a47d4e/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallConstants.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallConstants.java b/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallConstants.java index 951d25a..4cc89f1 100644 --- a/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallConstants.java +++ b/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallConstants.java @@ -17,6 +17,7 @@ package org.apache.camel.model.cloud; public final class ServiceCallConstants { + public static final String DEFAULT_COMPONENT = "http"; public static final String DEFAULT_SERVICE_CALL_CONFIG_ID = "service-call-configuration"; public static final String DEFAULT_SERVICE_CALL_EXPRESSION_ID = "service-call-expression"; public static final String DEFAULT_SERVICE_DISCOVERY_ID = "service-discovery"; http://git-wip-us.apache.org/repos/asf/camel/blob/d8a47d4e/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallDefinition.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallDefinition.java b/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallDefinition.java index fecbdc5..2fd4ed4 100644 --- a/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallDefinition.java +++ b/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallDefinition.java @@ -63,9 +63,9 @@ import static org.apache.camel.util.CamelContextHelper.lookup; public class ServiceCallDefinition extends NoOutputDefinition<ServiceCallDefinition> { @XmlAttribute @Metadata(required = "true") private String name; - @XmlAttribute @Metadata(defaultValue = "http") - private String uri; @XmlAttribute + private String uri; + @XmlAttribute @Metadata(defaultValue = ServiceCallConstants.DEFAULT_COMPONENT) private String component; @XmlAttribute private ExchangePattern pattern; @@ -733,21 +733,31 @@ public class ServiceCallDefinition extends NoOutputDefinition<ServiceCallDefinit // The component is used to configure the default scheme to use (eg camel component name). // The component configured on EIP takes precedence vs configured on configuration. - String component = this.component; - if (component == null) { + String scheme = this.component; + if (scheme == null) { ServiceCallConfigurationDefinition conf = retrieveConfig(camelContext); if (conf != null) { - component = conf.getComponent(); + scheme = conf.getComponent(); } } - if (component == null) { + if (scheme == null) { ServiceCallConfigurationDefinition conf = retrieveDefaultConfig(camelContext); if (conf != null) { - component = conf.getComponent(); + scheme = conf.getComponent(); } } - return new DefaultServiceCallProcessor(camelContext, name, component, uri, pattern, loadBalancer, expression); + // Service name is mandatory + ObjectHelper.notNull(name, "Service name"); + + return new DefaultServiceCallProcessor( + camelContext, + camelContext.resolvePropertyPlaceholders(name), + ObjectHelper.applyIfNotEmpty(scheme, camelContext::resolvePropertyPlaceholders, () -> ServiceCallConstants.DEFAULT_COMPONENT), + ObjectHelper.applyIfNotEmpty(uri, camelContext::resolvePropertyPlaceholders, () -> null), + pattern, + loadBalancer, + expression); } // ***************************** http://git-wip-us.apache.org/repos/asf/camel/blob/d8a47d4e/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java b/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java index 247853f..3ada9d0 100644 --- a/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java +++ b/camel-core/src/main/java/org/apache/camel/util/ObjectHelper.java @@ -48,6 +48,7 @@ import java.util.Scanner; import java.util.concurrent.Callable; import java.util.function.Consumer; import java.util.function.Function; +import java.util.function.Supplier; import org.w3c.dom.Node; import org.w3c.dom.NodeList; @@ -60,6 +61,7 @@ import org.apache.camel.Ordered; import org.apache.camel.RuntimeCamelException; import org.apache.camel.TypeConverter; import org.apache.camel.WrappedFile; +import org.apache.camel.util.function.ThrowingFunction; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -397,7 +399,7 @@ public final class ObjectHelper { } /** - * Tests whether the value is <b>not</b> <tt>null</tt>, an empty string or an empty collection. + * Tests whether the value is <b>not</b> <tt>null</tt>, an empty string, an empty collection or a map * * @param value the value, if its a String it will be tested for text length as well * @param consumer the consumer, the operation to be executed against value if not empty @@ -409,6 +411,35 @@ public final class ObjectHelper { } /** + * Tests whether the value is <b>not</b> <tt>null</tt>, an empty string, an empty collection or a map and transform it using the given function. + * + * @param value the value, if its a String it will be tested for text length as well + * @param function the function to be executed against value if not empty + */ + public static <I, R, T extends Throwable> Optional<R> applyIfNotEmpty(I value, ThrowingFunction<I, R, T> function) throws T { + if (isNotEmpty(value)) { + return Optional.ofNullable(function.apply(value)); + } + + return Optional.empty(); + } + + /** + * Tests whether the value is <b>not</b> <tt>null</tt>, an empty string, an empty collection or a map and transform it using the given function. + * + * @param value the value, if its a String it will be tested for text length as well + * @param consumer the function to be executed against value if not empty + * @param orElse the supplier to use to retrieve a result if the given value is empty + */ + public static <I, R, T extends Throwable> R applyIfNotEmpty(I value, ThrowingFunction<I, R, T> consumer, Supplier<R> orElse) throws T { + if (isNotEmpty(value)) { + return consumer.apply(value); + } + + return orElse.get(); + } + + /** * @deprecated use * {@link StringHelper#splitOnCharacter(String, String, int)} instead */ http://git-wip-us.apache.org/repos/asf/camel/blob/d8a47d4e/camel-core/src/test/java/org/apache/camel/impl/cloud/ServiceCallConfigurationTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/impl/cloud/ServiceCallConfigurationTest.java b/camel-core/src/test/java/org/apache/camel/impl/cloud/ServiceCallConfigurationTest.java index de15763..00dfe35 100644 --- a/camel-core/src/test/java/org/apache/camel/impl/cloud/ServiceCallConfigurationTest.java +++ b/camel-core/src/test/java/org/apache/camel/impl/cloud/ServiceCallConfigurationTest.java @@ -269,6 +269,56 @@ public class ServiceCallConfigurationTest { } // ********************************************** + // test placeholders + // ********************************************** + + @Test + public void testPlaceholders() throws Exception { + CamelContext context = null; + + try { + System.setProperty("scall.name", "service-name"); + System.setProperty("scall.scheme", "file"); + + context = new DefaultCamelContext(); + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .routeId("default") + .serviceCall() + .name("{{scall.name}}") + .component("{{scall.scheme}}") + .uri("direct:{{scall.name}}") + .serviceDiscovery(new StaticServiceDiscovery()) + .end(); + } + }); + + context.start(); + + DefaultServiceCallProcessor proc = findServiceCallProcessor(context.getRoute("default")); + + Assert.assertNotNull(proc); + Assert.assertTrue(proc.getLoadBalancer() instanceof DefaultLoadBalancer); + Assert.assertEquals("service-name", proc.getName()); + Assert.assertEquals("file", proc.getScheme()); + Assert.assertEquals("direct:service-name", proc.getUri()); + + } finally { + if (context != null) { + context.stop(); + } + + // Cleanup system properties + System.clearProperty("scall.name"); + System.clearProperty("scall.component"); + } + + context.stop(); + } + + // ********************************************** // Helper // **********************************************