Skip to content

Commit

Permalink
[RESTEASY-2541] Fix RestClientListeners
Browse files Browse the repository at this point in the history
  • Loading branch information
asoldano committed Apr 1, 2020
1 parent d7a3f52 commit 1adc448
Showing 1 changed file with 29 additions and 25 deletions.
Expand Up @@ -5,30 +5,34 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;


public class RestClientListeners {

private RestClientListeners() {
}

private static final Collection<RestClientListener> listeners;

static {
listeners = loadListeners();
}

private static List<RestClientListener> loadListeners() {
List<RestClientListener> listeners = new ArrayList<>();
ServiceLoader.load(RestClientListener.class)
.forEach(listeners::add);
Collections.unmodifiableCollection(listeners);
return listeners;
}

public static Collection<RestClientListener> get() {
return listeners;
}
import java.util.WeakHashMap;

public class RestClientListeners
{

private RestClientListeners()
{
}

/**
* A synchronized weak hash map that keeps RestClientListener instances retrieved using ServiceLoader for each classloader.
* Weak keys are used to remove entries when classloaders are garbage collected.
*/
private static Map<ClassLoader, Collection<RestClientListener>> map = Collections
.synchronizedMap(new WeakHashMap<ClassLoader, Collection<RestClientListener>>());

public static Collection<RestClientListener> get()
{
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Collection<RestClientListener> c;
c = map.get(loader);
if (c == null) {
c = new ArrayList<>();
ServiceLoader.load(RestClientListener.class, loader).forEach(c::add);
map.put(loader, Collections.unmodifiableCollection(c));
}
return c;
}
}

0 comments on commit 1adc448

Please sign in to comment.