ソースを参照

first impl draft

Armin Schrenk 2 年 前
コミット
c46eeb0401

+ 66 - 0
src/main/java/org/cryptomator/common/PropertiesPreprocessor.java

@@ -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;
+			}
+		}
+
+	}
+
+
+}

+ 2 - 1
src/main/java/org/cryptomator/launcher/Cryptomator.java

@@ -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.