From 19c564a6f7d6a1f09fc2082376198a15fca5481e Mon Sep 17 00:00:00 2001 From: Ludy87 Date: Fri, 31 May 2024 23:51:42 +0200 Subject: [PATCH] Fix: Initialization Issue and Enhance Configuration Management for ConfigInitializer #1324 --- .../SPDF/config/ConfigInitializer.java | 31 ++++++++++++++----- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/main/java/stirling/software/SPDF/config/ConfigInitializer.java b/src/main/java/stirling/software/SPDF/config/ConfigInitializer.java index d258ea51..874d43c3 100644 --- a/src/main/java/stirling/software/SPDF/config/ConfigInitializer.java +++ b/src/main/java/stirling/software/SPDF/config/ConfigInitializer.java @@ -1,8 +1,10 @@ package stirling.software.SPDF.config; +import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.net.URISyntaxException; import java.nio.file.Files; import java.nio.file.Path; @@ -45,15 +47,27 @@ public class ConfigInitializer } } } else { - Path templatePath = - Paths.get( - getClass() - .getClassLoader() - .getResource("settings.yml.template") - .toURI()); - Path userPath = Paths.get("configs", "settings.yml"); + // Load the template content from classpath + List templateLines; + try (InputStream in = + getClass().getClassLoader().getResourceAsStream("settings.yml.template")) { + if (in == null) { + throw new FileNotFoundException( + "Resource file not found: settings.yml.template"); + } + templateLines = new ArrayList<>(); + try (var reader = new InputStreamReader(in)) { + try (var bufferedReader = new BufferedReader(reader)) { + String line; + while ((line = bufferedReader.readLine()) != null) { + templateLines.add(line); + } + } + } + } - List templateLines = Files.readAllLines(templatePath); + // Read the user settings file if it exists + Path userPath = Paths.get("configs", "settings.yml"); List userLines = Files.exists(userPath) ? Files.readAllLines(userPath) : new ArrayList<>(); @@ -87,6 +101,7 @@ public class ConfigInitializer Files.write(userPath, resultLines); } + // Ensure the custom settings file exists Path customSettingsPath = Paths.get("configs", "custom_settings.yml"); if (!Files.exists(customSettingsPath)) { Files.createFile(customSettingsPath);