Files
SiteBackend/src/main/java/com/alttd/forms/properties/PropertiesLoader.java
T
auto 2c3cb48667 Add REST annotations and refactor properties file handling
Introduced `@RestController` and `@RequestMapping` in `StaffAppController` for standardized API endpoints. Refactored properties file handling in `PropertiesLoader` and `PropertiesWriter` to simplify file creation logic.
2024-08-06 23:17:06 +02:00

57 lines
1.7 KiB
Java

package com.alttd.forms.properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Path;
import java.util.Optional;
import java.util.Properties;
public class PropertiesLoader {
private static final Logger logger = LoggerFactory.getLogger(PropertiesLoader.class);
public static Optional<Properties> loadProperties(String path, String fileName) {
Properties prop = new Properties();
File file;
if (path.isEmpty()) {
Optional<File> currentJarPath = JarPath.getCurrentJarDir();
if (currentJarPath.isEmpty()) {
logger.error("Unable to find jar path to load properties (" + fileName + ")");
return Optional.empty();
}
file = new File(currentJarPath.get(), fileName);
} else {
file = new File(path, fileName);
}
if (!file.exists()) {
logger.warn("Tried to load properties file that doesnt exists (" + fileName + ")");
return Optional.empty();
}
if (!file.canRead()) {
logger.error("Unable to read properties file (" + fileName + ")");
return Optional.empty();
}
if (!file.isFile()) {
logger.error("Invalid properties file (" + fileName + ")");
return Optional.empty();
}
try (FileInputStream input = new FileInputStream(file)) {
prop.load(input);
return Optional.of(prop);
} catch (IOException ex) {
logger.error("Failed to load properties due to an unexpected error", ex);
}
return Optional.empty();
}
}