Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deadlock issue in #50 #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentSkipListSet;

/**
* Abstract class loader that can load classes from different resources
*
Expand All @@ -37,13 +39,21 @@
@SuppressWarnings("unchecked")
public abstract class AbstractClassLoader extends ClassLoader {

// we could use concurrent sorted set like ConcurrentSkipListSet here instead, which would be automatically sorted
// We could use concurrent sorted set like ConcurrentSkipListSet here instead, which would be automatically sorted
// and wouldn't require the lock.
// But that was added in 1.6, and according to Maven we're targeting 1.5+.
/**
* Note that all iterations over this list *must* synchronize on it first!
*/
protected final List<ProxyClassLoader> loaders = Collections.synchronizedList(new ArrayList<ProxyClassLoader>());
// protected final List<ProxyClassLoader> loaders = Collections.synchronizedList(new ArrayList<ProxyClassLoader>());

// Now we use concurrent sorted set to just resolve the deadlock issue
// https://github.com/kamranzafar/JCL/issues/50
// protected final List<ProxyClassLoader> loaders = new SetList<ProxyClassLoader>(new ConcurrentSkipListSet<ProxyClassLoader>());
/**
* Omit type argument since it's not needed in backport implementation
*/
protected final List<ProxyClassLoader> loaders = new SetList<ProxyClassLoader>(new ConcurrentSkipListSet());

private final ProxyClassLoader systemLoader = new SystemLoader();
private final ProxyClassLoader parentLoader = new ParentLoader();
Expand All @@ -70,20 +80,14 @@ public AbstractClassLoader() {
}

protected void addDefaultLoader() {
synchronized (loaders) {
loaders.add(systemLoader);
loaders.add(parentLoader);
loaders.add(currentLoader);
loaders.add(threadLoader);
Collections.sort(loaders);
}
loaders.add(systemLoader);
loaders.add(parentLoader);
loaders.add(currentLoader);
loaders.add(threadLoader);
}

public void addLoader(ProxyClassLoader loader) {
synchronized (loaders) {
loaders.add(loader);
Collections.sort(loaders);
}
loaders.add(loader);
}

/*
Expand Down Expand Up @@ -116,13 +120,11 @@ public Class loadClass(String className, boolean resolveIt) throws ClassNotFound
}

if (clazz == null) {
synchronized (loaders) {
for (ProxyClassLoader l : loaders) {
if (l.isEnabled()) {
clazz = l.loadClass(className, resolveIt);
if (clazz != null)
break;
}
for (ProxyClassLoader l : loaders) {
if (l.isEnabled()) {
clazz = l.loadClass(className, resolveIt);
if (clazz != null)
break;
}
}
}
Expand Down Expand Up @@ -153,13 +155,11 @@ public URL getResource(String name) {
}

if (url == null) {
synchronized (loaders) {
for (ProxyClassLoader l : loaders) {
if (l.isEnabled()) {
url = l.findResource(name);
if (url != null)
break;
}
for (ProxyClassLoader l : loaders) {
if (l.isEnabled()) {
url = l.findResource(name);
if (url != null)
break;
}
}
}
Expand Down Expand Up @@ -187,13 +187,11 @@ public Enumeration<URL> getResources(String name) throws IOException {
}

if (url == null) {
synchronized (loaders) {
for (ProxyClassLoader l : loaders) {
if (l.isEnabled()) {
url = l.findResource(name);
if (url != null) {
urlVector.add(url);
}
for (ProxyClassLoader l : loaders) {
if (l.isEnabled()) {
url = l.findResource(name);
if (url != null) {
urlVector.add(url);
}
}
}
Expand Down Expand Up @@ -222,13 +220,11 @@ public InputStream getResourceAsStream(String name) {
}

if (is == null) {
synchronized (loaders) {
for (ProxyClassLoader l : loaders) {
if (l.isEnabled()) {
is = l.loadResource(name);
if (is != null)
break;
}
for (ProxyClassLoader l : loaders) {
if (l.isEnabled()) {
is = l.loadResource(name);
if (is != null)
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import org.xeustechnologies.jcl.exception.JclException;
import org.xeustechnologies.jcl.exception.ResourceNotFoundException;

import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;

/**
* Reads the class bytes from jar files and other resources using
* ClasspathResources
Expand All @@ -53,14 +55,16 @@ public class JarClassLoader extends AbstractClassLoader {

public JarClassLoader() {
classpathResources = new ClasspathResources();
classes = Collections.synchronizedMap( new HashMap<String, Class>() );
// classes = Collections.synchronizedMap( new HashMap<String, Class>() );
classes = new ConcurrentHashMap();
initialize();
}

public JarClassLoader(final ClassLoader parent) {
super(parent);
classpathResources = new ClasspathResources();
classes = Collections.synchronizedMap( new HashMap<String, Class>() );
// classes = Collections.synchronizedMap( new HashMap<String, Class>() );
classes = new ConcurrentHashMap();
initialize();
}

Expand Down
57 changes: 57 additions & 0 deletions JCL2/core/src/main/java/org/xeustechnologies/jcl/SetList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.xeustechnologies.jcl;

import java.util.*;
import java.io.Serializable;

import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentSkipListSet;

public class SetList<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, Serializable {
protected final Set<E> underlying;
public SetList() {
// underlying = new ConcurrentSkipListSet<E>();
underlying = new ConcurrentSkipListSet();
}
public SetList(Set<E> other) {
underlying = other;
}
public E get(int index) {
Iterator<E> it = iterator();
for (int i = 0; i < index; i++) {
if (it.hasNext()) {
it.next();
} else {
throw new IndexOutOfBoundsException();
}
}
return it.next();
}
public int size() {
return underlying.size();
}
public E set(int index, E elem) {
if (add(elem)) {
return elem;
} else {
return null;
}
}
public boolean add(E elem) {
return underlying.add(elem);
}
public void add(int index, E elem) {
add(elem);
}
public E remove(int index) {
E elem = get(index);
if (underlying.remove(elem)) {
return elem;
} else {
return null;
}
}
public Iterator<E> iterator() {
return underlying.iterator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.xeustechnologies.jcl.JarClassLoader;
import org.xeustechnologies.jcl.exception.JclContextException;

import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;

/**
* JclContext holds all the JarClassLoader instances so that they can be
* accessed from anywhere in the application.
Expand All @@ -32,8 +34,12 @@
*
*/
public class JclContext {
/*
private static final Map<String, JarClassLoader> loaders = Collections
.synchronizedMap( new HashMap<String, JarClassLoader>() );
private static final Map<String, JarClassLoader> loaders = new ConcurrentHashMap<String, JarClassLoader>();
*/
private static final Map<String, JarClassLoader> loaders = new ConcurrentHashMap();
public static final String DEFAULT_NAME = "jcl";

public JclContext() {
Expand Down
5 changes: 5 additions & 0 deletions JCL2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@
<artifactId>object-cloner</artifactId>
<version>0.1</version>
</dependency>
<dependency>
<groupId>backport-util-concurrent</groupId>
<artifactId>backport-util-concurrent</artifactId>
<version>3.1</version>
</dependency>
</dependencies>

<profiles>
Expand Down