switch back to properties

This commit is contained in:
Armin Schrenk
2026-01-21 11:53:31 +01:00
parent d53f0880ca
commit c198adaf3f

View File

@@ -1,30 +1,27 @@
package org.cryptomator.launcher;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import org.apache.commons.lang3.SystemUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
/**
* Class to overwrite system properties with an external properties file
* <p>
* To overwrite system properties, the method {@link #adjustSystemProperties()} loads the JSON file {@value CONFIG_NAME} from an OS-dependent location and adds all whitelisted properties to the {@link System} properties.
* The predefined locations are:
* <ul>
* <li>Linux - {@value LINUX_DIR }</li>
* <li>macOS - {@value MAC_DIR }</li>
* <li>Windows - {@value WIN_DIR }</li>
* </ul>
* To overwrite system properties, the method {@link #adjustSystemProperties()} loads the properties file {@value CONFIG_NAME} from an OS-dependent location and adds all supported properties to the {@link System} properties.
* The predefined location is for
* <ul>
* <li>Linux: {@value LINUX_DIR }</li>
* <li>macOS: {@value MAC_DIR }</li>
* <li>Windows: {@value WIN_DIR }</li>
* </ul>
* </p>
* <p>
* The overridable properties are:
@@ -40,12 +37,12 @@ import java.util.Set;
*/
class AdminPropertiesSetter {
private static final ObjectMapper JSON = JsonMapper.builder().build();
private static final Logger LOG = LoggerFactory.getLogger(AdminPropertiesSetter.class);
private static final String LINUX_DIR = "/etc/cryptomator";
private static final String MAC_DIR = "/Library/Application Support/Cryptomator";
private static final String WIN_DIR = "%PROGRAMDATA%\\Cryptomator";
private static final String CONFIG_NAME = "config.json";
private static final String WIN_DIR = "C:\\ProgramData\\Cryptomator";
private static final String CONFIG_NAME = "config.properties";
private static final Set<String> ALLOWED_OVERRIDES = Set.of( //
"cryptomator.logDir", //
"cryptomator.pluginDir", //
@@ -57,7 +54,7 @@ class AdminPropertiesSetter {
static {
final Path adminDir;
if (SystemUtils.IS_OS_WINDOWS) {
adminDir = Path.of(System.getenv().getOrDefault("ProgramData", "C:\\ProgramData"), "Cryptomator");
adminDir = Path.of(WIN_DIR);
} else if (SystemUtils.IS_OS_MAC) {
adminDir = Path.of(MAC_DIR);
} else { //LINUX
@@ -71,7 +68,7 @@ class AdminPropertiesSetter {
/**
* Adjusts the system properties by loading administrative properties from a predefined file location.
* <p>
* If the file exists and is a valid JSON file, its content will overwrite existing system properties.
* If the file exists and is a valid properties file, its content will overwrite existing system properties.
* Only some properties can be overridden, see {@link AdminPropertiesSetter}
*
* @return The adjusted system properties.
@@ -80,35 +77,29 @@ class AdminPropertiesSetter {
var systemProps = System.getProperties();
var adminProps = loadAdminProperties(ADMIN_PROPERTIES_FILE);
adminProps.forEach((key, value) -> {
if (ALLOWED_OVERRIDES.contains(key) && value instanceof String v) {
log("Overwriting {} with value {} from admin properties.", List.of(key, v));
systemProps.setProperty(key, v);
for (var key : adminProps.stringPropertyNames()) {
if (ALLOWED_OVERRIDES.contains(key)) {
var value = adminProps.getProperty(key);
LOG.info("Overwriting {} with value {} from admin properties.", key, value);
systemProps.setProperty(key, value);
} else {
var reason = value instanceof String ? "Unsupported" : "Not a string";
log("Property {} in admin config ignored: {}.", List.of(key, reason));
LOG.debug("Property {} in admin properties is not supported for override.", key);
}
});
}
return systemProps;
}
static Map<String, Object> loadAdminProperties(Path adminPropertiesPath) {
try (var in = Files.newInputStream(adminPropertiesPath, StandardOpenOption.READ)) {
var map = JSON.readValue(in, new TypeReference<Map<String, Object>>() {});
if (map == null) {
throw new NullPointerException("JSON parsing returned null");
}
return map;
private static Properties loadAdminProperties(Path adminPropertiesPath) {
var adminProps = new Properties();
try (var reader = Files.newBufferedReader(adminPropertiesPath, StandardCharsets.UTF_8)) {
adminProps.load(reader);
} catch (NoSuchFileException _) {
//NO-OP
log("No admin properties found at {}.", List.of(adminPropertiesPath));
} catch (IOException | RuntimeException e) {
log("Failed to read administrative properties from {}. Returning empty properties.", List.of(adminPropertiesPath, e));
LOG.debug("No admin properties found at {}.", adminPropertiesPath);
} catch (IOException | IllegalArgumentException e) {
LOG.warn("Failed to read administrative properties from {}. Returning empty properties.", adminPropertiesPath, e);
}
return Map.of();
return adminProps;
}
static void log(String message, List<Object> messageInput) {
BufferedLog.log(AdminPropertiesSetter.class.getName(), message, messageInput);
}
}