Skip to content

Commit

Permalink
ISPN-6303 Fix ScriptEngine lookup classloader
Browse files Browse the repository at this point in the history
* Getting ScriptEngine instances from ScriptEngineManager results in
  class loading calls for referenced classes in script. By default it
  uses the thread context classloader. This change fixes this
  classloader to the ScriptManagerImpl one.
  • Loading branch information
galderz authored and pruivo committed Jun 2, 2016
1 parent e69e55b commit 5135142
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Expand Up @@ -5,6 +5,7 @@
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentMap;
import java.util.function.BiFunction;
import javax.script.Bindings;
import javax.script.Compilable;
import javax.script.CompiledScript;
Expand Down Expand Up @@ -220,15 +221,40 @@ ScriptEngine getEngineForScript(ScriptMetadata metadata) {
ScriptEngine engine;
if (metadata.language().isPresent()) {
engine = scriptEnginesByLanguage.computeIfAbsent(metadata.language().get(),
scriptEngineManager::getEngineByName);
this::getEngineByName);
} else {
engine = scriptEnginesByExtension.computeIfAbsent(metadata.extension(),
scriptEngineManager::getEngineByExtension);
this::getEngineByExtension);
}
if (engine == null) {
throw log.noEngineForScript(metadata.name());
} else {
return engine;
}
}

private ScriptEngine getEngineByName(String shortName) {
return withClassLoader(ScriptingManagerImpl.class.getClassLoader(),
scriptEngineManager, shortName,
ScriptEngineManager::getEngineByName);
}

private ScriptEngine getEngineByExtension(String extension) {
return withClassLoader(ScriptingManagerImpl.class.getClassLoader(),
scriptEngineManager, extension,
ScriptEngineManager::getEngineByExtension);
}

private static ScriptEngine withClassLoader(ClassLoader cl,
ScriptEngineManager manager, String name,
BiFunction<ScriptEngineManager, String, ScriptEngine> f) {
ClassLoader curr = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(cl);
return f.apply(manager, name);
} finally {
Thread.currentThread().setContextClassLoader(curr);
}
}

}
Expand Up @@ -143,7 +143,7 @@ public void shouldExecuteMapReduceOnReplCacheViaTask() throws Exception {
}

@Test
@Ignore(value="Is disabled until the issue ISPN-6303 and ISPN-6173 are fixed.")
@Ignore(value="Is disabled until ISPN-6173 is fixed.")
public void shouldExecuteMapReduceViaJavaScriptInTask() throws Exception {
RemoteCache remoteCache = managers.get(1).getCache(DistributedJSExecutingServerTask.CACHE_NAME);
remoteCache.put(1, "word1 word2 word3");
Expand Down

0 comments on commit 5135142

Please sign in to comment.