This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit ba504866ae28fd466bf9a78dd164602e76791164 Author: Claus Ibsen <[email protected]> AuthorDate: Thu Apr 14 17:13:47 2022 +0200 CAMEL-17969: camel-main - Property-placeholder summary --- .../org/apache/camel/main/BaseMainSupport.java | 2 + .../apache/camel/main/MainCommandLineSupport.java | 60 ++++++++++++++++++++++ .../apache/camel/dsl/jbang/core/commands/Run.java | 4 +- 3 files changed, 64 insertions(+), 2 deletions(-) diff --git a/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java b/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java index c5c8a16ebc7..9cc72fb8cd8 100644 --- a/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java +++ b/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java @@ -1662,6 +1662,8 @@ public abstract class BaseMainSupport extends BaseService { loc = "JVM System Property"; } else if ("ENV".equals(loc)) { loc = "OS Environment Variable"; + } else if ("arguments".equals(loc) || "CLI".equals(loc)) { + loc = "Command Line"; } loc = "[" + loc + "]"; loc = String.format("%-30s", loc); diff --git a/core/camel-main/src/main/java/org/apache/camel/main/MainCommandLineSupport.java b/core/camel-main/src/main/java/org/apache/camel/main/MainCommandLineSupport.java index 1b7c2f3999a..25bb1ef60b8 100644 --- a/core/camel-main/src/main/java/org/apache/camel/main/MainCommandLineSupport.java +++ b/core/camel-main/src/main/java/org/apache/camel/main/MainCommandLineSupport.java @@ -20,8 +20,14 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.LinkedList; import java.util.List; +import java.util.Map; +import java.util.Properties; import org.apache.camel.CamelConfiguration; +import org.apache.camel.CamelContext; +import org.apache.camel.spi.PropertiesComponent; +import org.apache.camel.spi.PropertiesSource; +import org.apache.camel.util.OrderedProperties; /** * Support for command line arguments to Camel main. @@ -29,6 +35,7 @@ import org.apache.camel.CamelConfiguration; public abstract class MainCommandLineSupport extends MainSupport { protected final List<Option> options = new ArrayList<>(); + protected Properties argumentProperties; private volatile boolean initOptionsDone; @SafeVarargs @@ -39,6 +46,38 @@ public abstract class MainCommandLineSupport extends MainSupport { public MainCommandLineSupport() { } + public Properties getArgumentProperties() { + return argumentProperties; + } + + /** + * Sets command line argument as properties for the properties component. + */ + public void setArgumentProperties(Properties argumentProperties) { + this.argumentProperties = argumentProperties; + } + + /** + * Sets command line argument as properties for the properties component. + */ + public void setArgumentProperties(Map<String, Object> initialProperties) { + this.argumentProperties = new OrderedProperties(); + this.argumentProperties.putAll(initialProperties); + } + + /** + * Adds a property (command line) for the properties component. + * + * @param key the property key + * @param value the property value + */ + public void addArgumentProperty(String key, String value) { + if (this.argumentProperties == null) { + this.argumentProperties = new OrderedProperties(); + } + this.argumentProperties.put(key, value); + } + protected void initOptions() { if (initOptionsDone) { return; @@ -179,6 +218,27 @@ public abstract class MainCommandLineSupport extends MainSupport { return getExitCode(); } + @Override + protected void configurePropertiesService(CamelContext camelContext) throws Exception { + super.configurePropertiesService(camelContext); + + PropertiesComponent pc = camelContext.getPropertiesComponent(); + if (argumentProperties != null && !argumentProperties.isEmpty()) { + // register source for command line arguments to be used for property placeholders + pc.addPropertiesSource(new PropertiesSource() { + @Override + public String getName() { + return "CLI"; + } + + @Override + public String getProperty(String name) { + return argumentProperties.getProperty(name); + } + }); + } + } + /** * Displays the header message for the command line options. */ diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java index 5df6f627f20..a8861b3ba3b 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/Run.java @@ -184,13 +184,13 @@ class Run implements Callable<Integer> { main.addInitialProperty("camel.main.tracing", trace ? "true" : "false"); main.addInitialProperty("camel.main.modeline", modeline ? "true" : "false"); - // override properties as arguments + // command line arguments if (property != null) { for (String p : property) { String k = StringHelper.before(p, "="); String v = StringHelper.after(p, "="); if (k != null && v != null) { - main.addOverrideProperty(k, v); + main.addArgumentProperty(k, v); } } }
