diff --git a/common/tls/src/main/java/io/helidon/common/tls/spi/TlsManagerCache.java b/common/tls/src/main/java/io/helidon/common/tls/spi/TlsManagerCache.java index 9bc8acc9d66..b1b374a3f7b 100644 --- a/common/tls/src/main/java/io/helidon/common/tls/spi/TlsManagerCache.java +++ b/common/tls/src/main/java/io/helidon/common/tls/spi/TlsManagerCache.java @@ -16,24 +16,40 @@ package io.helidon.common.tls.spi; +import java.util.HashMap; +import java.util.Map; import java.util.Objects; -import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.locks.ReentrantLock; import java.util.function.Function; import io.helidon.common.tls.TlsManager; class TlsManagerCache { - private static final ConcurrentHashMap CACHE = new ConcurrentHashMap<>(); + private static final ReentrantLock LOCK = new ReentrantLock(); + private static final Map CACHE = new HashMap<>(); private TlsManagerCache() { } - @SuppressWarnings("unchecked") static TlsManager getOrCreate(T configBean, Function creator) { Objects.requireNonNull(configBean); Objects.requireNonNull(creator); - return CACHE.computeIfAbsent(configBean, (Function) creator); + LOCK.lock(); + try { + TlsManager manager = CACHE.get(configBean); + if (manager != null) { + return manager; + } + + manager = creator.apply(configBean); + Object existing = CACHE.put(configBean, manager); + assert (existing == null); + + return manager; + } finally { + LOCK.unlock(); + } } }