This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit 856f14a5a13008837d2891b08d6ae8e9a48a3915 Author: Claus Ibsen <[email protected]> AuthorDate: Thu Apr 14 12:56:50 2022 +0200 CAMEL-17968: camel-jbang - Add option to specify override property --- .../component/properties/PropertiesComponent.java | 43 +--------------------- .../apache/camel/dsl/jbang/core/commands/Run.java | 15 ++++++++ .../src/main/resources/templates/java.tmpl | 4 +- .../src/main/resources/templates/xml.tmpl | 2 +- 4 files changed, 19 insertions(+), 45 deletions(-) diff --git a/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java b/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java index 7c0e2b30059..807476cd392 100644 --- a/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java +++ b/core/camel-base/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java @@ -224,48 +224,7 @@ public class PropertiesComponent extends ServiceSupport @Override public Properties loadProperties(Predicate<String> filter) { - OrderedLocationProperties prop = new OrderedLocationProperties(); - - // use initial properties - if (initialProperties != null) { - for (String name : initialProperties.stringPropertyNames()) { - if (filter.test(name)) { - prop.put("initial", name, initialProperties.get(name)); - } - } - } - - if (!sources.isEmpty()) { - // sources are ordered according to {@link org.apache.camel.support.OrderComparator} so - // it is needed to iterate them in reverse order otherwise lower priority sources may - // override properties from higher priority ones - for (int i = sources.size(); i-- > 0;) { - PropertiesSource ps = sources.get(i); - if (ps instanceof LoadablePropertiesSource) { - LoadablePropertiesSource lps = (LoadablePropertiesSource) ps; - Properties p = lps.loadProperties(filter); - if (p instanceof OrderedLocationProperties) { - prop.putAll((OrderedLocationProperties) p); - } else if (ps instanceof LocationPropertiesSource) { - String loc = ((LocationPropertiesSource) ps).getLocation().getPath(); - prop.putAll(loc, p); - } else { - prop.putAll(lps.getName(), p); - } - } - } - } - - // use override properties - if (overrideProperties != null) { - for (String name : overrideProperties.stringPropertyNames()) { - if (filter.test(name)) { - prop.put("override", name, overrideProperties.get(name)); - } - } - } - - return prop; + return loadProperties(filter, k -> k); } @Override 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 c68bdae82e0..5df6f627f20 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 @@ -40,6 +40,7 @@ import org.apache.camel.support.ResourceHelper; import org.apache.camel.util.AntPathMatcher; import org.apache.camel.util.FileUtil; import org.apache.camel.util.ObjectHelper; +import org.apache.camel.util.StringHelper; import picocli.CommandLine.Command; import picocli.CommandLine.Option; import picocli.CommandLine.Parameters; @@ -97,6 +98,9 @@ class Run implements Callable<Integer> { description = "Load properties file for route placeholders (ex. /path/to/file.properties") private String propertiesFiles; + @Option(names = { "-p", "--prop", "--property" }, description = "Additional properties (override existing)", arity = "0") + private String[] property; + @Option(names = { "--file-lock" }, defaultValue = "true", description = "Whether to create a temporary file lock, which upon deleting triggers this process to terminate") private boolean fileLock = true; @@ -180,6 +184,17 @@ 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 + 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); + } + } + } + if (maxMessages > 0) { main.addInitialProperty("camel.main.durationMaxMessages", String.valueOf(maxMessages)); } diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/java.tmpl b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/java.tmpl index 47b14123e12..722e7fc0fe6 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/java.tmpl +++ b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/java.tmpl @@ -1,4 +1,4 @@ -// camel-k: language=java +// camel-k: language=java property=time=1000 import org.apache.camel.builder.RouteBuilder; @@ -8,7 +8,7 @@ public class {{ .Name }} extends RouteBuilder { public void configure() throws Exception { // Write your routes here, for example: - from("timer:java?period=1000").routeId("java") + from("timer:java?period={{time}}").routeId("java") .setBody() .simple("Hello Camel from ${routeId}") .log("${body}"); diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/xml.tmpl b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/xml.tmpl index 4287489e30a..290384f2898 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/xml.tmpl +++ b/dsl/camel-jbang/camel-jbang-core/src/main/resources/templates/xml.tmpl @@ -9,7 +9,7 @@ <!-- Write your routes here, for example: --> <route id="xml"> - <from uri="timer:xml?period=1000"/> + <from uri="timer:xml?period={{time:1000}}"/> <setBody> <simple>Hello Camel from ${routeId}</simple> </setBody>
