103 lines
3.7 KiB
Java
103 lines
3.7 KiB
Java
package de.opcua.app.config;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
|
|
|
import java.io.IOException;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Path;
|
|
import java.util.Map;
|
|
|
|
public final class SettingsService {
|
|
private static final Path FILE = Path.of("settings.json");
|
|
private final ObjectMapper om = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
|
|
|
|
public Settings load() {
|
|
if (!Files.exists(FILE)) return Settings.defaults();
|
|
try {
|
|
@SuppressWarnings("unchecked")
|
|
Map<String, Object> m = om.readValue(Files.readString(FILE), Map.class);
|
|
|
|
Settings defaults = Settings.defaults();
|
|
|
|
String endpoint = (String) m.getOrDefault("Endpoint", defaults.endpoint());
|
|
String host = (String) m.getOrDefault("Host", defaults.host());
|
|
|
|
// HTTP Configuration
|
|
boolean httpEnabled = m.containsKey("httpEnabled")
|
|
? (Boolean) m.get("httpEnabled")
|
|
: true;
|
|
|
|
int httpPort = 8081;
|
|
if (m.containsKey("httpPort")) {
|
|
httpPort = ((Number) m.get("httpPort")).intValue();
|
|
} else if (m.containsKey("httpUrl")) {
|
|
// Backward compatibility - extract port from httpUrl
|
|
try {
|
|
String url = (String) m.get("httpUrl");
|
|
if (url.contains(":")) {
|
|
String portStr = url.substring(url.lastIndexOf(":") + 1).replace("/", "");
|
|
httpPort = Integer.parseInt(portStr);
|
|
}
|
|
} catch (Exception e) {}
|
|
}
|
|
|
|
// HTTPS Configuration
|
|
boolean httpsEnabled = m.containsKey("httpsEnabled")
|
|
? (Boolean) m.get("httpsEnabled")
|
|
: defaults.httpsEnabled();
|
|
|
|
int httpsPort = m.containsKey("httpsPort")
|
|
? ((Number) m.get("httpsPort")).intValue()
|
|
: defaults.httpsPort();
|
|
|
|
String keystorePath = (String) m.getOrDefault("keystorePath", defaults.keystorePath());
|
|
String keystorePassword = (String) m.getOrDefault("keystorePassword", defaults.keystorePassword());
|
|
String keystoreType = (String) m.getOrDefault("keystoreType", defaults.keystoreType());
|
|
|
|
// Logging fields
|
|
boolean loggingEnabled = m.containsKey("scriptLoggingEnabled")
|
|
? (Boolean) m.get("scriptLoggingEnabled")
|
|
: defaults.scriptLoggingEnabled();
|
|
|
|
String logLevel = (String) m.getOrDefault("scriptLogLevel", defaults.scriptLogLevel());
|
|
String logDir = (String) m.getOrDefault("scriptLogDirectory", defaults.scriptLogDirectory());
|
|
|
|
return new Settings(
|
|
endpoint, host,
|
|
httpEnabled, httpPort,
|
|
httpsEnabled, httpsPort, keystorePath, keystorePassword, keystoreType,
|
|
loggingEnabled, logLevel, logDir
|
|
);
|
|
} catch (Exception e) {
|
|
System.err.println("Error loading settings: " + e.getMessage());
|
|
e.printStackTrace();
|
|
return Settings.defaults();
|
|
}
|
|
}
|
|
|
|
public void save(Settings s) throws IOException {
|
|
Map<String, Object> m = new java.util.HashMap<>();
|
|
m.put("Endpoint", s.endpoint());
|
|
m.put("Host", s.host());
|
|
|
|
// HTTP Configuration
|
|
m.put("httpEnabled", s.httpEnabled());
|
|
m.put("httpPort", s.httpPort());
|
|
|
|
// HTTPS Configuration
|
|
m.put("httpsEnabled", s.httpsEnabled());
|
|
m.put("httpsPort", s.httpsPort());
|
|
m.put("keystorePath", s.keystorePath());
|
|
m.put("keystorePassword", s.keystorePassword());
|
|
m.put("keystoreType", s.keystoreType());
|
|
|
|
// Logging
|
|
m.put("scriptLoggingEnabled", s.scriptLoggingEnabled());
|
|
m.put("scriptLogLevel", s.scriptLogLevel());
|
|
m.put("scriptLogDirectory", s.scriptLogDirectory());
|
|
|
|
Files.writeString(FILE, om.writeValueAsString(m));
|
|
}
|
|
}
|