From 152af13ee88f4cce64f2dbe67db0563a29caa1dd Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Sat, 30 Jul 2022 17:57:01 -0700 Subject: [PATCH] Demonstrating usage of `SetContextClassLoader` --- pom.xml | 4 ++-- .../java/au/com/rayh/XCodeBuildOutputParser.java | 14 +++++--------- .../java/au/com/rayh/XcodeTestSummariesParser.java | 14 +++++--------- 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/pom.xml b/pom.xml index 35980ff..77fa4b0 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,7 @@ org.jenkins-ci.plugins plugin - 4.41 + 4.44 xcode-plugin @@ -146,7 +146,7 @@ 2.0.17 999999-SNAPSHOT jenkinsci/xcode-plugin - 2.319.1 + 2.362-SNAPSHOT diff --git a/src/main/java/au/com/rayh/XCodeBuildOutputParser.java b/src/main/java/au/com/rayh/XCodeBuildOutputParser.java index ddd5ffb..701938b 100644 --- a/src/main/java/au/com/rayh/XCodeBuildOutputParser.java +++ b/src/main/java/au/com/rayh/XCodeBuildOutputParser.java @@ -41,6 +41,7 @@ import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; +import jenkins.util.SetContextClassLoader; import au.com.rayh.report.TestCase; import au.com.rayh.report.TestError; @@ -176,16 +177,11 @@ private void requireTestCase(String name) { private void writeTestReport() throws IOException, InterruptedException, JAXBException { + JAXBContext jaxbContext; + try (SetContextClassLoader sccl = new SetContextClassLoader()) { + jaxbContext = JAXBContext.newInstance(TestSuite.class); + } try (OutputStream testReportOutputStream = outputForSuite()) { - JAXBContext jaxbContext; - Thread t = Thread.currentThread(); - ClassLoader orig = t.getContextClassLoader(); - t.setContextClassLoader(XCodeBuildOutputParser.class.getClassLoader()); - try { - jaxbContext = JAXBContext.newInstance(TestSuite.class); - } finally { - t.setContextClassLoader(orig); - } Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.marshal(currentTestSuite, testReportOutputStream); } diff --git a/src/main/java/au/com/rayh/XcodeTestSummariesParser.java b/src/main/java/au/com/rayh/XcodeTestSummariesParser.java index a85a4a4..22a3b06 100644 --- a/src/main/java/au/com/rayh/XcodeTestSummariesParser.java +++ b/src/main/java/au/com/rayh/XcodeTestSummariesParser.java @@ -7,6 +7,7 @@ import java.util.regex.Pattern; import java.text.ParseException; import hudson.FilePath; +import jenkins.util.SetContextClassLoader; import com.dd.plist.NSDictionary; import com.dd.plist.NSArray; @@ -40,16 +41,11 @@ public XcodeTestSummariesParser(FilePath workspace) throws IOException, Interrup private void writeTestReport(TestSuite currentTestSuite) throws IOException, InterruptedException, JAXBException { + JAXBContext jaxbContext; + try (SetContextClassLoader sccl = new SetContextClassLoader()) { + jaxbContext = JAXBContext.newInstance(TestSuite.class); + } try (OutputStream testReportOutputStream = outputForSuite(currentTestSuite)) { - JAXBContext jaxbContext; - Thread t = Thread.currentThread(); - ClassLoader orig = t.getContextClassLoader(); - t.setContextClassLoader(XcodeTestSummariesParser.class.getClassLoader()); - try { - jaxbContext = JAXBContext.newInstance(TestSuite.class); - } finally { - t.setContextClassLoader(orig); - } Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.marshal(currentTestSuite, testReportOutputStream); }