Skip to content

Commit

Permalink
Fixes delegating classloader thread safety
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Pinčuk <alexander.v.pinchuk@gmail.com>
  • Loading branch information
avpinchuk committed Jun 12, 2024
1 parent fb174f0 commit 58bf6eb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.Collections;
import java.net.URL;
import java.io.IOException;
import java.util.concurrent.CopyOnWriteArrayList;

/**
* This classloader has a list of classloaders called as delegates
Expand Down Expand Up @@ -77,13 +78,14 @@ public interface ClassFinder {
Enumeration<URL> findResources(String name) throws IOException;
}

private final CopyOnWriteArrayList<ClassFinder> delegates = new CopyOnWriteArrayList<>();

/**
* Name of this class loader. Used mostly for reporting purpose.
* No guarantee about its uniqueness.
*/
private String name;
private volatile String name;

private List<ClassFinder> delegates = new ArrayList<ClassFinder>();

/**
* @throws IllegalArgumentException when the delegate does not have same parent
Expand Down Expand Up @@ -114,13 +116,10 @@ public DelegatingClassLoader(ClassLoader parent) {
* @throws IllegalArgumentException when the delegate does not have same parent
* as this classloader.
*/
public synchronized boolean addDelegate(ClassFinder d) throws
public boolean addDelegate(ClassFinder d) throws
IllegalStateException, IllegalArgumentException {
checkDelegate(d);
if (delegates.contains(d)) {
return false;
}
return delegates.add(d);
return delegates.addIfAbsent(d);
}

/**
Expand All @@ -146,7 +145,7 @@ private void checkDelegate(ClassFinder d) throws IllegalArgumentException {
* @throws IllegalStateException when this method is called after the
* classloader has been used to load any class.
*/
public synchronized boolean removeDelegate(ClassFinder d) {
public boolean removeDelegate(ClassFinder d) {
return delegates.remove(d);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.glassfish.common.util.GlassfishUrlClassLoader;
import org.glassfish.hk2.api.ServiceLocator;
Expand Down Expand Up @@ -177,7 +177,7 @@ private void addDelegates(Collection<URI> libURIs, DelegatingClassLoader holder)
*/
private static class URLClassFinder extends GlassfishUrlClassLoader implements ClassFinder {

private final Set<String> notFoundResources = new HashSet<>();
private final Set<String> notFoundResources = ConcurrentHashMap.newKeySet();

URLClassFinder(URL url, ClassLoader parent) {
super(new URL[] {url}, parent);
Expand Down

0 comments on commit 58bf6eb

Please sign in to comment.