private synchronized void updateTokenFuture()

in sdk/communication/azure-communication-common/src/main/java/com/azure/android/communication/common/AutoRefreshUserCredential.java [67:103]


    private synchronized void updateTokenFuture() {
        if (this.isDisposed()) {
            this.tokenFuture = CompletableFuture.failedFuture(new IllegalStateException(CREDENTIAL_DISPOSED));
            return;
        }

        final CompletableFuture<CommunicationAccessToken> tokenFuture = this.tokenFuture;
        if (tokenFuture != null && !tokenFuture.isDone() && !tokenFuture.isCancelled()) {
            // don't update if the tokenFuture in-progress.
            return;
        }

        this.tokenFuture = CompletableFuture.supplyAsync(() -> {
            if (this.isDisposed()) {
                throw logger.logExceptionAsError(new IllegalStateException(CREDENTIAL_DISPOSED));
            }

            final CommunicationAccessToken accessToken;

            try {
                final String tokenStr = this.tokenRefreshCallable.call();
                accessToken = TokenParser.createAccessToken(tokenStr);
                if (accessToken.isExpired()) {
                    throw logger.logExceptionAsError(
                        new IllegalArgumentException("The token returned from the tokenRefresher is expired."));
                }
            } catch (Exception e) {
                throw logger.logExceptionAsError(new RuntimeException(e));
            }

            if (this.refreshProactively) {
                this.scheduleTokenFutureUpdate(accessToken);
            }

            return accessToken;
        });
    }