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 URI Loader SPI #176

Draft
wants to merge 30 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
2e7f67f
Add Uri Loader SPI
SentryMan Sep 20, 2024
9c09b7b
Delete EnvLoader.java
SentryMan Sep 23, 2024
78ac3e2
Update URILoaders.java
SentryMan Sep 23, 2024
9d4e474
make an interface
SentryMan Sep 23, 2024
77eb18f
Update FileWatchTest.java
SentryMan Sep 23, 2024
6546ce6
Update URILoaders.java
SentryMan Sep 24, 2024
6b83064
Update InitialLoader.java
SentryMan Sep 26, 2024
6199632
replace configparsers with a map
SentryMan Sep 28, 2024
e8ab6cb
fix tests
SentryMan Sep 28, 2024
e9a7162
replace URILoaders with a map
SentryMan Sep 28, 2024
dc4c661
prevent duplicate loading
SentryMan Sep 28, 2024
00abb12
Revert "prevent duplicate loading"
SentryMan Sep 28, 2024
b2a5c26
Update InitialLoader.java
SentryMan Sep 28, 2024
f02a32a
Update InitialLoader.java
SentryMan Oct 9, 2024
6aecc89
add redact function
SentryMan Oct 13, 2024
b173f09
Update InitialLoader.java
SentryMan Oct 13, 2024
5655302
add uri context
SentryMan Oct 13, 2024
a85dbb9
rename to uriParser
SentryMan Oct 13, 2024
905d233
add utility for query parsing
SentryMan Oct 13, 2024
45a3377
Merge branch 'master' into uri-loader
SentryMan Oct 13, 2024
6f995a2
rename
SentryMan Oct 14, 2024
a1927cb
Update URILoadContext.java
SentryMan Oct 14, 2024
c818296
multiple schemas to a loader
SentryMan Oct 15, 2024
4d0198f
load test properties before load properties
SentryMan Oct 16, 2024
aa6d5e2
add log
SentryMan Oct 16, 2024
133fe18
supports uri
SentryMan Oct 24, 2024
b4fccb7
Merge branch 'master' into uri-loader
SentryMan Oct 25, 2024
e1987a9
Update module-info.java
SentryMan Oct 28, 2024
c2e623e
re-add parsers
SentryMan Oct 29, 2024
e18267a
only parsers
SentryMan Oct 29, 2024
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
5 changes: 3 additions & 2 deletions avaje-config-toml/src/main/java/module-info.java
SentryMan marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import io.avaje.config.ConfigExtension;
import io.avaje.config.toml.TomlParser;

module io.avaje.config.toml {

requires io.avaje.config;
requires transitive io.avaje.config;
requires org.tomlj;

exports io.avaje.config.toml;

provides io.avaje.config.ConfigExtension with TomlParser;
provides ConfigExtension with TomlParser;

}
SentryMan marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
import java.util.Set;

/**
* Holds the non-properties ConfigParsers.
* Holds the ConfigParsers for various extension types.
*/
final class Parsers {
public final class ConfigParsers {
SentryMan marked this conversation as resolved.
Show resolved Hide resolved

private final Map<String, ConfigParser> parserMap = new HashMap<>();
SentryMan marked this conversation as resolved.
Show resolved Hide resolved

Parsers(List<ConfigParser> otherParsers) {
ConfigParsers(List<ConfigParser> otherParsers) {
parserMap.put("properties", new PropertiesParser());
if (!"true".equals(System.getProperty("skipYaml"))) {
initYamlParser();
Expand Down Expand Up @@ -45,28 +45,28 @@ private void initParsers(List<ConfigParser> otherParsers) {
/**
* Return the extension ConfigParser pairs.
*/
Set<Map.Entry<String, ConfigParser>> entrySet() {
public Set<Map.Entry<String, ConfigParser>> entrySet() {
return parserMap.entrySet();
}

/**
* Return the ConfigParser for the given extension.
*/
ConfigParser get(String extension) {
public ConfigParser get(String extension) {
return parserMap.get(extension.toLowerCase());
}

/**
* Return true if the extension has a matching parser.
*/
boolean supportsExtension(String extension) {
public boolean supportsExtension(String extension) {
return parserMap.containsKey(extension.toLowerCase());
}

/**
* Return the set of supported extensions.
*/
Set<String> supportedExtensions() {
public Set<String> supportedExtensions() {
return parserMap.keySet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import java.util.ServiceLoader;

/**
* Load all the avaje-config extensions via ServiceLoader using the single
* common ConfigExtension interface.
* Load all the avaje-config extensions via ServiceLoader using the single common ConfigExtension
* interface.
*/
final class ConfigServiceLoader {

Expand All @@ -21,13 +21,15 @@ static ConfigServiceLoader get() {
private final ModificationEventRunner eventRunner;
private final List<ConfigurationSource> sources = new ArrayList<>();
private final List<ConfigurationPlugin> plugins = new ArrayList<>();
private final Parsers parsers;
private final URILoaders uriLoaders;
private final ConfigParsers parsers;

ConfigServiceLoader() {
ModificationEventRunner _eventRunner = null;
ConfigurationLog _log = null;
ResourceLoader _resourceLoader = null;
List<ConfigParser> otherParsers = new ArrayList<>();
List<URIConfigLoader> loaders = new ArrayList<>();

for (var spi : ServiceLoader.load(ConfigExtension.class)) {
if (spi instanceof ConfigurationSource) {
Expand All @@ -36,6 +38,8 @@ static ConfigServiceLoader get() {
plugins.add((ConfigurationPlugin) spi);
} else if (spi instanceof ConfigParser) {
otherParsers.add((ConfigParser) spi);
} else if (spi instanceof URIConfigLoader) {
loaders.add((URIConfigLoader) spi);
} else if (spi instanceof ConfigurationLog) {
_log = (ConfigurationLog) spi;
} else if (spi instanceof ResourceLoader) {
Expand All @@ -47,14 +51,20 @@ static ConfigServiceLoader get() {

this.log = _log == null ? new DefaultConfigurationLog() : _log;
this.resourceLoader = _resourceLoader == null ? new DefaultResourceLoader() : _resourceLoader;
this.eventRunner = _eventRunner == null ? new CoreConfiguration.ForegroundEventRunner() : _eventRunner;
this.parsers = new Parsers(otherParsers);
this.eventRunner =
_eventRunner == null ? new CoreConfiguration.ForegroundEventRunner() : _eventRunner;
this.parsers = new ConfigParsers(otherParsers);
this.uriLoaders = new URILoaders(loaders);
}

Parsers parsers() {
ConfigParsers parsers() {
return parsers;
}

public URILoaders uriLoaders() {
return uriLoaders;
}

ConfigurationLog log() {
return log;
}
Expand Down
26 changes: 19 additions & 7 deletions avaje-config/src/main/java/io/avaje/config/CoreComponents.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
package io.avaje.config;

import java.util.Collections;
import java.util.List;

final class CoreComponents {

private final ModificationEventRunner runner;
private final ConfigurationLog log;
private final Parsers parsers;
private final ConfigParsers parsers;
private final URILoaders uriLoaders;
private final List<ConfigurationSource> sources;
private final List<ConfigurationPlugin> plugins;

CoreComponents(ModificationEventRunner runner, ConfigurationLog log, Parsers parsers, List<ConfigurationSource> sources, List<ConfigurationPlugin> plugins) {
CoreComponents(
ModificationEventRunner runner,
ConfigurationLog log,
ConfigParsers parsers,
URILoaders uriLoaders,
List<ConfigurationSource> sources,
List<ConfigurationPlugin> plugins) {
this.runner = runner;
this.log = log;
this.parsers = parsers;
this.uriLoaders = uriLoaders;
this.sources = sources;
this.plugins = plugins;
}
Expand All @@ -23,15 +30,20 @@ final class CoreComponents {
CoreComponents() {
this.runner = new CoreConfiguration.ForegroundEventRunner();
this.log = new DefaultConfigurationLog();
this.parsers = new Parsers(Collections.emptyList());
this.sources = Collections.emptyList();
this.plugins = Collections.emptyList();
this.parsers = new ConfigParsers(List.of());
this.uriLoaders = new URILoaders(List.of());
this.sources = List.of();
this.plugins = List.of();
}

Parsers parsers() {
ConfigParsers parsers() {
return parsers;
}

public URILoaders uriLoaders() {
return uriLoaders;
}

ConfigurationLog log() {
return log;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
@NullMarked
final class CoreConfiguration implements Configuration {

private final Parsers parsers;
private final ConfigParsers parsers;
private final ConfigurationLog log;
private final ModifyAwareProperties properties;
private final ReentrantLock lock = new ReentrantLock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ final class CoreConfigurationBuilder implements Configuration.Builder {

private final CoreEntry.CoreMap sourceMap = CoreEntry.newMap();
private final ConfigServiceLoader serviceLoader = ConfigServiceLoader.get();
private final Parsers parsers = serviceLoader.parsers();
private final ConfigParsers parsers = serviceLoader.parsers();
private final URILoaders uriLoaders = serviceLoader.uriLoaders();
private ConfigurationLog log = serviceLoader.log();
private ResourceLoader resourceLoader = serviceLoader.resourceLoader();
private ModificationEventRunner eventRunner = serviceLoader.eventRunner();
Expand Down Expand Up @@ -130,7 +131,14 @@ public Configuration.Builder includeResourceLoading() {

@Override
public Configuration build() {
var components = new CoreComponents(eventRunner, log, parsers, serviceLoader.sources(), serviceLoader.plugins());
var components =
new CoreComponents(
eventRunner,
log,
parsers,
uriLoaders,
serviceLoader.sources(),
serviceLoader.plugins());
if (includeResourceLoading) {
log.preInitialisation();
initialLoader = new InitialLoader(components, resourceLoader);
Expand Down
4 changes: 2 additions & 2 deletions avaje-config/src/main/java/io/avaje/config/FileWatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ final class FileWatch {

private final ConfigurationLog log;
private final Configuration configuration;
private final Parsers parsers;
private final ConfigParsers parsers;
private final List<Entry> files;
private final long delay;
private final long period;

FileWatch(CoreConfiguration configuration, List<File> loadedFiles, Parsers parsers) {
FileWatch(CoreConfiguration configuration, List<File> loadedFiles, ConfigParsers parsers) {
this.log = configuration.log();
this.configuration = configuration;
this.delay = configuration.getLong("config.watch.delay", 60);
Expand Down
32 changes: 31 additions & 1 deletion avaje-config/src/main/java/io/avaje/config/InitialLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import static io.avaje.config.InitialLoader.Source.FILE;
import static io.avaje.config.InitialLoader.Source.RESOURCE;
import static java.lang.System.Logger.Level.WARNING;
import static java.util.stream.Collectors.joining;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.net.URI;
import java.util.*;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -41,10 +43,12 @@ enum Source {
private final ConfigurationLog log;
private final InitialLoadContext loadContext;
private final Set<String> profileResourceLoaded = new HashSet<>();
private final Parsers parsers;
private final ConfigParsers parsers;
private final URILoaders uriLoaders;

InitialLoader(CoreComponents components, ResourceLoader resourceLoader) {
this.parsers = components.parsers();
this.uriLoaders = components.uriLoaders();
this.log = components.log();
this.loadContext = new InitialLoadContext(log, resourceLoader);
}
Expand Down Expand Up @@ -253,8 +257,17 @@ private void loadViaSystemProperty() {
}

boolean loadWithExtensionCheck(String fileName) {

// no need to custom URL load regular cp and file schemes
fileName = fileName.replace("classpath:/", "").replace("file:/", "");
if (fileName.contains(":/")) {
SentryMan marked this conversation as resolved.
Show resolved Hide resolved

return loadURI(fileName);
}

var extension = fileName.substring(fileName.lastIndexOf(".") + 1);
if ("properties".equals(extension)) {

return loadProperties(fileName, RESOURCE) | loadProperties(fileName, FILE);
} else {
var parser = parsers.get(extension);
Expand All @@ -272,6 +285,23 @@ boolean loadWithExtensionCheck(String fileName) {
}
}

private boolean loadURI(String uriPath) {
var uri = URI.create(uriPath);
final var scheme = uri.getScheme();
SentryMan marked this conversation as resolved.
Show resolved Hide resolved
var loader = uriLoaders.get(scheme);
if (loader != null) {
loader.load(uri, parsers).forEach((k, v) -> loadContext.put(k, v, "uri scheme " + scheme));
SentryMan marked this conversation as resolved.
Show resolved Hide resolved
return true;
}

throw new IllegalArgumentException(
"Expecting only properties or "
+ uriLoaders.supportedSchemes().stream().map(s -> s + ":/").collect(joining(","))
+ " uris but got ["
+ uriPath
+ "]");
}

/**
* Evaluate all the configuration entries and return as properties.
*/
Expand Down
24 changes: 24 additions & 0 deletions avaje-config/src/main/java/io/avaje/config/URIConfigLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.avaje.config;

import java.net.URI;
import java.util.Map;

/** Additional source to load and update configuration. */
public interface URIConfigLoader extends ConfigExtension {

/** URI Scheme Supported by this loader */
String supportedScheme();

/**
* Load additional configuration.
*
* <p>The {@link Configuration#setProperty(String, String)} method is used when loading the
* additional properties from the source.
*
* <p>Also note that the source can additionally use {@link Configuration#schedule(long, long,
* Runnable)} to schedule a period task to for example refresh data etc.
*
* @param configuration The configuration with initially properties.
*/
Map<String, String> load(URI uri, ConfigParsers parsers);
}
40 changes: 40 additions & 0 deletions avaje-config/src/main/java/io/avaje/config/URILoaders.java
SentryMan marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.avaje.config;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/** Holds the non-properties ConfigParsers. */
public final class URILoaders {

private final Map<String, URIConfigLoader> parserMap = new HashMap<>();

URILoaders(List<URIConfigLoader> loaders) {
for (var parser : loaders) {

parserMap.put(parser.supportedScheme(), parser);
}
}

/** Return the extension ConfigParser pairs. */
public Set<Entry<String, URIConfigLoader>> entrySet() {
return parserMap.entrySet();
}

/** Return the ConfigParser for the given extension. */
public URIConfigLoader get(String extension) {
SentryMan marked this conversation as resolved.
Show resolved Hide resolved
return parserMap.get(extension.toLowerCase());
}

/** Return true if the extension has a matching parser. */
public boolean supportsScheme(String extension) {
return parserMap.containsKey(extension.toLowerCase());
}

/** Return the set of supported extensions. */
public Set<String> supportedSchemes() {
return parserMap.keySet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void test_check_whenFileWritten() throws Exception {
}

private static FileWatch fileWatch(CoreConfiguration config, List<File> files) {
return new FileWatch(config, files, new Parsers(Collections.emptyList()));
return new FileWatch(config, files, new ConfigParsers(Collections.emptyList()));
}

private void writeContent(String content) throws IOException {
Expand Down