Skip to content

[MSHARED-1386] Manage context ClassLoader for current thread #121

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

Merged
merged 1 commit into from
Apr 29, 2024
Merged
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
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,18 @@
<executions>
<execution>
<goals>
<goal>copy-dependencies</goal>
<goal>copy</goal>
</goals>
<phase>generate-test-resources</phase>
<configuration>
<includeGroupIds>org.slf4j</includeGroupIds>
<stripVersion>true</stripVersion>
<artifactItems>
<item>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8-standalone</artifactId>
<version>2.35.2</version>
</item>
</artifactItems>
</configuration>
</execution>
</executions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,18 @@ public Object evaluateScript(String script, Map<String, ?> globalVariables, Prin
}
}
}

ClassLoader curentClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(classLoader);
return engine.eval(script);
} catch (TargetError e) {
throw new ScriptEvaluationException(e.getTarget());
} catch (ThreadDeath e) {
throw e;
} catch (Throwable e) {
throw new ScriptEvaluationException(e);
} finally {
Thread.currentThread().setContextClassLoader(curentClassLoader);
}
} finally {
System.setErr(origErr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public Object evaluateScript(String script, Map<String, ?> globalVariables, Prin
PrintStream origOut = System.out;
PrintStream origErr = System.err;

ClassLoader curentClassLoader = Thread.currentThread().getContextClassLoader();
try {

if (scriptOutput != null) {
Expand All @@ -80,10 +81,12 @@ public Object evaluateScript(String script, Map<String, ?> globalVariables, Prin
new Binding(globalVariables),
new CompilerConfiguration(CompilerConfiguration.DEFAULT));

Thread.currentThread().setContextClassLoader(childFirstLoader);
return interpreter.evaluate(script);
} catch (Throwable e) {
throw new ScriptEvaluationException(e);
} finally {
Thread.currentThread().setContextClassLoader(curentClassLoader);
System.setErr(origErr);
System.setOut(origOut);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import java.io.File;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -251,18 +251,17 @@ void theSameClassloaderShouldBeUsed(String scriptType) throws Exception {

try (FileLogger logger = new FileLogger(logFile);
ScriptRunner scriptRunner = new ScriptRunner()) {
scriptRunner.setClassPath(
Arrays.asList("target/dependency/slf4j-api.jar", "target/dependency/slf4j-simple.jar"));
scriptRunner.setClassPath(Collections.singletonList("target/dependency/wiremock-jre8-standalone.jar"));

scriptRunner.run("test classpath 1", basedir, "class-path1", context, logger);
assertNotNull(context.get("logger"));
assertNotNull(context.get("wireMockServer"));

scriptRunner.run("test classpath 2", basedir, "class-path2", context, logger);
}

String logContent = new String(Files.readAllBytes(logFile.toPath()));
assertTrue(logContent.contains("INFO test - Test log 1"));
assertTrue(logContent.contains("INFO test - Test log 2"));
assertTrue(logContent.contains("wireMockServer started with port="));
assertTrue(logContent.contains("wireMockServer stopped"));
}

private Map<String, ?> buildContext() {
Expand Down
14 changes: 9 additions & 5 deletions src/test/resources/bsh-test/class-path1.bsh
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@
* under the License.
*/

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;

Logger logger = LoggerFactory.getLogger("test");
context.put("logger", logger);
logger.info("Test log 1");

WireMockServer wireMockServer = new WireMockServer(WireMockConfiguration.options().dynamicPort().dynamicHttpsPort());
wireMockServer.start();

System.out.println("wireMockServer started with port=" + wireMockServer.port());

context.put("wireMockServer", wireMockServer);

return true;
8 changes: 5 additions & 3 deletions src/test/resources/bsh-test/class-path2.bsh
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
* under the License.
*/

import org.slf4j.Logger;
import com.github.tomakehurst.wiremock.WireMockServer;

Logger logger = context.get("logger");
logger.info("Test log 2");
WireMockServer wireMockServer = context.get("wireMockServer");
wireMockServer.stop();

System.out.println("wireMockServer stopped");

return true;
13 changes: 8 additions & 5 deletions src/test/resources/groovy-test/class-path1.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,14 @@
* under the License.
*/

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import com.github.tomakehurst.wiremock.WireMockServer
import com.github.tomakehurst.wiremock.core.WireMockConfiguration

Logger logger = LoggerFactory.getLogger("test")
context.put("logger", logger)
logger.info("Test log 1")
WireMockServer wireMockServer = new WireMockServer(WireMockConfiguration.options().dynamicPort().dynamicHttpsPort())
wireMockServer.start()

println("wireMockServer started with port=" + wireMockServer.port())

context.put("wireMockServer", wireMockServer)

return true
8 changes: 5 additions & 3 deletions src/test/resources/groovy-test/class-path2.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@
* under the License.
*/

import org.slf4j.Logger
import com.github.tomakehurst.wiremock.WireMockServer;

Logger logger = context.get("logger")
logger.info("Test log 2")
WireMockServer wireMockServer = context.get("wireMockServer")
wireMockServer.stop()

println("wireMockServer stopped")

return true