2023-08-26 17:30:49 +01:00
|
|
|
package stirling.software.SPDF.config;
|
2023-09-05 20:58:18 +01:00
|
|
|
|
2023-08-26 17:30:49 +01:00
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.io.InputStream;
|
2024-05-03 20:43:48 +01:00
|
|
|
import java.net.URISyntaxException;
|
2024-06-15 10:05:31 +02:00
|
|
|
import java.net.URL;
|
2023-08-26 17:30:49 +01:00
|
|
|
import java.nio.file.Files;
|
|
|
|
|
import java.nio.file.Path;
|
|
|
|
|
import java.nio.file.Paths;
|
2024-06-15 10:05:31 +02:00
|
|
|
import java.nio.file.StandardCopyOption;
|
2025-03-25 17:57:17 +00:00
|
|
|
import java.util.List;
|
2023-08-26 17:30:49 +01:00
|
|
|
|
2024-12-17 10:26:18 +01:00
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
2025-01-31 11:00:03 +00:00
|
|
|
/**
|
|
|
|
|
* A naive, line-based approach to merging "settings.yml" with "settings.yml.template" while
|
|
|
|
|
* preserving exact whitespace, blank lines, and inline comments -- but we only rewrite the file if
|
|
|
|
|
* the merged content actually differs.
|
|
|
|
|
*/
|
2024-12-17 10:26:18 +01:00
|
|
|
@Slf4j
|
2025-01-06 12:41:30 +00:00
|
|
|
public class ConfigInitializer {
|
2023-12-30 19:11:27 +00:00
|
|
|
|
2024-05-03 20:43:48 +01:00
|
|
|
public void ensureConfigExists() throws IOException, URISyntaxException {
|
2025-01-31 11:00:03 +00:00
|
|
|
// 1) If settings file doesn't exist, create from template
|
2025-01-06 12:41:30 +00:00
|
|
|
Path destPath = Paths.get(InstallationPathConfig.getSettingsPath());
|
2023-09-24 21:09:34 +01:00
|
|
|
if (Files.notExists(destPath)) {
|
|
|
|
|
Files.createDirectories(destPath.getParent());
|
|
|
|
|
try (InputStream in =
|
|
|
|
|
getClass().getClassLoader().getResourceAsStream("settings.yml.template")) {
|
2025-01-31 11:00:03 +00:00
|
|
|
if (in == null) {
|
2023-09-24 21:09:34 +01:00
|
|
|
throw new FileNotFoundException(
|
|
|
|
|
"Resource file not found: settings.yml.template");
|
|
|
|
|
}
|
2025-01-31 11:00:03 +00:00
|
|
|
Files.copy(in, destPath);
|
2023-09-24 21:09:34 +01:00
|
|
|
}
|
2025-01-06 12:41:30 +00:00
|
|
|
log.info("Created settings file from template");
|
2023-09-24 21:09:34 +01:00
|
|
|
} else {
|
2025-01-31 11:00:03 +00:00
|
|
|
// 2) Merge existing file with the template
|
|
|
|
|
URL templateResource = getClass().getClassLoader().getResource("settings.yml.template");
|
|
|
|
|
if (templateResource == null) {
|
2024-06-15 10:05:31 +02:00
|
|
|
throw new IOException("Resource not found: settings.yml.template");
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-31 11:00:03 +00:00
|
|
|
// Copy template to a temp location so we can read lines
|
2024-06-15 10:05:31 +02:00
|
|
|
Path tempTemplatePath = Files.createTempFile("settings.yml", ".template");
|
2025-01-31 11:00:03 +00:00
|
|
|
try (InputStream in = templateResource.openStream()) {
|
2024-06-15 10:05:31 +02:00
|
|
|
Files.copy(in, tempTemplatePath, StandardCopyOption.REPLACE_EXISTING);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-25 22:52:59 +01:00
|
|
|
// Copy setting.yaml to a temp location so we can read lines
|
|
|
|
|
Path settingTempPath = Files.createTempFile("settings", ".yaml");
|
|
|
|
|
try (InputStream in = Files.newInputStream(destPath)) {
|
|
|
|
|
Files.copy(in, settingTempPath, StandardCopyOption.REPLACE_EXISTING);
|
|
|
|
|
}
|
2025-01-31 11:00:03 +00:00
|
|
|
|
2025-02-25 22:52:59 +01:00
|
|
|
YamlHelper settingsTemplateFile = new YamlHelper(tempTemplatePath);
|
|
|
|
|
YamlHelper settingsFile = new YamlHelper(settingTempPath);
|
2025-01-31 11:00:03 +00:00
|
|
|
|
2025-03-25 17:57:17 +00:00
|
|
|
migrateEnterpriseEditionToPremium(settingsFile, settingsTemplateFile);
|
|
|
|
|
|
2025-02-25 22:52:59 +01:00
|
|
|
boolean changesMade =
|
|
|
|
|
settingsTemplateFile.updateValuesFromYaml(settingsFile, settingsTemplateFile);
|
|
|
|
|
if (changesMade) {
|
|
|
|
|
settingsTemplateFile.save(destPath);
|
2025-01-31 11:00:03 +00:00
|
|
|
log.info("Settings file updated based on template changes.");
|
|
|
|
|
} else {
|
|
|
|
|
log.info("No changes detected; settings file left as-is.");
|
2024-05-03 20:43:48 +01:00
|
|
|
}
|
2024-06-15 10:05:31 +02:00
|
|
|
|
2025-01-31 11:00:03 +00:00
|
|
|
Files.deleteIfExists(tempTemplatePath);
|
2025-02-25 22:52:59 +01:00
|
|
|
Files.deleteIfExists(settingTempPath);
|
2023-09-24 21:09:34 +01:00
|
|
|
}
|
2024-06-15 10:05:31 +02:00
|
|
|
|
2025-01-31 11:00:03 +00:00
|
|
|
// 3) Ensure custom settings file exists
|
2025-01-06 12:41:30 +00:00
|
|
|
Path customSettingsPath = Paths.get(InstallationPathConfig.getCustomSettingsPath());
|
2025-02-25 22:52:59 +01:00
|
|
|
if (Files.notExists(customSettingsPath)) {
|
2024-06-15 10:05:31 +02:00
|
|
|
Files.createFile(customSettingsPath);
|
2025-02-25 22:52:59 +01:00
|
|
|
log.info("Created custom_settings file: {}", customSettingsPath.toString());
|
2024-05-03 22:23:21 +01:00
|
|
|
}
|
2024-05-25 18:19:03 +02:00
|
|
|
}
|
2025-03-25 17:57:17 +00:00
|
|
|
|
|
|
|
|
// TODO: Remove post migration
|
|
|
|
|
private void migrateEnterpriseEditionToPremium(YamlHelper yaml, YamlHelper template) {
|
|
|
|
|
if (yaml.getValueByExactKeyPath("enterpriseEdition", "enabled") != null) {
|
|
|
|
|
template.updateValue(
|
|
|
|
|
List.of("premium", "enabled"),
|
|
|
|
|
yaml.getValueByExactKeyPath("enterpriseEdition", "enabled"));
|
|
|
|
|
}
|
|
|
|
|
if (yaml.getValueByExactKeyPath("enterpriseEdition", "key") != null) {
|
|
|
|
|
template.updateValue(
|
|
|
|
|
List.of("premium", "key"),
|
|
|
|
|
yaml.getValueByExactKeyPath("enterpriseEdition", "key"));
|
|
|
|
|
}
|
|
|
|
|
if (yaml.getValueByExactKeyPath("enterpriseEdition", "SSOAutoLogin") != null) {
|
|
|
|
|
template.updateValue(
|
|
|
|
|
List.of("premium", "proFeatures", "SSOAutoLogin"),
|
|
|
|
|
yaml.getValueByExactKeyPath("enterpriseEdition", "SSOAutoLogin"));
|
|
|
|
|
}
|
|
|
|
|
if (yaml.getValueByExactKeyPath("enterpriseEdition", "CustomMetadata", "autoUpdateMetadata")
|
|
|
|
|
!= null) {
|
|
|
|
|
template.updateValue(
|
|
|
|
|
List.of("premium", "proFeatures", "CustomMetadata", "autoUpdateMetadata"),
|
|
|
|
|
yaml.getValueByExactKeyPath(
|
|
|
|
|
"enterpriseEdition", "CustomMetadata", "autoUpdateMetadata"));
|
|
|
|
|
}
|
|
|
|
|
if (yaml.getValueByExactKeyPath("enterpriseEdition", "CustomMetadata", "author") != null) {
|
|
|
|
|
template.updateValue(
|
|
|
|
|
List.of("premium", "proFeatures", "CustomMetadata", "author"),
|
|
|
|
|
yaml.getValueByExactKeyPath("enterpriseEdition", "CustomMetadata", "author"));
|
|
|
|
|
}
|
|
|
|
|
if (yaml.getValueByExactKeyPath("enterpriseEdition", "CustomMetadata", "creator") != null) {
|
|
|
|
|
template.updateValue(
|
|
|
|
|
List.of("premium", "proFeatures", "CustomMetadata", "creator"),
|
|
|
|
|
yaml.getValueByExactKeyPath("enterpriseEdition", "CustomMetadata", "creator"));
|
|
|
|
|
}
|
|
|
|
|
if (yaml.getValueByExactKeyPath("enterpriseEdition", "CustomMetadata", "producer")
|
|
|
|
|
!= null) {
|
|
|
|
|
template.updateValue(
|
|
|
|
|
List.of("premium", "proFeatures", "CustomMetadata", "producer"),
|
|
|
|
|
yaml.getValueByExactKeyPath("enterpriseEdition", "CustomMetadata", "producer"));
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-05 20:58:18 +01:00
|
|
|
}
|