| 1 |
/* |
| 2 |
* JBoss, Home of Professional Open Source. |
| 3 |
* Copyright 2014 Red Hat, Inc., and individual contributors |
| 4 |
* as indicated by the @author tags. |
| 5 |
* |
| 6 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
* you may not use this file except in compliance with the License. |
| 8 |
* You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software |
| 13 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
* See the License for the specific language governing permissions and |
| 16 |
* limitations under the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
package org.jboss.modules; |
| 20 |
|
| 21 |
import org.jboss.modules.test.ClassA; |
| 22 |
import org.jboss.modules.test.ClassB; |
| 23 |
import org.jboss.modules.test.ClassC; |
| 24 |
import org.jboss.modules.test.ClassD; |
| 25 |
import org.jboss.modules.util.Util; |
| 26 |
import org.junit.Test; |
| 27 |
|
| 28 |
import java.util.Arrays; |
| 29 |
import java.util.Collection; |
| 30 |
import java.util.HashSet; |
| 31 |
import java.util.Set; |
| 32 |
import java.util.concurrent.CountDownLatch; |
| 33 |
|
| 34 |
/** |
| 35 |
* Test case to verify the concurrent classloader base correctly handles common concurrency issues with classloading.. |
| 36 |
* |
| 37 |
* @author John E. Bailey |
| 38 |
*/ |
| 39 |
public class ConcurrentClassLoaderTest { |
| 40 |
volatile Throwable threadOneProblem; |
| 41 |
volatile Throwable threadTwoProblem; |
| 42 |
|
| 43 |
@Test |
| 44 |
public void testClassLoadingDeadlockAvoidance() throws Throwable { |
| 45 |
/* |
| 46 |
Uncomment the following lines to demonstrate a deadlock that occurs with normal classloader delegation |
| 47 |
*/ |
| 48 |
//final DeadLockingLoader classLoaderOne = new DeadLockingLoader(ConcurrentClassLoaderTest.class.getClassLoader(), Arrays.asList(ClassA.class.getName(), ClassD.class.getName())); |
| 49 |
//final DeadLockingLoader classLoaderTwo = new DeadLockingLoader(ConcurrentClassLoaderTest.class.getClassLoader(), Arrays.asList(ClassB.class.getName(), ClassC.class.getName())); |
| 50 |
final TestConcurrentClassLoader classLoaderOne = new TestConcurrentClassLoader(ConcurrentClassLoaderTest.class.getClassLoader(), Arrays.asList(ClassA.class.getName(), ClassD.class.getName())); |
| 51 |
final TestConcurrentClassLoader classLoaderTwo = new TestConcurrentClassLoader(ConcurrentClassLoaderTest.class.getClassLoader(), Arrays.asList(ClassB.class.getName(), ClassC.class.getName())); classLoaderOne.delegate = classLoaderTwo; |
| 52 |
classLoaderTwo.delegate = classLoaderOne; |
| 53 |
|
| 54 |
final CountDownLatch latch = new CountDownLatch(1); |
| 55 |
final Thread threadOne = new Thread(new Runnable() { |
| 56 |
@Override |
| 57 |
public void run() { |
| 58 |
try { |
| 59 |
latch.await(); |
| 60 |
classLoaderOne.loadClass(ClassA.class.getName()); |
| 61 |
} catch (Throwable t) { |
| 62 |
threadOneProblem = t; |
| 63 |
} |
| 64 |
} |
| 65 |
}); |
| 66 |
final Thread threadTwo = new Thread(new Runnable() { |
| 67 |
@Override |
| 68 |
public void run() { |
| 69 |
try { |
| 70 |
latch.await(); |
| 71 |
classLoaderTwo.loadClass(ClassC.class.getName()); |
| 72 |
} catch (Throwable t) { |
| 73 |
threadTwoProblem = t; |
| 74 |
} |
| 75 |
} |
| 76 |
}); |
| 77 |
threadOne.start(); |
| 78 |
threadTwo.start(); |
| 79 |
|
| 80 |
latch.countDown(); |
| 81 |
|
| 82 |
threadOne.join(); |
| 83 |
threadTwo.join(); |
| 84 |
|
| 85 |
if (threadOneProblem != null) throw threadOneProblem; |
| 86 |
if (threadTwoProblem != null) throw threadTwoProblem; |
| 87 |
} |
| 88 |
|
| 89 |
private static final class TestConcurrentClassLoader extends ConcurrentClassLoader { |
| 90 |
static { |
| 91 |
boolean parallelOk = true; |
| 92 |
try { |
| 93 |
parallelOk = ClassLoader.registerAsParallelCapable(); |
| 94 |
} catch (Throwable ignored) { |
| 95 |
} |
| 96 |
if (! parallelOk) { |
| 97 |
throw new Error("Failed to register " + TestConcurrentClassLoader.class.getName() + " as parallel-capable"); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
private final ClassLoader realLoader; |
| 102 |
private final Set<String> allowedClasses = new HashSet<String>(); |
| 103 |
private ClassLoader delegate; |
| 104 |
|
| 105 |
private TestConcurrentClassLoader(final ClassLoader realLoader, final Collection<String> allowedClasses) { |
| 106 |
this.realLoader = realLoader; |
| 107 |
this.allowedClasses.addAll(allowedClasses); |
| 108 |
} |
| 109 |
|
| 110 |
@Override |
| 111 |
protected Class<?> findClass(String className, boolean exportsOnly, final boolean resolve) throws ClassNotFoundException { |
| 112 |
Class<?> c = findLoadedClass(className); |
| 113 |
if(c == null && className.startsWith("java")) |
| 114 |
c = findSystemClass(className); |
| 115 |
if(c == null && allowedClasses.contains(className)) { |
| 116 |
try { |
| 117 |
final byte[] classBytes = Util.getClassBytes(realLoader.loadClass(className)); |
| 118 |
c = defineClass(className, classBytes, 0, classBytes.length); |
| 119 |
} catch(Throwable t) { |
| 120 |
throw new ClassNotFoundException("Failed to load class " + className, t); |
| 121 |
} |
| 122 |
} |
| 123 |
if(c == null) |
| 124 |
c = delegate.loadClass(className); |
| 125 |
return c; |
| 126 |
} |
| 127 |
}; |
| 128 |
} |