Skip to content

Commit

Permalink
CAMEL-20033: camel-jbang - Write to settings properties file using JD…
Browse files Browse the repository at this point in the history
…K Properties code that escapes key and value so it works on Windows.
  • Loading branch information
davsclaus committed Oct 25, 2023
1 parent ff942fa commit 0033e2b
Showing 1 changed file with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
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;
Expand Down Expand Up @@ -1282,10 +1283,22 @@ private boolean jkubeFile(String name) {
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 {
Expand Down

0 comments on commit 0033e2b

Please sign in to comment.