CAMEL-10991: ServiceCall - Allow to configure global configuration
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/9628b85c Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/9628b85c Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/9628b85c Branch: refs/heads/master Commit: 9628b85c472d64e5856d03282c524426b6a09b1f Parents: 32f39ea Author: lburgazzoli <lburgazz...@gmail.com> Authored: Fri Mar 10 18:41:45 2017 +0100 Committer: lburgazzoli <lburgazz...@gmail.com> Committed: Tue Mar 14 21:18:07 2017 +0100 ---------------------------------------------------------------------- .../impl/cloud/DefaultServiceCallProcessor.java | 14 + .../ServiceCallConfigurationDefinition.java | 12 + .../camel/model/cloud/ServiceCallConstants.java | 27 +- .../model/cloud/ServiceCallDefinition.java | 434 +++++++++++-------- ...erviceCallServiceDiscoveryConfiguration.java | 15 +- .../apache/camel/util/CamelContextHelper.java | 13 + .../apache/camel/util/function/Suppliers.java | 13 + .../camel/impl/cloud/LoadBalancerTest.java | 1 - .../cloud/ServiceCallConfigurationTest.java | 285 ++++++++++++ .../blueprint/CamelContextFactoryBean.java | 11 + .../camel/cdi/xml/CamelContextFactoryBean.java | 12 + .../ConsulDefaultServiceCallRouteTest.java | 3 +- .../cloud/ConsulRibbonServiceCallRouteTest.java | 3 +- .../cloud/SpringConsulServiceCallRouteTest.java | 32 +- .../SpringConsulDefaultServiceCallRouteTest.xml | 81 +++- .../SpringConsulRibbonServiceCallRouteTest.xml | 74 +++- .../xml/AbstractCamelContextFactoryBean.java | 5 + .../SpringEtcdServiceCallDefaultRouteTest.xml | 4 +- .../camel/spring/CamelContextFactoryBean.java | 11 + .../spring/handler/CamelNamespaceHandler.java | 1 + .../cloud/ConsulCloudAutoConfiguration.java | 31 +- .../cloud/DnsCloudAutoConfiguration.java | 31 +- .../cloud/EtcdCloudAutoConfiguration.java | 35 +- .../cloud/KubernetesCloudAutoConfiguration.java | 31 +- .../cloud/RibbonCloudAutoConfiguration.java | 31 +- 25 files changed, 852 insertions(+), 358 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/9628b85c/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 8e36968..e75c8cb 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 @@ -80,6 +80,19 @@ public class DefaultServiceCallProcessor extends ServiceSupport implements Async } // ************************************* + // Properties + // ************************************* + + public LoadBalancer getLoadBalancer() { + return loadBalancer; + } + + public Expression getExpression() { + return expression; + } + + + // ************************************* // Lifecycle // ************************************* @@ -112,6 +125,7 @@ public class DefaultServiceCallProcessor extends ServiceSupport implements Async // Processor // ************************************* + @Override public void process(Exchange exchange) throws Exception { AsyncProcessorHelper.process(this, exchange); http://git-wip-us.apache.org/repos/asf/camel/blob/9628b85c/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 82b2603..e0b3818 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 @@ -530,6 +530,12 @@ public class ServiceCallConfigurationDefinition extends IdentifiedType { return conf; } + public ServiceCallConfigurationDefinition multiServiceDiscovery(ChainedServiceCallServiceDiscoveryConfiguration conf) { + setServiceDiscoveryConfiguration(conf); + + return this; + } + public StaticServiceCallServiceDiscoveryConfiguration staticServiceDiscovery() { StaticServiceCallServiceDiscoveryConfiguration conf = new StaticServiceCallServiceDiscoveryConfiguration(); setServiceDiscoveryConfiguration(conf); @@ -537,6 +543,12 @@ public class ServiceCallConfigurationDefinition extends IdentifiedType { return conf; } + public ServiceCallConfigurationDefinition staticServiceDiscovery(StaticServiceCallServiceDiscoveryConfiguration conf) { + setServiceDiscoveryConfiguration(conf); + + return this; + } + // ***************************** // Shortcuts - ServiceFilter // ***************************** http://git-wip-us.apache.org/repos/asf/camel/blob/9628b85c/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 04ee654..951d25a 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 @@ -16,26 +16,13 @@ */ package org.apache.camel.model.cloud; -import java.util.Arrays; -import java.util.List; - -final class ServiceCallConstants { - public static final List<ServiceCallServiceDiscoveryConfiguration> SERVICE_DISCOVERY_CONFIGURATIONS = Arrays.asList( - new ConsulServiceCallServiceDiscoveryConfiguration(), - new DnsServiceCallServiceDiscoveryConfiguration(), - new EtcdServiceCallServiceDiscoveryConfiguration(), - new KubernetesServiceCallServiceDiscoveryConfiguration() - ); - - public static final List<ServiceCallServiceFilterConfiguration> SERVICE_FILTER_CONFIGURATIONS = Arrays.asList( - new HealthyServiceCallServiceFilterConfiguration(), - new PassThroughServiceCallServiceFilterConfiguration() - ); - - public static final List<ServiceCallLoadBalancerConfiguration> LOAD_BALANCER_CONFIGURATIONS = Arrays.asList( - new RibbonServiceCallLoadBalancerConfiguration(), - new DefaultServiceCallLoadBalancerConfiguration() - ); +public final class ServiceCallConstants { + 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"; + public static final String DEFAULT_SERVICE_FILTER_ID = "service-filter"; + public static final String DEFAULT_SERVICE_CHOOSER_ID = "service-chooser"; + public static final String DEFAULT_LOAD_BALANCER_ID = "load-balancer"; private ServiceCallConstants() { } http://git-wip-us.apache.org/repos/asf/camel/blob/9628b85c/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 b6f7ab0..72a7701 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 @@ -16,7 +16,7 @@ */ package org.apache.camel.model.cloud; -import java.util.Set; +import java.util.function.Function; import java.util.function.Supplier; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; @@ -30,7 +30,6 @@ import org.apache.camel.CamelContext; import org.apache.camel.CamelContextAware; import org.apache.camel.ExchangePattern; import org.apache.camel.Expression; -import org.apache.camel.NoFactoryAvailableException; import org.apache.camel.Processor; import org.apache.camel.cloud.LoadBalancer; import org.apache.camel.cloud.ServiceChooser; @@ -49,8 +48,11 @@ import org.apache.camel.impl.cloud.RoundRobinServiceChooser; import org.apache.camel.model.NoOutputDefinition; import org.apache.camel.spi.Metadata; import org.apache.camel.spi.RouteContext; -import org.apache.camel.util.CamelContextHelper; import org.apache.camel.util.ObjectHelper; +import org.apache.camel.util.function.Suppliers; + +import static org.apache.camel.util.CamelContextHelper.findByType; +import static org.apache.camel.util.CamelContextHelper.lookup; /** * Remote service call definition @@ -522,6 +524,15 @@ public class ServiceCallDefinition extends NoOutputDefinition<ServiceCallDefinit return conf; } + public ServiceCallDefinition consulServiceDiscovery(String url) { + ConsulServiceCallServiceDiscoveryConfiguration conf = new ConsulServiceCallServiceDiscoveryConfiguration(this); + conf.setUrl(url); + + setServiceDiscoveryConfiguration(conf); + + return this; + } + public DnsServiceCallServiceDiscoveryConfiguration dnsServiceDiscovery() { DnsServiceCallServiceDiscoveryConfiguration conf = new DnsServiceCallServiceDiscoveryConfiguration(this); setServiceDiscoveryConfiguration(conf); @@ -555,6 +566,25 @@ public class ServiceCallDefinition extends NoOutputDefinition<ServiceCallDefinit return conf; } + public ServiceCallDefinition etcdServiceDiscovery(String uris) { + EtcdServiceCallServiceDiscoveryConfiguration conf = new EtcdServiceCallServiceDiscoveryConfiguration(this); + conf.setUris(uris); + + setServiceDiscoveryConfiguration(conf); + + return this; + } + + public ServiceCallDefinition etcdServiceDiscovery(String uris, String servicePath) { + EtcdServiceCallServiceDiscoveryConfiguration conf = new EtcdServiceCallServiceDiscoveryConfiguration(this); + conf.setUris(uris); + conf.setServicePath(servicePath); + + setServiceDiscoveryConfiguration(conf); + + return this; + } + public KubernetesServiceCallServiceDiscoveryConfiguration kubernetesServiceDiscovery() { KubernetesServiceCallServiceDiscoveryConfiguration conf = new KubernetesServiceCallServiceDiscoveryConfiguration(this); setServiceDiscoveryConfiguration(conf); @@ -682,13 +712,11 @@ public class ServiceCallDefinition extends NoOutputDefinition<ServiceCallDefinit @Override public Processor createProcessor(RouteContext routeContext) throws Exception { final CamelContext camelContext = routeContext.getCamelContext(); - final ServiceCallConfigurationDefinition config = retrieveConfig(camelContext); - - ServiceDiscovery serviceDiscovery = retrieveServiceDiscovery(camelContext, config); - ServiceFilter serviceFilter = retrieveServiceFilter(camelContext, config); - ServiceChooser serviceChooser = retrieveServiceChooser(camelContext, config); - LoadBalancer loadBalancer = retrieveLoadBalancer(camelContext, config); - Expression expression = retrieveExpression(camelContext, config); + final ServiceDiscovery serviceDiscovery = retrieveServiceDiscovery(camelContext); + final ServiceFilter serviceFilter = retrieveServiceFilter(camelContext); + final ServiceChooser serviceChooser = retrieveServiceChooser(camelContext); + final LoadBalancer loadBalancer = retrieveLoadBalancer(camelContext); + final Expression expression = retrieveExpression(camelContext); if (loadBalancer instanceof CamelContextAware) { ((CamelContextAware) loadBalancer).setCamelContext(camelContext); @@ -703,11 +731,20 @@ public class ServiceCallDefinition extends NoOutputDefinition<ServiceCallDefinit ((ServiceChooserAware) loadBalancer).setServiceChooser(serviceChooser); } - // The component is used to configure what the default scheme to use (eg camel component name). + // 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) { - component = config != null ? config.getComponent() : null; + ServiceCallConfigurationDefinition conf = retrieveConfig(camelContext); + if (conf != null) { + component = conf.getComponent(); + } + } + if (component == null) { + ServiceCallConfigurationDefinition conf = retrieveDefaultConfig(camelContext); + if (conf != null) { + component = conf.getComponent(); + } } return new DefaultServiceCallProcessor(camelContext, name, component, uri, pattern, loadBalancer, expression); @@ -717,118 +754,151 @@ public class ServiceCallDefinition extends NoOutputDefinition<ServiceCallDefinit // Helpers // ***************************** - private ServiceCallConfigurationDefinition retrieveConfig(CamelContext camelContext) { - ServiceCallConfigurationDefinition config = null; - if (configurationRef != null) { - // lookup in registry first - config = CamelContextHelper.lookup(camelContext, configurationRef, ServiceCallConfigurationDefinition.class); - if (config == null) { - // and fallback as service configuration - config = camelContext.getServiceCallConfiguration(configurationRef); - } - } + private ServiceCallConfigurationDefinition retrieveDefaultConfig(CamelContext camelContext) { + // check if a default configuration is bound to the registry + ServiceCallConfigurationDefinition config = camelContext.getServiceCallConfiguration(null); if (config == null) { - config = camelContext.getServiceCallConfiguration(null); + // Or if it is in the registry + config = lookup( + camelContext, + ServiceCallConstants.DEFAULT_SERVICE_CALL_CONFIG_ID, + ServiceCallConfigurationDefinition.class); } + if (config == null) { - // if no default then try to find if there configuration in the registry of the given type - Set<ServiceCallConfigurationDefinition> set = camelContext.getRegistry().findByType(ServiceCallConfigurationDefinition.class); - if (set.size() == 1) { - config = set.iterator().next(); - } + // If no default is set either by searching by name or bound to the + // camel context, assume that if there is a single instance in the + // registry, that is the default one + config = findByType(camelContext, ServiceCallConfigurationDefinition.class); } return config; } - private ServiceDiscovery retrieveServiceDiscovery(CamelContext camelContext, ServiceCallConfigurationDefinition config) throws Exception { - ServiceDiscovery answer; - if (serviceDiscoveryConfiguration != null) { - answer = serviceDiscoveryConfiguration.newInstance(camelContext); - } else if (config != null && config.getServiceDiscoveryConfiguration() != null) { - answer = config.getServiceDiscoveryConfiguration().newInstance(camelContext); - } else { - answer = retrieve(ServiceDiscovery.class, camelContext, this::getServiceDiscovery, this::getServiceDiscoveryRef); - if (answer == null && config != null) { - answer = retrieve(ServiceDiscovery.class, camelContext, config::getServiceDiscovery, config::getServiceDiscoveryRef); - } - - if (answer == null) { - answer = camelContext.getRegistry().lookupByNameAndType("service-discovery", ServiceDiscovery.class); - } - if (answer == null) { - answer = findByType(camelContext, ServiceDiscovery.class); + private ServiceCallConfigurationDefinition retrieveConfig(CamelContext camelContext) { + ServiceCallConfigurationDefinition config = null; + if (configurationRef != null) { + // lookup in registry firstNotNull + config = lookup(camelContext, configurationRef, ServiceCallConfigurationDefinition.class); + if (config == null) { + // and fallback as service configuration + config = camelContext.getServiceCallConfiguration(configurationRef); } } - // If there's no configuration, let's try to find a suitable implementation - if (answer == null) { - for (ServiceCallServiceDiscoveryConfiguration configuration : ServiceCallConstants.SERVICE_DISCOVERY_CONFIGURATIONS) { - try { - answer = configuration.newInstance(camelContext); - - if (answer != null) { - break; - } - } catch (NoFactoryAvailableException e) { - // skip - } + return config; + } + + // ****************************************** + // ServiceDiscovery + // ****************************************** + + private ServiceDiscovery retrieveServiceDiscovery(CamelContext camelContext, Function<CamelContext, ServiceCallConfigurationDefinition> function) throws Exception { + ServiceDiscovery answer = null; + + ServiceCallConfigurationDefinition config = function.apply(camelContext); + if (config != null) { + if (config.getServiceDiscoveryConfiguration() != null) { + answer = config.getServiceDiscoveryConfiguration().newInstance(camelContext); + } else { + answer = retrieve( + ServiceDiscovery.class, + camelContext, + config::getServiceDiscovery, + config::getServiceDiscoveryRef + ); } } return answer; } - private ServiceFilter retrieveServiceFilter(CamelContext camelContext, ServiceCallConfigurationDefinition config) throws Exception { - ServiceFilter answer; - - if (serviceFilterConfiguration != null) { - answer = serviceFilterConfiguration.newInstance(camelContext); - } else if (config != null && config.getServiceFilterConfiguration() != null) { - answer = config.getServiceFilterConfiguration().newInstance(camelContext); - } else { - answer = retrieve(ServiceFilter.class, camelContext, this::getServiceFilter, this::getServiceFilterRef); - if (answer == null && config != null) { - answer = retrieve(ServiceFilter.class, camelContext, config::getServiceFilter, config::getServiceFilterRef); - - // If the ServiceFilter is not found but a ref is set, try to determine - // the implementation according to the ref name. - if (answer == null) { - String ref = config.getServiceFilterRef(); - if (ObjectHelper.equal("healthy", ref, true)) { - answer = new HealthyServiceFilter(); - } else if (ObjectHelper.equal("pass-through", ref, true)) { - answer = new PassThroughServiceFilter(); - } else if (ObjectHelper.equal("passthrough", ref, true)) { - answer = new PassThroughServiceFilter(); - } - } + private ServiceDiscovery retrieveServiceDiscovery(CamelContext camelContext) throws Exception { + return Suppliers.firstNotNull( + () -> (serviceDiscoveryConfiguration != null) ? serviceDiscoveryConfiguration.newInstance(camelContext) : null, + // Local configuration + () -> retrieve(ServiceDiscovery.class, camelContext, this::getServiceDiscovery, this::getServiceDiscoveryRef), + // Linked configuration + () -> retrieveServiceDiscovery(camelContext, this::retrieveConfig), + // Default configuration + () -> retrieveServiceDiscovery(camelContext, this::retrieveDefaultConfig), + // Check if there is a single instance in the registry + () -> findByType(camelContext, ServiceDiscovery.class), + // From registry + () -> lookup(camelContext, ServiceCallConstants.DEFAULT_SERVICE_DISCOVERY_ID, ServiceDiscovery.class) + ); + } + + // ****************************************** + // ServiceFilter + // ****************************************** + + private ServiceFilter retrieveServiceFilter(CamelContext camelContext, Function<CamelContext, ServiceCallConfigurationDefinition> function) throws Exception { + ServiceFilter answer = null; + + ServiceCallConfigurationDefinition config = function.apply(camelContext); + if (config != null) { + if (config.getServiceFilterConfiguration() != null) { + answer = config.getServiceFilterConfiguration().newInstance(camelContext); + } else { + answer = retrieve( + ServiceFilter.class, + camelContext, + config::getServiceFilter, + config::getServiceFilterRef + ); } - } - - if (answer == null) { - answer = camelContext.getRegistry().lookupByNameAndType("service-filter", ServiceFilter.class); - } - if (answer == null) { - answer = findByType(camelContext, ServiceFilter.class); - } - // If there's no configuration, let's use the healthy strategy - if (answer == null) { - answer = new HealthyServiceFilter(); + if (answer == null) { + String ref = config.getServiceFilterRef(); + if (ObjectHelper.equal("healthy", ref, true)) { + answer = new HealthyServiceFilter(); + } else if (ObjectHelper.equal("pass-through", ref, true)) { + answer = new PassThroughServiceFilter(); + } else if (ObjectHelper.equal("passthrough", ref, true)) { + answer = new PassThroughServiceFilter(); + } + } } return answer; } - private ServiceChooser retrieveServiceChooser(CamelContext camelContext, ServiceCallConfigurationDefinition config) { - ServiceChooser answer = retrieve(ServiceChooser.class, camelContext, this::getServiceChooser, this::getServiceChooserRef); - if (answer == null && config != null) { - answer = retrieve(ServiceChooser.class, camelContext, config::getServiceChooser, config::getServiceChooserRef); + private ServiceFilter retrieveServiceFilter(CamelContext camelContext) throws Exception { + return Suppliers.firstNotNull( + () -> (serviceFilterConfiguration != null) ? serviceFilterConfiguration.newInstance(camelContext) : null, + // Local configuration + () -> retrieve(ServiceFilter.class, camelContext, this::getServiceFilter, this::getServiceFilterRef), + // Linked configuration + () -> retrieveServiceFilter(camelContext, this::retrieveConfig), + // Default configuration + () -> retrieveServiceFilter(camelContext, this::retrieveDefaultConfig), + // Check if there is a single instance in the registry + () -> findByType(camelContext, ServiceFilter.class), + // From registry + () -> lookup(camelContext, ServiceCallConstants.DEFAULT_SERVICE_FILTER_ID, ServiceFilter.class), + // Default + () -> new HealthyServiceFilter() + ); + } + + // ****************************************** + // ServiceChooser + // ****************************************** + + private ServiceChooser retrieveServiceChooser(CamelContext camelContext, Function<CamelContext, ServiceCallConfigurationDefinition> function) throws Exception { + ServiceChooser answer = null; + + ServiceCallConfigurationDefinition config = function.apply(camelContext); + if (config != null) { + answer = retrieve( + ServiceChooser.class, + camelContext, + config::getServiceChooser, + config::getServiceChooserRef + ); - // If the ServiceChooser is not found but a ref is set, try to determine - // the implementation according to the ref name. if (answer == null) { String ref = config.getServiceChooserRef(); if (ObjectHelper.equal("roundrobin", ref, true)) { @@ -841,87 +911,112 @@ public class ServiceCallDefinition extends NoOutputDefinition<ServiceCallDefinit } } - if (answer == null) { - answer = camelContext.getRegistry().lookupByNameAndType("service-chooser", ServiceChooser.class); - } - if (answer == null) { - answer = findByType(camelContext, ServiceChooser.class); - } - - // If there's no configuration, let's use the round-robin strategy - if (answer == null) { - answer = new RoundRobinServiceChooser(); - } - return answer; } - private LoadBalancer retrieveLoadBalancer(CamelContext camelContext, ServiceCallConfigurationDefinition config) throws Exception { - LoadBalancer answer; - if (loadBalancerConfiguration != null) { - answer = loadBalancerConfiguration.newInstance(camelContext); - } else if (config != null && config.getLoadBalancerConfiguration() != null) { - answer = config.getLoadBalancerConfiguration().newInstance(camelContext); - } else { - answer = retrieve(LoadBalancer.class, camelContext, this::getLoadBalancer, this::getLoadBalancerRef); - if (answer == null && config != null) { - answer = retrieve(LoadBalancer.class, camelContext, config::getLoadBalancer, config::getLoadBalancerRef); - } - - if (answer == null) { - answer = camelContext.getRegistry().lookupByNameAndType("load-balancer", LoadBalancer.class); - } - if (answer == null) { - answer = findByType(camelContext, LoadBalancer.class); + private ServiceChooser retrieveServiceChooser(CamelContext camelContext) throws Exception { + return Suppliers.firstNotNull( + // Local configuration + () -> retrieve(ServiceChooser.class, camelContext, this::getServiceChooser, this::getServiceChooserRef), + // Linked configuration + () -> retrieveServiceChooser(camelContext, this::retrieveConfig), + // Default configuration + () -> retrieveServiceChooser(camelContext, this::retrieveDefaultConfig), + // Check if there is a single instance in the registry + () -> findByType(camelContext, ServiceChooser.class), + // From registry + () -> lookup(camelContext, ServiceCallConstants.DEFAULT_SERVICE_CHOOSER_ID, ServiceChooser.class), + // Default + () -> new RoundRobinServiceChooser() + ); + } + + // ****************************************** + // LoadBalancer + // ****************************************** + + private LoadBalancer retrieveLoadBalancer(CamelContext camelContext, Function<CamelContext, ServiceCallConfigurationDefinition> function) throws Exception { + LoadBalancer answer = null; + + ServiceCallConfigurationDefinition config = function.apply(camelContext); + if (config != null) { + if (config.getLoadBalancerConfiguration() != null) { + answer = config.getLoadBalancerConfiguration().newInstance(camelContext); + } else { + answer = retrieve( + LoadBalancer.class, + camelContext, + config::getLoadBalancer, + config::getLoadBalancerRef + ); } } - // If there's no configuration, let's try to find a suitable implementation - if (answer == null) { - for (ServiceCallLoadBalancerConfiguration configuration : ServiceCallConstants.LOAD_BALANCER_CONFIGURATIONS) { - try { - answer = configuration.newInstance(camelContext); - - if (answer != null) { - break; - } - } catch (NoFactoryAvailableException e) { - // skip - } - } - } - - if (answer == null) { - answer = new DefaultLoadBalancer(); - } - return answer; } - private Expression retrieveExpression(CamelContext camelContext, ServiceCallConfigurationDefinition config) throws Exception { - Expression answer; - - if (expressionConfiguration != null) { - answer = expressionConfiguration.newInstance(camelContext); - } else if (config != null && config.getExpressionConfiguration() != null) { - answer = config.getExpressionConfiguration().newInstance(camelContext); - } else { - answer = retrieve(Expression.class, camelContext, this::getExpression, this::getExpressionRef); - if (answer == null && config != null) { - answer = retrieve(Expression.class, camelContext, config::getExpression, config::getExpressionRef); - } - if (answer == null) { - answer = findByType(camelContext, Expression.class); + private LoadBalancer retrieveLoadBalancer(CamelContext camelContext) throws Exception { + return Suppliers.firstNotNull( + () -> (loadBalancerConfiguration != null) ? loadBalancerConfiguration.newInstance(camelContext) : null, + // Local configuration + () -> retrieve(LoadBalancer.class, camelContext, this::getLoadBalancer, this::getLoadBalancerRef), + // Linked configuration + () -> retrieveLoadBalancer(camelContext, this::retrieveConfig), + // Default configuration + () -> retrieveLoadBalancer(camelContext, this::retrieveDefaultConfig), + // Check if there is a single instance in the registry + () -> findByType(camelContext, LoadBalancer.class), + // From registry + () -> lookup(camelContext, ServiceCallConstants.DEFAULT_LOAD_BALANCER_ID, LoadBalancer.class), + // Default + () -> new DefaultLoadBalancer() + ); + } + + // ****************************************** + // Expression + // ****************************************** + + private Expression retrieveExpression(CamelContext camelContext, Function<CamelContext, ServiceCallConfigurationDefinition> function) throws Exception { + Expression answer = null; + + ServiceCallConfigurationDefinition config = function.apply(camelContext); + if (config != null) { + if (config.getExpressionConfiguration() != null) { + answer = config.getExpressionConfiguration().newInstance(camelContext); + } else { + answer = retrieve( + Expression.class, + camelContext, + config::getExpression, + config::getExpressionRef + ); } } - if (answer == null) { - answer = new DefaultServiceCallExpression(); - } - return answer; } + private Expression retrieveExpression(CamelContext camelContext) throws Exception { + return Suppliers.firstNotNull( + () -> (expressionConfiguration != null) ? expressionConfiguration.newInstance(camelContext) : null, + // Local configuration + () -> retrieve(Expression.class, camelContext, this::getExpression, this::getExpressionRef), + // Linked configuration + () -> retrieveExpression(camelContext, this::retrieveConfig), + // Default configuration + () -> retrieveExpression(camelContext, this::retrieveDefaultConfig), + // From registry + () -> lookup(camelContext, ServiceCallConstants.DEFAULT_SERVICE_CALL_EXPRESSION_ID, Expression.class), + // Default + () -> new DefaultServiceCallExpression() + ); + } + + // ************************************ + // Helpers + // ************************************ + private <T> T retrieve(Class<T> type, CamelContext camelContext, Supplier<T> instanceSupplier, Supplier<String> refSupplier) { T answer = null; if (instanceSupplier != null) { @@ -931,19 +1026,10 @@ public class ServiceCallDefinition extends NoOutputDefinition<ServiceCallDefinit if (answer == null && refSupplier != null) { String ref = refSupplier.get(); if (ref != null) { - answer = CamelContextHelper.lookup(camelContext, ref, type); + answer = lookup(camelContext, ref, type); } } return answer; } - - private <T> T findByType(CamelContext camelContext, Class<T> type) { - Set<T> set = camelContext.getRegistry().findByType(type); - if (set.size() == 1) { - return set.iterator().next(); - } - - return null; - } } http://git-wip-us.apache.org/repos/asf/camel/blob/9628b85c/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallServiceDiscoveryConfiguration.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallServiceDiscoveryConfiguration.java b/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallServiceDiscoveryConfiguration.java index fed3134..b6e59df 100644 --- a/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallServiceDiscoveryConfiguration.java +++ b/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallServiceDiscoveryConfiguration.java @@ -22,6 +22,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; @@ -47,7 +48,7 @@ public class ServiceCallServiceDiscoveryConfiguration extends IdentifiedType imp private static final String RESOURCE_PATH = "META-INF/services/org/apache/camel/cloud/"; @XmlTransient - private final ServiceCallDefinition parent; + private final Optional<ServiceCallDefinition> parent; @XmlTransient private final String factoryKey; @XmlElement(name = "properties") @Metadata(label = "advanced") @@ -58,16 +59,22 @@ public class ServiceCallServiceDiscoveryConfiguration extends IdentifiedType imp } public ServiceCallServiceDiscoveryConfiguration(ServiceCallDefinition parent, String factoryKey) { - this.parent = parent; + this.parent = Optional.ofNullable(parent); this.factoryKey = factoryKey; } public ServiceCallDefinition end() { - return this.parent; + return this.parent.orElseThrow( + () -> new IllegalStateException("Parent definition is not set") + ); } public ProcessorDefinition<?> endParent() { - return this.parent.end(); + return this.parent.map( + ServiceCallDefinition::end + ).orElseThrow( + () -> new IllegalStateException("Parent definition is not set") + ); } // ************************************************************************* http://git-wip-us.apache.org/repos/asf/camel/blob/9628b85c/camel-core/src/main/java/org/apache/camel/util/CamelContextHelper.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/util/CamelContextHelper.java b/camel-core/src/main/java/org/apache/camel/util/CamelContextHelper.java index 3e7a970..d126005 100644 --- a/camel-core/src/main/java/org/apache/camel/util/CamelContextHelper.java +++ b/camel-core/src/main/java/org/apache/camel/util/CamelContextHelper.java @@ -139,6 +139,19 @@ public final class CamelContextHelper { } /** + * Look up a bean of the give type in the {@link org.apache.camel.spi.Registry} on the + * {@link CamelContext} returning an instance if only one bean is present, + */ + public static <T> T findByType(CamelContext camelContext, Class<T> type) { + Set<T> set = camelContext.getRegistry().findByType(type); + if (set.size() == 1) { + return set.iterator().next(); + } + + return null; + } + + /** * Look up the given named bean in the {@link org.apache.camel.spi.Registry} on the * {@link CamelContext} or throws {@link NoSuchBeanException} if not found. */ http://git-wip-us.apache.org/repos/asf/camel/blob/9628b85c/camel-core/src/main/java/org/apache/camel/util/function/Suppliers.java ---------------------------------------------------------------------- diff --git a/camel-core/src/main/java/org/apache/camel/util/function/Suppliers.java b/camel-core/src/main/java/org/apache/camel/util/function/Suppliers.java index 4f8f845..888de13 100644 --- a/camel-core/src/main/java/org/apache/camel/util/function/Suppliers.java +++ b/camel-core/src/main/java/org/apache/camel/util/function/Suppliers.java @@ -40,4 +40,17 @@ public final class Suppliers { return supplied; }; } + + public static <T> T firstNotNull(ThrowingSupplier<T, Exception>... suppliers) throws Exception { + T answer = null; + + for (int i = 0; i < suppliers.length; i++) { + answer = suppliers[i].get(); + if (answer != null) { + break; + } + } + + return answer; + } } http://git-wip-us.apache.org/repos/asf/camel/blob/9628b85c/camel-core/src/test/java/org/apache/camel/impl/cloud/LoadBalancerTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/impl/cloud/LoadBalancerTest.java b/camel-core/src/test/java/org/apache/camel/impl/cloud/LoadBalancerTest.java index 280d8af..5e17bfb 100644 --- a/camel-core/src/test/java/org/apache/camel/impl/cloud/LoadBalancerTest.java +++ b/camel-core/src/test/java/org/apache/camel/impl/cloud/LoadBalancerTest.java @@ -14,7 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package org.apache.camel.impl.cloud; import java.util.stream.Collectors; http://git-wip-us.apache.org/repos/asf/camel/blob/9628b85c/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 new file mode 100644 index 0000000..de15763 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/impl/cloud/ServiceCallConfigurationTest.java @@ -0,0 +1,285 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.impl.cloud; + +import java.util.UUID; + +import org.apache.camel.CamelContext; +import org.apache.camel.Processor; +import org.apache.camel.Route; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.impl.SimpleRegistry; +import org.apache.camel.model.cloud.ServiceCallConfigurationDefinition; +import org.junit.Assert; +import org.junit.Test; + +public class ServiceCallConfigurationTest { + + // **************************************** + // test default resolution + // **************************************** + + @Test + public void testDefaultConfigurationFromCamelContext() throws Exception { + StaticServiceDiscovery sd = new StaticServiceDiscovery(); + sd.addServer("127.0.0.1:8080"); + sd.addServer("127.0.0.1:8081"); + + BlacklistServiceFilter sf = new BlacklistServiceFilter(); + sf.addServer("127.0.0.1:8080"); + + ServiceCallConfigurationDefinition conf = new ServiceCallConfigurationDefinition(); + conf.setServiceDiscovery(sd); + conf.setServiceFilter(sf); + + CamelContext context = new DefaultCamelContext(); + context.setServiceCallConfiguration(conf); + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .routeId("default") + .serviceCall() + .name("scall") + .component("file") + .end(); + } + }); + + context.start(); + + DefaultServiceCallProcessor proc = findServiceCallProcessor(context.getRoute("default")); + + Assert.assertNotNull(proc); + Assert.assertTrue(proc.getLoadBalancer() instanceof DefaultLoadBalancer); + + DefaultLoadBalancer loadBalancer = (DefaultLoadBalancer)proc.getLoadBalancer(); + Assert.assertEquals(sd, loadBalancer.getServiceDiscovery()); + Assert.assertEquals(sf, loadBalancer.getServiceFilter()); + + context.stop(); + } + + @Test + public void testDefaultConfigurationFromRegistryWithDefaultName() throws Exception { + StaticServiceDiscovery sd = new StaticServiceDiscovery(); + sd.addServer("127.0.0.1:8080"); + sd.addServer("127.0.0.1:8081"); + + BlacklistServiceFilter sf = new BlacklistServiceFilter(); + sf.addServer("127.0.0.1:8080"); + + ServiceCallConfigurationDefinition conf = new ServiceCallConfigurationDefinition(); + conf.setServiceDiscovery(sd); + conf.serviceFilter(sf); + + SimpleRegistry reg = new SimpleRegistry(); + reg.put(org.apache.camel.model.cloud.ServiceCallConstants.DEFAULT_SERVICE_CALL_CONFIG_ID, conf); + + CamelContext context = new DefaultCamelContext(reg); + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .routeId("default") + .serviceCall() + .name("scall") + .component("file") + .end(); + } + }); + + context.start(); + + DefaultServiceCallProcessor proc = findServiceCallProcessor(context.getRoute("default")); + + Assert.assertNotNull(proc); + Assert.assertTrue(proc.getLoadBalancer() instanceof DefaultLoadBalancer); + + DefaultLoadBalancer loadBalancer = (DefaultLoadBalancer)proc.getLoadBalancer(); + Assert.assertEquals(sd, loadBalancer.getServiceDiscovery()); + Assert.assertEquals(sf, loadBalancer.getServiceFilter()); + + context.stop(); + } + + + @Test + public void testDefaultConfigurationFromRegistryWithNonDefaultName() throws Exception { + StaticServiceDiscovery sd = new StaticServiceDiscovery(); + sd.addServer("127.0.0.1:8080"); + sd.addServer("127.0.0.1:8081"); + + BlacklistServiceFilter sf = new BlacklistServiceFilter(); + sf.addServer("127.0.0.1:8080"); + + ServiceCallConfigurationDefinition conf = new ServiceCallConfigurationDefinition(); + conf.setServiceDiscovery(sd); + conf.serviceFilter(sf); + + SimpleRegistry reg = new SimpleRegistry(); + reg.put(UUID.randomUUID().toString(), conf); + + CamelContext context = new DefaultCamelContext(reg); + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start") + .routeId("default") + .serviceCall() + .name("scall") + .component("file") + .end(); + } + }); + + context.start(); + + DefaultServiceCallProcessor proc = findServiceCallProcessor(context.getRoute("default")); + + Assert.assertNotNull(proc); + Assert.assertTrue(proc.getLoadBalancer() instanceof DefaultLoadBalancer); + + DefaultLoadBalancer loadBalancer = (DefaultLoadBalancer)proc.getLoadBalancer(); + Assert.assertEquals(sd, loadBalancer.getServiceDiscovery()); + Assert.assertEquals(sf, loadBalancer.getServiceFilter()); + + context.stop(); + } + + // **************************************** + // test mixed resolution + // **************************************** + + @Test + public void testMixedConfiguration() throws Exception { + // Default + StaticServiceDiscovery defaultServiceDiscovery = new StaticServiceDiscovery(); + defaultServiceDiscovery.addServer("127.0.0.1:8080"); + defaultServiceDiscovery.addServer("127.0.0.1:8081"); + defaultServiceDiscovery.addServer("127.0.0.1:8082"); + + BlacklistServiceFilter defaultServiceFilter = new BlacklistServiceFilter(); + defaultServiceFilter.addServer("127.0.0.1:8080"); + + ServiceCallConfigurationDefinition defaultConfiguration = new ServiceCallConfigurationDefinition(); + defaultConfiguration.setServiceDiscovery(defaultServiceDiscovery); + defaultConfiguration.serviceFilter(defaultServiceFilter); + + // Named + BlacklistServiceFilter namedServiceFilter = new BlacklistServiceFilter(); + namedServiceFilter.addServer("127.0.0.1:8081"); + + ServiceCallConfigurationDefinition namedConfiguration = new ServiceCallConfigurationDefinition(); + namedConfiguration.serviceFilter(namedServiceFilter); + + // Local + StaticServiceDiscovery localServiceDiscovery = new StaticServiceDiscovery(); + localServiceDiscovery.addServer("127.0.0.1:8080"); + localServiceDiscovery.addServer("127.0.0.1:8081"); + localServiceDiscovery.addServer("127.0.0.1:8082"); + localServiceDiscovery.addServer("127.0.0.1:8084"); + + + // Camel context + CamelContext context = new DefaultCamelContext(); + context.setServiceCallConfiguration(defaultConfiguration); + context.addServiceCallConfiguration("named", namedConfiguration); + context.addRoutes(new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:default") + .id("default") + .serviceCall() + .name("default-scall") + .component("file") + .end(); + from("direct:named") + .id("named") + .serviceCall() + .serviceCallConfiguration("named") + .name("named-scall") + .component("file") + .end(); + from("direct:local") + .id("local") + .serviceCall() + .serviceCallConfiguration("named") + .name("local-scall") + .component("file") + .serviceDiscovery(localServiceDiscovery) + .end(); + } + }); + + context.start(); + + { + // Default + DefaultServiceCallProcessor proc = findServiceCallProcessor(context.getRoute("default")); + + Assert.assertNotNull(proc); + Assert.assertTrue(proc.getLoadBalancer() instanceof DefaultLoadBalancer); + + DefaultLoadBalancer loadBalancer = (DefaultLoadBalancer) proc.getLoadBalancer(); + Assert.assertEquals(defaultServiceDiscovery, loadBalancer.getServiceDiscovery()); + Assert.assertEquals(defaultServiceFilter, loadBalancer.getServiceFilter()); + } + + { + // Named + DefaultServiceCallProcessor proc = findServiceCallProcessor(context.getRoute("named")); + + Assert.assertNotNull(proc); + Assert.assertTrue(proc.getLoadBalancer() instanceof DefaultLoadBalancer); + + DefaultLoadBalancer loadBalancer = (DefaultLoadBalancer) proc.getLoadBalancer(); + Assert.assertEquals(defaultServiceDiscovery, loadBalancer.getServiceDiscovery()); + Assert.assertEquals(namedServiceFilter, loadBalancer.getServiceFilter()); + } + + { + // Local + DefaultServiceCallProcessor proc = findServiceCallProcessor(context.getRoute("local")); + + Assert.assertNotNull(proc); + Assert.assertTrue(proc.getLoadBalancer() instanceof DefaultLoadBalancer); + + DefaultLoadBalancer loadBalancer = (DefaultLoadBalancer) proc.getLoadBalancer(); + Assert.assertEquals(localServiceDiscovery, loadBalancer.getServiceDiscovery()); + Assert.assertEquals(namedServiceFilter, loadBalancer.getServiceFilter()); + } + + context.stop(); + } + + // ********************************************** + // Helper + // ********************************************** + + private DefaultServiceCallProcessor findServiceCallProcessor(Route route) { + for (Processor processor : route.navigate().next()) { + if (processor instanceof DefaultServiceCallProcessor) { + return (DefaultServiceCallProcessor)processor; + } + } + + throw new IllegalStateException("Unable to find a ServiceCallProcessor"); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/9628b85c/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java ---------------------------------------------------------------------- diff --git a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java index 31b54de..e14132b 100644 --- a/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java +++ b/components/camel-blueprint/src/main/java/org/apache/camel/blueprint/CamelContextFactoryBean.java @@ -158,6 +158,8 @@ public class CamelContextFactoryBean extends AbstractCamelContextFactoryBean<Blu @XmlElements({ @XmlElement(name = "export", type = CamelServiceExporterDefinition.class) }) private List<?> beans; + @XmlElement(name = "defaultServiceCallConfiguration") + private ServiceCallConfigurationDefinition defaultServiceCallConfiguration; @XmlElement(name = "serviceCallConfiguration", type = ServiceCallConfigurationDefinition.class) private List<ServiceCallConfigurationDefinition> serviceCallConfigurations; @XmlElement(name = "hystrixConfiguration", type = HystrixConfigurationDefinition.class) @@ -650,6 +652,15 @@ public class CamelContextFactoryBean extends AbstractCamelContextFactoryBean<Blu } @Override + public ServiceCallConfigurationDefinition getDefaultServiceCallConfiguration() { + return defaultServiceCallConfiguration; + } + + public void setDefaultServiceCallConfiguration(ServiceCallConfigurationDefinition defaultServiceCallConfiguration) { + this.defaultServiceCallConfiguration = defaultServiceCallConfiguration; + } + + @Override public List<ServiceCallConfigurationDefinition> getServiceCallConfigurations() { return serviceCallConfigurations; } http://git-wip-us.apache.org/repos/asf/camel/blob/9628b85c/components/camel-cdi/src/main/java/org/apache/camel/cdi/xml/CamelContextFactoryBean.java ---------------------------------------------------------------------- diff --git a/components/camel-cdi/src/main/java/org/apache/camel/cdi/xml/CamelContextFactoryBean.java b/components/camel-cdi/src/main/java/org/apache/camel/cdi/xml/CamelContextFactoryBean.java index d91605b..5dbe230 100644 --- a/components/camel-cdi/src/main/java/org/apache/camel/cdi/xml/CamelContextFactoryBean.java +++ b/components/camel-cdi/src/main/java/org/apache/camel/cdi/xml/CamelContextFactoryBean.java @@ -173,6 +173,9 @@ public class CamelContextFactoryBean extends AbstractCamelContextFactoryBean<Def @XmlTransient private List<?> beans; + @XmlElement(name = "defaultServiceCallConfiguration") + private ServiceCallConfigurationDefinition defaultServiceCallConfiguration; + @XmlElement(name = "serviceCallConfiguration", type = ServiceCallConfigurationDefinition.class) private List<ServiceCallConfigurationDefinition> serviceCallConfigurations; @@ -341,6 +344,15 @@ public class CamelContextFactoryBean extends AbstractCamelContextFactoryBean<Def } @Override + public ServiceCallConfigurationDefinition getDefaultServiceCallConfiguration() { + return defaultServiceCallConfiguration; + } + + public void setDefaultServiceCallConfiguration(ServiceCallConfigurationDefinition defaultServiceCallConfiguration) { + this.defaultServiceCallConfiguration = defaultServiceCallConfiguration; + } + + @Override public List<ServiceCallConfigurationDefinition> getServiceCallConfigurations() { return serviceCallConfigurations; } http://git-wip-us.apache.org/repos/asf/camel/blob/9628b85c/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulDefaultServiceCallRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulDefaultServiceCallRouteTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulDefaultServiceCallRouteTest.java index fd790e1..fcb4479 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulDefaultServiceCallRouteTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulDefaultServiceCallRouteTest.java @@ -97,7 +97,8 @@ public class ConsulDefaultServiceCallRouteTest extends ConsulTestSupport { .serviceCall() .name(SERVICE_NAME) .defaultLoadBalancer() - .end() + .consulServiceDiscovery() + .endParent() .to("log:org.apache.camel.component.consul.cloud?level=INFO&showAll=true&multiline=true") .to("mock:result"); http://git-wip-us.apache.org/repos/asf/camel/blob/9628b85c/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulRibbonServiceCallRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulRibbonServiceCallRouteTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulRibbonServiceCallRouteTest.java index 9009b20..b154835 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulRibbonServiceCallRouteTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/ConsulRibbonServiceCallRouteTest.java @@ -97,7 +97,8 @@ public class ConsulRibbonServiceCallRouteTest extends ConsulTestSupport { from("direct:start") .serviceCall() .name(SERVICE_NAME) - .end() + .consulServiceDiscovery() + .endParent() .to("log:org.apache.camel.component.consul.processor.service?level=INFO&showAll=true&multiline=true") .to("mock:result"); http://git-wip-us.apache.org/repos/asf/camel/blob/9628b85c/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/SpringConsulServiceCallRouteTest.java ---------------------------------------------------------------------- diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/SpringConsulServiceCallRouteTest.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/SpringConsulServiceCallRouteTest.java index 81517be..ac10b06 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/SpringConsulServiceCallRouteTest.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cloud/SpringConsulServiceCallRouteTest.java @@ -40,28 +40,40 @@ public abstract class SpringConsulServiceCallRouteTest extends CamelSpringTestSu this.client = Consul.builder().build().agentClient(); this.registrations = Arrays.asList( ImmutableRegistration.builder() - .id("service-1") + .id("service-1-1") .name("http-service-1") .address("127.0.0.1") - .port(9091) + .port(9011) .build(), ImmutableRegistration.builder() - .id("service-2") + .id("service-1-2") .name("http-service-1") .address("127.0.0.1") - .port(9092) + .port(9012) .build(), ImmutableRegistration.builder() - .id("service-3") + .id("service-1-3") + .name("http-service-1") + .address("127.0.0.1") + .port(9013) + .build(), + ImmutableRegistration.builder() + .id("service-2-1") + .name("http-service-2") + .address("127.0.0.1") + .port(9021) + .build(), + ImmutableRegistration.builder() + .id("service-2-2") .name("http-service-2") .address("127.0.0.1") - .port(9093) + .port(9022) .build(), ImmutableRegistration.builder() - .id("service-4") + .id("service-2-3") .name("http-service-2") .address("127.0.0.1") - .port(9094) + .port(9023) .build() ); @@ -82,9 +94,9 @@ public abstract class SpringConsulServiceCallRouteTest extends CamelSpringTestSu @Test public void testServiceCall() throws Exception { getMockEndpoint("mock:result-1").expectedMessageCount(2); - getMockEndpoint("mock:result-1").expectedBodiesReceivedInAnyOrder("service-1 9091", "service-1 9092"); + getMockEndpoint("mock:result-1").expectedBodiesReceivedInAnyOrder("service-1 9012", "service-1 9013"); getMockEndpoint("mock:result-2").expectedMessageCount(2); - getMockEndpoint("mock:result-2").expectedBodiesReceivedInAnyOrder("service-2 9093", "service-2 9094"); + getMockEndpoint("mock:result-2").expectedBodiesReceivedInAnyOrder("service-2 9021", "service-2 9023"); template.sendBody("direct:start", "service-1"); template.sendBody("direct:start", "service-1"); http://git-wip-us.apache.org/repos/asf/camel/blob/9628b85c/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulDefaultServiceCallRouteTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulDefaultServiceCallRouteTest.xml b/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulDefaultServiceCallRouteTest.xml index c4d0950..f050001 100644 --- a/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulDefaultServiceCallRouteTest.xml +++ b/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulDefaultServiceCallRouteTest.xml @@ -25,63 +25,100 @@ <camelContext xmlns="http://camel.apache.org/schema/spring"> - <!-- shared config --> - <serviceCallConfiguration id="shared-config" serviceChooserRef="round-robin"> - <consulServiceDiscovery readTimeoutMillis="1444" url="http://localhost:8500"/> + <!-- ************************************* --> + <!-- ServiceCall --> + <!-- ************************************* --> + + <defaultServiceCallConfiguration id="default"> + <!-- service discovery --> + <consulServiceDiscovery url="http://localhost:8500"/> + + <!-- service filter --> + <blacklistServiceFilter> + <servers>http-service-1@127.0.0.1:9011</servers> + <servers>http-service-2@127.0.0.1:9021</servers> + </blacklistServiceFilter> + </defaultServiceCallConfiguration> + + <serviceCallConfiguration id="service-2"> + <!-- service filter --> + <blacklistServiceFilter> + <servers>http-service-2@127.0.0.1:9022</servers> + </blacklistServiceFilter> </serviceCallConfiguration> + <!-- ************************************* --> + <!-- Routes --> + <!-- ************************************* --> + <route> <from uri="direct:start"/> <choice> <when> <simple>${body} == 'service-1'</simple> - <serviceCall name="http-service-1"> - <consulServiceDiscovery readTimeoutMillis="1222" url="http://localhost:8500"/> - </serviceCall> + <serviceCall name="http-service-1"/> <to uri="mock:result-1"/> </when> <when> <simple>${body} == 'service-2'</simple> - <serviceCall name="http-service-2"> - <consulServiceDiscovery readTimeoutMillis="1333" url="http://localhost:8500"/> - </serviceCall> - <to uri="mock:result-2"/> - </when> - <when> - <simple>${body} == 'service-3'</simple> - <serviceCall name="http-service-3" configurationRef="shared-config"/> + <serviceCall name="http-service-2" configurationRef="service-2"/> <to uri="mock:result-2"/> </when> </choice> </route> + <!-- ************************************* --> + <!-- http-service-1 --> + <!-- ************************************* --> + + <route> + <from uri="jetty:http://localhost:9011"/> + <transform> + <simple>${body} 9011</simple> + </transform> + </route> + <route> - <from uri="jetty:http://localhost:9091"/> + <from uri="jetty:http://localhost:9012"/> <transform> - <simple>${body} 9091</simple> + <simple>${body} 9012</simple> </transform> </route> <route> - <from uri="jetty:http://localhost:9092"/> + <from uri="jetty:http://localhost:9013"/> <transform> - <simple>${body} 9092</simple> + <simple>${body} 9013</simple> </transform> </route> + <!-- ************************************* --> + <!-- http-service-2 --> + <!-- ************************************* --> + <route> - <from uri="jetty:http://localhost:9093"/> + <from uri="jetty:http://localhost:9021"/> <transform> - <simple>${body} 9093</simple> + <simple>${body} 9021</simple> </transform> </route> <route> - <from uri="jetty:http://localhost:9094"/> + <from uri="jetty:http://localhost:9022"/> <transform> - <simple>${body} 9094</simple> + <simple>${body} 9022</simple> </transform> </route> + + <route> + <from uri="jetty:http://localhost:9023"/> + <transform> + <simple>${body} 9023</simple> + </transform> + </route> + + + </camelContext> </beans> http://git-wip-us.apache.org/repos/asf/camel/blob/9628b85c/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulRibbonServiceCallRouteTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulRibbonServiceCallRouteTest.xml b/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulRibbonServiceCallRouteTest.xml index ac1b642..582ccee 100644 --- a/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulRibbonServiceCallRouteTest.xml +++ b/components/camel-consul/src/test/resources/org/apache/camel/component/consul/cloud/SpringConsulRibbonServiceCallRouteTest.xml @@ -24,11 +24,32 @@ http://camel.apache.org/schema/spring/camel-spring.xsd"> <camelContext xmlns="http://camel.apache.org/schema/spring"> - <!-- + + <!-- ************************************* --> + <!-- ServiceCall --> + <!-- ************************************* --> + + <defaultServiceCallConfiguration id="default"> + <!-- service discovery --> + <consulServiceDiscovery url="http://localhost:8500"/> + + <!-- service filter --> + <blacklistServiceFilter> + <servers>http-service-1@127.0.0.1:9011</servers> + <servers>http-service-2@127.0.0.1:9021</servers> + </blacklistServiceFilter> + </defaultServiceCallConfiguration> + <serviceCallConfiguration id="service-2"> - <consulServiceDiscovery"/> + <!-- service filter --> + <blacklistServiceFilter> + <servers>http-service-2@127.0.0.1:9022</servers> + </blacklistServiceFilter> </serviceCallConfiguration> - --> + + <!-- ************************************* --> + <!-- Routes --> + <!-- ************************************* --> <route> <from uri="direct:start"/> @@ -36,15 +57,13 @@ <when> <simple>${body} == 'service-1'</simple> <serviceCall name="http-service-1"> - <consulServiceDiscovery readTimeoutMillis="1222" url="http://localhost:8500"/> <ribbonLoadBalancer/> </serviceCall> <to uri="mock:result-1"/> </when> <when> <simple>${body} == 'service-2'</simple> - <serviceCall name="http-service-2"> - <consulServiceDiscovery readTimeoutMillis="1333" url="http://localhost:8500"/> + <serviceCall name="http-service-2" configurationRef="service-2"> <ribbonLoadBalancer/> </serviceCall> <to uri="mock:result-2"/> @@ -52,33 +71,58 @@ </choice> </route> + <!-- ************************************* --> + <!-- http-service-1 --> + <!-- ************************************* --> + + <route> + <from uri="jetty:http://localhost:9011"/> + <transform> + <simple>${body} 9011</simple> + </transform> + </route> + + <route> + <from uri="jetty:http://localhost:9012"/> + <transform> + <simple>${body} 9012</simple> + </transform> + </route> + <route> - <from uri="jetty:http://localhost:9091"/> + <from uri="jetty:http://localhost:9013"/> <transform> - <simple>${body} 9091</simple> + <simple>${body} 9013</simple> </transform> </route> + <!-- ************************************* --> + <!-- http-service-2 --> + <!-- ************************************* --> + <route> - <from uri="jetty:http://localhost:9092"/> + <from uri="jetty:http://localhost:9021"/> <transform> - <simple>${body} 9092</simple> + <simple>${body} 9021</simple> </transform> </route> <route> - <from uri="jetty:http://localhost:9093"/> + <from uri="jetty:http://localhost:9022"/> <transform> - <simple>${body} 9093</simple> + <simple>${body} 9022</simple> </transform> </route> <route> - <from uri="jetty:http://localhost:9094"/> + <from uri="jetty:http://localhost:9023"/> <transform> - <simple>${body} 9094</simple> + <simple>${body} 9023</simple> </transform> </route> + + + </camelContext> -</beans> \ No newline at end of file +</beans> http://git-wip-us.apache.org/repos/asf/camel/blob/9628b85c/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java ---------------------------------------------------------------------- diff --git a/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java b/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java index 1b89a39..998d782 100644 --- a/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java +++ b/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java @@ -797,6 +797,8 @@ public abstract class AbstractCamelContextFactoryBean<T extends ModelCamelContex public abstract List<HystrixConfigurationDefinition> getHystrixConfigurations(); + public abstract ServiceCallConfigurationDefinition getDefaultServiceCallConfiguration(); + public abstract List<ServiceCallConfigurationDefinition> getServiceCallConfigurations(); // Implementation methods @@ -878,6 +880,9 @@ public abstract class AbstractCamelContextFactoryBean<T extends ModelCamelContex if (getRestConfiguration() != null) { ctx.setRestConfiguration(getRestConfiguration().asRestConfiguration(ctx)); } + if (getDefaultServiceCallConfiguration() != null) { + ctx.setServiceCallConfiguration(getDefaultServiceCallConfiguration()); + } if (getServiceCallConfigurations() != null) { for (ServiceCallConfigurationDefinition bean : getServiceCallConfigurations()) { ctx.addServiceCallConfiguration(bean.getId(), bean); http://git-wip-us.apache.org/repos/asf/camel/blob/9628b85c/components/camel-etcd/src/test/resources/org/apache/camel/component/etcd/cloud/SpringEtcdServiceCallDefaultRouteTest.xml ---------------------------------------------------------------------- diff --git a/components/camel-etcd/src/test/resources/org/apache/camel/component/etcd/cloud/SpringEtcdServiceCallDefaultRouteTest.xml b/components/camel-etcd/src/test/resources/org/apache/camel/component/etcd/cloud/SpringEtcdServiceCallDefaultRouteTest.xml index d0194c8..006893c 100644 --- a/components/camel-etcd/src/test/resources/org/apache/camel/component/etcd/cloud/SpringEtcdServiceCallDefaultRouteTest.xml +++ b/components/camel-etcd/src/test/resources/org/apache/camel/component/etcd/cloud/SpringEtcdServiceCallDefaultRouteTest.xml @@ -26,7 +26,9 @@ <camelContext xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="direct:start"/> - <serviceCall name="http-service"/> + <serviceCall name="http-service"> + <etcdServiceDiscovery/> + </serviceCall> <to uri="mock:result"/> </route> http://git-wip-us.apache.org/repos/asf/camel/blob/9628b85c/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java b/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java index e0a3f07..514985e 100644 --- a/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java +++ b/components/camel-spring/src/main/java/org/apache/camel/spring/CamelContextFactoryBean.java @@ -167,6 +167,8 @@ public class CamelContextFactoryBean extends AbstractCamelContextFactoryBean<Spr @XmlElement(name = "export", type = CamelServiceExporterDefinition.class), @XmlElement(name = "errorHandler", type = ErrorHandlerDefinition.class) }) private List<?> beans; + @XmlElement(name = "defaultServiceCallConfiguration") + private ServiceCallConfigurationDefinition defaultServiceCallConfiguration; @XmlElement(name = "serviceCallConfiguration", type = ServiceCallConfigurationDefinition.class) private List<ServiceCallConfigurationDefinition> serviceCallConfigurations; @XmlElement(name = "hystrixConfiguration", type = HystrixConfigurationDefinition.class) @@ -953,6 +955,15 @@ public class CamelContextFactoryBean extends AbstractCamelContextFactoryBean<Spr } @Override + public ServiceCallConfigurationDefinition getDefaultServiceCallConfiguration() { + return defaultServiceCallConfiguration; + } + + public void setDefaultServiceCallConfiguration(ServiceCallConfigurationDefinition defaultServiceCallConfiguration) { + this.defaultServiceCallConfiguration = defaultServiceCallConfiguration; + } + + @Override public List<ServiceCallConfigurationDefinition> getServiceCallConfigurations() { return serviceCallConfigurations; } http://git-wip-us.apache.org/repos/asf/camel/blob/9628b85c/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java ---------------------------------------------------------------------- diff --git a/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java b/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java index 04737cf..072e904 100644 --- a/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java +++ b/components/camel-spring/src/main/java/org/apache/camel/spring/handler/CamelNamespaceHandler.java @@ -403,6 +403,7 @@ public class CamelNamespaceHandler extends NamespaceHandlerSupport { builder.addPropertyValue("threadPoolProfiles", factoryBean.getThreadPoolProfiles()); builder.addPropertyValue("beansFactory", factoryBean.getBeansFactory()); builder.addPropertyValue("beans", factoryBean.getBeans()); + builder.addPropertyValue("defaultServiceCallConfiguration", factoryBean.getDefaultServiceCallConfiguration()); builder.addPropertyValue("serviceCallConfigurations", factoryBean.getServiceCallConfigurations()); builder.addPropertyValue("hystrixConfigurations", factoryBean.getHystrixConfigurations()); // add any depends-on http://git-wip-us.apache.org/repos/asf/camel/blob/9628b85c/platforms/spring-boot/components-starter/camel-consul-starter/src/main/java/org/apache/camel/component/consul/springboot/cloud/ConsulCloudAutoConfiguration.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-consul-starter/src/main/java/org/apache/camel/component/consul/springboot/cloud/ConsulCloudAutoConfiguration.java b/platforms/spring-boot/components-starter/camel-consul-starter/src/main/java/org/apache/camel/component/consul/springboot/cloud/ConsulCloudAutoConfiguration.java index db5cf32..240a8fc 100644 --- a/platforms/spring-boot/components-starter/camel-consul-starter/src/main/java/org/apache/camel/component/consul/springboot/cloud/ConsulCloudAutoConfiguration.java +++ b/platforms/spring-boot/components-starter/camel-consul-starter/src/main/java/org/apache/camel/component/consul/springboot/cloud/ConsulCloudAutoConfiguration.java @@ -23,21 +23,16 @@ import org.apache.camel.CamelContext; import org.apache.camel.cloud.ServiceDiscovery; import org.apache.camel.component.consul.cloud.ConsulServiceDiscoveryFactory; import org.apache.camel.spring.boot.CamelAutoConfiguration; +import org.apache.camel.spring.boot.util.GroupCondition; import org.apache.camel.util.IntrospectionSupport; import org.springframework.boot.autoconfigure.AutoConfigureAfter; -import org.springframework.boot.autoconfigure.condition.ConditionMessage; -import org.springframework.boot.autoconfigure.condition.ConditionOutcome; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -import org.springframework.boot.autoconfigure.condition.SpringBootCondition; -import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Lazy; -import org.springframework.core.type.AnnotatedTypeMetadata; @Configuration @ConditionalOnBean(CamelAutoConfiguration.class) @@ -58,22 +53,16 @@ public class ConsulCloudAutoConfiguration { return factory.newInstance(camelContext); } - public static class Condition extends SpringBootCondition { - @Override - public ConditionOutcome getMatchOutcome(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { - boolean groupEnabled = isEnabled(conditionContext, "camel.cloud.", true); + // ******************************* + // Condition + // ******************************* - ConditionMessage.Builder message = ConditionMessage.forCondition("camel.cloud.consul"); - if (isEnabled(conditionContext, "camel.cloud.consul.", groupEnabled)) { - return ConditionOutcome.match(message.because("enabled")); - } - - return ConditionOutcome.noMatch(message.because("not enabled")); - } - - private boolean isEnabled(ConditionContext context, String prefix, boolean defaultValue) { - RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(context.getEnvironment(), prefix); - return resolver.getProperty("enabled", Boolean.class, defaultValue); + public static class Condition extends GroupCondition { + public Condition() { + super( + "camel.cloud", + "camel.cloud.consul" + ); } } } http://git-wip-us.apache.org/repos/asf/camel/blob/9628b85c/platforms/spring-boot/components-starter/camel-dns-starter/src/main/java/org/apache/camel/component/dns/springboot/cloud/DnsCloudAutoConfiguration.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-dns-starter/src/main/java/org/apache/camel/component/dns/springboot/cloud/DnsCloudAutoConfiguration.java b/platforms/spring-boot/components-starter/camel-dns-starter/src/main/java/org/apache/camel/component/dns/springboot/cloud/DnsCloudAutoConfiguration.java index a3e9b09..3d794d3 100644 --- a/platforms/spring-boot/components-starter/camel-dns-starter/src/main/java/org/apache/camel/component/dns/springboot/cloud/DnsCloudAutoConfiguration.java +++ b/platforms/spring-boot/components-starter/camel-dns-starter/src/main/java/org/apache/camel/component/dns/springboot/cloud/DnsCloudAutoConfiguration.java @@ -23,21 +23,16 @@ import org.apache.camel.CamelContext; import org.apache.camel.cloud.ServiceDiscovery; import org.apache.camel.component.dns.cloud.DnsServiceDiscoveryFactory; import org.apache.camel.spring.boot.CamelAutoConfiguration; +import org.apache.camel.spring.boot.util.GroupCondition; import org.apache.camel.util.IntrospectionSupport; import org.springframework.boot.autoconfigure.AutoConfigureAfter; -import org.springframework.boot.autoconfigure.condition.ConditionMessage; -import org.springframework.boot.autoconfigure.condition.ConditionOutcome; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -import org.springframework.boot.autoconfigure.condition.SpringBootCondition; -import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Lazy; -import org.springframework.core.type.AnnotatedTypeMetadata; @Configuration @ConditionalOnBean(CamelAutoConfiguration.class) @@ -58,22 +53,16 @@ public class DnsCloudAutoConfiguration { return factory.newInstance(camelContext); } - public static class Condition extends SpringBootCondition { - @Override - public ConditionOutcome getMatchOutcome(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { - boolean groupEnabled = isEnabled(conditionContext, "camel.cloud.", true); + // ******************************* + // Condition + // ******************************* - ConditionMessage.Builder message = ConditionMessage.forCondition("camel.cloud.dns"); - if (isEnabled(conditionContext, "camel.cloud.dns.", groupEnabled)) { - return ConditionOutcome.match(message.because("enabled")); - } - - return ConditionOutcome.noMatch(message.because("not enabled")); - } - - private boolean isEnabled(ConditionContext context, String prefix, boolean defaultValue) { - RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(context.getEnvironment(), prefix); - return resolver.getProperty("enabled", Boolean.class, defaultValue); + public static class Condition extends GroupCondition { + public Condition() { + super( + "camel.cloud", + "camel.cloud.dns" + ); } } } http://git-wip-us.apache.org/repos/asf/camel/blob/9628b85c/platforms/spring-boot/components-starter/camel-etcd-starter/src/main/java/org/apache/camel/component/etcd/springboot/cloud/EtcdCloudAutoConfiguration.java ---------------------------------------------------------------------- diff --git a/platforms/spring-boot/components-starter/camel-etcd-starter/src/main/java/org/apache/camel/component/etcd/springboot/cloud/EtcdCloudAutoConfiguration.java b/platforms/spring-boot/components-starter/camel-etcd-starter/src/main/java/org/apache/camel/component/etcd/springboot/cloud/EtcdCloudAutoConfiguration.java index 9af3218..a202698 100644 --- a/platforms/spring-boot/components-starter/camel-etcd-starter/src/main/java/org/apache/camel/component/etcd/springboot/cloud/EtcdCloudAutoConfiguration.java +++ b/platforms/spring-boot/components-starter/camel-etcd-starter/src/main/java/org/apache/camel/component/etcd/springboot/cloud/EtcdCloudAutoConfiguration.java @@ -21,27 +21,18 @@ import java.util.Map; import org.apache.camel.CamelContext; import org.apache.camel.cloud.ServiceDiscovery; -import org.apache.camel.component.etcd.cloud.EtcdOnDemandServiceDiscovery; import org.apache.camel.component.etcd.cloud.EtcdServiceDiscoveryFactory; -import org.apache.camel.component.etcd.cloud.EtcdWatchServiceDiscovery; import org.apache.camel.spring.boot.CamelAutoConfiguration; +import org.apache.camel.spring.boot.util.GroupCondition; import org.apache.camel.util.IntrospectionSupport; -import org.apache.camel.util.ObjectHelper; import org.springframework.boot.autoconfigure.AutoConfigureAfter; -import org.springframework.boot.autoconfigure.condition.ConditionMessage; -import org.springframework.boot.autoconfigure.condition.ConditionOutcome; import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.boot.autoconfigure.condition.SpringBootCondition; -import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ConditionContext; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Lazy; -import org.springframework.core.type.AnnotatedTypeMetadata; @Configuration @ConditionalOnBean(CamelAutoConfiguration.class) @@ -62,22 +53,16 @@ public class EtcdCloudAutoConfiguration { return factory.newInstance(camelContext); } - public static class Condition extends SpringBootCondition { - @Override - public ConditionOutcome getMatchOutcome(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) { - boolean groupEnabled = isEnabled(conditionContext, "camel.cloud.", true); + // ******************************* + // Condition + // ******************************* - ConditionMessage.Builder message = ConditionMessage.forCondition("camel.cloud.etcd"); - if (isEnabled(conditionContext, "camel.cloud.etcd.", groupEnabled)) { - return ConditionOutcome.match(message.because("enabled")); - } - - return ConditionOutcome.noMatch(message.because("not enabled")); - } - - private boolean isEnabled(ConditionContext context, String prefix, boolean defaultValue) { - RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(context.getEnvironment(), prefix); - return resolver.getProperty("enabled", Boolean.class, defaultValue); + public static class Condition extends GroupCondition { + public Condition() { + super( + "camel.cloud", + "camel.cloud.etcd" + ); } } }