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 2019/02/06 15:15:38 UTC

[camel-k] branch master updated: Renamed RuntimeTrait in ContextCustomizer since the name clashed and created confusion. Changed ContextCustomizer.apply(...) signature to pass a RuntimeRegistry in addition to CamelContext.

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.git


The following commit(s) were added to refs/heads/master by this push:
     new d956b23  Renamed RuntimeTrait in ContextCustomizer since the name clashed and created confusion. Changed ContextCustomizer.apply(...) signature to pass a RuntimeRegistry in addition to CamelContext.
d956b23 is described below

commit d956b23252148ac8be93544d347ba1221d3c0708
Author: Andrea Tarocchi <at...@redhat.com>
AuthorDate: Wed Feb 6 15:35:29 2019 +0100

    Renamed RuntimeTrait in ContextCustomizer since the name clashed and created confusion.
    Changed ContextCustomizer.apply(...) signature to pass a RuntimeRegistry in addition to CamelContext.
---
 .../main/java/org/apache/camel/k/Constants.java    |  5 ++--
 .../{RuntimeTrait.java => ContextCustomizer.java}  |  7 +++--
 .../org/apache/camel/k/support/RuntimeSupport.java | 30 +++++++++++----------
 .../java/org/apache/camel/k/jvm/Application.java   | 11 ++++++--
 .../org/apache/camel/k/jvm/PropertiesTest.java     | 31 ++++++++++++----------
 .../k/jvm/{TestTrait.java => TestCustomizer.java}  |  7 ++---
 .../org/apache/camel/k/{trait => customizer}/test  |  2 +-
 .../apache/camel/k/spring/boot/Application.java    |  2 +-
 8 files changed, 56 insertions(+), 39 deletions(-)

diff --git a/runtime/camel-k-runtime-core/src/main/java/org/apache/camel/k/Constants.java b/runtime/camel-k-runtime-core/src/main/java/org/apache/camel/k/Constants.java
index e00fed4..d9d2d9d 100644
--- a/runtime/camel-k-runtime-core/src/main/java/org/apache/camel/k/Constants.java
+++ b/runtime/camel-k-runtime-core/src/main/java/org/apache/camel/k/Constants.java
@@ -20,13 +20,14 @@ public final class Constants {
     public static final String ENV_CAMEL_K_ROUTES = "CAMEL_K_ROUTES";
     public static final String ENV_CAMEL_K_CONF = "CAMEL_K_CONF";
     public static final String ENV_CAMEL_K_CONF_D = "CAMEL_K_CONF_D";
-    public static final String ENV_CAMEL_K_TRAITS = "CAMEL_K_TRAITS";
+    public static final String ENV_CAMEL_K_CUSTOMIZERS = "CAMEL_K_CUSTOMIZERS";
     public static final String SCHEME_CLASSPATH = "classpath:";
     public static final String SCHEME_FILE = "file:";
     public static final String SCHEME_ENV = "env:";
     public static final String LOGGING_LEVEL_PREFIX = "logging.level.";
     public static final String ROUTES_LOADER_RESOURCE_PATH = "META-INF/services/org/apache/camel/k/loader/";
-    public static final String RUNTIME_TRAIT_RESOURCE_PATH = "META-INF/services/org/apache/camel/k/trait/";
+    public static final String CONTEXT_CUSTOMIZER_RESOURCE_PATH = "META-INF/services/org/apache/camel/k/customizer/";
+    public static final String PROPERTY_CAMEL_K_CUSTOMIZER = "camel.k.customizer";
 
     private Constants() {
     }
diff --git a/runtime/camel-k-runtime-core/src/main/java/org/apache/camel/k/RuntimeTrait.java b/runtime/camel-k-runtime-core/src/main/java/org/apache/camel/k/ContextCustomizer.java
similarity index 81%
rename from runtime/camel-k-runtime-core/src/main/java/org/apache/camel/k/RuntimeTrait.java
rename to runtime/camel-k-runtime-core/src/main/java/org/apache/camel/k/ContextCustomizer.java
index 2bc7868..4bcfcdc 100644
--- a/runtime/camel-k-runtime-core/src/main/java/org/apache/camel/k/RuntimeTrait.java
+++ b/runtime/camel-k-runtime-core/src/main/java/org/apache/camel/k/ContextCustomizer.java
@@ -19,9 +19,12 @@ package org.apache.camel.k;
 import org.apache.camel.CamelContext;
 
 @FunctionalInterface
-public interface RuntimeTrait {
+public interface ContextCustomizer {
     /**
      * Perform CamelContext customization.
+     *
+     * @param camelContext the camel context to customize.
+     * @param registry the runtime registry.
      */
-    void apply(CamelContext camelContext);
+    void apply(CamelContext camelContext, RuntimeRegistry registry);
 }
diff --git a/runtime/camel-k-runtime-core/src/main/java/org/apache/camel/k/support/RuntimeSupport.java b/runtime/camel-k-runtime-core/src/main/java/org/apache/camel/k/support/RuntimeSupport.java
index ec8d47e..4811f9f 100644
--- a/runtime/camel-k-runtime-core/src/main/java/org/apache/camel/k/support/RuntimeSupport.java
+++ b/runtime/camel-k-runtime-core/src/main/java/org/apache/camel/k/support/RuntimeSupport.java
@@ -24,7 +24,8 @@ import org.apache.camel.NoFactoryAvailableException;
 import org.apache.camel.component.properties.PropertiesComponent;
 import org.apache.camel.k.Constants;
 import org.apache.camel.k.RoutesLoader;
-import org.apache.camel.k.RuntimeTrait;
+import org.apache.camel.k.ContextCustomizer;
+import org.apache.camel.k.RuntimeRegistry;
 import org.apache.camel.k.Source;
 import org.apache.camel.spi.FactoryFinder;
 import org.apache.camel.spi.RestConfiguration;
@@ -33,37 +34,38 @@ import org.apache.camel.util.ObjectHelper;
 
 
 public final class RuntimeSupport {
+
     private RuntimeSupport() {
     }
 
-    public static void configureContext(CamelContext context) {
+    public static void configureContext(CamelContext context, RuntimeRegistry registry) {
         try {
-            FactoryFinder finder = context.getFactoryFinder(Constants.RUNTIME_TRAIT_RESOURCE_PATH);
-            String traitIDs = System.getenv().getOrDefault(Constants.ENV_CAMEL_K_TRAITS, "");
+            FactoryFinder finder = context.getFactoryFinder(Constants.CONTEXT_CUSTOMIZER_RESOURCE_PATH);
+            String customizerIDs = System.getenv().getOrDefault(Constants.ENV_CAMEL_K_CUSTOMIZERS, "");
 
-            if (ObjectHelper.isEmpty(traitIDs)) {
+            if (ObjectHelper.isEmpty(customizerIDs)) {
                 PropertiesComponent component = context.getComponent("properties", PropertiesComponent.class);
                 Properties properties = component.getInitialProperties();
 
-                traitIDs = properties.getProperty("camel.k.traits", "");
+                customizerIDs = properties.getProperty(Constants.PROPERTY_CAMEL_K_CUSTOMIZER, "");
             }
 
-            for (String traitId: traitIDs.split(",", -1)) {
-                configureContext(context, traitId, (RuntimeTrait)finder.newInstance(traitId));
+            for (String customizerId: customizerIDs.split(",", -1)) {
+                configureContext(context, customizerId, (ContextCustomizer)finder.newInstance(customizerId), registry);
             }
         } catch (NoFactoryAvailableException e) {
             // ignored
         }
 
-        //this is to initialize all traits that might be already present in the context injected by other means.
-        context.getRegistry().findByTypeWithName(RuntimeTrait.class).forEach(
-            (traitId, trait) -> configureContext(context, traitId, trait)
+        //this is to initialize all customizers that might be already present in the context injected by other means.
+        context.getRegistry().findByTypeWithName(ContextCustomizer.class).forEach(
+            (customizerId, customizer) -> configureContext(context, customizerId, customizer, registry)
         );
     }
 
-    public static void configureContext(CamelContext context, String traitId, RuntimeTrait trait) {
-        bindProperties(context, trait, "trait." + traitId + ".");
-        trait.apply(context);
+    public static void configureContext(CamelContext context, String customizerId, ContextCustomizer customizer, RuntimeRegistry registry) {
+        bindProperties(context, customizer, "customizer." + customizerId + ".");
+        customizer.apply(context, registry);
     }
 
     public static void configureRest(CamelContext context) {
diff --git a/runtime/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/Application.java b/runtime/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/Application.java
index b514485..e82f713 100644
--- a/runtime/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/Application.java
+++ b/runtime/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/Application.java
@@ -21,6 +21,7 @@ import javax.xml.bind.JAXBException;
 import org.apache.camel.CamelContext;
 import org.apache.camel.Component;
 import org.apache.camel.k.Constants;
+import org.apache.camel.k.RuntimeRegistry;
 import org.apache.camel.k.support.RuntimeSupport;
 import org.apache.camel.main.MainListenerSupport;
 import org.apache.camel.main.MainSupport;
@@ -68,7 +69,7 @@ public class Application {
         Runtime runtime = new Runtime();
         runtime.setProperties(ApplicationSupport.loadProperties());
         runtime.load(routes.split(",", -1));
-        runtime.addMainListener(new ComponentPropertiesBinder());
+        runtime.addMainListener(new ComponentPropertiesBinder(runtime.getRegistry()));
         runtime.addMainListener(new RoutesDumper());
         runtime.run();
     }
@@ -81,6 +82,12 @@ public class Application {
 
     static class ComponentPropertiesBinder extends MainListenerSupport {
 
+        private RuntimeRegistry registry;
+
+        public ComponentPropertiesBinder(RuntimeRegistry registry){
+            this.registry = registry;
+        }
+
         @Override
         public void configure(CamelContext context) {
             //
@@ -103,7 +110,7 @@ public class Application {
             // This is useful to configure services such as the ClusterService,
             // RouteController, etc
             //
-            RuntimeSupport.configureContext(context);
+            RuntimeSupport.configureContext(context, registry);
 
             //
             // Configure components upon creation
diff --git a/runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/PropertiesTest.java b/runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/PropertiesTest.java
index 74931fd..e618860 100644
--- a/runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/PropertiesTest.java
+++ b/runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/PropertiesTest.java
@@ -19,8 +19,11 @@ package org.apache.camel.k.jvm;
 import java.util.Properties;
 import java.util.concurrent.ThreadLocalRandom;
 
+import org.apache.camel.CamelContext;
 import org.apache.camel.component.seda.SedaComponent;
-import org.apache.camel.k.RuntimeTrait;
+import org.apache.camel.k.Constants;
+import org.apache.camel.k.ContextCustomizer;
+import org.apache.camel.k.RuntimeRegistry;
 import org.junit.jupiter.api.Test;
 
 import static org.apache.camel.k.jvm.RuntimeTestSupport.afterStart;
@@ -35,7 +38,7 @@ public class PropertiesTest {
         Runtime runtime = new Runtime();
         runtime.setProperties(properties);
         runtime.setDuration(5);
-        runtime.addMainListener(new Application.ComponentPropertiesBinder());
+        runtime.addMainListener(new Application.ComponentPropertiesBinder(runtime.getRegistry()));
         runtime.addMainListener(afterStart((main, context) -> {
             assertThat(context.resolvePropertyPlaceholders("{{root.key}}")).isEqualTo("root.value");
             assertThat(context.resolvePropertyPlaceholders("{{001.key}}")).isEqualTo("001.value");
@@ -55,7 +58,7 @@ public class PropertiesTest {
             Runtime runtime = new Runtime();
             runtime.setProperties(System.getProperties());
             runtime.setDuration(5);
-            runtime.addMainListener(new Application.ComponentPropertiesBinder());
+            runtime.addMainListener(new Application.ComponentPropertiesBinder(runtime.getRegistry()));
             runtime.addMainListener(afterStart((main, context) -> {
                 String value = context.resolvePropertyPlaceholders("{{my.property}}");
 
@@ -82,7 +85,7 @@ public class PropertiesTest {
             runtime.setProperties(System.getProperties());
             runtime.setDuration(5);
             runtime.getRegistry().bind("my-seda", new SedaComponent());
-            runtime.addMainListener(new Application.ComponentPropertiesBinder());
+            runtime.addMainListener(new Application.ComponentPropertiesBinder(runtime.getRegistry()));
             runtime.addMainListener(afterStart((main, context) -> {
                 assertThat(context.getComponent("seda", true)).hasFieldOrPropertyWithValue("queueSize", queueSize1);
                 assertThat(context.getComponent("my-seda", true)).hasFieldOrPropertyWithValue("queueSize", queueSize2);
@@ -105,7 +108,7 @@ public class PropertiesTest {
             Runtime runtime = new Runtime();
             runtime.setProperties(System.getProperties());
             runtime.setDuration(5);
-            runtime.addMainListener(new Application.ComponentPropertiesBinder());
+            runtime.addMainListener(new Application.ComponentPropertiesBinder(runtime.getRegistry()));
             runtime.addMainListener(afterStart((main, context) -> {
                 assertThat(context.isMessageHistory()).isFalse();
                 assertThat(context.isLoadTypeConverters()).isFalse();
@@ -120,14 +123,14 @@ public class PropertiesTest {
     }
 
     @Test
-    public void testContextTraitFromProperty() throws Exception {
-        System.setProperty("camel.k.traits", "test");
-        System.setProperty("trait.test.messageHistory", "false");
+    public void testContextCustomizerFromProperty() throws Exception {
+        System.setProperty(Constants.PROPERTY_CAMEL_K_CUSTOMIZER, "test");
+        System.setProperty("customizer.test.messageHistory", "false");
 
         Runtime runtime = new Runtime();
         runtime.setProperties(System.getProperties());
         runtime.setDuration(5);
-        runtime.addMainListener(new Application.ComponentPropertiesBinder());
+        runtime.addMainListener(new Application.ComponentPropertiesBinder(runtime.getRegistry()));
         runtime.addMainListener(afterStart((main, context) -> {
             assertThat(context.isMessageHistory()).isFalse();
             assertThat(context.isLoadTypeConverters()).isFalse();
@@ -138,15 +141,15 @@ public class PropertiesTest {
     }
 
     @Test
-    public void testContextTraitFromRegistry() throws Exception {
+    public void testContextCustomizerFromRegistry() throws Exception {
         Runtime runtime = new Runtime();
         runtime.setProperties(System.getProperties());
         runtime.setDuration(5);
-        runtime.getRegistry().bind("c1", (RuntimeTrait) context -> {
-            context.setMessageHistory(false);
-            context.setLoadTypeConverters(false);
+        runtime.getRegistry().bind("c1", (ContextCustomizer) (camelContext, registry) -> {
+            camelContext.setMessageHistory(false);
+            camelContext.setLoadTypeConverters(false);
         });
-        runtime.addMainListener(new Application.ComponentPropertiesBinder());
+        runtime.addMainListener(new Application.ComponentPropertiesBinder(runtime.getRegistry()));
         runtime.addMainListener(afterStart((main, context) -> {
             assertThat(context.isMessageHistory()).isFalse();
             assertThat(context.isLoadTypeConverters()).isFalse();
diff --git a/runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/TestTrait.java b/runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/TestCustomizer.java
similarity index 84%
rename from runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/TestTrait.java
rename to runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/TestCustomizer.java
index cefbe12..1cf0d75 100644
--- a/runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/TestTrait.java
+++ b/runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/TestCustomizer.java
@@ -17,9 +17,10 @@
 package org.apache.camel.k.jvm;
 
 import org.apache.camel.CamelContext;
-import org.apache.camel.k.RuntimeTrait;
+import org.apache.camel.k.ContextCustomizer;
+import org.apache.camel.k.RuntimeRegistry;
 
-public class TestTrait implements RuntimeTrait {
+public class TestCustomizer implements ContextCustomizer {
     private boolean messageHistory = true;
 
     public boolean isMessageHistory() {
@@ -31,7 +32,7 @@ public class TestTrait implements RuntimeTrait {
     }
 
     @Override
-    public void apply(CamelContext camelContext) {
+    public void apply(CamelContext camelContext, RuntimeRegistry registry) {
         camelContext.setMessageHistory(messageHistory);
         camelContext.setLoadTypeConverters(false);
     }
diff --git a/runtime/camel-k-runtime-jvm/src/test/resources/META-INF/services/org/apache/camel/k/trait/test b/runtime/camel-k-runtime-jvm/src/test/resources/META-INF/services/org/apache/camel/k/customizer/test
similarity index 94%
rename from runtime/camel-k-runtime-jvm/src/test/resources/META-INF/services/org/apache/camel/k/trait/test
rename to runtime/camel-k-runtime-jvm/src/test/resources/META-INF/services/org/apache/camel/k/customizer/test
index 9f0a363..dc0dd22 100644
--- a/runtime/camel-k-runtime-jvm/src/test/resources/META-INF/services/org/apache/camel/k/trait/test
+++ b/runtime/camel-k-runtime-jvm/src/test/resources/META-INF/services/org/apache/camel/k/customizer/test
@@ -15,4 +15,4 @@
 # limitations under the License.
 #
 
-class=org.apache.camel.k.jvm.TestTrait
\ No newline at end of file
+class=org.apache.camel.k.jvm.TestCustomizer
\ No newline at end of file
diff --git a/runtime/camel-k-runtime-spring-boot/src/main/java/org/apache/camel/k/spring/boot/Application.java b/runtime/camel-k-runtime-spring-boot/src/main/java/org/apache/camel/k/spring/boot/Application.java
index 91839aa..bfc5b3b 100644
--- a/runtime/camel-k-runtime-spring-boot/src/main/java/org/apache/camel/k/spring/boot/Application.java
+++ b/runtime/camel-k-runtime-spring-boot/src/main/java/org/apache/camel/k/spring/boot/Application.java
@@ -99,7 +99,7 @@ public class Application {
                 // This is useful to configure services such as the ClusterService,
                 // RouteController, etc
                 //
-                RuntimeSupport.configureContext( context);
+                RuntimeSupport.configureContext(context, registry);
 
                 try {
                     for (String route : routes.split(",")) {