You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2020/04/17 15:43:19 UTC

[camel-k-runtime] 01/04: Set camel.k.customizer as base property for customizer #305

This is an automated email from the ASF dual-hosted git repository.

lburgazzoli pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel-k-runtime.git

commit b52c5111e6c8d94cc8b6de4f58491b4a7eaa8c4a
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Fri Apr 17 15:10:29 2020 +0200

    Set camel.k.customizer as base property for customizer #305
---
 .../org/apache/camel/k/main/PropertiesTest.java    | 23 ++++++++++++--
 .../main/java/org/apache/camel/k/Constants.java    |  7 +++-
 .../org/apache/camel/k/support/RuntimeSupport.java | 24 ++++++++++----
 .../apache/camel/k/support/RuntimeSupportTest.java | 37 +++++++++++++++++++---
 .../org/apache/camel/k/webhook/WebhookTest.java    | 12 +++----
 .../data/application.properties                    |  4 +--
 .../data/application.properties                    |  4 +--
 .../data/application.properties                    |  8 ++---
 8 files changed, 91 insertions(+), 28 deletions(-)

diff --git a/camel-k-main/camel-k-runtime-main/src/test/java/org/apache/camel/k/main/PropertiesTest.java b/camel-k-main/camel-k-runtime-main/src/test/java/org/apache/camel/k/main/PropertiesTest.java
index 46a31b0..90d5260 100644
--- a/camel-k-main/camel-k-runtime-main/src/test/java/org/apache/camel/k/main/PropertiesTest.java
+++ b/camel-k-main/camel-k-runtime-main/src/test/java/org/apache/camel/k/main/PropertiesTest.java
@@ -145,7 +145,26 @@ public class PropertiesTest {
     }
 
     @Test
-    public void testContextCustomizerFromProperty() throws Exception {
+    public void testContextCustomizerFromProperties() throws Exception {
+        Properties properties = new Properties();
+        properties.setProperty("camel.k.customizer.test.enabled", "true");
+        properties.setProperty("camel.k.customizer.test.message-history", "false");
+
+        ApplicationRuntime runtime = new ApplicationRuntime();
+        runtime.setProperties(properties);
+        runtime.addListener(new ContextConfigurer());
+        runtime.addListener(Runtime.Phase.Started, r -> {
+            CamelContext context = r.getCamelContext();
+            assertThat(context.isMessageHistory()).isFalse();
+            assertThat(context.isLoadTypeConverters()).isFalse();
+            runtime.stop();
+        });
+
+        runtime.run();
+    }
+
+    @Test
+    public void testContextCustomizerFromPropertiesFallback() throws Exception {
         Properties properties = new Properties();
         properties.setProperty("customizer.test.enabled", "true");
         properties.setProperty("customizer.test.message-history", "false");
@@ -166,7 +185,7 @@ public class PropertiesTest {
     @Test
     public void testContextCustomizerFromRegistry() throws Exception {
         Properties properties = new Properties();
-        properties.setProperty("customizer.c1.enabled", "true");
+        properties.setProperty("camel.k.customizer.c1.enabled", "true");
 
         ApplicationRuntime runtime = new ApplicationRuntime();
         runtime.setProperties(properties);
diff --git a/camel-k-runtime-core/src/main/java/org/apache/camel/k/Constants.java b/camel-k-runtime-core/src/main/java/org/apache/camel/k/Constants.java
index 81fc3af..ed848fd 100644
--- a/camel-k-runtime-core/src/main/java/org/apache/camel/k/Constants.java
+++ b/camel-k-runtime-core/src/main/java/org/apache/camel/k/Constants.java
@@ -49,10 +49,15 @@ public final class Constants {
     public static final String SOURCE_LOADER_INTERCEPTOR_RESOURCE_PATH = "META-INF/services/org/apache/camel/k/loader/interceptor/";
     public static final String CONTEXT_CUSTOMIZER_RESOURCE_PATH = "META-INF/services/org/apache/camel/k/customizer/";
 
-    public static final String ENABLE_CUSTOMIZER_PATTERN = "customizer.([\\w][\\w-]*).enabled";
+    public static final String ENABLE_CUSTOMIZER_PATTERN = "(camel\\.k\\.)?customizer\\.([\\w][\\w-]*)\\.enabled";
+
     public static final String PROPERTY_PREFIX_REST_COMPONENT_PROPERTY = "camel.rest.componentProperty.";
     public static final String PROPERTY_PREFIX_REST_ENDPOINT_PROPERTY = "camel.rest.endpointProperty.";
 
+    public static final String CUSTOMIZER_PREFIX = "camel.k.customizer.";
+    public static final String CUSTOMIZER_PREFIX_FALLBACK = "customizer.";
+
+
     private Constants() {
     }
 }
diff --git a/camel-k-runtime-core/src/main/java/org/apache/camel/k/support/RuntimeSupport.java b/camel-k-runtime-core/src/main/java/org/apache/camel/k/support/RuntimeSupport.java
index 0d01404..04a58b1 100644
--- a/camel-k-runtime-core/src/main/java/org/apache/camel/k/support/RuntimeSupport.java
+++ b/camel-k-runtime-core/src/main/java/org/apache/camel/k/support/RuntimeSupport.java
@@ -64,7 +64,9 @@ public final class RuntimeSupport {
             .forEach(e -> {
                 LOGGER.info("Apply ContextCustomizer with id={} and type={}", e.getKey(), e.getValue().getClass().getName());
 
-                PropertiesSupport.bindProperties(context, e.getValue(), "customizer." + e.getKey() + ".");
+                PropertiesSupport.bindProperties(context, e.getValue(), Constants.CUSTOMIZER_PREFIX + e.getKey() + ".");
+                PropertiesSupport.bindProperties(context, e.getValue(), Constants.CUSTOMIZER_PREFIX_FALLBACK + e.getKey() + ".");
+
                 e.getValue().apply(context);
 
                 appliedCustomizers.add(e.getValue());
@@ -75,7 +77,7 @@ public final class RuntimeSupport {
 
     public static Map<String, ContextCustomizer> lookupCustomizers(CamelContext context) {
         Map<String, ContextCustomizer> customizers = new ConcurrentHashMap<>();
-        Properties properties = context.getPropertiesComponent().loadProperties(n -> n.startsWith("customizer."));
+        Properties properties = context.getPropertiesComponent().loadProperties(n -> n.startsWith(Constants.CUSTOMIZER_PREFIX) || n.startsWith(Constants.CUSTOMIZER_PREFIX_FALLBACK));
 
         if (properties != null) {
             //
@@ -96,13 +98,21 @@ public final class RuntimeSupport {
                     final Object val = entry.getValue();
                     final Matcher matcher = pattern.matcher(key);
 
-                    if (matcher.matches() && matcher.groupCount() == 1) {
-                        if (Boolean.valueOf(String.valueOf(val))) {
+                    if (matcher.matches()) {
+                        String customizerId = null;
+
+                        if (matcher.groupCount() == 1) {
+                            customizerId = matcher.group(1);
+                        } else if (matcher.groupCount() == 2) {
+                            customizerId = matcher.group(2);
+                        }
+
+                        if (customizerId != null && Boolean.parseBoolean(String.valueOf(val))) {
                             //
                             // Do not override customizers eventually found
                             // in the registry
                             //
-                            customizers.computeIfAbsent(matcher.group(1), id -> lookupCustomizerByID(context, id));
+                            customizers.computeIfAbsent(customizerId, id -> lookupCustomizerByID(context, id));
                         }
                     }
                 });
@@ -250,7 +260,7 @@ public final class RuntimeSupport {
     public static Optional<RoutesBuilder> beforeConfigure(Optional<RoutesBuilder> builder, Consumer<RouteBuilder> consumer) {
         return builder.map(b -> {
             if (b instanceof RouteBuilder) {
-                RouteBuilder.class.cast(b).addLifecycleInterceptor(beforeConfigure(consumer));
+                ((RouteBuilder) b).addLifecycleInterceptor(beforeConfigure(consumer));
             }
             return b;
         });
@@ -268,7 +278,7 @@ public final class RuntimeSupport {
     public static Optional<RoutesBuilder> afterConfigure(Optional<RoutesBuilder> builder, Consumer<RouteBuilder> consumer) {
         return builder.map(b -> {
             if (b instanceof RouteBuilder) {
-                RouteBuilder.class.cast(b).addLifecycleInterceptor(afterConfigure(consumer));
+                ((RouteBuilder) b).addLifecycleInterceptor(afterConfigure(consumer));
             }
             return b;
         });
diff --git a/camel-k-runtime-core/src/test/java/org/apache/camel/k/support/RuntimeSupportTest.java b/camel-k-runtime-core/src/test/java/org/apache/camel/k/support/RuntimeSupportTest.java
index 4757321..962cc05 100644
--- a/camel-k-runtime-core/src/test/java/org/apache/camel/k/support/RuntimeSupportTest.java
+++ b/camel-k-runtime-core/src/test/java/org/apache/camel/k/support/RuntimeSupportTest.java
@@ -44,7 +44,7 @@ public class RuntimeSupportTest {
         assertThat(customizers).hasSize(0);
 
         Properties properties = new Properties();
-        properties.setProperty("customizer.name.enabled", "true");
+        properties.setProperty("camel.k.customizer.name.enabled", "true");
         context.getPropertiesComponent().setInitialProperties(properties);
 
         customizers = RuntimeSupport.configureContextCustomizers(context);
@@ -85,6 +85,34 @@ public class RuntimeSupportTest {
         assertThat(customizers).hasSize(0);
 
         Properties properties = new Properties();
+        properties.setProperty("camel.k.customizer.name.enabled", "true");
+        context.getPropertiesComponent().setInitialProperties(properties);
+
+        customizers = RuntimeSupport.configureContextCustomizers(context);
+        assertThat(context.getName()).isEqualTo("default");
+        assertThat(customizers).hasSize(1);
+
+        properties.setProperty("camel.k.customizer.converters.enabled", "true");
+        context.getPropertiesComponent().setInitialProperties(properties);
+
+        customizers = RuntimeSupport.configureContextCustomizers(context);
+        assertThat(context.getName()).isEqualTo("default");
+        assertThat(context.isLoadTypeConverters()).isTrue();
+        assertThat(customizers).hasSize(2);
+    }
+
+    @Test
+    public void testLoadCustomizersFallback() {
+        CamelContext context = new DefaultCamelContext();
+        context.getRegistry().bind("converters", (ContextCustomizer)camelContext -> camelContext.setLoadTypeConverters(true));
+
+        List<ContextCustomizer> customizers = RuntimeSupport.configureContextCustomizers(context);
+        assertThat(context.getName()).isNotEqualTo("from-registry");
+        assertThat(context.getName()).isNotEqualTo("default");
+        assertThat(context.isLoadTypeConverters()).isFalse();
+        assertThat(customizers).hasSize(0);
+
+        Properties properties = new Properties();
         properties.setProperty("customizer.name.enabled", "true");
         context.getPropertiesComponent().setInitialProperties(properties);
 
@@ -93,6 +121,7 @@ public class RuntimeSupportTest {
         assertThat(customizers).hasSize(1);
 
         properties.setProperty("customizer.converters.enabled", "true");
+        properties.setProperty("customizer.converters.enabled", "true");
         context.getPropertiesComponent().setInitialProperties(properties);
 
         customizers = RuntimeSupport.configureContextCustomizers(context);
@@ -135,9 +164,9 @@ public class RuntimeSupportTest {
         });
 
         Properties properties = new Properties();
-        properties.setProperty("customizer.c1.enabled", "true");
-        properties.setProperty("customizer.c2.enabled", "true");
-        properties.setProperty("customizer.c3.enabled", "true");
+        properties.setProperty("camel.k.customizer.c1.enabled", "true");
+        properties.setProperty("camel.k.customizer.c2.enabled", "true");
+        properties.setProperty("camel.k.customizer.c3.enabled", "true");
 
         context.getPropertiesComponent().setInitialProperties(properties);
 
diff --git a/camel-k-runtime-webhook/src/test/java/org/apache/camel/k/webhook/WebhookTest.java b/camel-k-runtime-webhook/src/test/java/org/apache/camel/k/webhook/WebhookTest.java
index e6c1338..5c65628 100644
--- a/camel-k-runtime-webhook/src/test/java/org/apache/camel/k/webhook/WebhookTest.java
+++ b/camel-k-runtime-webhook/src/test/java/org/apache/camel/k/webhook/WebhookTest.java
@@ -60,8 +60,8 @@ public class WebhookTest {
     public void testWebhookRegistration(WebhookAction action) throws Exception {
         Properties properties = new Properties();
         properties.setProperty("camel.component.webhook.configuration.webhook-auto-register", "false");
-        properties.setProperty("customizer.webhook.enabled", "true");
-        properties.setProperty("customizer.webhook.action", action.name().toLowerCase());
+        properties.setProperty("camel.k.customizer.webhook.enabled", "true");
+        properties.setProperty("camel.k.customizer.webhook.action", action.name().toLowerCase());
         runtime.setProperties(properties);
 
         CountDownLatch operation = new CountDownLatch(1);
@@ -126,8 +126,8 @@ public class WebhookTest {
     public void testRegistrationFailure(WebhookAction action) throws Exception {
         Properties properties = new Properties();
         properties.setProperty("camel.component.webhook.configuration.webhook-auto-register", "false");
-        properties.setProperty("customizer.webhook.enabled", "true");
-        properties.setProperty("customizer.webhook.action", action.name());
+        properties.setProperty("camel.k.customizer.webhook.enabled", "true");
+        properties.setProperty("camel.k.customizer.webhook.action", action.name());
         runtime.setProperties(properties);
 
         runtime.getCamelContext().addComponent(
@@ -147,8 +147,8 @@ public class WebhookTest {
     @Test
     public void testAutoRegistrationNotDisabled() throws Exception {
         Properties properties = new Properties();
-        properties.setProperty("customizer.webhook.enabled", "true");
-        properties.setProperty("customizer.webhook.action", WebhookAction.REGISTER.name());
+        properties.setProperty("camel.k.customizer.webhook.enabled", "true");
+        properties.setProperty("camel.k.customizer.webhook.action", WebhookAction.REGISTER.name());
         runtime.setProperties(properties);
 
         runtime.getCamelContext().addComponent(
diff --git a/examples/camel-k-runtime-example-api/data/application.properties b/examples/camel-k-runtime-example-api/data/application.properties
index bf3307d..5493f7e 100644
--- a/examples/camel-k-runtime-example-api/data/application.properties
+++ b/examples/camel-k-runtime-example-api/data/application.properties
@@ -35,5 +35,5 @@ camel.context.rest-configuration.api-context-path=/api-doc
 #
 # Camel K
 #
-customizer.platform-http.enabled = true
-customizer.platform-http.bind-port = 8080
+camel.k.customizer.platform-http.enabled = true
+camel.k.customizer.platform-http.bind-port = 8080
diff --git a/examples/camel-k-runtime-example-knative/data/application.properties b/examples/camel-k-runtime-example-knative/data/application.properties
index 1dc4084..19ea10d 100644
--- a/examples/camel-k-runtime-example-knative/data/application.properties
+++ b/examples/camel-k-runtime-example-knative/data/application.properties
@@ -23,8 +23,8 @@ logging.level.org.apache.camel.k = DEBUG
 #
 # camel-k
 #
-customizer.platform-http.enabled = true
-customizer.platform-http.bind-port = 8080
+camel.k.customizer.platform-http.enabled = true
+camel.k.customizer.platform-http.bind-port = 8080
 
 #
 # camel - main
diff --git a/examples/camel-k-runtime-example-rest/data/application.properties b/examples/camel-k-runtime-example-rest/data/application.properties
index 59e7a2d..34ca888 100644
--- a/examples/camel-k-runtime-example-rest/data/application.properties
+++ b/examples/camel-k-runtime-example-rest/data/application.properties
@@ -30,11 +30,11 @@ camel.main.stream-caching-spool-directory = ${java.io.tmpdir}/camel-k
 #
 # camel-k - platform http
 #
-customizer.platform-http.enabled = true
-customizer.platform-http.bind-port = 8080
-customizer.platform-http.cors.enabled = true
+camel.k.customizer.platform-http.enabled = true
+camel.k.customizer.platform-http.bind-port = 8080
+camel.k.customizer.platform-http.cors.enabled = true
 
 #
 # camel-k - health checks
 #
-customizer.health.enabled = true
\ No newline at end of file
+camel.k.customizer.health.enabled = true
\ No newline at end of file