first impl draft

This commit is contained in:
Armin Schrenk
2023-06-15 10:38:19 +02:00
parent 5d796efe30
commit c46eeb0401
2 changed files with 68 additions and 1 deletions

View File

@@ -0,0 +1,66 @@
package org.cryptomator.common;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;
import java.util.regex.Pattern;
public class PropertiesPreprocessor {
private static final Logger LOG = LoggerFactory.getLogger(PropertiesPreprocessor.class);
private static final Pattern TEMPLATE = Pattern.compile("@\\((.+)\\)");
private static final LoggingEnvironment ENV = new LoggingEnvironment(System.getenv(), LOG);
public static void run() {
var properties = System.getProperties();
properties.stringPropertyNames().stream() //
.filter(s -> s.startsWith("cryptomator.")) //
.forEach(key -> {
var value = properties.getProperty(key);
var newValue = process(value);
if(! value.equals(newValue)) {
LOG.info("Changed property {} from {} to {}.", key, value, newValue);
}
properties.setProperty(key, newValue);
});
LOG.info("Preprocessed cryptomator properties.");
}
private static String process(String value) {
return TEMPLATE.matcher(value).replaceAll(match -> //
switch (match.group().substring(2, match.group().length() - 1)) {
case "appdir" -> ENV.get("APPDIR");
case "appdata" -> ENV.get("APPDATA");
case "userhome" -> System.getProperty("user.home");
default -> {
LOG.warn("Found unknown keyword {} in property value {}. Keyword is not replaced.", match.group(), value);
yield match.group();
}
});
}
private static class LoggingEnvironment {
private final Map<String, String> env;
private final Logger logger;
LoggingEnvironment(Map<String, String> env, Logger logger) {
this.env = env;
this.logger = logger;
}
String get(String key) {
var val = env.get(key);
if (val == null) {
logger.warn("Variable {} used for substitution not found in environment", key);
return "";
} else {
return val;
}
}
}
}

View File

@@ -9,6 +9,7 @@ import com.google.common.util.concurrent.ThreadFactoryBuilder;
import dagger.Lazy;
import org.apache.commons.lang3.SystemUtils;
import org.cryptomator.common.Environment;
import org.cryptomator.common.PropertiesPreprocessor;
import org.cryptomator.common.ShutdownHook;
import org.cryptomator.ipc.IpcCommunicator;
import org.cryptomator.logging.DebugMode;
@@ -63,7 +64,7 @@ public class Cryptomator {
System.out.printf("Cryptomator version %s (build %s)%n", appVer, buildNumber);
return;
}
PropertiesPreprocessor.run();
int exitCode = CRYPTOMATOR_COMPONENT.application().run(args);
LOG.info("Exit {}", exitCode);
System.exit(exitCode); // end remaining non-daemon threads.