Skip to content

Commit

Permalink
Merge branch 'eclipse' into feature/connection-log-publishing
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Jaeckle <thomas.jaeckle@bosch.io>
  • Loading branch information
thjaeckle committed Oct 25, 2021
2 parents 15e3390 + 241321f commit 90a56bc
Show file tree
Hide file tree
Showing 108 changed files with 1,932 additions and 1,493 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class AcknowledgementLabels {
* Ack labels starting with unresolved placeholders are also valid, e.g.: {@code {{connection:id}}:my-ack}.
*/
public static final String ACK_LABEL_REGEX =
"(?<" + PLACEHOLDER_GROUP + ">\\{\\{\\w*[a-z]+:[a-z]+\\w*}}:)?[a-zA-Z0-9-_:]{3,145}";
"(?<" + PLACEHOLDER_GROUP + ">\\{\\{\\w*[a-z]+:[a-z]+\\w*}}:)?[a-zA-Z0-9-_:]{3,165}";

private static final Pattern ACK_LABEL_PATTERN = Pattern.compile(ACK_LABEL_REGEX);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ public static List<RegexValidationParameter> validationParameters() {
RegexValidationParameter.invalid("A"),
RegexValidationParameter.invalid("AB"),
RegexValidationParameter.valid("ABC"),
RegexValidationParameter.valid(IntStream.range(0, 145)
RegexValidationParameter.valid(IntStream.range(0, 165)
.mapToObj(i -> "a")
.collect(Collectors.joining())),
RegexValidationParameter.invalid(IntStream.range(0, 146)
RegexValidationParameter.invalid(IntStream.range(0, 166)
.mapToObj(i -> "b")
.collect(Collectors.joining())),
RegexValidationParameter.invalid("ab?"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
@TypedEntityId(type = "connection")
public final class ConnectionId extends AbstractEntityId {

static final String ID_REGEX = "[a-zA-Z0-9-_:]{1,80}";
static final String ID_REGEX = "[a-zA-Z0-9-_:]{1,100}";
static final Pattern ID_PATTERN = Pattern.compile(ID_REGEX);

private ConnectionId(final String stringRepresentation, final boolean shouldValidate) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2021 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/

package org.eclipse.ditto.connectivity.service.config;

import java.util.Optional;
import java.util.concurrent.CompletionStage;

import org.atteo.classindex.IndexSubclasses;
import org.eclipse.ditto.base.model.signals.events.Event;
import org.eclipse.ditto.connectivity.model.ConnectionId;

import com.typesafe.config.Config;

import akka.actor.ActorRef;

/**
* Provides methods to load {@link ConnectivityConfig} and register for changes to {@link ConnectivityConfig}.
*/
@IndexSubclasses
public interface ConnectionConfigProvider {

CompletionStage<Config> getConnectivityConfigOverwrites(ConnectionId connectionId);

/**
* Loads a {@link ConnectivityConfig} by a connection ID.
*
* @param connectionId the connection id for which to load the {@link ConnectivityConfig}
* @return the future connectivity config
*/
CompletionStage<ConnectivityConfig> getConnectivityConfig(ConnectionId connectionId);

/**
* Register the given {@code subscriber} for changes to the {@link ConnectivityConfig} of the given {@code
* connectionId}. The given {@link ActorRef} will receive {@link Event}s to build the modified
* {@link ConnectivityConfig}.
*
* @param connectionId the connection id
* @param subscriber the subscriber that will receive {@link org.eclipse.ditto.base.model.signals.events.Event}s
* @return a future that succeeds or fails depending on whether registration was successful.
*/
CompletionStage<Void> registerForConnectivityConfigChanges(ConnectionId connectionId, ActorRef subscriber);

/**
* Returns {@code true} if the implementation can handle the given {@code event} to generate a modified {@link
* ConnectivityConfig} when passed to {@link #handleEvent(Event)}.
*
* @param event the event that may be used to generate modified config
* @return {@code true} if the event is compatible
*/
boolean canHandle(Event<?> event);

/**
* Uses the given {@code event} to create a config which should overwrite the default connectivity config.
*
* @param event the event used to create a config which should overwrite the default connectivity config.
* @return Potentially empty config which holds the overwrites for the default connectivity config.
*/
Optional<Config> handleEvent(Event<?> event);

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
import scala.util.Try;

/**
* Factory to instantiate new {@link ConnectionContextProvider}s.
* Factory to instantiate new {@link ConnectionConfigProvider}s.
*/
public final class ConnectionContextProviderFactory implements Extension {
public final class ConnectionConfigProviderFactory implements Extension {

/**
* If this config property is {@code false} then {@code #getInstance} will throw an exception if no config
Expand All @@ -50,72 +50,72 @@ public final class ConnectionContextProviderFactory implements Extension {
* implementation is used as a fallback or an exception is thrown, depending on the config value of
* {@value #DEFAULT_CONFIG_PROVIDER_CONFIG}.
*/
private static final Class<DittoConnectionContextProvider>
DEFAULT_CONNECTIVITY_CONFIG_PROVIDER_CLASS = DittoConnectionContextProvider.class;
private static final Class<DittoConnectionConfigProvider>
DEFAULT_CONNECTIVITY_CONFIG_PROVIDER_CLASS = DittoConnectionConfigProvider.class;

/**
* Holds the instance of the {@link ConnectionContextProvider}.
* Holds the instance of the {@link ConnectionConfigProvider}.
*/
private final ConnectionContextProvider connectionContextProvider;
private final ConnectionConfigProvider connectionContextProvider;

/**
* Returns the {@link ConnectionContextProvider} instance.
* Returns the {@link ConnectionConfigProvider} instance.
*
* @return the instance of the {@link ConnectionContextProvider}
* @return the instance of the {@link ConnectionConfigProvider}
*/
public ConnectionContextProvider getInstance() {
public ConnectionConfigProvider getInstance() {
return connectionContextProvider;
}

/**
* Returns the {@link ConnectionContextProvider} instance.
* Returns the {@link ConnectionConfigProvider} instance.
*
* @param actorSystem the actor system
* @return the instance of the {@link ConnectionContextProvider}
* @return the instance of the {@link ConnectionConfigProvider}
*/
public static ConnectionContextProvider getInstance(final ActorSystem actorSystem) {
return ConnectionContextProviderFactory.get(actorSystem).getInstance();
public static ConnectionConfigProvider getInstance(final ActorSystem actorSystem) {
return ConnectionConfigProviderFactory.get(actorSystem).getInstance();
}

private ConnectionContextProviderFactory(final ActorSystem actorSystem) {
private ConnectionConfigProviderFactory(final ActorSystem actorSystem) {
final Config config = actorSystem.settings().config();
final boolean loadDefaultProvider = config.getBoolean(DEFAULT_CONFIG_PROVIDER_CONFIG);

final Class<? extends ConnectionContextProvider> providerClass =
final Class<? extends ConnectionConfigProvider> providerClass =
findProviderClass(c -> filterDefaultProvider(c, loadDefaultProvider));

try {
final ClassTag<ConnectionContextProvider> tag =
ClassTag$.MODULE$.apply(ConnectionContextProvider.class);
final ClassTag<ConnectionConfigProvider> tag =
ClassTag$.MODULE$.apply(ConnectionConfigProvider.class);
final Tuple2<Class<?>, Object> args = new Tuple2<>(ActorSystem.class, actorSystem);
final DynamicAccess dynamicAccess = ((ExtendedActorSystem) actorSystem).dynamicAccess();
final Try<ConnectionContextProvider> providerBox = dynamicAccess.createInstanceFor(providerClass,
final Try<ConnectionConfigProvider> providerBox = dynamicAccess.createInstanceFor(providerClass,
CollectionConverters.asScala(Collections.singleton(args)).toList(), tag);
connectionContextProvider = providerBox.get();
} catch (final Exception e) {
throw configProviderInstantiationFailed(providerClass, e);
}
}

private static Class<? extends ConnectionContextProvider> findProviderClass(
final Predicate<Class<? extends ConnectionContextProvider>> classPredicate) {
private static Class<? extends ConnectionConfigProvider> findProviderClass(
final Predicate<Class<? extends ConnectionConfigProvider>> classPredicate) {

final Iterable<Class<? extends ConnectionContextProvider>> subclasses =
ClassIndex.getSubclasses(ConnectionContextProvider.class);
final Iterable<Class<? extends ConnectionConfigProvider>> subclasses =
ClassIndex.getSubclasses(ConnectionConfigProvider.class);

final List<Class<? extends ConnectionContextProvider>> candidates =
final List<Class<? extends ConnectionConfigProvider>> candidates =
StreamSupport.stream(subclasses.spliterator(), false)
.filter(classPredicate)
.collect(Collectors.toList());

if (candidates.size() == 1) {
return candidates.get(0);
} else {
throw ConnectionContextProviderFactory.configProviderNotFound(candidates);
throw ConnectionConfigProviderFactory.configProviderNotFound(candidates);
}
}

private static boolean filterDefaultProvider(final Class<? extends ConnectionContextProvider> c,
private static boolean filterDefaultProvider(final Class<? extends ConnectionConfigProvider> c,
final boolean loadDefaultProvider) {

if (loadDefaultProvider) {
Expand All @@ -126,12 +126,12 @@ private static boolean filterDefaultProvider(final Class<? extends ConnectionCon
}

private static DittoRuntimeException configProviderNotFound(
final List<Class<? extends ConnectionContextProvider>> candidates) {
final List<Class<? extends ConnectionConfigProvider>> candidates) {
return ConnectionContextProviderMissingException.newBuilder(candidates).build();
}

private static DittoRuntimeException configProviderInstantiationFailed(final Class<?
extends ConnectionContextProvider> c, final Exception cause) {
extends ConnectionConfigProvider> c, final Exception cause) {
return ConnectionContextProviderFailedException.newBuilder(c)
.cause(cause)
.build();
Expand All @@ -143,21 +143,21 @@ private static DittoRuntimeException configProviderInstantiationFailed(final Cla
* @param actorSystem The actor system in which to load the provider.
* @return the {@code ConnectivityConfigProviderFactory}.
*/
public static ConnectionContextProviderFactory get(final ActorSystem actorSystem) {
public static ConnectionConfigProviderFactory get(final ActorSystem actorSystem) {
return ExtensionId.INSTANCE.get(actorSystem);
}

/**
* ID of the actor system extension to provide a {@link ConnectionContextProviderFactory}.
* ID of the actor system extension to provide a {@link ConnectionConfigProviderFactory}.
*/
private static final class ExtensionId extends AbstractExtensionId<ConnectionContextProviderFactory> {
private static final class ExtensionId extends AbstractExtensionId<ConnectionConfigProviderFactory> {

private static final ExtensionId INSTANCE =
new ExtensionId();

@Override
public ConnectionContextProviderFactory createExtension(final ExtendedActorSystem system) {
return new ConnectionContextProviderFactory(system);
public ConnectionConfigProviderFactory createExtension(final ExtendedActorSystem system) {
return new ConnectionConfigProviderFactory(system);
}

}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private ConnectionContextProviderFailedException(
super(ERROR_CODE, HttpStatus.INTERNAL_SERVER_ERROR, dittoHeaders, message, description, cause, href);
}

public static Builder newBuilder(final Class<? extends ConnectionContextProvider> failedConfigProvider) {
public static Builder newBuilder(final Class<? extends ConnectionConfigProvider> failedConfigProvider) {
return new Builder(MessageFormat.format(MESSAGE_TEMPLATE, failedConfigProvider.getName()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private ConnectionContextProviderMissingException(
super(ERROR_CODE, HttpStatus.INTERNAL_SERVER_ERROR, dittoHeaders, message, description, cause, href);
}

public static Builder newBuilder(final List<Class<? extends ConnectionContextProvider>> candidates) {
public static Builder newBuilder(final List<Class<? extends ConnectionConfigProvider>> candidates) {
return new Builder(MessageFormat.format(MESSAGE_TEMPLATE, candidates));
}

Expand Down

0 comments on commit 90a56bc

Please sign in to comment.