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 14 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;

}
73 changes: 58 additions & 15 deletions avaje-config/src/main/java/io/avaje/config/ConfigServiceLoader.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package io.avaje.config;

import static java.util.stream.Collectors.toMap;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.function.Function;

/**
* 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,40 +27,77 @@ 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 Map<String, URIConfigLoader> uriLoaders;
private final Map<String, ConfigParser> parsers;

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

for (var spi : ServiceLoader.load(ConfigExtension.class)) {
if (spi instanceof ConfigurationSource) {
sources.add((ConfigurationSource) spi);
} else if (spi instanceof ConfigurationPlugin) {
plugins.add((ConfigurationPlugin) spi);
} else if (spi instanceof ConfigParser) {
} else if (spi instanceof ConfigParser && !Boolean.getBoolean("skipCustomParsing")) {
SentryMan marked this conversation as resolved.
Show resolved Hide resolved
otherParsers.add((ConfigParser) spi);
} else if (spi instanceof URIConfigLoader) {
loaders.add((URIConfigLoader) spi);
} else if (spi instanceof ConfigurationLog) {
_log = (ConfigurationLog) spi;
spiLog = (ConfigurationLog) spi;
} else if (spi instanceof ResourceLoader) {
_resourceLoader = (ResourceLoader) spi;
spiResourceLoader = (ResourceLoader) spi;
} else if (spi instanceof ModificationEventRunner) {
_eventRunner = (ModificationEventRunner) spi;
spiEventRunner = (ModificationEventRunner) spi;
}
}

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.log = spiLog == null ? new DefaultConfigurationLog() : spiLog;
this.resourceLoader =
spiResourceLoader == null ? new DefaultResourceLoader() : spiResourceLoader;
this.eventRunner =
spiEventRunner == null ? new CoreConfiguration.ForegroundEventRunner() : spiEventRunner;

this.parsers = initParsers(otherParsers);
this.uriLoaders = loaders.stream().collect(toMap((Function<? super URIConfigLoader, ? extends String>) URIConfigLoader::supportedScheme, Function.identity()));
}

Parsers parsers() {
Map<String, ConfigParser> initParsers(List<ConfigParser> parsers) {

var parserMap = new HashMap<String, ConfigParser>();
parserMap.put("properties", new PropertiesParser());
if (!Boolean.getBoolean("skipYaml")) {

YamlLoader yamlLoader;
try {
Class.forName("org.yaml.snakeyaml.Yaml");
yamlLoader = new YamlLoaderSnake();
} catch (ClassNotFoundException e) {
yamlLoader = new YamlLoaderSimple();
}
parserMap.put("yml", yamlLoader);
parserMap.put("yaml", yamlLoader);
}

for (ConfigParser parser : parsers) {
for (var ext : parser.supportedExtensions()) {
parserMap.put(ext, parser);
}
}
return Collections.unmodifiableMap(parserMap);
}

Map<String, ConfigParser> parsers() {
return parsers;
}

public Map<String, URIConfigLoader> uriLoaders() {
return uriLoaders;
}

ConfigurationLog log() {
return log;
}
Expand Down
27 changes: 20 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,28 @@
package io.avaje.config;

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

final class CoreComponents {

private final ModificationEventRunner runner;
private final ConfigurationLog log;
private final Parsers parsers;
private final Map<String, ConfigParser> parsers;
private final Map<String, URIConfigLoader> 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,
Map<String, ConfigParser> parsers,
Map<String, URIConfigLoader> 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 +31,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 = ConfigServiceLoader.get().parsers();
this.uriLoaders = ConfigServiceLoader.get().uriLoaders();
this.sources = List.of();
this.plugins = List.of();
}

Parsers parsers() {
Map<String, ConfigParser> parsers() {
return parsers;
}

public Map<String, URIConfigLoader> 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 Map<String, ConfigParser> 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 Map<String, ConfigParser> parsers = serviceLoader.parsers();
private final Map<String, URIConfigLoader> 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 Map<String, ConfigParser> 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, Map<String, ConfigParser> parsers) {
this.log = configuration.log();
this.configuration = configuration;
this.delay = configuration.getLong("config.watch.delay", 60);
Expand Down
51 changes: 43 additions & 8 deletions 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,14 @@
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.net.URISyntaxException;
import java.util.*;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -41,10 +44,12 @@ enum Source {
private final ConfigurationLog log;
private final InitialLoadContext loadContext;
private final Set<String> profileResourceLoaded = new HashSet<>();
private final Parsers parsers;
private final Map<String, ConfigParser> parsers;
private final Map<String, URIConfigLoader> 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 @@ -153,7 +158,7 @@ private void loadCommandLineArg(String arg) {

private boolean isValidExtension(String arg) {
var extension = arg.substring(arg.lastIndexOf(".") + 1);
return "properties".equals(extension) || parsers.supportsExtension(extension);
return "properties".equals(extension) || parsers.containsKey(extension);
}

/**
Expand Down Expand Up @@ -253,23 +258,53 @@ private void loadViaSystemProperty() {
}

boolean loadWithExtensionCheck(String fileName) {

if (loadURI(fileName)) return true;

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

return loadProperties(fileName, RESOURCE) | loadProperties(fileName, FILE);
} else {
var parser = parsers.get(extension);
if (parser == null) {
throw new IllegalArgumentException(
"Expecting only properties or "
+ parsers.supportedExtensions()
+ " file extensions but got ["
+ fileName
+ "]");
"Expecting only properties or "
+ parsers.keySet()
+ " file extensions or "
+ uriLoaders.keySet().stream().map(s -> s + ":/").collect(joining(","))
+ "uri schemes but got ["
+ fileName
+ "]");
}

return loadCustomExtension(fileName, parser, RESOURCE)
| loadCustomExtension(fileName, parser, FILE);
| loadCustomExtension(fileName, parser, FILE);
}
}

private boolean loadURI(String fileName) {
URI uri;
try {
uri = new URI(fileName);
} catch (URISyntaxException e) {
return false;
}

var scheme = uri.getScheme();

if (scheme == null || "classpath".equals(scheme) || "file".equals(scheme)) {

return false;
}

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;
}
return false;
}

/**
Expand Down
Loading