oscerd closed pull request #178: configure camel context from properties URL: https://github.com/apache/camel-k/pull/178
This is a PR merged from a forked repository. As GitHub hides the original diff on merge, it is displayed below for the sake of provenance: As this is a foreign pull request (from a fork), the diff is supplied below (as it won't show otherwise due to GitHub magic): diff --git a/runtime/jvm/src/main/java/org/apache/camel/k/jvm/Application.java b/runtime/jvm/src/main/java/org/apache/camel/k/jvm/Application.java index c756047..0c22f03 100644 --- a/runtime/jvm/src/main/java/org/apache/camel/k/jvm/Application.java +++ b/runtime/jvm/src/main/java/org/apache/camel/k/jvm/Application.java @@ -16,13 +16,10 @@ */ package org.apache.camel.k.jvm; -import java.util.Properties; - import org.apache.camel.CamelContext; import org.apache.camel.Component; import org.apache.camel.main.MainListenerSupport; import org.apache.camel.support.LifecycleStrategySupport; -import org.apache.camel.util.IntrospectionSupport; import org.apache.camel.util.ObjectHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -77,39 +74,25 @@ public static void main(String[] args) throws Exception { static class ComponentPropertiesBinder extends MainListenerSupport { @Override public void configure(CamelContext context) { + // Configure the camel context using properties in the form: + // + // camel.context.${name} = ${value} + // + RuntimeSupport.bindProperties(context, "camel.context."); + context.addLifecycleStrategy(new LifecycleStrategySupport() { @SuppressWarnings("unchecked") @Override public void onComponentAdd(String name, Component component) { - // Integration properties are defined as system properties - final Properties properties = System.getProperties(); - - // Set the prefix used by setProperties to filter - // and apply properties to match the one used by - // camel spring boot: + // The prefix that identifies component properties is the + // same one used by camel-spring-boot to configure components + // using starters: // - // camel.component.${scheme}.${value} + // camel.component.${scheme}.${name} = ${value} // - final String prefix = "camel.component." + name + "."; - - properties.entrySet().stream() - .filter(entry -> entry.getKey() instanceof String) - .filter(entry -> entry.getValue() != null) - .filter(entry -> ((String)entry.getKey()).startsWith(prefix)) - .forEach(entry -> { - final String key = ((String)entry.getKey()).substring(prefix.length()); - final Object val = entry.getValue(); - - try { - IntrospectionSupport.setProperty(component, key, val, false); - } catch (Exception ex) { - throw new RuntimeException(ex); - } - } - ); + RuntimeSupport.bindProperties(component, "camel.component." + name + "."); } }); } } - } diff --git a/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RuntimeSupport.java b/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RuntimeSupport.java index 6210b54..5826204 100644 --- a/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RuntimeSupport.java +++ b/runtime/jvm/src/main/java/org/apache/camel/k/jvm/RuntimeSupport.java @@ -28,6 +28,7 @@ import java.util.Objects; import java.util.Properties; +import org.apache.camel.util.IntrospectionSupport; import org.apache.camel.util.ObjectHelper; import org.apache.commons.io.FilenameUtils; import org.apache.logging.log4j.Level; @@ -107,4 +108,25 @@ public static void configureLogging() { } ); } + + public static void bindProperties(Object target, String prefix) { + // Integration properties are defined as system properties + final Properties properties = System.getProperties(); + + properties.entrySet().stream() + .filter(entry -> entry.getKey() instanceof String) + .filter(entry -> entry.getValue() != null) + .filter(entry -> ((String)entry.getKey()).startsWith(prefix)) + .forEach(entry -> { + final String key = ((String)entry.getKey()).substring(prefix.length()); + final Object val = entry.getValue(); + + try { + IntrospectionSupport.setProperty(target, key, val, false); + } catch (Exception ex) { + throw new RuntimeException(ex); + } + } + ); + } } diff --git a/runtime/jvm/src/test/java/org/apache/camel/k/jvm/PropertiesTest.java b/runtime/jvm/src/test/java/org/apache/camel/k/jvm/PropertiesTest.java index f77e417..ddca7a7 100644 --- a/runtime/jvm/src/test/java/org/apache/camel/k/jvm/PropertiesTest.java +++ b/runtime/jvm/src/test/java/org/apache/camel/k/jvm/PropertiesTest.java @@ -58,7 +58,6 @@ public void afterStart(MainSupport main) { } } - @Test public void testComponentConfiguration() throws Exception { int queueSize1 = ThreadLocalRandom.current().nextInt(10, 100); @@ -91,6 +90,40 @@ public void afterStart(MainSupport main) { runtime.run(); } finally { System.getProperties().remove("camel.component.seda.queueSize"); + System.getProperties().remove("camel.component.my-seda.queueSize"); + } + } + + @Test + public void testContextConfiguration() throws Exception { + System.setProperty("camel.context.messageHistory", "false"); + System.setProperty("camel.context.loadTypeConverters", "false"); + + try { + Runtime runtime = new Runtime(); + runtime.setDuration(5); + runtime.getRegistry().bind("my-seda", new SedaComponent()); + runtime.addMainListener(new Application.ComponentPropertiesBinder()); + runtime.addMainListener(new MainListenerSupport() { + @Override + public void afterStart(MainSupport main) { + try { + CamelContext context = main.getCamelContexts().get(0); + + assertThat(context.isMessageHistory()).isFalse(); + assertThat(context.isLoadTypeConverters()).isFalse(); + + main.stop(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + }); + + runtime.run(); + } finally { + System.getProperties().remove("camel.context.messageHistory"); + System.getProperties().remove("camel.context.loadTypeConverters"); } } } ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services