You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2019/01/24 13:07:30 UTC

[camel-k] branch master updated: runtime: improve discovery and configuration of runtime traits

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

acosentino 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 c03f531  runtime: improve discovery and configuration of runtime traits
     new b06cc94  Merge pull request #371 from lburgazzoli/runtime-traits
c03f531 is described below

commit c03f5317da7840a2f2124449b4ea230f9a4ac5fe
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Thu Jan 24 13:43:26 2019 +0100

    runtime: improve discovery and configuration of runtime traits
---
 .../org/apache/camel/k/support/RuntimeSupport.java | 23 ++++++++++------
 .../org/apache/camel/k/jvm/PropertiesTest.java     | 20 +++++++++++++-
 .../java/org/apache/camel/k/jvm/RuntimeTest.java   |  2 +-
 .../org/apache/camel/k/jvm/RuntimeTestSupport.java |  1 +
 .../{RuntimeTestSupport.java => TestTrait.java}    | 31 ++++++++++------------
 .../services/org/apache/camel/k/trait/test         | 18 +++++++++++++
 6 files changed, 68 insertions(+), 27 deletions(-)

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 9a35041..7bee678 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
@@ -29,6 +29,7 @@ import org.apache.camel.k.Source;
 import org.apache.camel.spi.FactoryFinder;
 import org.apache.camel.spi.RestConfiguration;
 import org.apache.camel.util.IntrospectionSupport;
+import org.apache.camel.util.ObjectHelper;
 
 
 public final class RuntimeSupport {
@@ -40,24 +41,30 @@ public final class RuntimeSupport {
             FactoryFinder finder = context.getFactoryFinder(Constants.RUNTIME_TRAIT_RESOURCE_PATH);
             String traitIDs = System.getenv().getOrDefault(Constants.ENV_CAMEL_K_TRAITS, "");
 
-            for (String traitId: traitIDs.split(",", -1)) {
-                RuntimeTrait trait = (RuntimeTrait)finder.newInstance(traitId);
+            if (ObjectHelper.isEmpty(traitIDs)) {
+                PropertiesComponent component = context.getComponent("properties", PropertiesComponent.class);
+                Properties properties = component.getInitialProperties();
 
-                bindProperties(context, trait, "trait." + traitId);
+                traitIDs = properties.getProperty("camel.k.traits", "");
+            }
 
-                trait.apply(context);
+            for (String traitId: traitIDs.split(",", -1)) {
+                configureContext(context, traitId, (RuntimeTrait)finder.newInstance(traitId));
             }
         } catch (NoFactoryAvailableException e) {
             // ignored
         }
 
-        context.getRegistry().findByType(RuntimeTrait.class).forEach(
-            customizer -> {
-                customizer.apply(context);
-            }
+        context.getRegistry().findByTypeWithName(RuntimeTrait.class).forEach(
+            (traitId, trait) -> configureContext(context, traitId, trait)
         );
     }
 
+    public static void configureContext(CamelContext context, String traitId, RuntimeTrait trait) {
+        bindProperties(context, trait, "trait." + traitId + ".");
+        trait.apply(context);
+    }
+
     public static void configureRest(CamelContext context) {
         RestConfiguration configuration = new RestConfiguration();
 
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 808664c..74931fd 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
@@ -120,7 +120,25 @@ public class PropertiesTest {
     }
 
     @Test
-    public void testContextTrait() throws Exception {
+    public void testContextTraitFromProperty() throws Exception {
+        System.setProperty("camel.k.traits", "test");
+        System.setProperty("trait.test.messageHistory", "false");
+
+        Runtime runtime = new Runtime();
+        runtime.setProperties(System.getProperties());
+        runtime.setDuration(5);
+        runtime.addMainListener(new Application.ComponentPropertiesBinder());
+        runtime.addMainListener(afterStart((main, context) -> {
+            assertThat(context.isMessageHistory()).isFalse();
+            assertThat(context.isLoadTypeConverters()).isFalse();
+            main.stop();
+        }));
+
+        runtime.run();
+    }
+
+    @Test
+    public void testContextTraitFromRegistry() throws Exception {
         Runtime runtime = new Runtime();
         runtime.setProperties(System.getProperties());
         runtime.setDuration(5);
diff --git a/runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/RuntimeTest.java b/runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/RuntimeTest.java
index afb9043..24cd457 100644
--- a/runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/RuntimeTest.java
+++ b/runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/RuntimeTest.java
@@ -55,7 +55,7 @@ public class RuntimeTest {
 
 
     @Test
-    void testLoadRouteAndrest() throws Exception {
+    void testLoadRouteAndRest() throws Exception {
         Runtime runtime = new Runtime();
         runtime.addMainListener(new Application.RoutesDumper());
         runtime.load(new String[]{
diff --git a/runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/RuntimeTestSupport.java b/runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/RuntimeTestSupport.java
index d72dddd..0918705 100644
--- a/runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/RuntimeTestSupport.java
+++ b/runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/RuntimeTestSupport.java
@@ -38,4 +38,5 @@ public final class RuntimeTestSupport {
             }
         };
     }
+
 }
diff --git a/runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/RuntimeTestSupport.java b/runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/TestTrait.java
similarity index 54%
copy from runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/RuntimeTestSupport.java
copy to runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/TestTrait.java
index d72dddd..cefbe12 100644
--- a/runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/RuntimeTestSupport.java
+++ b/runtime/camel-k-runtime-jvm/src/test/java/org/apache/camel/k/jvm/TestTrait.java
@@ -17,25 +17,22 @@
 package org.apache.camel.k.jvm;
 
 import org.apache.camel.CamelContext;
-import org.apache.camel.main.MainListener;
-import org.apache.camel.main.MainListenerSupport;
-import org.apache.camel.main.MainSupport;
-import org.apache.camel.util.function.ThrowingBiConsumer;
+import org.apache.camel.k.RuntimeTrait;
 
-public final class RuntimeTestSupport {
-    private RuntimeTestSupport() {
+public class TestTrait implements RuntimeTrait {
+    private boolean messageHistory = true;
+
+    public boolean isMessageHistory() {
+        return messageHistory;
+    }
+
+    public void setMessageHistory(boolean messageHistory) {
+        this.messageHistory = messageHistory;
     }
 
-    public static MainListener afterStart(ThrowingBiConsumer<MainSupport, CamelContext, Exception> consumer) {
-        return new MainListenerSupport() {
-            @Override
-            public void afterStart(MainSupport main) {
-                try {
-                    consumer.accept(main, main.getCamelContexts().get(0));
-                } catch (Exception e) {
-                    throw new RuntimeException(e);
-                }
-            }
-        };
+    @Override
+    public void apply(CamelContext camelContext) {
+        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/trait/test
new file mode 100644
index 0000000..9f0a363
--- /dev/null
+++ b/runtime/camel-k-runtime-jvm/src/test/resources/META-INF/services/org/apache/camel/k/trait/test
@@ -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.k.jvm.TestTrait
\ No newline at end of file