GEODE-2796: ClassPathLoader does not blow up with null TCCL
Project: http://git-wip-us.apache.org/repos/asf/geode/repo Commit: http://git-wip-us.apache.org/repos/asf/geode/commit/b842c708 Tree: http://git-wip-us.apache.org/repos/asf/geode/tree/b842c708 Diff: http://git-wip-us.apache.org/repos/asf/geode/diff/b842c708 Branch: refs/heads/feature/GEODE-2681 Commit: b842c708fa5b38a374201f4592e253fb4c7a46b8 Parents: 3261319 Author: Jared Stewart <[email protected]> Authored: Tue Apr 18 14:08:19 2017 -0700 Committer: Ken Howe <[email protected]> Committed: Thu Apr 20 15:13:17 2017 -0700 ---------------------------------------------------------------------- .../apache/geode/internal/ClassPathLoader.java | 10 +++++++-- .../ClassPathLoaderIntegrationTest.java | 22 +++++++++++++++++--- 2 files changed, 27 insertions(+), 5 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/geode/blob/b842c708/geode-core/src/main/java/org/apache/geode/internal/ClassPathLoader.java ---------------------------------------------------------------------- diff --git a/geode-core/src/main/java/org/apache/geode/internal/ClassPathLoader.java b/geode-core/src/main/java/org/apache/geode/internal/ClassPathLoader.java index 41cce05..39e9d62 100755 --- a/geode-core/src/main/java/org/apache/geode/internal/ClassPathLoader.java +++ b/geode-core/src/main/java/org/apache/geode/internal/ClassPathLoader.java @@ -32,6 +32,7 @@ import java.net.URLClassLoader; import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicReference; +import java.util.stream.Collectors; /** * The delegating <tt>ClassLoader</tt> used by GemFire to load classes and other resources. This @@ -303,10 +304,15 @@ public final class ClassPathLoader { ArrayList<ClassLoader> classLoaders = new ArrayList<>(); if (!excludeTCCL) { - classLoaders.add(Thread.currentThread().getContextClassLoader()); + ClassLoader tccl = Thread.currentThread().getContextClassLoader(); + if (tccl != null) { + classLoaders.add(tccl); + } } - classLoaders.add(classLoaderForDeployedJars); + if (classLoaderForDeployedJars != null) { + classLoaders.add(classLoaderForDeployedJars); + } return classLoaders; } http://git-wip-us.apache.org/repos/asf/geode/blob/b842c708/geode-core/src/test/java/org/apache/geode/internal/ClassPathLoaderIntegrationTest.java ---------------------------------------------------------------------- diff --git a/geode-core/src/test/java/org/apache/geode/internal/ClassPathLoaderIntegrationTest.java b/geode-core/src/test/java/org/apache/geode/internal/ClassPathLoaderIntegrationTest.java index c783318..2a3a7dd 100644 --- a/geode-core/src/test/java/org/apache/geode/internal/ClassPathLoaderIntegrationTest.java +++ b/geode-core/src/test/java/org/apache/geode/internal/ClassPathLoaderIntegrationTest.java @@ -91,6 +91,24 @@ public class ClassPathLoaderIntegrationTest { ClassPathLoader.setLatestToDefault(temporaryFolder.getRoot()); } + @Test + public void testClassLoaderWithNullTccl() throws IOException, ClassNotFoundException { + // GEODE-2796 + Thread.currentThread().setContextClassLoader(null); + String jarName = "JarDeployerIntegrationTest.jar"; + + String classAResource = "integration/parent/ClassA.class"; + + String classAName = "integration.parent.ClassA"; + + byte[] firstJarBytes = createJarWithClass("ClassA"); + + // First deploy of the JAR file + ClassPathLoader.getLatest().getJarDeployer().deploy(jarName, firstJarBytes).getFile(); + + assertThatClassCanBeLoaded(classAName); + assertThatResourceCanBeLoaded(classAResource); + } @Test public void testDeployFileAndChange() throws IOException, ClassNotFoundException { @@ -258,13 +276,11 @@ public class ClassPathLoaderIntegrationTest { List<String> result = (List<String>) execution.execute("MyFunction").getResult(); assertThat(result.get(0)).isEqualTo("Version1"); - ClassPathLoader.getLatest().getJarDeployer().deploy("MyJar.jar", FileUtils.readFileToByteArray(jarVersion2)); result = (List<String>) execution.execute("MyFunction").getResult(); assertThat(result.get(0)).isEqualTo("Version2"); - serverStarterRule.after(); } @@ -451,7 +467,7 @@ public class ClassPathLoaderIntegrationTest { /** * Currently unused but potentially useful for some future test. This causes this loader to only * generate a class that the parent could not find. - * + * * @param parent the parent class loader to check with first */ @SuppressWarnings("unused")
