This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch cloud2 in repository https://gitbox.apache.org/repos/asf/camel.git
commit d93461599919034ce6e6880aff4d47829232c740 Author: Claus Ibsen <[email protected]> AuthorDate: Fri Apr 5 16:43:36 2019 +0200 CAMEL-13380: camel-core - Move cloud out into camel-cloud component. Woprk in progress --- core/camel-cloud/pom.xml | 37 +- .../impl/cloud/ServiceCallProcessorFactory.java} | 179 ++++----- .../org/apache/camel/model/ServiceCallDefinition | 18 + .../impl/cloud/CachingServiceDiscoveryTest.java | 0 .../impl/cloud/CombinedServiceDiscoveryTest.java | 0 .../impl/cloud/CombinedServiceFilterTest.java | 0 .../apache/camel/impl/cloud/LoadBalancerTest.java | 0 .../impl/cloud/ServiceCallConfigurationTest.java | 0 .../camel/impl/cloud/ServiceDefinitionTest.java | 0 .../camel/impl/cloud/ServiceRegistryTest.java | 0 .../src/test/resources/log4j2.properties | 33 ++ .../cloud/ServiceCallExpressionConfiguration.java | 11 +- .../apache/camel/reifier/ServiceCallReifier.java | 431 +-------------------- core/pom.xml | 2 +- 14 files changed, 173 insertions(+), 538 deletions(-) diff --git a/core/camel-cloud/pom.xml b/core/camel-cloud/pom.xml index 3ac8325..28f48e5 100644 --- a/core/camel-cloud/pom.xml +++ b/core/camel-cloud/pom.xml @@ -41,24 +41,43 @@ <dependency> <groupId>org.apache.camel</groupId> - <artifactId>spi-annotations</artifactId> - <optional>true</optional> + <artifactId>camel-core</artifactId> </dependency> + <!-- testing --> <dependency> <groupId>org.apache.camel</groupId> - <artifactId>meta-annotations</artifactId> - <optional>true</optional> + <artifactId>camel-core</artifactId> + <type>test-jar</type> + <scope>test</scope> + </dependency> + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <scope>test</scope> + <exclusions> + <exclusion> + <groupId>org.hamcrest</groupId> + <artifactId>hamcrest-core</artifactId> + </exclusion> + </exclusions> </dependency> + <!-- logging --> <dependency> - <groupId>org.apache.camel</groupId> - <artifactId>camel-support</artifactId> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-api</artifactId> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-core</artifactId> + <scope>test</scope> </dependency> <dependency> - <groupId>com.github.ben-manes.caffeine</groupId> - <artifactId>caffeine</artifactId> - <version>${caffeine-version}</version> + <groupId>org.apache.logging.log4j</groupId> + <artifactId>log4j-slf4j-impl</artifactId> + <scope>test</scope> </dependency> </dependencies> diff --git a/core/camel-core/src/main/java/org/apache/camel/reifier/ServiceCallReifier.java b/core/camel-cloud/src/main/java/org/apache/camel/impl/cloud/ServiceCallProcessorFactory.java similarity index 73% copy from core/camel-core/src/main/java/org/apache/camel/reifier/ServiceCallReifier.java copy to core/camel-cloud/src/main/java/org/apache/camel/impl/cloud/ServiceCallProcessorFactory.java index 74a3457..eb0904a 100644 --- a/core/camel-core/src/main/java/org/apache/camel/reifier/ServiceCallReifier.java +++ b/core/camel-cloud/src/main/java/org/apache/camel/impl/cloud/ServiceCallProcessorFactory.java @@ -1,20 +1,20 @@ -/* +/** * 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 - * + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> * 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.reifier; +package org.apache.camel.impl.cloud; import java.util.Optional; import java.util.function.Function; @@ -32,15 +32,8 @@ import org.apache.camel.cloud.ServiceExpressionFactory; import org.apache.camel.cloud.ServiceFilter; import org.apache.camel.cloud.ServiceFilterAware; import org.apache.camel.cloud.ServiceLoadBalancer; -import org.apache.camel.impl.cloud.DefaultServiceCallExpression; -import org.apache.camel.impl.cloud.DefaultServiceCallProcessor; -import org.apache.camel.impl.cloud.DefaultServiceLoadBalancer; -import org.apache.camel.impl.cloud.HealthyServiceFilter; -import org.apache.camel.impl.cloud.PassThroughServiceFilter; -import org.apache.camel.impl.cloud.RandomServiceChooser; -import org.apache.camel.impl.cloud.RoundRobinServiceChooser; +import org.apache.camel.impl.TypedProcessorFactory; import org.apache.camel.model.ModelCamelContext; -import org.apache.camel.model.ProcessorDefinition; import org.apache.camel.model.cloud.ServiceCallConfigurationDefinition; import org.apache.camel.model.cloud.ServiceCallDefinition; import org.apache.camel.model.cloud.ServiceCallDefinitionConstants; @@ -49,14 +42,13 @@ import org.apache.camel.support.CamelContextHelper; import org.apache.camel.util.ObjectHelper; import org.apache.camel.util.function.Suppliers; import org.apache.camel.util.function.ThrowingHelper; - import static org.apache.camel.support.CamelContextHelper.findByType; import static org.apache.camel.support.CamelContextHelper.lookup; -class ServiceCallReifier extends ProcessorReifier<ServiceCallDefinition> { +public class ServiceCallProcessorFactory extends TypedProcessorFactory<ServiceCallDefinition> { - ServiceCallReifier(ProcessorDefinition<?> definition) { - super((ServiceCallDefinition) definition); + protected ServiceCallProcessorFactory() { + super(ServiceCallDefinition.class); } // ***************************** @@ -64,12 +56,12 @@ class ServiceCallReifier extends ProcessorReifier<ServiceCallDefinition> { // ***************************** @Override - public Processor createProcessor(RouteContext routeContext) throws Exception { + public Processor doCreateProcessor(RouteContext routeContext, ServiceCallDefinition definition) throws Exception { final CamelContext camelContext = routeContext.getCamelContext(); - final ServiceDiscovery serviceDiscovery = retrieveServiceDiscovery(camelContext); - final ServiceFilter serviceFilter = retrieveServiceFilter(camelContext); - final ServiceChooser serviceChooser = retrieveServiceChooser(camelContext); - final ServiceLoadBalancer loadBalancer = retrieveLoadBalancer(camelContext); + final ServiceDiscovery serviceDiscovery = retrieveServiceDiscovery(camelContext, definition); + final ServiceFilter serviceFilter = retrieveServiceFilter(camelContext, definition); + final ServiceChooser serviceChooser = retrieveServiceChooser(camelContext, definition); + final ServiceLoadBalancer loadBalancer = retrieveLoadBalancer(camelContext, definition); CamelContextAware.trySetCamelContext(serviceDiscovery, camelContext); CamelContextAware.trySetCamelContext(serviceFilter, camelContext); @@ -90,7 +82,7 @@ class ServiceCallReifier extends ProcessorReifier<ServiceCallDefinition> { // The component configured on EIP takes precedence vs configured on configuration. String endpointScheme = definition.getComponent(); if (endpointScheme == null) { - ServiceCallConfigurationDefinition conf = retrieveConfig(camelContext); + ServiceCallConfigurationDefinition conf = retrieveConfig(camelContext, definition); if (conf != null) { endpointScheme = conf.getComponent(); } @@ -106,7 +98,7 @@ class ServiceCallReifier extends ProcessorReifier<ServiceCallDefinition> { // The uri configured on EIP takes precedence vs configured on configuration. String endpointUri = definition.getUri(); if (endpointUri == null) { - ServiceCallConfigurationDefinition conf = retrieveConfig(camelContext); + ServiceCallConfigurationDefinition conf = retrieveConfig(camelContext, definition); if (conf != null) { endpointUri = conf.getUri(); } @@ -131,7 +123,7 @@ class ServiceCallReifier extends ProcessorReifier<ServiceCallDefinition> { endpointUri, definition.getPattern(), loadBalancer, - retrieveExpression(camelContext, endpointScheme)); + retrieveExpression(camelContext, endpointScheme, definition)); } // ***************************** @@ -160,7 +152,7 @@ class ServiceCallReifier extends ProcessorReifier<ServiceCallDefinition> { return config; } - private ServiceCallConfigurationDefinition retrieveConfig(CamelContext camelContext) { + private ServiceCallConfigurationDefinition retrieveConfig(CamelContext camelContext, ServiceCallDefinition definition) { ServiceCallConfigurationDefinition config = null; if (definition.getConfigurationRef() != null) { // lookup in registry firstNotNull @@ -198,24 +190,24 @@ class ServiceCallReifier extends ProcessorReifier<ServiceCallDefinition> { return answer; } - private ServiceDiscovery retrieveServiceDiscovery(CamelContext camelContext) throws Exception { + private ServiceDiscovery retrieveServiceDiscovery(CamelContext camelContext, ServiceCallDefinition definition) throws Exception { return Suppliers.firstNotNull( - () -> (definition.getServiceDiscoveryConfiguration() != null) ? definition.getServiceDiscoveryConfiguration().newInstance(camelContext) : null, - // Local configuration - () -> retrieve(ServiceDiscovery.class, camelContext, definition::getServiceDiscovery, definition::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, ServiceCallDefinitionConstants.DEFAULT_SERVICE_DISCOVERY_ID, ServiceDiscovery.class) + () -> (definition.getServiceDiscoveryConfiguration() != null) ? definition.getServiceDiscoveryConfiguration().newInstance(camelContext) : null, + // Local configuration + () -> retrieve(ServiceDiscovery.class, camelContext, definition::getServiceDiscovery, definition::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, ServiceCallDefinitionConstants.DEFAULT_SERVICE_DISCOVERY_ID, ServiceDiscovery.class) ).orElseGet( - // Default, that's s little ugly but a load balancer may live without - // (i.e. the Ribbon one) so let's delegate the null check to the actual - // impl. - () -> null + // Default, that's s little ugly but a load balancer may live without + // (i.e. the Ribbon one) so let's delegate the null check to the actual + // impl. + () -> null ); } @@ -254,22 +246,22 @@ class ServiceCallReifier extends ProcessorReifier<ServiceCallDefinition> { return answer; } - private ServiceFilter retrieveServiceFilter(CamelContext camelContext) throws Exception { + private ServiceFilter retrieveServiceFilter(CamelContext camelContext, ServiceCallDefinition definition) throws Exception { return Suppliers.firstNotNull( - () -> (definition.getServiceFilterConfiguration() != null) ? definition.getServiceFilterConfiguration().newInstance(camelContext) : null, - // Local configuration - () -> retrieve(ServiceFilter.class, camelContext, definition::getServiceFilter, definition::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, ServiceCallDefinitionConstants.DEFAULT_SERVICE_FILTER_ID, ServiceFilter.class) + () -> (definition.getServiceFilterConfiguration() != null) ? definition.getServiceFilterConfiguration().newInstance(camelContext) : null, + // Local configuration + () -> retrieve(ServiceFilter.class, camelContext, definition::getServiceFilter, definition::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, ServiceCallDefinitionConstants.DEFAULT_SERVICE_FILTER_ID, ServiceFilter.class) ).orElseGet( - // Default - () -> new HealthyServiceFilter() + // Default + () -> new HealthyServiceFilter() ); } @@ -304,21 +296,21 @@ class ServiceCallReifier extends ProcessorReifier<ServiceCallDefinition> { return answer; } - private ServiceChooser retrieveServiceChooser(CamelContext camelContext) throws Exception { + private ServiceChooser retrieveServiceChooser(CamelContext camelContext, ServiceCallDefinition definition) throws Exception { return Suppliers.firstNotNull( - // Local configuration - () -> retrieve(ServiceChooser.class, camelContext, definition::getServiceChooser, definition::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, ServiceCallDefinitionConstants.DEFAULT_SERVICE_CHOOSER_ID, ServiceChooser.class) + // Local configuration + () -> retrieve(ServiceChooser.class, camelContext, definition::getServiceChooser, definition::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, ServiceCallDefinitionConstants.DEFAULT_SERVICE_CHOOSER_ID, ServiceChooser.class) ).orElseGet( - // Default - () -> new RoundRobinServiceChooser() + // Default + () -> new RoundRobinServiceChooser() ); } @@ -346,22 +338,22 @@ class ServiceCallReifier extends ProcessorReifier<ServiceCallDefinition> { return answer; } - private ServiceLoadBalancer retrieveLoadBalancer(CamelContext camelContext) throws Exception { + private ServiceLoadBalancer retrieveLoadBalancer(CamelContext camelContext, ServiceCallDefinition definition) throws Exception { return Suppliers.firstNotNull( - () -> (definition.getLoadBalancerConfiguration() != null) ? definition.getLoadBalancerConfiguration().newInstance(camelContext) : null, - // Local configuration - () -> retrieve(ServiceLoadBalancer.class, camelContext, definition::getLoadBalancer, definition::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, ServiceLoadBalancer.class), - // From registry - () -> lookup(camelContext, ServiceCallDefinitionConstants.DEFAULT_LOAD_BALANCER_ID, ServiceLoadBalancer.class) + () -> (definition.getLoadBalancerConfiguration() != null) ? definition.getLoadBalancerConfiguration().newInstance(camelContext) : null, + // Local configuration + () -> retrieve(ServiceLoadBalancer.class, camelContext, definition::getLoadBalancer, definition::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, ServiceLoadBalancer.class), + // From registry + () -> lookup(camelContext, ServiceCallDefinitionConstants.DEFAULT_LOAD_BALANCER_ID, ServiceLoadBalancer.class) ).orElseGet( - // Default - () -> new DefaultServiceLoadBalancer() + // Default + () -> new DefaultServiceLoadBalancer() ); } @@ -389,17 +381,17 @@ class ServiceCallReifier extends ProcessorReifier<ServiceCallDefinition> { return answer; } - private Expression retrieveExpression(CamelContext camelContext, String component) throws Exception { + private Expression retrieveExpression(CamelContext camelContext, String component, ServiceCallDefinition definition) throws Exception { Optional<Expression> expression = Suppliers.firstNotNull( - () -> (definition.getExpressionConfiguration() != null) ? definition.getExpressionConfiguration().newInstance(camelContext) : null, - // Local configuration - () -> retrieve(Expression.class, camelContext, definition::getExpression, definition::getExpressionRef), - // Linked configuration - () -> retrieveExpression(camelContext, this::retrieveConfig), - // Default configuration - () -> retrieveExpression(camelContext, this::retrieveDefaultConfig), - // From registry - () -> lookup(camelContext, ServiceCallDefinitionConstants.DEFAULT_SERVICE_CALL_EXPRESSION_ID, Expression.class) + () -> (definition.getExpressionConfiguration() != null) ? definition.getExpressionConfiguration().newInstance(camelContext) : null, + // Local configuration + () -> retrieve(Expression.class, camelContext, definition::getExpression, definition::getExpressionRef), + // Linked configuration + () -> retrieveExpression(camelContext, this::retrieveConfig), + // Default configuration + () -> retrieveExpression(camelContext, this::retrieveDefaultConfig), + // From registry + () -> lookup(camelContext, ServiceCallDefinitionConstants.DEFAULT_SERVICE_CALL_EXPRESSION_ID, Expression.class) ); if (expression.isPresent()) { @@ -458,5 +450,4 @@ class ServiceCallReifier extends ProcessorReifier<ServiceCallDefinition> { return answer; } - } diff --git a/core/camel-cloud/src/main/resources/META-INF/services/org/apache/camel/model/ServiceCallDefinition b/core/camel-cloud/src/main/resources/META-INF/services/org/apache/camel/model/ServiceCallDefinition new file mode 100644 index 0000000..ad3c7d9 --- /dev/null +++ b/core/camel-cloud/src/main/resources/META-INF/services/org/apache/camel/model/ServiceCallDefinition @@ -0,0 +1,18 @@ +# +# 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. +# + +class=org.apache.camel.impl.cloud.ServiceCallProcessorFactory diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/cloud/CachingServiceDiscoveryTest.java b/core/camel-cloud/src/test/java/org/apache/camel/impl/cloud/CachingServiceDiscoveryTest.java similarity index 100% rename from core/camel-core/src/test/java/org/apache/camel/impl/cloud/CachingServiceDiscoveryTest.java rename to core/camel-cloud/src/test/java/org/apache/camel/impl/cloud/CachingServiceDiscoveryTest.java diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/cloud/CombinedServiceDiscoveryTest.java b/core/camel-cloud/src/test/java/org/apache/camel/impl/cloud/CombinedServiceDiscoveryTest.java similarity index 100% rename from core/camel-core/src/test/java/org/apache/camel/impl/cloud/CombinedServiceDiscoveryTest.java rename to core/camel-cloud/src/test/java/org/apache/camel/impl/cloud/CombinedServiceDiscoveryTest.java diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/cloud/CombinedServiceFilterTest.java b/core/camel-cloud/src/test/java/org/apache/camel/impl/cloud/CombinedServiceFilterTest.java similarity index 100% rename from core/camel-core/src/test/java/org/apache/camel/impl/cloud/CombinedServiceFilterTest.java rename to core/camel-cloud/src/test/java/org/apache/camel/impl/cloud/CombinedServiceFilterTest.java diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/cloud/LoadBalancerTest.java b/core/camel-cloud/src/test/java/org/apache/camel/impl/cloud/LoadBalancerTest.java similarity index 100% rename from core/camel-core/src/test/java/org/apache/camel/impl/cloud/LoadBalancerTest.java rename to core/camel-cloud/src/test/java/org/apache/camel/impl/cloud/LoadBalancerTest.java diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/cloud/ServiceCallConfigurationTest.java b/core/camel-cloud/src/test/java/org/apache/camel/impl/cloud/ServiceCallConfigurationTest.java similarity index 100% rename from core/camel-core/src/test/java/org/apache/camel/impl/cloud/ServiceCallConfigurationTest.java rename to core/camel-cloud/src/test/java/org/apache/camel/impl/cloud/ServiceCallConfigurationTest.java diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/cloud/ServiceDefinitionTest.java b/core/camel-cloud/src/test/java/org/apache/camel/impl/cloud/ServiceDefinitionTest.java similarity index 100% rename from core/camel-core/src/test/java/org/apache/camel/impl/cloud/ServiceDefinitionTest.java rename to core/camel-cloud/src/test/java/org/apache/camel/impl/cloud/ServiceDefinitionTest.java diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/cloud/ServiceRegistryTest.java b/core/camel-cloud/src/test/java/org/apache/camel/impl/cloud/ServiceRegistryTest.java similarity index 100% rename from core/camel-core/src/test/java/org/apache/camel/impl/cloud/ServiceRegistryTest.java rename to core/camel-cloud/src/test/java/org/apache/camel/impl/cloud/ServiceRegistryTest.java diff --git a/core/camel-cloud/src/test/resources/log4j2.properties b/core/camel-cloud/src/test/resources/log4j2.properties new file mode 100644 index 0000000..65d5151 --- /dev/null +++ b/core/camel-cloud/src/test/resources/log4j2.properties @@ -0,0 +1,33 @@ +## --------------------------------------------------------------------------- +## 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. +## --------------------------------------------------------------------------- +appender.console.type = Console +appender.console.name = console +appender.console.layout.type = PatternLayout +appender.console.layout.pattern = %d [%-15.15t] %-5p %-30.30c{1} - %m%n + +appender.file.type = File +appender.file.name = file +appender.file.fileName = target/camel-cloud-test.log +appender.file.append = true +appender.file.layout.type = PatternLayout +appender.file.layout.pattern = %d [%-15.15t] %-5p %-30.30c{1} - %m%n + +rootLogger.level = INFO + +rootLogger.appenderRef.file.ref = file +#rootLogger.appenderRef.console.ref = console + diff --git a/core/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallExpressionConfiguration.java b/core/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallExpressionConfiguration.java index c82c067..34169ec 100644 --- a/core/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallExpressionConfiguration.java +++ b/core/camel-core/src/main/java/org/apache/camel/model/cloud/ServiceCallExpressionConfiguration.java @@ -35,7 +35,7 @@ import org.apache.camel.Expression; import org.apache.camel.NoFactoryAvailableException; import org.apache.camel.cloud.ServiceCallConstants; import org.apache.camel.cloud.ServiceExpressionFactory; -import org.apache.camel.impl.cloud.DefaultServiceCallExpression; +//import org.apache.camel.impl.cloud.DefaultServiceCallExpression; import org.apache.camel.model.IdentifiedType; import org.apache.camel.model.ProcessorDefinition; import org.apache.camel.model.PropertyDefinition; @@ -276,10 +276,11 @@ public class ServiceCallExpressionConfiguration extends IdentifiedType implement } } } else { - answer = new DefaultServiceCallExpression( - ObjectHelper.notNull(hostHeader, "hostHeader"), - ObjectHelper.notNull(portHeader, "portHeader") - ); + // TODO: Make this possible to create in camel-cloud +// answer = new DefaultServiceCallExpression( +// ObjectHelper.notNull(hostHeader, "hostHeader"), +// ObjectHelper.notNull(portHeader, "portHeader") +// ); } return answer; diff --git a/core/camel-core/src/main/java/org/apache/camel/reifier/ServiceCallReifier.java b/core/camel-core/src/main/java/org/apache/camel/reifier/ServiceCallReifier.java index 74a3457..b5c1cbd 100644 --- a/core/camel-core/src/main/java/org/apache/camel/reifier/ServiceCallReifier.java +++ b/core/camel-core/src/main/java/org/apache/camel/reifier/ServiceCallReifier.java @@ -16,447 +16,20 @@ */ package org.apache.camel.reifier; -import java.util.Optional; -import java.util.function.Function; -import java.util.function.Supplier; - -import org.apache.camel.CamelContext; -import org.apache.camel.CamelContextAware; -import org.apache.camel.Expression; import org.apache.camel.Processor; -import org.apache.camel.cloud.ServiceChooser; -import org.apache.camel.cloud.ServiceChooserAware; -import org.apache.camel.cloud.ServiceDiscovery; -import org.apache.camel.cloud.ServiceDiscoveryAware; -import org.apache.camel.cloud.ServiceExpressionFactory; -import org.apache.camel.cloud.ServiceFilter; -import org.apache.camel.cloud.ServiceFilterAware; -import org.apache.camel.cloud.ServiceLoadBalancer; -import org.apache.camel.impl.cloud.DefaultServiceCallExpression; -import org.apache.camel.impl.cloud.DefaultServiceCallProcessor; -import org.apache.camel.impl.cloud.DefaultServiceLoadBalancer; -import org.apache.camel.impl.cloud.HealthyServiceFilter; -import org.apache.camel.impl.cloud.PassThroughServiceFilter; -import org.apache.camel.impl.cloud.RandomServiceChooser; -import org.apache.camel.impl.cloud.RoundRobinServiceChooser; -import org.apache.camel.model.ModelCamelContext; import org.apache.camel.model.ProcessorDefinition; -import org.apache.camel.model.cloud.ServiceCallConfigurationDefinition; import org.apache.camel.model.cloud.ServiceCallDefinition; -import org.apache.camel.model.cloud.ServiceCallDefinitionConstants; import org.apache.camel.spi.RouteContext; -import org.apache.camel.support.CamelContextHelper; -import org.apache.camel.util.ObjectHelper; -import org.apache.camel.util.function.Suppliers; -import org.apache.camel.util.function.ThrowingHelper; - -import static org.apache.camel.support.CamelContextHelper.findByType; -import static org.apache.camel.support.CamelContextHelper.lookup; class ServiceCallReifier extends ProcessorReifier<ServiceCallDefinition> { ServiceCallReifier(ProcessorDefinition<?> definition) { - super((ServiceCallDefinition) definition); + super(ServiceCallDefinition.class.cast(definition)); } - // ***************************** - // Processor Factory - // ***************************** - @Override public Processor createProcessor(RouteContext routeContext) throws Exception { - final CamelContext camelContext = routeContext.getCamelContext(); - final ServiceDiscovery serviceDiscovery = retrieveServiceDiscovery(camelContext); - final ServiceFilter serviceFilter = retrieveServiceFilter(camelContext); - final ServiceChooser serviceChooser = retrieveServiceChooser(camelContext); - final ServiceLoadBalancer loadBalancer = retrieveLoadBalancer(camelContext); - - CamelContextAware.trySetCamelContext(serviceDiscovery, camelContext); - CamelContextAware.trySetCamelContext(serviceFilter, camelContext); - CamelContextAware.trySetCamelContext(serviceChooser, camelContext); - CamelContextAware.trySetCamelContext(loadBalancer, camelContext); - - if (loadBalancer instanceof ServiceDiscoveryAware) { - ((ServiceDiscoveryAware) loadBalancer).setServiceDiscovery(serviceDiscovery); - } - if (loadBalancer instanceof ServiceFilterAware) { - ((ServiceFilterAware) loadBalancer).setServiceFilter(serviceFilter); - } - if (loadBalancer instanceof ServiceChooserAware) { - ((ServiceChooserAware) loadBalancer).setServiceChooser(serviceChooser); - } - - // 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 endpointScheme = definition.getComponent(); - if (endpointScheme == null) { - ServiceCallConfigurationDefinition conf = retrieveConfig(camelContext); - if (conf != null) { - endpointScheme = conf.getComponent(); - } - } - if (endpointScheme == null) { - ServiceCallConfigurationDefinition conf = retrieveDefaultConfig(camelContext); - if (conf != null) { - endpointScheme = conf.getComponent(); - } - } - - // The uri is used to tweak the uri. - // The uri configured on EIP takes precedence vs configured on configuration. - String endpointUri = definition.getUri(); - if (endpointUri == null) { - ServiceCallConfigurationDefinition conf = retrieveConfig(camelContext); - if (conf != null) { - endpointUri = conf.getUri(); - } - } - if (endpointUri == null) { - ServiceCallConfigurationDefinition conf = retrieveDefaultConfig(camelContext); - if (conf != null) { - endpointUri = conf.getUri(); - } - } - - // Service name is mandatory - ObjectHelper.notNull(definition.getName(), "Service name"); - - endpointScheme = ThrowingHelper.applyIfNotEmpty(endpointScheme, camelContext::resolvePropertyPlaceholders, () -> ServiceCallDefinitionConstants.DEFAULT_COMPONENT); - endpointUri = ThrowingHelper.applyIfNotEmpty(endpointUri, camelContext::resolvePropertyPlaceholders, () -> null); - - return new DefaultServiceCallProcessor( - camelContext, - camelContext.resolvePropertyPlaceholders(definition.getName()), - endpointScheme, - endpointUri, - definition.getPattern(), - loadBalancer, - retrieveExpression(camelContext, endpointScheme)); - } - - // ***************************** - // Helpers - // ***************************** - - private ServiceCallConfigurationDefinition retrieveDefaultConfig(CamelContext camelContext) { - // check if a default configuration is bound to the registry - ServiceCallConfigurationDefinition config = camelContext.adapt(ModelCamelContext.class).getServiceCallConfiguration(null); - - if (config == null) { - // Or if it is in the registry - config = lookup( - camelContext, - ServiceCallDefinitionConstants.DEFAULT_SERVICE_CALL_CONFIG_ID, - ServiceCallConfigurationDefinition.class); - } - - if (config == null) { - // 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 ServiceCallConfigurationDefinition retrieveConfig(CamelContext camelContext) { - ServiceCallConfigurationDefinition config = null; - if (definition.getConfigurationRef() != null) { - // lookup in registry firstNotNull - config = lookup(camelContext, definition.getConfigurationRef(), ServiceCallConfigurationDefinition.class); - if (config == null) { - // and fallback as service configuration - config = camelContext.adapt(ModelCamelContext.class).getServiceCallConfiguration(definition.getConfigurationRef()); - } - } - - 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 ServiceDiscovery retrieveServiceDiscovery(CamelContext camelContext) throws Exception { - return Suppliers.firstNotNull( - () -> (definition.getServiceDiscoveryConfiguration() != null) ? definition.getServiceDiscoveryConfiguration().newInstance(camelContext) : null, - // Local configuration - () -> retrieve(ServiceDiscovery.class, camelContext, definition::getServiceDiscovery, definition::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, ServiceCallDefinitionConstants.DEFAULT_SERVICE_DISCOVERY_ID, ServiceDiscovery.class) - ).orElseGet( - // Default, that's s little ugly but a load balancer may live without - // (i.e. the Ribbon one) so let's delegate the null check to the actual - // impl. - () -> null - ); - } - - // ****************************************** - // 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) { - 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 ServiceFilter retrieveServiceFilter(CamelContext camelContext) throws Exception { - return Suppliers.firstNotNull( - () -> (definition.getServiceFilterConfiguration() != null) ? definition.getServiceFilterConfiguration().newInstance(camelContext) : null, - // Local configuration - () -> retrieve(ServiceFilter.class, camelContext, definition::getServiceFilter, definition::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, ServiceCallDefinitionConstants.DEFAULT_SERVICE_FILTER_ID, ServiceFilter.class) - ).orElseGet( - // 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 (answer == null) { - String ref = config.getServiceChooserRef(); - if (ObjectHelper.equal("roundrobin", ref, true)) { - answer = new RoundRobinServiceChooser(); - } else if (ObjectHelper.equal("round-robin", ref, true)) { - answer = new RoundRobinServiceChooser(); - } else if (ObjectHelper.equal("random", ref, true)) { - answer = new RandomServiceChooser(); - } - } - } - - return answer; - } - - private ServiceChooser retrieveServiceChooser(CamelContext camelContext) throws Exception { - return Suppliers.firstNotNull( - // Local configuration - () -> retrieve(ServiceChooser.class, camelContext, definition::getServiceChooser, definition::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, ServiceCallDefinitionConstants.DEFAULT_SERVICE_CHOOSER_ID, ServiceChooser.class) - ).orElseGet( - // Default - () -> new RoundRobinServiceChooser() - ); - } - - // ****************************************** - // LoadBalancer - // ****************************************** - - private ServiceLoadBalancer retrieveLoadBalancer(CamelContext camelContext, Function<CamelContext, ServiceCallConfigurationDefinition> function) throws Exception { - ServiceLoadBalancer answer = null; - - ServiceCallConfigurationDefinition config = function.apply(camelContext); - if (config != null) { - if (config.getLoadBalancerConfiguration() != null) { - answer = config.getLoadBalancerConfiguration().newInstance(camelContext); - } else { - answer = retrieve( - ServiceLoadBalancer.class, - camelContext, - config::getLoadBalancer, - config::getLoadBalancerRef - ); - } - } - - return answer; - } - - private ServiceLoadBalancer retrieveLoadBalancer(CamelContext camelContext) throws Exception { - return Suppliers.firstNotNull( - () -> (definition.getLoadBalancerConfiguration() != null) ? definition.getLoadBalancerConfiguration().newInstance(camelContext) : null, - // Local configuration - () -> retrieve(ServiceLoadBalancer.class, camelContext, definition::getLoadBalancer, definition::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, ServiceLoadBalancer.class), - // From registry - () -> lookup(camelContext, ServiceCallDefinitionConstants.DEFAULT_LOAD_BALANCER_ID, ServiceLoadBalancer.class) - ).orElseGet( - // Default - () -> new DefaultServiceLoadBalancer() - ); - } - - // ****************************************** - // 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 - ); - } - } - - return answer; - } - - private Expression retrieveExpression(CamelContext camelContext, String component) throws Exception { - Optional<Expression> expression = Suppliers.firstNotNull( - () -> (definition.getExpressionConfiguration() != null) ? definition.getExpressionConfiguration().newInstance(camelContext) : null, - // Local configuration - () -> retrieve(Expression.class, camelContext, definition::getExpression, definition::getExpressionRef), - // Linked configuration - () -> retrieveExpression(camelContext, this::retrieveConfig), - // Default configuration - () -> retrieveExpression(camelContext, this::retrieveDefaultConfig), - // From registry - () -> lookup(camelContext, ServiceCallDefinitionConstants.DEFAULT_SERVICE_CALL_EXPRESSION_ID, Expression.class) - ); - - if (expression.isPresent()) { - return expression.get(); - } else { - String lookupName = component + "-service-expression"; - // First try to find the factory from the registry. - ServiceExpressionFactory factory = CamelContextHelper.lookup(camelContext, lookupName, ServiceExpressionFactory.class); - if (factory != null) { - // If a factory is found in the registry do not re-configure it as - // it should be pre-configured. - return factory.newInstance(camelContext); - } else { - - Class<?> type = null; - - try { - // Then use Service factory. - type = camelContext.getFactoryFinder(ServiceCallDefinitionConstants.RESOURCE_PATH).findClass(lookupName); - } catch (Exception e) { - } - - if (ObjectHelper.isNotEmpty(type)) { - if (ServiceExpressionFactory.class.isAssignableFrom(type)) { - factory = (ServiceExpressionFactory) camelContext.getInjector().newInstance(type); - } else { - throw new IllegalArgumentException( - "Resolving Expression: " + lookupName + " detected type conflict: Not a ServiceExpressionFactory implementation. Found: " + type.getName()); - } - } else { - // If no factory is found, returns the default - factory = context -> new DefaultServiceCallExpression(); - } - - return factory.newInstance(camelContext); - } - } - } - - // ************************************ - // Helpers - // ************************************ - - private <T> T retrieve(Class<T> type, CamelContext camelContext, Supplier<T> instanceSupplier, Supplier<String> refSupplier) { - T answer = null; - if (instanceSupplier != null) { - answer = instanceSupplier.get(); - } - - if (answer == null && refSupplier != null) { - String ref = refSupplier.get(); - if (ref != null) { - answer = lookup(camelContext, ref, type); - } - } - - return answer; + throw new IllegalStateException("Cannot find camel-cloud on the classpath."); } } diff --git a/core/pom.xml b/core/pom.xml index 08b7958..539c51c 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -40,13 +40,13 @@ <module>camel-support</module> <module>camel-caffeine-lrucache</module> <module>camel-headersmap</module> - <module>camel-cloud</module> <module>camel-management-api</module> <module>camel-management-impl</module> <module>camel-base</module> <module>camel-core</module> <module>camel-core-osgi</module> <module>camel-core-xml</module> + <module>camel-cloud</module> </modules> <build>
