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 13:22:02 UTC

[camel] 01/01: 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 jbang-win
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 0033e2b8d1b7ecc76f360e18e6aa817d5c721bd5
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Wed Oct 25 15:21:47 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.
---
 .../org/apache/camel/dsl/jbang/core/commands/Run.java | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

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 3c718fe5eca..d432e2daec1 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
@@ -26,6 +26,7 @@ import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.StringWriter;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.FileSystems;
 import java.nio.file.Files;
@@ -1282,10 +1283,22 @@ public class Run extends CamelCommand {
     private void writeSettings(String key, String value) {
         FileOutputStream fos = null;
         try {
+            // use java.util.Properties to ensure the value is escaped correctly
+            Properties prop = new Properties();
+            prop.setProperty(key, value);
+            StringWriter sw = new StringWriter();
+            prop.store(sw, null);
+
             fos = new FileOutputStream(WORK_DIR + "/" + RUN_SETTINGS_FILE, true);
-            String line = key + "=" + value;
-            fos.write(line.getBytes(StandardCharsets.UTF_8));
-            fos.write(System.lineSeparator().getBytes(StandardCharsets.UTF_8));
+
+            String[] lines = sw.toString().split(System.lineSeparator());
+            for (String line : lines) {
+                // properties store timestamp as comment which we want to skip
+                if (!line.startsWith("#")) {
+                    fos.write(line.getBytes(StandardCharsets.UTF_8));
+                    fos.write(System.lineSeparator().getBytes(StandardCharsets.UTF_8));
+                }
+            }
         } catch (Exception e) {
             // ignore
         } finally {