Skip to content

Commit

Permalink
Fixes #3997 : Make SupplierFactoryBridge thread-safe.
Browse files Browse the repository at this point in the history
Signed-off-by: Francois JACQUES <fjacques@murex.com>
  • Loading branch information
hypnoce committed Jul 8, 2019
1 parent 01cb903 commit 955181d
Showing 1 changed file with 7 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,15 +43,15 @@
*/
public class SupplierFactoryBridge<T> implements Factory<T> {

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<Object, DisposableSupplier<T>> disposableSuppliers = new IdentityHashMap<>();
private Map<Object, DisposableSupplier<T>> disposableSuppliers = Collections.synchronizedMap(new IdentityHashMap<>());

/**
* Constructor for a new bridge.
Expand Down Expand Up @@ -85,9 +86,8 @@ public T provide() {
@Override
public void dispose(T instance) {
if (disposable) {
DisposableSupplier<T> disposableSupplier = disposableSuppliers.get(instance);
DisposableSupplier<T> disposableSupplier = disposableSuppliers.remove(instance);
disposableSupplier.dispose(instance);
disposableSuppliers.remove(instance);
}
}
}

0 comments on commit 955181d

Please sign in to comment.