Skip to content

Commit

Permalink
fix: @setting annotations placement (#4024)
Browse files Browse the repository at this point in the history
  • Loading branch information
ndr-brt committed Mar 19, 2024
1 parent 87e80e8 commit 1055dac
Show file tree
Hide file tree
Showing 49 changed files with 626 additions and 707 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.eclipse.edc.runtime.metamodel.annotation.BaseExtension;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Provider;
import org.eclipse.edc.runtime.metamodel.annotation.Setting;
import org.eclipse.edc.spi.system.ServiceExtension;
import org.eclipse.edc.spi.telemetry.Telemetry;
import org.eclipse.edc.spi.types.TypeManager;
Expand All @@ -31,6 +32,9 @@ public class BootServicesExtension implements ServiceExtension {

public static final String NAME = "Boot Services";

@Setting(value = "Configures the participant id this runtime is operating on behalf of")
public static final String PARTICIPANT_ID = "edc.participant.id";

@Override
public String name() {
return NAME;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

package org.eclipse.edc.boot.system;

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;
Expand Down Expand Up @@ -107,7 +108,7 @@ public void initialize() {
});
config = loadConfig();
connectorId = getSetting("edc.connector.name", "edc-" + UUID.randomUUID());
participantId = getSetting(PARTICIPANT_ID, ANONYMOUS_PARTICIPANT);
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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@
import okhttp3.EventListener;
import okhttp3.OkHttpClient;
import org.eclipse.edc.connector.core.base.EdcHttpClientImpl;
import org.eclipse.edc.connector.core.base.OkHttpClientConfiguration;
import org.eclipse.edc.connector.core.base.OkHttpClientFactory;
import org.eclipse.edc.connector.core.base.RetryPolicyConfiguration;
import org.eclipse.edc.connector.core.base.RetryPolicyFactory;
import org.eclipse.edc.connector.core.base.agent.NoOpParticipantIdMapper;
import org.eclipse.edc.connector.core.event.EventExecutorServiceContainer;
import org.eclipse.edc.connector.core.vault.InMemoryVault;
import org.eclipse.edc.runtime.metamodel.annotation.Extension;
import org.eclipse.edc.runtime.metamodel.annotation.Inject;
import org.eclipse.edc.runtime.metamodel.annotation.Provider;
import org.eclipse.edc.runtime.metamodel.annotation.Setting;
import org.eclipse.edc.spi.agent.ParticipantIdMapper;
import org.eclipse.edc.spi.http.EdcHttpClient;
import org.eclipse.edc.spi.security.Vault;
Expand All @@ -39,6 +42,8 @@

import java.util.concurrent.Executors;

import static java.lang.Integer.parseInt;

/**
* Provides default service implementations for fallback
* Omitted {@link Extension} since this module contains the extension {@link CoreServicesExtension}
Expand All @@ -47,6 +52,40 @@ public class CoreDefaultServicesExtension implements ServiceExtension {

public static final String NAME = "Core Default Services";

private static final String RETRY_POLICY_DEFAULT_RETRIES = "5";
private static final String RETRY_POLICY_DEFAULT_MIN_BACKOFF = "500";
private static final String RETRY_POLICY_DEFAULT_MAX_BACKOFF = "10000";
private static final String RETRY_POLICY_DEFAULT_LOG = "false";
@Setting(value = "RetryPolicy: Maximum retries before a failure is propagated", defaultValue = RETRY_POLICY_DEFAULT_RETRIES)
private static final String RETRY_POLICY_MAX_RETRIES = "edc.core.retry.retries.max";
@Setting(value = "RetryPolicy: Minimum number of milliseconds for exponential backoff", defaultValue = RETRY_POLICY_DEFAULT_MIN_BACKOFF)
private static final String RETRY_POLICY_BACKOFF_MIN_MILLIS = "edc.core.retry.backoff.min";
@Setting(value = "RetryPolicy: Maximum number of milliseconds for exponential backoff.", defaultValue = RETRY_POLICY_DEFAULT_MAX_BACKOFF)
private static final String RETRY_POLICY_BACKOFF_MAX_MILLIS = "edc.core.retry.backoff.max";
@Setting(value = "RetryPolicy: Log onRetry events", defaultValue = RETRY_POLICY_DEFAULT_LOG)
static final String RETRY_POLICY_LOG_ON_RETRY = "edc.core.retry.log.on.retry";
@Setting(value = "RetryPolicy: Log onRetryScheduled events", defaultValue = RETRY_POLICY_DEFAULT_LOG)
static final String RETRY_POLICY_LOG_ON_RETRY_SCHEDULED = "edc.core.retry.log.on.retry.scheduled";
@Setting(value = "RetryPolicy: Log onRetriesExceeded events", defaultValue = RETRY_POLICY_DEFAULT_LOG)
static final String RETRY_POLICY_LOG_ON_RETRIES_EXCEEDED = "edc.core.retry.log.on.retries.exceeded";
@Setting(value = "RetryPolicy: Log onFailedAttempt events", defaultValue = RETRY_POLICY_DEFAULT_LOG)
static final String RETRY_POLICY_LOG_ON_FAILED_ATTEMPT = "edc.core.retry.log.on.failed.attempt";
@Setting(value = "RetryPolicy: Log onAbort events", defaultValue = RETRY_POLICY_DEFAULT_LOG)
static final String RETRY_POLICY_LOG_ON_ABORT = "edc.core.retry.log.on.abort";

private static final String OK_HTTP_CLIENT_DEFAULT_TIMEOUT = "30";
private static final String OK_HTTP_CLIENT_DEFAULT_HTTPS_ENFORCE = "false";
@Setting(value = "OkHttpClient: If true, enable HTTPS call enforcement.", defaultValue = OK_HTTP_CLIENT_DEFAULT_HTTPS_ENFORCE, type = "boolean")
public static final String OK_HTTP_CLIENT_HTTPS_ENFORCE = "edc.http.client.https.enforce";
@Setting(value = "OkHttpClient: connect timeout, in seconds", defaultValue = OK_HTTP_CLIENT_DEFAULT_TIMEOUT, type = "int")
public static final String OK_HTTP_CLIENT_TIMEOUT_CONNECT = "edc.http.client.timeout.connect";
@Setting(value = "OkHttpClient: read timeout, in seconds", defaultValue = OK_HTTP_CLIENT_DEFAULT_TIMEOUT, type = "int")
public static final String OK_HTTP_CLIENT_TIMEOUT_READ = "edc.http.client.timeout.read";
@Setting(value = "OkHttpClient: send buffer size, in bytes", type = "int", min = 1)
public static final String OK_HTTP_CLIENT_SEND_BUFFER_SIZE = "edc.http.client.send.buffer.size";
@Setting(value = "OkHttpClient: receive buffer size, in bytes", type = "int", min = 1)
public static final String OK_HTTP_CLIENT_RECEIVE_BUFFER_SIZE = "edc.http.client.receive.buffer.size";

/**
* An optional OkHttp {@link EventListener} that can be used to instrument OkHttp client for collecting metrics.
*/
Expand Down Expand Up @@ -97,12 +136,31 @@ public EdcHttpClient edcHttpClient(ServiceExtensionContext context) {

@Provider
public OkHttpClient okHttpClient(ServiceExtensionContext context) {
return OkHttpClientFactory.create(context, okHttpEventListener);
var configuration = OkHttpClientConfiguration.Builder.newInstance()
.enforceHttps(context.getSetting(OK_HTTP_CLIENT_HTTPS_ENFORCE, Boolean.parseBoolean(OK_HTTP_CLIENT_DEFAULT_HTTPS_ENFORCE)))
.connectTimeout(context.getSetting(OK_HTTP_CLIENT_TIMEOUT_CONNECT, parseInt(OK_HTTP_CLIENT_DEFAULT_TIMEOUT)))
.readTimeout(context.getSetting(OK_HTTP_CLIENT_TIMEOUT_READ, parseInt(OK_HTTP_CLIENT_DEFAULT_TIMEOUT)))
.sendBufferSize(context.getSetting(OK_HTTP_CLIENT_SEND_BUFFER_SIZE, 0))
.receiveBufferSize(context.getSetting(OK_HTTP_CLIENT_RECEIVE_BUFFER_SIZE, 0))
.build();

return OkHttpClientFactory.create(configuration, okHttpEventListener, context.getMonitor());
}

@Provider
public <T> RetryPolicy<T> retryPolicy(ServiceExtensionContext context) {
return RetryPolicyFactory.create(context);
var configuration = RetryPolicyConfiguration.Builder.newInstance()
.maxRetries(context.getSetting(RETRY_POLICY_MAX_RETRIES, parseInt(RETRY_POLICY_DEFAULT_RETRIES)))
.minBackoff(context.getSetting(RETRY_POLICY_BACKOFF_MIN_MILLIS, parseInt(RETRY_POLICY_DEFAULT_MIN_BACKOFF)))
.maxBackoff(context.getSetting(RETRY_POLICY_BACKOFF_MAX_MILLIS, parseInt(RETRY_POLICY_DEFAULT_MAX_BACKOFF)))
.logOnRetry(context.getSetting(RETRY_POLICY_LOG_ON_RETRY, false))
.logOnRetryScheduled(context.getSetting(RETRY_POLICY_LOG_ON_RETRY_SCHEDULED, false))
.logOnRetriesExceeded(context.getSetting(RETRY_POLICY_LOG_ON_RETRIES_EXCEEDED, false))
.logOnFailedAttempt(context.getSetting(RETRY_POLICY_LOG_ON_FAILED_ATTEMPT, false))
.logOnAbort(context.getSetting(RETRY_POLICY_LOG_ON_ABORT, false))
.build();

return RetryPolicyFactory.create(configuration, context.getMonitor());
}

@Provider(isDefault = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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.connector.core.base;

public class OkHttpClientConfiguration {

private boolean enforceHttps;
private int connectTimeout;
private int readTimeout;
private int sendBufferSize;
private int receiveBufferSize;

private OkHttpClientConfiguration() {
}

public boolean isEnforceHttps() {
return enforceHttps;
}

public int getConnectTimeout() {
return connectTimeout;
}

public int getReadTimeout() {
return readTimeout;
}

public int getSendBufferSize() {
return sendBufferSize;
}

public int getReceiveBufferSize() {
return receiveBufferSize;
}

public static class Builder {

private final OkHttpClientConfiguration instance = new OkHttpClientConfiguration();

public static Builder newInstance() {
return new Builder();
}

private Builder() {
}

public Builder enforceHttps(boolean enforceHttps) {
instance.enforceHttps = enforceHttps;
return this;
}

public Builder connectTimeout(int connectTimeout) {
instance.connectTimeout = connectTimeout;
return this;
}

public Builder readTimeout(int readTimeout) {
instance.readTimeout = readTimeout;
return this;
}

public Builder sendBufferSize(int sendBufferSize) {
instance.sendBufferSize = sendBufferSize;
return this;
}

public Builder receiveBufferSize(int receiveBufferSize) {
instance.receiveBufferSize = receiveBufferSize;
return this;
}

public OkHttpClientConfiguration build() {
return instance;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,70 +18,45 @@
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Response;
import org.eclipse.edc.runtime.metamodel.annotation.Setting;
import org.eclipse.edc.spi.EdcException;
import org.eclipse.edc.spi.system.ServiceExtensionContext;
import org.eclipse.edc.spi.monitor.Monitor;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import javax.net.SocketFactory;

import static java.lang.Integer.parseInt;
import static java.lang.String.format;
import static java.util.Optional.ofNullable;
import static java.util.concurrent.TimeUnit.SECONDS;

public class OkHttpClientFactory {

private static final String DEFAULT_TIMEOUT = "30";
private static final String DEFAULT_HTTPS_ENFORCE = "false";

@Setting(value = "If true, enable HTTPS call enforcement.", defaultValue = DEFAULT_HTTPS_ENFORCE, type = "boolean")
public static final String EDC_HTTP_CLIENT_HTTPS_ENFORCE = "edc.http.client.https.enforce";

@Setting(value = "HTTP Client connect timeout, in seconds", defaultValue = DEFAULT_TIMEOUT, type = "int")
public static final String EDC_HTTP_CLIENT_TIMEOUT_CONNECT = "edc.http.client.timeout.connect";

@Setting(value = "HTTP Client read timeout, in seconds", defaultValue = DEFAULT_TIMEOUT, type = "int")
public static final String EDC_HTTP_CLIENT_TIMEOUT_READ = "edc.http.client.timeout.read";

@Setting(value = "HTTP Client send buffer size, in bytes", type = "int", min = 1)
public static final String EDC_HTTP_CLIENT_SEND_BUFFER_SIZE = "edc.http.client.send.buffer.size";

@Setting(value = "HTTP Client receive buffer size, in bytes", type = "int", min = 1)
public static final String EDC_HTTP_CLIENT_RECEIVE_BUFFER_SIZE = "edc.http.client.receive.buffer.size";

/**
* Create an OkHttpClient instance
*
* @param context the service extension context
* @param configuration the configuration
* @param okHttpEventListener used to instrument OkHttp client for collecting metrics, can be null
* @param monitor the monitor
* @return the OkHttpClient
*/
@NotNull
public static OkHttpClient create(ServiceExtensionContext context, EventListener okHttpEventListener) {
var connectTimeout = context.getSetting(EDC_HTTP_CLIENT_TIMEOUT_CONNECT, parseInt(DEFAULT_TIMEOUT));
var readTimeout = context.getSetting(EDC_HTTP_CLIENT_TIMEOUT_READ, parseInt(DEFAULT_TIMEOUT));
var sendBufferSize = context.getSetting(EDC_HTTP_CLIENT_SEND_BUFFER_SIZE, 0);
var receiveBufferSize = context.getSetting(EDC_HTTP_CLIENT_RECEIVE_BUFFER_SIZE, 0);

public static OkHttpClient create(OkHttpClientConfiguration configuration, EventListener okHttpEventListener, Monitor monitor) {
var builder = new OkHttpClient.Builder()
.connectTimeout(connectTimeout, SECONDS)
.readTimeout(readTimeout, SECONDS);
.connectTimeout(configuration.getConnectTimeout(), SECONDS)
.readTimeout(configuration.getReadTimeout(), SECONDS);

if (sendBufferSize > 0 || receiveBufferSize > 0) {
builder.socketFactory(new CustomSocketFactory(sendBufferSize, receiveBufferSize));
if (configuration.getSendBufferSize() > 0 || configuration.getReceiveBufferSize() > 0) {
builder.socketFactory(new CustomSocketFactory(configuration.getSendBufferSize(), configuration.getReceiveBufferSize()));
}

ofNullable(okHttpEventListener).ifPresent(builder::eventListener);

var enforceHttps = context.getSetting(EDC_HTTP_CLIENT_HTTPS_ENFORCE, Boolean.parseBoolean(DEFAULT_HTTPS_ENFORCE));
if (enforceHttps) {
if (configuration.isEnforceHttps()) {
builder.addInterceptor(new EnforceHttps());
} else {
context.getMonitor().info("HTTPS enforcement it not enabled, please enable it in a production environment");
monitor.info("HTTPS enforcement it not enabled, please enable it in a production environment");
}

return builder.build();
Expand Down
Loading

0 comments on commit 1055dac

Please sign in to comment.