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

refactor: move configuration loading logic out of the extension context #4162

Merged
merged 3 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
15 changes: 11 additions & 4 deletions DEPENDENCIES
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
maven/mavencentral/com.apicatalog/carbon-did/0.3.0, Apache-2.0, approved, clearlydefined

Check warning on line 1 in DEPENDENCIES

View workflow job for this annotation

GitHub Actions / check / Dash-Verify-Licenses

Restricted Dependencies found

Some dependencies are marked 'restricted' - please review them
maven/mavencentral/com.apicatalog/copper-multibase/0.5.0, Apache-2.0, approved, #14501
maven/mavencentral/com.apicatalog/copper-multicodec/0.1.1, Apache-2.0, approved, #14500
maven/mavencentral/com.apicatalog/iron-ed25519-cryptosuite-2020/0.14.0, Apache-2.0, approved, #14503
Expand All @@ -16,27 +16,34 @@
maven/mavencentral/com.fasterxml.jackson.core/jackson-annotations/2.14.1, Apache-2.0, approved, #5303
maven/mavencentral/com.fasterxml.jackson.core/jackson-annotations/2.15.1, Apache-2.0, approved, #7947
maven/mavencentral/com.fasterxml.jackson.core/jackson-annotations/2.17.0, Apache-2.0, approved, #13672
maven/mavencentral/com.fasterxml.jackson.core/jackson-annotations/2.17.1, Apache-2.0, approved, #13672
maven/mavencentral/com.fasterxml.jackson.core/jackson-core/2.14.1, Apache-2.0 AND MIT, approved, #4303
maven/mavencentral/com.fasterxml.jackson.core/jackson-core/2.15.1, MIT AND Apache-2.0, approved, #7932
maven/mavencentral/com.fasterxml.jackson.core/jackson-core/2.17.0, , approved, #13665
maven/mavencentral/com.fasterxml.jackson.core/jackson-core/2.17.1, , approved, #13665
maven/mavencentral/com.fasterxml.jackson.core/jackson-databind/2.11.0, Apache-2.0, approved, CQ23093
maven/mavencentral/com.fasterxml.jackson.core/jackson-databind/2.14.0, Apache-2.0, approved, #4105
maven/mavencentral/com.fasterxml.jackson.core/jackson-databind/2.14.1, Apache-2.0, approved, #4105
maven/mavencentral/com.fasterxml.jackson.core/jackson-databind/2.15.1, Apache-2.0, approved, #7934
maven/mavencentral/com.fasterxml.jackson.core/jackson-databind/2.17.0, Apache-2.0, approved, #13671
maven/mavencentral/com.fasterxml.jackson.core/jackson-databind/2.17.1, Apache-2.0, approved, #13671
maven/mavencentral/com.fasterxml.jackson.dataformat/jackson-dataformat-yaml/2.14.0, Apache-2.0, approved, #5933
maven/mavencentral/com.fasterxml.jackson.dataformat/jackson-dataformat-yaml/2.15.1, Apache-2.0, approved, #8802
maven/mavencentral/com.fasterxml.jackson.dataformat/jackson-dataformat-yaml/2.17.0, Apache-2.0, approved, #13669
maven/mavencentral/com.fasterxml.jackson.dataformat/jackson-dataformat-yaml/2.17.1, Apache-2.0, approved, #13669
maven/mavencentral/com.fasterxml.jackson.datatype/jackson-datatype-jakarta-jsonp/2.17.0, Apache-2.0, approved, #14161
maven/mavencentral/com.fasterxml.jackson.datatype/jackson-datatype-jakarta-jsonp/2.17.1, Apache-2.0, approved, #14161
maven/mavencentral/com.fasterxml.jackson.datatype/jackson-datatype-jsr310/2.14.0, Apache-2.0, approved, #4699
maven/mavencentral/com.fasterxml.jackson.datatype/jackson-datatype-jsr310/2.15.1, Apache-2.0, approved, #7930
maven/mavencentral/com.fasterxml.jackson.datatype/jackson-datatype-jsr310/2.17.0, Apache-2.0, approved, #14160
maven/mavencentral/com.fasterxml.jackson.jakarta.rs/jackson-jakarta-rs-base/2.17.0, Apache-2.0, approved, #14194
maven/mavencentral/com.fasterxml.jackson.datatype/jackson-datatype-jsr310/2.17.1, Apache-2.0, approved, #14160
maven/mavencentral/com.fasterxml.jackson.jakarta.rs/jackson-jakarta-rs-base/2.17.1, Apache-2.0, approved, #14194
maven/mavencentral/com.fasterxml.jackson.jakarta.rs/jackson-jakarta-rs-json-provider/2.15.1, Apache-2.0, approved, #9236
maven/mavencentral/com.fasterxml.jackson.jakarta.rs/jackson-jakarta-rs-json-provider/2.17.0, Apache-2.0, approved, #14195
maven/mavencentral/com.fasterxml.jackson.jakarta.rs/jackson-jakarta-rs-json-provider/2.17.1, Apache-2.0, approved, #14195
maven/mavencentral/com.fasterxml.jackson.module/jackson-module-jakarta-xmlbind-annotations/2.17.0, Apache-2.0, approved, #13668
maven/mavencentral/com.fasterxml.jackson.module/jackson-module-jakarta-xmlbind-annotations/2.17.1, Apache-2.0, approved, #13668
maven/mavencentral/com.fasterxml.jackson/jackson-bom/2.15.1, Apache-2.0, approved, #7929
maven/mavencentral/com.fasterxml.jackson/jackson-bom/2.17.0, Apache-2.0, approved, #14162
maven/mavencentral/com.fasterxml.jackson/jackson-bom/2.17.1, Apache-2.0, approved, #14162
maven/mavencentral/com.fasterxml.uuid/java-uuid-generator/4.1.0, Apache-2.0, approved, clearlydefined
maven/mavencentral/com.github.cliftonlabs/json-simple/3.0.2, Apache-2.0, approved, clearlydefined
maven/mavencentral/com.github.docker-java/docker-java-api/3.3.6, Apache-2.0, approved, #10346
Expand Down Expand Up @@ -78,7 +85,7 @@
maven/mavencentral/com.networknt/json-schema-validator/1.0.76, Apache-2.0, approved, CQ22638
maven/mavencentral/com.nimbusds/nimbus-jose-jwt/9.28, Apache-2.0, approved, clearlydefined
maven/mavencentral/com.nimbusds/nimbus-jose-jwt/9.37.3, Apache-2.0, approved, #11701
maven/mavencentral/com.puppycrawl.tools/checkstyle/10.15.0, LGPL-2.1-or-later, restricted, clearlydefined
maven/mavencentral/com.puppycrawl.tools/checkstyle/10.16.0, , restricted, clearlydefined
maven/mavencentral/com.samskivert/jmustache/1.15, BSD-2-Clause, approved, clearlydefined
maven/mavencentral/com.squareup.okhttp3/okhttp-dnsoverhttps/4.12.0, Apache-2.0, approved, #11159
maven/mavencentral/com.squareup.okhttp3/okhttp/4.12.0, Apache-2.0, approved, #11156
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
*
*/

package org.eclipse.edc.boot.config;

import org.eclipse.edc.boot.system.ServiceLocator;
import org.eclipse.edc.spi.monitor.Monitor;
import org.eclipse.edc.spi.system.ConfigurationExtension;
import org.eclipse.edc.spi.system.configuration.Config;
import org.eclipse.edc.spi.system.configuration.ConfigFactory;

import java.util.Objects;

/**
* Load configuration from configuration extensions, environment variables and system properties.
*/
public class ConfigurationLoader {
private final ServiceLocator serviceLocator;
private final EnvironmentVariables environmentVariables;
private final SystemProperties systemProperties;

public ConfigurationLoader(ServiceLocator serviceLocator, EnvironmentVariables environmentVariables, SystemProperties systemProperties) {
this.serviceLocator = serviceLocator;
this.environmentVariables = environmentVariables;
this.systemProperties = systemProperties;
}

/**
* Load configuration.
* Please note that Environment variables keys will be converted from the ENVIRONMENT_NOTATION to the dot.notation.
*
* @param monitor the monitor.
* @return the Config instance.
*/
public Config loadConfiguration(Monitor monitor) {
var config = serviceLocator.loadImplementors(ConfigurationExtension.class, false)
.stream().peek(extension -> {
extension.initialize(monitor);
monitor.info("Initialized " + extension.name());
})
.map(ConfigurationExtension::getConfig)
.filter(Objects::nonNull)
.reduce(Config::merge)
.orElse(ConfigFactory.empty());

var environmentConfig = ConfigFactory.fromEnvironment(environmentVariables.get());
var systemPropertyConfig = ConfigFactory.fromProperties(systemProperties.get());

return config.merge(environmentConfig).merge(systemPropertyConfig);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
*
*/

package org.eclipse.edc.boot.config;

import java.util.Map;
import java.util.function.Supplier;


/**
* Wrapping interface that provides Environment Variables
*/
public interface EnvironmentVariables extends Supplier<Map<String, String>> {

/**
* Default implementation.
*
* @return the EnvironmentVariables default implementation.
*/
static EnvironmentVariables ofDefault() {
return System::getenv;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2024 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
*
* This program and the accompanying materials are made available under the
* terms of the Apache License, Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - initial API and implementation
*
*/

package org.eclipse.edc.boot.config;

import java.util.Properties;
import java.util.function.Supplier;

/**
* Wrapping interface that provides System Properties
*/
public interface SystemProperties extends Supplier<Properties> {

/**
* Default implementation.
*
* @return the SystemProperties default implementation.
*/
static SystemProperties ofDefault() {
return System::getProperties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,11 @@
import org.eclipse.edc.boot.BootServicesExtension;
import org.eclipse.edc.spi.EdcException;
import org.eclipse.edc.spi.monitor.Monitor;
import org.eclipse.edc.spi.system.ConfigurationExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.system.configuration.Config;
import org.eclipse.edc.spi.system.configuration.ConfigFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

import static org.eclipse.edc.boot.BootServicesExtension.RUNTIME_ID;
Expand All @@ -41,14 +37,13 @@ public class DefaultServiceExtensionContext implements ServiceExtensionContext {
private static final String EDC_CONNECTOR_NAME = "edc.connector.name";

private final Map<Class<?>, Object> services = new HashMap<>();
private final List<ConfigurationExtension> configurationExtensions;
private final Config config;
private boolean isReadOnly = false;
private String participantId;
private String runtimeId;
private Config config;

public DefaultServiceExtensionContext(Monitor monitor, List<ConfigurationExtension> configurationExtensions) {
this.configurationExtensions = configurationExtensions;
public DefaultServiceExtensionContext(Monitor monitor, Config config) {
this.config = config;
// register as service
registerService(Monitor.class, monitor);
}
Expand Down Expand Up @@ -108,11 +103,6 @@ public <T> void registerService(Class<T> type, T service) {

@Override
public void initialize() {
configurationExtensions.forEach(ext -> {
ext.initialize(getMonitor());
getMonitor().info("Initialized " + ext.name());
});
config = loadConfig();
participantId = getSetting(BootServicesExtension.PARTICIPANT_ID, ANONYMOUS_PARTICIPANT);
if (ANONYMOUS_PARTICIPANT.equals(participantId)) {
getMonitor().warning("The runtime is configured as an anonymous participant. DO NOT DO THIS IN PRODUCTION.");
Expand All @@ -136,21 +126,4 @@ public void initialize() {

}

// this method exists so that getting env vars can be mocked during testing
protected Map<String, String> getEnvironmentVariables() {
return System.getenv();
}

private Config loadConfig() {
var config = configurationExtensions.stream()
.map(ConfigurationExtension::getConfig)
.filter(Objects::nonNull)
.reduce(Config::merge)
.orElse(ConfigFactory.empty());

var environmentConfig = ConfigFactory.fromEnvironment(getEnvironmentVariables());
var systemPropertyConfig = ConfigFactory.fromProperties(System.getProperties());

return config.merge(environmentConfig).merge(systemPropertyConfig);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@
package org.eclipse.edc.boot.system.runtime;


import org.eclipse.edc.boot.config.ConfigurationLoader;
import org.eclipse.edc.boot.config.EnvironmentVariables;
import org.eclipse.edc.boot.config.SystemProperties;
import org.eclipse.edc.boot.system.DefaultServiceExtensionContext;
import org.eclipse.edc.boot.system.ExtensionLoader;
import org.eclipse.edc.boot.system.ServiceLocator;
import org.eclipse.edc.boot.system.ServiceLocatorImpl;
import org.eclipse.edc.boot.system.injection.InjectionContainer;
import org.eclipse.edc.spi.EdcException;
import org.eclipse.edc.spi.monitor.Monitor;
import org.eclipse.edc.spi.system.ConfigurationExtension;
import org.eclipse.edc.spi.system.MonitorExtension;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.system.configuration.Config;
import org.eclipse.edc.spi.system.health.HealthCheckResult;
import org.eclipse.edc.spi.system.health.HealthCheckService;
import org.jetbrains.annotations.NotNull;
Expand All @@ -43,7 +46,7 @@
* the connector. It goes through the following steps, all of which are overridable:
* <ul>
* <li>{@link BaseRuntime#createMonitor()} : instantiates a new {@link Monitor}</li>
* <li>{@link BaseRuntime#createContext(Monitor)}: creates a new {@link DefaultServiceExtensionContext} and invokes its {@link DefaultServiceExtensionContext#initialize()} method</li>
* <li>{@link BaseRuntime#createContext(Monitor, Config)}: creates a new {@link DefaultServiceExtensionContext} and invokes its {@link DefaultServiceExtensionContext#initialize()} method</li>
* <li>{@link BaseRuntime#createExtensions(ServiceExtensionContext)}: creates a list of {@code ServiceExtension} objects. By default, these are created through {@link ExtensionLoader#loadServiceExtensions(ServiceExtensionContext)}</li>
* <li>{@link BaseRuntime#bootExtensions(ServiceExtensionContext, List)}: initializes the service extensions by putting them through their lifecycle.
* By default this calls {@link ExtensionLoader#bootServiceExtensions(List, ServiceExtensionContext)} </li>
Expand All @@ -54,6 +57,7 @@ public class BaseRuntime {

private static String[] programArgs = new String[0];
private final ExtensionLoader extensionLoader;
private final ConfigurationLoader configurationLoader;
private final List<ServiceExtension> serviceExtensions = new ArrayList<>();
protected Monitor monitor;

Expand All @@ -63,6 +67,7 @@ public BaseRuntime() {

protected BaseRuntime(ServiceLocator serviceLocator) {
extensionLoader = new ExtensionLoader(serviceLocator);
configurationLoader = new ConfigurationLoader(serviceLocator, EnvironmentVariables.ofDefault(), SystemProperties.ofDefault());
}

public static void main(String[] args) {
Expand Down Expand Up @@ -106,20 +111,10 @@ protected Monitor getMonitor() {
}

@NotNull
protected ServiceExtensionContext createServiceExtensionContext() {
var context = createContext(monitor);
initializeContext(context);
return context;
}

/**
* Initializes the context. If {@link BaseRuntime#createContext(Monitor)} is overridden and the (custom) context
* needs to be initialized, this method should be overridden as well.
*
* @param context The context.
*/
protected void initializeContext(ServiceExtensionContext context) {
protected ServiceExtensionContext createServiceExtensionContext(Config config) {
var context = createContext(monitor, config);
context.initialize();
return context;
}

/**
Expand Down Expand Up @@ -155,15 +150,12 @@ protected List<InjectionContainer<ServiceExtension>> createExtensions(ServiceExt
* this would likely need to be overridden.
*
* @param monitor a Monitor
* @param config the cofiguratiohn
* @return a {@code ServiceExtensionContext}
*/
@NotNull
protected ServiceExtensionContext createContext(Monitor monitor) {
return new DefaultServiceExtensionContext(monitor, loadConfigurationExtensions());
}

protected List<ConfigurationExtension> loadConfigurationExtensions() {
return extensionLoader.loadExtensions(ConfigurationExtension.class, false);
protected ServiceExtensionContext createContext(Monitor monitor, Config config) {
return new DefaultServiceExtensionContext(monitor, config);
}

/**
Expand All @@ -179,7 +171,8 @@ protected Monitor createMonitor() {

private void boot(boolean addShutdownHook) {
monitor = createMonitor();
var context = createServiceExtensionContext();
var config = configurationLoader.loadConfiguration(monitor);
var context = createServiceExtensionContext(config);

try {
var newExtensions = createExtensions(context);
Expand Down
Loading
Loading