From 9668605fa9b885c6683514aa05d8fb8f8f45663e Mon Sep 17 00:00:00 2001 From: Stephane Epardaud Date: Tue, 27 Oct 2020 12:27:31 +0100 Subject: [PATCH 1/3] Reset to the system CL in Application ThreadContext instead of null null is just always bad --- .../ApplicationContextProvider.java | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/application/src/main/java/io/smallrye/context/application/context/propagation/ApplicationContextProvider.java b/application/src/main/java/io/smallrye/context/application/context/propagation/ApplicationContextProvider.java index 3efcde76..2033d393 100644 --- a/application/src/main/java/io/smallrye/context/application/context/propagation/ApplicationContextProvider.java +++ b/application/src/main/java/io/smallrye/context/application/context/propagation/ApplicationContextProvider.java @@ -1,5 +1,7 @@ package io.smallrye.context.application.context.propagation; +import java.security.AccessController; +import java.security.PrivilegedAction; import java.util.Map; import org.eclipse.microprofile.context.ThreadContext; @@ -8,6 +10,28 @@ public class ApplicationContextProvider implements ThreadContextProvider { + static final ClassLoader SYSTEM_CL; + + static { + final SecurityManager sm = System.getSecurityManager(); + if (sm != null) { + SYSTEM_CL = AccessController + .doPrivileged((PrivilegedAction) ApplicationContextProvider::calculateSystemClassLoader); + } else { + SYSTEM_CL = calculateSystemClassLoader(); + } + } + + private static ClassLoader calculateSystemClassLoader() { + ClassLoader cl = ClassLoader.getSystemClassLoader(); + if (cl == null) { + // non-null ref that delegates to the system + cl = new ClassLoader(null) { + }; + } + return cl; + } + @Override public ThreadContextSnapshot currentContext(Map props) { ClassLoader capturedTCCL = Thread.currentThread().getContextClassLoader(); @@ -24,7 +48,7 @@ public ThreadContextSnapshot currentContext(Map props) { @Override public ThreadContextSnapshot clearedContext(Map props) { - ClassLoader capturedTCCL = null; + ClassLoader capturedTCCL = SYSTEM_CL; return () -> { ClassLoader movedTCCL = Thread.currentThread().getContextClassLoader(); if (capturedTCCL != movedTCCL) From 44f25e8a4e1ba811534252d56559cbcb946d9cd1 Mon Sep 17 00:00:00 2001 From: Stephane Epardaud Date: Tue, 27 Oct 2020 12:28:16 +0100 Subject: [PATCH 2/3] Bump to MP-CP 1.1, whose TCK requires application context --- pom.xml | 2 +- tck/pom.xml | 7 +++++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 6046e62e..23aca179 100644 --- a/pom.xml +++ b/pom.xml @@ -37,7 +37,7 @@ - 1.0.2 + 1.1 1.4 1.7.0 3.1.1.Final diff --git a/tck/pom.xml b/tck/pom.xml index f2a76ec6..eae780fb 100644 --- a/tck/pom.xml +++ b/tck/pom.xml @@ -39,6 +39,13 @@ ${project.version} test + + + ${project.groupId} + smallrye-context-propagation-application + ${project.version} + test + ${project.groupId} From 5f39e3803845c14976f91c58094a4adc5070e815 Mon Sep 17 00:00:00 2001 From: Stephane Epardaud Date: Tue, 27 Oct 2020 12:28:39 +0100 Subject: [PATCH 3/3] New SR-Config --- pom.xml | 3 ++- tests/pom.xml | 6 ++++++ .../context/test/classloading/MultiClassloadingTest.java | 5 +++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 23aca179..d15f45f8 100644 --- a/pom.xml +++ b/pom.xml @@ -39,7 +39,7 @@ 1.1 1.4 - 1.7.0 + 1.9.2 3.1.1.Final 1.3 5.10.4.Final @@ -47,6 +47,7 @@ 1.3.8 + 1.4.1 5.0.5.Final 4.5.3.Final 5.4.15.Final diff --git a/tests/pom.xml b/tests/pom.xml index 61f9451f..18565056 100644 --- a/tests/pom.xml +++ b/tests/pom.xml @@ -91,6 +91,12 @@ test + + io.smallrye.common + smallrye-common-function + ${version.smallrye.common} + + org.jboss.resteasy resteasy-core diff --git a/tests/src/test/java/io/smallrye/context/test/classloading/MultiClassloadingTest.java b/tests/src/test/java/io/smallrye/context/test/classloading/MultiClassloadingTest.java index 15cfdf9a..9c77806d 100644 --- a/tests/src/test/java/io/smallrye/context/test/classloading/MultiClassloadingTest.java +++ b/tests/src/test/java/io/smallrye/context/test/classloading/MultiClassloadingTest.java @@ -21,6 +21,7 @@ import java.util.Map; import java.util.function.BiConsumer; +import javax.annotation.Priority; import javax.enterprise.util.AnnotationLiteral; import org.eclipse.microprofile.config.ConfigProvider; @@ -38,6 +39,7 @@ import org.junit.Assert; import org.junit.Test; +import io.smallrye.common.function.Functions; import io.smallrye.config.SmallRyeConfig; import io.smallrye.config.SmallRyeConfigProviderResolver; import io.smallrye.context.SmallRyeContextManagerProvider; @@ -118,7 +120,10 @@ public void test() throws InstantiationException, IllegalAccessException, ClassN .addPackages(true, ConfigProvider.class.getPackage().getName()) .addPackages(true, SmallRyeConfig.class.getPackage().getName()) .addPackages(true, Logger.class.getPackage().getName()) + .addPackages(true, Functions.class.getPackage().getName()) .addPackage(AnnotationLiteral.class.getPackage().getName()) + .addPackage(Priority.class.getPackage().getName()) + .addPackage(io.smallrye.common.constraint.Assert.class.getPackage().getName()) // dont use addPackages for Smallrye-Context because it // would include test packages .addPackage(SmallRyeContextManagerProvider.class.getPackage().getName())