Repository: apache/kafka
File: metadata/src/main/java/org/apache/kafka/metadata/publisher/DelegationTokenPublisher.java (Java)

Code before:
    public final String name() {
        return "DelegationTokenPublisher " + nodeType + " id=" + nodeId;
    }

    @Override
    public void onMetadataUpdate(MetadataDelta delta, MetadataImage newImage, LoaderManifest manifest) {
        String deltaName = firstPublish
            ? "initial MetadataDelta up to " + newImage.highestOffsetAndEpoch().offset()
            : "update MetadataDelta up to " + newImage.highestOffsetAndEpoch().offset();
        try {
            if (firstPublish) {
                // Initialize the tokenCache with the Image
                DelegationTokenImage delegationTokenImage = newImage.delegationTokens();
                if (delegationTokenImage != null) {
                    delegationTokenImage.tokens().forEach((tokenId, delegationTokenData) -> {
                        try {
                            tokenManager.updateToken(tokenManager.getDelegationToken(delegationTokenData.tokenInformation()));
                        } catch (Exception e) {
                            faultHandler.handleFault("Error updating delegation token during initialization", e);
                        }
                    });
                    firstPublish = false;
                }
            }
            // Apply changes to DelegationTokens.
            DelegationTokenDelta delegationTokenDelta = delta.delegationTokenDelta();
            if (delegationTokenDelta != null) {
                delegationTokenDelta.changes().forEach((tokenId, delegationTokenData) -> {
                    try {
                        if (delegationTokenData.isPresent())
                            tokenManager.updateToken(tokenManager.getDelegationToken(delegationTokenData.get().tokenInformation()));
                        else
                            tokenManager.removeToken(tokenId);
                    } catch (Exception e) {
                        faultHandler.handleFault("Error updating delegation token for tokenId: " + tokenId, e);
                    }
                });
            }
        } catch (Throwable t) {
            faultHandler.handleFault("Uncaught exception while publishing DelegationToken changes from " + deltaName, t);
        }
    }
}

Review comment (suggestion):
We could leverage `getOrCreateDelegationTokenDelta` to eliminate the null check

Code after:
        return "DelegationTokenPublisher " + nodeType + " id=" + nodeId;
    }

    @Override
    public void onMetadataUpdate(MetadataDelta delta, MetadataImage newImage, LoaderManifest manifest) {
        var first = firstPublish;
        try {
            if (firstPublish) {
                // Initialize the tokenCache with the Image
                DelegationTokenImage delegationTokenImage = newImage.delegationTokens();
                for (var token : delegationTokenImage.tokens().entrySet()) {
                    tokenManager.updateToken(tokenManager.getDelegationToken(token.getValue().tokenInformation()));
                }
                firstPublish = false;
            }
            // Apply changes to DelegationTokens.
            for (var token : delta.getOrCreateDelegationTokenDelta().changes().entrySet()) {
                var tokenId = token.getKey();
                var delegationTokenData = token.getValue();
                if (delegationTokenData.isPresent())
                    tokenManager.updateToken(tokenManager.getDelegationToken(delegationTokenData.get().tokenInformation()));
                else
                    tokenManager.removeToken(tokenId);
            }
        } catch (Throwable t) {
            var msg = String.format("Uncaught exception while publishing DelegationToken changes from %s MetadataDelta up to %s",
                first ? "initial" : "update", newImage.highestOffsetAndEpoch().offset());
            faultHandler.handleFault(msg, t);
        }
    }
}