Skip to content

Commit

Permalink
[MSHARED-1359] Optimize classloader in BeanShellScriptInterpreter
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Mar 28, 2024
1 parent 5f1df6e commit 9dfe91d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,26 @@
*/
class BeanShellScriptInterpreter implements ScriptInterpreter {

private URLClassLoader classLoader;
private static class AppendableURLClassLoader extends URLClassLoader {
AppendableURLClassLoader() {
super(new URL[] {}, Thread.currentThread().getContextClassLoader());
}

@Override
public void addURL(URL url) {
super.addURL(url);
}
}

private final AppendableURLClassLoader classLoader = new AppendableURLClassLoader();

@Override
public void setClassPath(List<String> classPath) {
if (classPath == null || classPath.isEmpty()) {
return;
}

URL[] urls = classPath.stream().map(this::toUrl).toArray(URL[]::new);
classLoader = new URLClassLoader(urls, Thread.currentThread().getContextClassLoader());
classPath.stream().map(this::toUrl).forEach(classLoader::addURL);
}

private URL toUrl(String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -54,11 +53,6 @@ public class ScriptRunner implements Closeable {
*/
private Map<String, Object> globalVariables;

/**
* The additional class path for the script interpreter, never <code>null</code>.
*/
private List<String> classPath;

/**
* The file encoding of the hook scripts or <code>null</code> to use platform encoding.
*/
Expand All @@ -72,7 +66,6 @@ public ScriptRunner() {
scriptInterpreters.put("bsh", new BeanShellScriptInterpreter());
scriptInterpreters.put("groovy", new GroovyScriptInterpreter());
globalVariables = new HashMap<>();
classPath = new ArrayList<>();
}

/**
Expand Down Expand Up @@ -104,7 +97,9 @@ public void setGlobalVariable(String name, Object value) {
* artifacts from the plugin class path.
*/
public void setClassPath(List<String> classPath) {
this.classPath = (classPath != null) ? new ArrayList<>(classPath) : new ArrayList<>();
if (classPath != null && !classPath.isEmpty()) {
scriptInterpreters.values().forEach(scriptInterpreter -> scriptInterpreter.setClassPath(classPath));
}
}

/**
Expand All @@ -114,7 +109,7 @@ public void setClassPath(List<String> classPath) {
* default encoding.
*/
public void setScriptEncoding(String encoding) {
this.encoding = (encoding != null && encoding.length() > 0) ? encoding : null;
this.encoding = (encoding != null && !encoding.isEmpty()) ? encoding : null;
}

/**
Expand Down Expand Up @@ -221,7 +216,6 @@ private void executeRun(
scriptVariables.put("basedir", scriptFile.getParentFile());
scriptVariables.put("context", context);

interpreter.setClassPath(classPath);
result = interpreter.evaluateScript(script, scriptVariables, out);
if (logger != null) {
logger.consumeLine("Finished " + scriptDescription + ": " + scriptFile);
Expand Down

0 comments on commit 9dfe91d

Please sign in to comment.