From d82216856982facc114fcac2f446327bcda6a0cb Mon Sep 17 00:00:00 2001 From: Hylke van der Schaaf Date: Tue, 30 Jul 2024 15:05:45 +0200 Subject: [PATCH] Fixed file loading on Windows --- .../plugin/modelloader/PluginModelLoader.java | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/Plugins/ModelLoader/src/main/java/de/fraunhofer/iosb/ilt/frostserver/plugin/modelloader/PluginModelLoader.java b/Plugins/ModelLoader/src/main/java/de/fraunhofer/iosb/ilt/frostserver/plugin/modelloader/PluginModelLoader.java index 96f1fc8e7..12f8a794a 100644 --- a/Plugins/ModelLoader/src/main/java/de/fraunhofer/iosb/ilt/frostserver/plugin/modelloader/PluginModelLoader.java +++ b/Plugins/ModelLoader/src/main/java/de/fraunhofer/iosb/ilt/frostserver/plugin/modelloader/PluginModelLoader.java @@ -43,12 +43,11 @@ import de.fraunhofer.iosb.ilt.frostserver.util.SecurityModel; import de.fraunhofer.iosb.ilt.frostserver.util.StringHelper; import de.fraunhofer.iosb.ilt.frostserver.util.exception.UpgradeFailedException; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.Writer; import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -58,6 +57,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; +import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -134,13 +134,13 @@ private void loadModelFiles() { } private void addModelFileWithPath(String fileName) { - final Path fullPath; + final File fullFile; if (StringHelper.isNullOrEmpty(modelPath)) { - fullPath = Path.of(fileName); + fullFile = new File(fileName); } else { - fullPath = Path.of(modelPath, fileName); + fullFile = new File(modelPath, fileName); } - modelFiles.add(fullPath.toString()); + modelFiles.add(fullFile.toString()); } public void addModelFile(String filename) { @@ -156,17 +156,17 @@ public void addLiquibaseFile(String filename) { } public void loadModelFile(String fullPathString) { - Path fullPath = Path.of(fullPathString); - LOGGER.info("Loading model definition from {}", fullPath.toAbsolutePath()); + File fullFile = new File(fullPathString); + LOGGER.info("Loading model definition from {}", fullFile.toString()); String data; try { ObjectMapper objectMapper = new ObjectMapper(); DefModel modelDefinition; - if (fullPath.toFile().exists()) { - data = new String(Files.readAllBytes(fullPath), StandardCharsets.UTF_8); + if (fullFile.exists()) { + data = FileUtils.readFileToString(fullFile, StandardCharsets.UTF_8); modelDefinition = objectMapper.readValue(data, DefModel.class); } else { - InputStream stream = getClass().getClassLoader().getResourceAsStream(fullPath.toString()); + InputStream stream = getClass().getClassLoader().getResourceAsStream(fullFile.toString()); modelDefinition = objectMapper.readValue(stream, DefModel.class); } modelDefinition.init(); @@ -200,19 +200,19 @@ public void installSecurityDefinitions(PersistenceManager pm) { } private SecurityModel loadSecurityFile(String fileName) { - final Path fullPath = Path.of(securityPath, fileName); - LOGGER.info("Loading security definition from {}", fullPath.toAbsolutePath()); + final File fullFile = new File(securityPath, fileName); + LOGGER.info("Loading security definition from {}", fullFile.toString()); String data; try { ObjectMapper objectMapper = new ObjectMapper(); SecurityModel securityModel; - if (fullPath.toFile().exists()) { - data = new String(Files.readAllBytes(fullPath), StandardCharsets.UTF_8); + if (fullFile.exists()) { + data = FileUtils.readFileToString(fullFile, StandardCharsets.UTF_8); securityModel = objectMapper.readValue(data, SecurityModel.class); } else { - InputStream stream = getClass().getClassLoader().getResourceAsStream(fullPath.toString()); + InputStream stream = getClass().getClassLoader().getResourceAsStream(fullFile.toString()); if (stream == null) { - LOGGER.info(" Not found: {}", fullPath); + LOGGER.info(" Not found: {}", fullFile); return null; } securityModel = objectMapper.readValue(stream, SecurityModel.class); @@ -283,18 +283,18 @@ private void mergeJson(Map target, Map toMerge) } private Map loadExtraMetadataFile(String fileName) { - final Path fullPath = Path.of(metadataPath, fileName); - LOGGER.info("Loading extra landing page meta data from {}", fullPath.toAbsolutePath()); + final File fullFile = new File(metadataPath, fileName); + LOGGER.info("Loading extra landing page meta data from {}", fullFile.toString()); String data; try { ObjectMapper objectMapper = new ObjectMapper(); - if (fullPath.toFile().exists()) { - data = new String(Files.readAllBytes(fullPath), StandardCharsets.UTF_8); + if (fullFile.exists()) { + data = FileUtils.readFileToString(fullFile, StandardCharsets.UTF_8); return objectMapper.readValue(data, TYPE_REFERENCE_MAP); } else { - InputStream stream = getClass().getClassLoader().getResourceAsStream(fullPath.toString()); + InputStream stream = getClass().getClassLoader().getResourceAsStream(fullFile.toString()); if (stream == null) { - LOGGER.info(" Not found: {}", fullPath); + LOGGER.info(" Not found: {}", fullFile); } else { return objectMapper.readValue(stream, TYPE_REFERENCE_MAP); }