From fcb3da2abead53b27ac8b5e0da1835994366c63f Mon Sep 17 00:00:00 2001 From: Claus Ibsen Date: Wed, 25 Oct 2023 17:11:59 +0200 Subject: [PATCH] CAMEL-20033: camel-jbang - Write to settings properties file using JDK Properties code that escapes key and value so it works on Windows. --- .../core/commands/ExportBaseCommand.java | 10 +++---- .../dsl/jbang/core/common/RuntimeUtil.java | 26 +++++++++++++++++-- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportBaseCommand.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportBaseCommand.java index a208ae7659c19..317047df8d6a4 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportBaseCommand.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/ExportBaseCommand.java @@ -304,7 +304,7 @@ protected Set resolveDependencies(File settings, File profile) throws Ex } } - List lines = Files.readAllLines(settings.toPath()); + List lines = RuntimeUtil.loadPropertiesLines(settings); boolean kamelets = lines.stream().anyMatch(l -> l.startsWith("kamelet=")); for (String line : lines) { if (line.startsWith("dependency=")) { @@ -644,7 +644,7 @@ protected String getMavenRepos(File settings, Properties prop, String camelVersi } // there may be additional extra repositories - List lines = Files.readAllLines(settings.toPath()); + List lines = RuntimeUtil.loadPropertiesLines(settings); for (String line : lines) { if (line.startsWith("repository=")) { String r = StringHelper.after(line, "repository="); @@ -661,7 +661,7 @@ protected String getMavenRepos(File settings, Properties prop, String camelVersi protected static boolean hasModeline(File settings) { try { - List lines = Files.readAllLines(settings.toPath()); + List lines = RuntimeUtil.loadPropertiesLines(settings); return lines.stream().anyMatch(l -> l.startsWith("modeline=")); } catch (Exception e) { // ignore @@ -671,7 +671,7 @@ protected static boolean hasModeline(File settings) { protected static int httpServerPort(File settings) { try { - List lines = Files.readAllLines(settings.toPath()); + List lines = RuntimeUtil.loadPropertiesLines(settings); String port = lines.stream().filter(l -> l.startsWith("camel.jbang.platform-http.port=")) .map(s -> StringHelper.after(s, "=")).findFirst().orElse("-1"); return Integer.parseInt(port); @@ -683,7 +683,7 @@ protected static int httpServerPort(File settings) { protected static String jibMavenPluginVersion(File settings) { try { - List lines = Files.readAllLines(settings.toPath()); + List lines = RuntimeUtil.loadPropertiesLines(settings); return lines.stream().filter(l -> l.startsWith("camel.jbang.jib-maven-plugin-version=")) .map(s -> StringHelper.after(s, "=")).findFirst().orElse("3.4.0"); } catch (Exception e) { diff --git a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/RuntimeUtil.java b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/RuntimeUtil.java index 1f1952aed314b..97d985590b653 100644 --- a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/RuntimeUtil.java +++ b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/RuntimeUtil.java @@ -19,9 +19,12 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; import java.util.Properties; import java.util.concurrent.atomic.AtomicBoolean; +import org.apache.camel.util.OrderedProperties; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.core.config.Configurator; @@ -88,9 +91,28 @@ public static void setRootLoggingLevel(String level) { } public static void loadProperties(Properties properties, File file) throws IOException { - try (final FileInputStream fileInputStream = new FileInputStream(file)) { - properties.load(fileInputStream); + if (file.exists()) { + try (final FileInputStream fileInputStream = new FileInputStream(file)) { + properties.load(fileInputStream); + } + } + } + + public static List loadPropertiesLines(File file) throws IOException { + List lines = new ArrayList<>(); + if (!file.exists()) { + return lines; + } + + Properties prop = new OrderedProperties(); + loadProperties(prop, file); + for (String k : prop.stringPropertyNames()) { + String v = prop.getProperty(k); + if (v != null) { + lines.add(k + "=" + v); + } } + return lines; } public static String getDependencies(Properties properties) {