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

Add updatable config source #3

Merged
merged 24 commits into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
7649111
first working impl (without CDI)
infeo Oct 22, 2021
3610ce8
simplify config writer
infeo Oct 22, 2021
ab96fb0
use user home dir as default hub config location
infeo Oct 22, 2021
e07110e
add static method to get instance of HubConfig for persistence
infeo Oct 22, 2021
3ed78e7
fix minor bugs
infeo Oct 25, 2021
a5d3839
make hub config persistence synchronously
infeo Oct 25, 2021
06fdb2e
prevent that hub config file overwrites quarkus properites file
infeo Oct 25, 2021
9ce57d3
clean up
infeo Oct 25, 2021
6a1af09
rename persistence for hub config
infeo Oct 25, 2021
c545195
change visibility of HubConfigPersistence constructor
infeo Oct 25, 2021
8718e35
do the wiring inside the configSourceFactory
infeo Oct 25, 2021
585a71c
rename HubConfigPersistence::read to load
infeo Oct 25, 2021
6519757
small fixes
infeo Oct 25, 2021
5d10b1f
remove default hub config path and choke when not set
infeo Oct 26, 2021
ed61794
throw uncheckedIo exception when unable to persist config
infeo Oct 26, 2021
64d0d5f
simplify
infeo Oct 26, 2021
e7b8b0d
* correctly use jboss logging
infeo Oct 26, 2021
ae82ae0
rework hub config:
infeo Oct 28, 2021
31d8c17
small changes:
infeo Oct 28, 2021
9f6b58e
use default implementation for `getProperties()`
overheadhunter Oct 28, 2021
95cab8d
increase ordinal, giving HubConfig higher priority than Quarkus' appl…
overheadhunter Oct 28, 2021
7a04616
formatted
overheadhunter Oct 28, 2021
0f5b3f2
Qualifier no longer required when injecting HubConfig
overheadhunter Oct 29, 2021
816ddcf
document `-Dhub.config.path`
overheadhunter Oct 29, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.cryptomator.hub.config;

import org.jboss.logging.Logger;
overheadhunter marked this conversation as resolved.
Show resolved Hide resolved

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

class HubConfigPersistence {

private static final Logger LOG = Logger.getLogger(HubConfigPersistence.class);
private static final String HUB_CONFIG_DESCRIPTION = "Cryptomator Hub configuration persistence file";

private final Path configPath;

HubConfigPersistence(Path configPath) {
this.configPath = configPath;
}

synchronized void persist(Map<String, String> config) {
var prop = new Properties();
prop.putAll(config);
try (var out = Files.newOutputStream(configPath, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
prop.store(out, HUB_CONFIG_DESCRIPTION);
} catch (IOException e) {
throw new UncheckedIOException("Unable to persist Hub config to "+configPath,e);
}
}


Map<String, String> load() {
Map<String, String> config = new HashMap<>();
try (var in = Files.newInputStream(configPath, StandardOpenOption.READ)) {
var prop = new Properties();
prop.load(in);
prop.forEach((k, v) -> config.put((String) k, (String) v));
} catch (IOException e) {
LOG.info("Unable to read hub config from {}, creating empty.", configPath, e);
}
return config;
}


}
65 changes: 65 additions & 0 deletions spi/src/main/java/org/cryptomator/hub/config/HubConfigSource.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.cryptomator.hub.config;

import org.eclipse.microprofile.config.ConfigProvider;
import org.eclipse.microprofile.config.spi.ConfigSource;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.StreamSupport;

public class HubConfigSource implements ConfigSource {

private static final String HUB_CONFIGSOURCE_NAME = "HubConfig";

private final HubConfigPersistence persistence;
private final ConcurrentHashMap<String, String> config;

public HubConfigSource(HubConfigPersistence persistence) {
this.config = new ConcurrentHashMap<>();
this.persistence = persistence;

config.putAll(persistence.load());
}

@Override
public Map<String, String> getProperties() {
return config;
}

@Override
public Set<String> getPropertyNames() {
return config.keySet();
}

@Override
//for default sources and their precedence see https://quarkus.io/guides/config-reference#configuration-sources
public int getOrdinal() {
return 240;
}

@Override
public String getValue(String s) {
return config.get(s);
}

@Override
public String getName() {
return HUB_CONFIGSOURCE_NAME;
}

public void setProperty(String key, String value) {
String oldVal = config.put(key, value);
if (oldVal == null || !oldVal.equals(value)) {
persistence.persist(config);
}
}

public static HubConfigSource getInstance() {
overheadhunter marked this conversation as resolved.
Show resolved Hide resolved
return (HubConfigSource) StreamSupport.stream(ConfigProvider.getConfig().getConfigSources().spliterator(), false)
.filter(s -> s instanceof HubConfigSource)
.findFirst()
.get();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.cryptomator.hub.config;

import io.smallrye.config.ConfigSourceContext;
import io.smallrye.config.ConfigSourceFactory;
import org.eclipse.microprofile.config.spi.ConfigSource;

import javax.enterprise.context.ApplicationScoped;
import java.nio.file.Path;
import java.util.List;

@ApplicationScoped
public class HubConfigSourceFactory implements ConfigSourceFactory {

private static final String HUBCONFIG_PROPERTY_KEY = "hub.config.path";
overheadhunter marked this conversation as resolved.
Show resolved Hide resolved

@Override
public Iterable<ConfigSource> getConfigSources(ConfigSourceContext configSourceContext) {
var s = configSourceContext.getValue(HUBCONFIG_PROPERTY_KEY);
if (s.getValue() == null) {
throw new IllegalStateException("Property \"" + HUBCONFIG_PROPERTY_KEY + "\" to point to hub config file is missing");
}
var hubConfigPersistence = new HubConfigPersistence(Path.of(s.getValue()));
var hubConfig = new HubConfigSource(hubConfigPersistence);
return List.of(hubConfig);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.cryptomator.hub.config.HubConfigSourceFactory