Skip to content

Commit

Permalink
usage of CopyOnWriteArrayList/AtomicRef
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Wessendorf <mwessend@redhat.com>
  • Loading branch information
matzew committed Nov 6, 2024
1 parent 716759f commit 26b2628
Showing 1 changed file with 13 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@
import io.vertx.core.Vertx;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -36,24 +35,25 @@ public class OIDCDiscoveryConfigListener implements AutoCloseable {
private final Vertx vertx;
private final FileWatcher configFeaturesWatcher;
private final int timeoutSeconds;
private List<Consumer<OIDCDiscoveryConfig>> callbacks;
private OIDCDiscoveryConfig oidcDiscoveryConfig;
private final CopyOnWriteArrayList<Consumer<OIDCDiscoveryConfig>> callbacks;
private final AtomicReference<OIDCDiscoveryConfig> oidcDiscoveryConfig;

public OIDCDiscoveryConfigListener(String featuresConfigPath, Vertx vertx, int timeoutSeconds) throws IOException {
this.featuresConfigPath = featuresConfigPath;
this.vertx = vertx;
this.timeoutSeconds = timeoutSeconds;
this.oidcDiscoveryConfig = new AtomicReference<>();
this.callbacks = new CopyOnWriteArrayList<>();

this.buildFeaturesAndOIDCDiscoveryConfig();

this.configFeaturesWatcher =
new FileWatcher(new File(featuresConfigPath + "/" + FeaturesConfig.KEY_AUTHENTICATION_OIDC), () -> {
if (this.oidcDiscoveryConfig == null) {
if (this.oidcDiscoveryConfig.get() == null) {
this.buildFeaturesAndOIDCDiscoveryConfig();
if (this.oidcDiscoveryConfig != null && this.callbacks != null) {
this.callbacks.stream()
.filter(Objects::nonNull)
.forEach(c -> c.accept(this.oidcDiscoveryConfig));
OIDCDiscoveryConfig config = this.oidcDiscoveryConfig.get();
if (config != null) {
this.callbacks.forEach(callback -> callback.accept(config));
}
}
});
Expand All @@ -62,14 +62,10 @@ public OIDCDiscoveryConfigListener(String featuresConfigPath, Vertx vertx, int t
}

public OIDCDiscoveryConfig getOidcDiscoveryConfig() {
return oidcDiscoveryConfig;
return oidcDiscoveryConfig.get();
}

public int registerCallback(Consumer<OIDCDiscoveryConfig> callback) {
if (this.callbacks == null) {
this.callbacks = new ArrayList<>();
}

this.callbacks.add(callback);
return this.callbacks.size() - 1;
}
Expand All @@ -79,10 +75,11 @@ public void deregisterCallback(int callbackId) {
}

private void buildOIDCDiscoveryConfig() throws ExecutionException, InterruptedException, TimeoutException {
this.oidcDiscoveryConfig = OIDCDiscoveryConfig.build(this.vertx)
OIDCDiscoveryConfig config = OIDCDiscoveryConfig.build(this.vertx)
.toCompletionStage()
.toCompletableFuture()
.get(this.timeoutSeconds, TimeUnit.SECONDS);
this.oidcDiscoveryConfig.set(config);
}

private void buildFeaturesAndOIDCDiscoveryConfig() {
Expand Down

0 comments on commit 26b2628

Please sign in to comment.