From e28f4b05196f1863d92c508a9b161c181628e579 Mon Sep 17 00:00:00 2001 From: Francois JACQUES Date: Tue, 25 Jun 2019 10:15:27 +0300 Subject: [PATCH] Fixes #3997 : Make SupplierFactoryBridge thread-safe. Signed-off-by: Francois JACQUES --- .../jersey/inject/hk2/SupplierFactoryBridge.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/inject/hk2/src/main/java/org/glassfish/jersey/inject/hk2/SupplierFactoryBridge.java b/inject/hk2/src/main/java/org/glassfish/jersey/inject/hk2/SupplierFactoryBridge.java index b1950b109ef..c038404ce08 100644 --- a/inject/hk2/src/main/java/org/glassfish/jersey/inject/hk2/SupplierFactoryBridge.java +++ b/inject/hk2/src/main/java/org/glassfish/jersey/inject/hk2/SupplierFactoryBridge.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2017, 2019 Oracle and/or its affiliates. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v. 2.0, which is available at @@ -18,6 +18,7 @@ import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; +import java.util.Collections; import java.util.IdentityHashMap; import java.util.Map; import java.util.function.Supplier; @@ -42,15 +43,15 @@ */ public class SupplierFactoryBridge implements Factory { - private ServiceLocator locator; - private ParameterizedType beanType; - private String beanName; - private boolean disposable; + private final ServiceLocator locator; + private final ParameterizedType beanType; + private final String beanName; + private final boolean disposable; // This bridge can create multiple instances using the method 'provide' therefore must map created suppliers because of // 'dispose' invocation later on. // TODO: Key as a WeakReference - prevent objects in scope which never dispose the objects such as PerLookup. - private Map> disposableSuppliers = new IdentityHashMap<>(); + private Map> disposableSuppliers = Collections.synchronizedMap(new IdentityHashMap<>()); /** * Constructor for a new bridge. @@ -85,9 +86,8 @@ public T provide() { @Override public void dispose(T instance) { if (disposable) { - DisposableSupplier disposableSupplier = disposableSuppliers.get(instance); + DisposableSupplier disposableSupplier = disposableSuppliers.remove(instance); disposableSupplier.dispose(instance); - disposableSuppliers.remove(instance); } } }