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 09c1a8e commit fcb3da2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ protected Set<String> resolveDependencies(File settings, File profile) throws Ex
}
}

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=")) {
Expand Down Expand Up @@ -644,7 +644,7 @@ protected String getMavenRepos(File settings, Properties prop, String camelVersi
}

// 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=");
Expand All @@ -661,7 +661,7 @@ protected String getMavenRepos(File settings, Properties prop, String camelVersi

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
Expand All @@ -671,7 +671,7 @@ protected static boolean hasModeline(File settings) {

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);
Expand All @@ -683,7 +683,7 @@ protected static int httpServerPort(File settings) {

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<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) {
Expand Down

0 comments on commit fcb3da2

Please sign in to comment.