You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2023/10/25 15:12:14 UTC

[camel] branch main updated: CAMEL-20033: camel-jbang - Write to settings properties file using JDK Properties code that escapes key and value so it works on Windows.

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


The following commit(s) were added to refs/heads/main by this push:
     new fcb3da2abea CAMEL-20033: camel-jbang - Write to settings properties file using JDK Properties code that escapes key and value so it works on Windows.
fcb3da2abea is described below

commit fcb3da2abead53b27ac8b5e0da1835994366c63f
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Oct 25 17:11:59 2023 +0200

    CAMEL-20033: camel-jbang - Write to settings properties file using JDK Properties code that escapes key and value so it works on Windows.
---
 .../dsl/jbang/core/commands/ExportBaseCommand.java | 10 ++++-----
 .../camel/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 a208ae7659c..317047df8d6 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 @@ abstract class ExportBaseCommand extends CamelCommand {
             }
         }
 
-        List<String> lines = Files.readAllLines(settings.toPath());
+        List<String> 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 @@ abstract class ExportBaseCommand extends CamelCommand {
         }
 
         // there may be additional extra repositories
-        List<String> lines = Files.readAllLines(settings.toPath());
+        List<String> lines = RuntimeUtil.loadPropertiesLines(settings);
         for (String line : lines) {
             if (line.startsWith("repository=")) {
                 String r = StringHelper.after(line, "repository=");
@@ -661,7 +661,7 @@ abstract class ExportBaseCommand extends CamelCommand {
 
     protected static boolean hasModeline(File settings) {
         try {
-            List<String> lines = Files.readAllLines(settings.toPath());
+            List<String> lines = RuntimeUtil.loadPropertiesLines(settings);
             return lines.stream().anyMatch(l -> l.startsWith("modeline="));
         } catch (Exception e) {
             // ignore
@@ -671,7 +671,7 @@ abstract class ExportBaseCommand extends CamelCommand {
 
     protected static int httpServerPort(File settings) {
         try {
-            List<String> lines = Files.readAllLines(settings.toPath());
+            List<String> 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 @@ abstract class ExportBaseCommand extends CamelCommand {
 
     protected static String jibMavenPluginVersion(File settings) {
         try {
-            List<String> lines = Files.readAllLines(settings.toPath());
+            List<String> 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 1f1952aed31..97d985590b6 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 @@ package org.apache.camel.dsl.jbang.core.common;
 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 final class RuntimeUtil {
     }
 
     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<String> loadPropertiesLines(File file) throws IOException {
+        List<String> 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) {