Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Preprocess JVM properties at app start to adjust to system environment. #2957

Merged
merged 25 commits into from
Jun 30, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c46eeb0
first impl draft
infeo Jun 15, 2023
4065e15
Apply code suggestion from review
infeo Jun 16, 2023
2c0474e
add test
infeo Jun 20, 2023
ec645a4
replace ~ by @{userhome} on unix systems
infeo Jun 20, 2023
ebea8ef
adjust windows buildscripts to use appdata instead of userhome/hard/c…
infeo Jun 20, 2023
b3d8df0
fix missing logDir path resolution
infeo Jun 20, 2023
5e52f71
Merge branch 'develop' into feature/preprocess-properties
infeo Jun 21, 2023
8417618
Revert "fix missing logDir path resolution"
infeo Jun 28, 2023
01da51e
Refactor properties preprocessing:
infeo Jun 29, 2023
ebc60c4
make process function reliable testable and adjust unit test
infeo Jun 29, 2023
c0368f2
Merge branch 'develop' into feature/preprocess-properties
infeo Jun 29, 2023
6c18e3a
apply suggestion from sonarCloud
infeo Jun 29, 2023
3098628
Fix problem of circular class init
infeo Jun 29, 2023
a31d318
move boilerplate code to own class
infeo Jun 29, 2023
9383abb
let child method match parent
infeo Jun 29, 2023
ce59669
stick to a unified path separator for cryptomator properties
infeo Jun 29, 2023
9b0c940
readd unit tests for Environment.java
infeo Jun 29, 2023
24fc288
more unit tests
infeo Jun 29, 2023
12e990a
cleanup
infeo Jun 29, 2023
cee0486
adding unit tests for getProperty
infeo Jun 29, 2023
79eea48
renamed class
infeo Jun 29, 2023
ab04850
change getProperty(key, default) and add unit test
infeo Jun 30, 2023
ee82965
adding "integration tests"
infeo Jun 30, 2023
8c3ede0
clean up
infeo Jun 30, 2023
29d63a0
final clean up
infeo Jun 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/main/java/org/cryptomator/common/PropertiesPreprocessor.java
Original file line number Diff line number Diff line change
@@ -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("@\\{(\\w+)}");
infeo marked this conversation as resolved.
Show resolved Hide resolved
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.");
}
infeo marked this conversation as resolved.
Show resolved Hide resolved

private static String process(String value) {
return TEMPLATE.matcher(value).replaceAll(match -> //
switch (match.group(1)) {
case "appdir" -> ENV.get("APPDIR");
case "appdata" -> ENV.get("APPDATA");
case "userhome" -> System.getProperty("user.home");
default -> {
LOG.warn("Found unknown variable @{{}} in property value {}.", 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;
}
}

}


}
3 changes: 2 additions & 1 deletion src/main/java/org/cryptomator/launcher/Cryptomator.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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;
Expand Down Expand Up @@ -63,7 +64,7 @@ public static void main(String[] args) {
System.out.printf("Cryptomator version %s (build %s)%n", appVer, buildNumber);
return;
}

PropertiesPreprocessor.run();
infeo marked this conversation as resolved.
Show resolved Hide resolved
int exitCode = CRYPTOMATOR_COMPONENT.application().run(args);
LOG.info("Exit {}", exitCode);
System.exit(exitCode); // end remaining non-daemon threads.
Expand Down