Skip to content

Commit

Permalink
Fixed file loading on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
hylkevds committed Jul 30, 2024
1 parent c63cf0a commit d822168
Showing 1 changed file with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -283,18 +283,18 @@ private void mergeJson(Map<String, Object> target, Map<String, Object> toMerge)
}

private Map<String, Object> 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);
}
Expand Down

0 comments on commit d822168

Please sign in to comment.