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 all 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
32 changes: 21 additions & 11 deletions avaje-config/src/main/java/io/avaje/config/ConfigServiceLoader.java
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 List<URIConfigLoader> uriLoaders;
private final Parsers 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) {
Expand All @@ -36,25 +38,33 @@ 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;
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.log = spiLog == null ? new DefaultConfigurationLog() : spiLog;
this.resourceLoader = spiResourceLoader == null ? new DefaultResourceLoader() : spiResourceLoader;
this.eventRunner =
spiEventRunner == null ? new CoreConfiguration.ForegroundEventRunner() : spiEventRunner;
this.parsers = new Parsers(otherParsers);
this.uriLoaders = loaders;
}

Parsers parsers() {
return parsers;
}

public List<URIConfigLoader> uriLoaders() {
return uriLoaders;
}

ConfigurationLog log() {
return log;
}
Expand Down
22 changes: 17 additions & 5 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 List<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,
Parsers parsers,
List<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 +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 = ConfigServiceLoader.get().parsers();
this.uriLoaders = ConfigServiceLoader.get().uriLoaders();
this.sources = List.of();
this.plugins = List.of();
}

Parsers parsers() {
return parsers;
}

public List<URIConfigLoader> uriLoaders() {
return uriLoaders;
}

ConfigurationLog log() {
return log;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.io.FileReader;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.List;
import java.util.Map;
import java.util.Properties;

Expand All @@ -19,6 +20,7 @@ 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 List<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 +132,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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

/**
Expand Down Expand Up @@ -109,6 +110,13 @@ private InputStream resourceStream(String resourcePath) {
return resourceLoader.getResourceAsStream(resourcePath);
}

/** Get a property */
Optional<String> get(String key) {

return Optional.ofNullable(map.get(key))
.map(e -> e.needsEvaluation() ? eval(e.value()) : e.value());
}

/**
* Add a property entry.
*/
Expand Down
65 changes: 58 additions & 7 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 @@ -40,13 +43,17 @@ enum Source {

private final ConfigurationLog log;
private final InitialLoadContext loadContext;
private final DURILoadContext uriContext;
private final Set<String> profileResourceLoaded = new HashSet<>();
private final Parsers parsers;
private final List<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);
this.uriContext = new DURILoadContext(log, parsers, loadContext::get);
}

Set<String> loadedFrom() {
Expand Down Expand Up @@ -113,12 +120,12 @@ void loadLocalFiles() {
loadViaProfiles(RESOURCE);
loadViaProfiles(FILE);
loadViaSystemProperty();
loadViaIndirection();
// test configuration (if found) overrides main configuration
// we should only find these resources when running tests
if (!loadTest()) {
loadLocalDev();
}
loadViaIndirection();
loadViaCommandLineArgs();
}

Expand Down Expand Up @@ -252,23 +259,67 @@ 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.supportedExtensions()
+ " file extensions or "
+ uriLoaders.stream().map(s -> s.getClass().getSimpleName()).collect(joining(","))
+ " compatible 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 = getLoader(uri);

if (loader != null) {
final var source = loader.redact(uri);
loader.load(uri, uriContext).forEach((k, v) -> loadContext.put(k, v, source));
return true;
}
return false;
}

@Nullable
private URIConfigLoader getLoader(URI uri) {

for (var loader : uriLoaders) {

if (loader.supports(uri)) {
return loader;
}
}

return null;
}

/**
Expand Down
23 changes: 4 additions & 19 deletions avaje-config/src/main/java/io/avaje/config/Parsers.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
import java.util.Map;
import java.util.Set;

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

private final Map<String, ConfigParser> parserMap = new HashMap<>();
Expand Down Expand Up @@ -42,31 +39,19 @@ 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();
}
}
25 changes: 25 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,25 @@
package io.avaje.config;

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

/** Custom URI configuration parser for reading load.properties properties that use a URI. */
public interface URIConfigLoader extends ConfigExtension {

/** redact any sensitive information in the URI when displayed by logging */
default String redact(URI uri) {

return uri.toString();
}

/** Whether the URI is supported by this loader */
boolean supports(URI uri);

/**
* @param uri uri from which to load data
* @param parsers map of {@link ConfigParser} available to assist in parsing data, keyed by {@link
* ConfigParser#supportedExtensions()}
* @return key/value map of loaded properties
*/
Map<String, String> load(URI uri, URILoadContext ctx);
}
Loading