20
20
import io .vertx .core .Vertx ;
21
21
import java .io .File ;
22
22
import java .io .IOException ;
23
- import java .util .ArrayList ;
24
- import java .util .List ;
25
- import java .util .Objects ;
23
+ import java .util .concurrent .ConcurrentHashMap ;
26
24
import java .util .concurrent .ExecutionException ;
27
25
import java .util .concurrent .TimeUnit ;
28
26
import java .util .concurrent .TimeoutException ;
27
+ import java .util .concurrent .atomic .AtomicInteger ;
28
+ import java .util .concurrent .atomic .AtomicReference ;
29
29
import java .util .function .Consumer ;
30
30
import org .slf4j .Logger ;
31
31
import org .slf4j .LoggerFactory ;
@@ -36,24 +36,27 @@ public class OIDCDiscoveryConfigListener implements AutoCloseable {
36
36
private final Vertx vertx ;
37
37
private final FileWatcher configFeaturesWatcher ;
38
38
private final int timeoutSeconds ;
39
- private List <Consumer <OIDCDiscoveryConfig >> callbacks ;
40
- private OIDCDiscoveryConfig oidcDiscoveryConfig ;
39
+ private final ConcurrentHashMap <Integer , Consumer <OIDCDiscoveryConfig >> callbacks ;
40
+ private final AtomicReference <OIDCDiscoveryConfig > oidcDiscoveryConfig ;
41
+ private final AtomicInteger callbackIdGenerator ;
41
42
42
43
public OIDCDiscoveryConfigListener (String featuresConfigPath , Vertx vertx , int timeoutSeconds ) throws IOException {
43
44
this .featuresConfigPath = featuresConfigPath ;
44
45
this .vertx = vertx ;
45
46
this .timeoutSeconds = timeoutSeconds ;
47
+ this .oidcDiscoveryConfig = new AtomicReference <>();
48
+ this .callbacks = new ConcurrentHashMap <>();
49
+ this .callbackIdGenerator = new AtomicInteger (0 );
46
50
47
51
this .buildFeaturesAndOIDCDiscoveryConfig ();
48
52
49
53
this .configFeaturesWatcher =
50
54
new FileWatcher (new File (featuresConfigPath + "/" + FeaturesConfig .KEY_AUTHENTICATION_OIDC ), () -> {
51
- if (this .oidcDiscoveryConfig == null ) {
55
+ if (this .oidcDiscoveryConfig . get () == null ) {
52
56
this .buildFeaturesAndOIDCDiscoveryConfig ();
53
- if (this .oidcDiscoveryConfig != null && this .callbacks != null ) {
54
- this .callbacks .stream ()
55
- .filter (Objects ::nonNull )
56
- .forEach (c -> c .accept (this .oidcDiscoveryConfig ));
57
+ OIDCDiscoveryConfig config = this .oidcDiscoveryConfig .get ();
58
+ if (config != null ) {
59
+ this .callbacks .values ().forEach (callback -> callback .accept (config ));
57
60
}
58
61
}
59
62
});
@@ -62,27 +65,25 @@ public OIDCDiscoveryConfigListener(String featuresConfigPath, Vertx vertx, int t
62
65
}
63
66
64
67
public OIDCDiscoveryConfig getOidcDiscoveryConfig () {
65
- return oidcDiscoveryConfig ;
68
+ return oidcDiscoveryConfig . get () ;
66
69
}
67
70
68
71
public int registerCallback (Consumer <OIDCDiscoveryConfig > callback ) {
69
- if (this .callbacks == null ) {
70
- this .callbacks = new ArrayList <>();
71
- }
72
-
73
- this .callbacks .add (callback );
74
- return this .callbacks .size () - 1 ;
72
+ int id = callbackIdGenerator .incrementAndGet ();
73
+ this .callbacks .put (id , callback );
74
+ return id ;
75
75
}
76
76
77
77
public void deregisterCallback (int callbackId ) {
78
- this .callbacks .set (callbackId , null );
78
+ this .callbacks .remove (callbackId );
79
79
}
80
80
81
81
private void buildOIDCDiscoveryConfig () throws ExecutionException , InterruptedException , TimeoutException {
82
- this . oidcDiscoveryConfig = OIDCDiscoveryConfig .build (this .vertx )
82
+ OIDCDiscoveryConfig config = OIDCDiscoveryConfig .build (this .vertx )
83
83
.toCompletionStage ()
84
84
.toCompletableFuture ()
85
85
.get (this .timeoutSeconds , TimeUnit .SECONDS );
86
+ this .oidcDiscoveryConfig .set (config );
86
87
}
87
88
88
89
private void buildFeaturesAndOIDCDiscoveryConfig () {
0 commit comments