Merge pull request #4049 from stianst/KEYCLOAK-4738

KEYCLOAK-4738 Make sure script engine always uses correct classloader
This commit is contained in:
Stian Thorgersen 2017-04-20 10:02:23 +02:00 committed by GitHub
commit 2a8b2aabb9
2 changed files with 21 additions and 8 deletions

View file

@ -110,7 +110,13 @@ public class DefaultScriptingProvider implements ScriptingProvider {
* Looks-up a {@link ScriptEngine} based on the MIME-type provided by the given {@link Script}.
*/
private ScriptEngine lookupScriptEngineFor(ScriptModel script) {
return scriptEngineManager.getEngineByMimeType(script.getMimeType());
ClassLoader cl = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(DefaultScriptingProvider.class.getClassLoader());
return scriptEngineManager.getEngineByMimeType(script.getMimeType());
} finally {
Thread.currentThread().setContextClassLoader(cl);
}
}
@Override

View file

@ -29,9 +29,13 @@ public class DefaultScriptingProviderFactory implements ScriptingProviderFactory
static final String ID = "script-based-auth";
private ScriptEngineManager scriptEngineManager;
@Override
public ScriptingProvider create(KeycloakSession session) {
return new DefaultScriptingProvider(ScriptEngineManagerHolder.SCRIPT_ENGINE_MANAGER);
lazyInit();
return new DefaultScriptingProvider(scriptEngineManager);
}
@Override
@ -54,11 +58,14 @@ public class DefaultScriptingProviderFactory implements ScriptingProviderFactory
return ID;
}
/**
* Holder class for lazy initialization of {@link ScriptEngineManager}.
*/
private static class ScriptEngineManagerHolder {
private static final ScriptEngineManager SCRIPT_ENGINE_MANAGER = new ScriptEngineManager();
private void lazyInit() {
if (scriptEngineManager == null) {
synchronized (this) {
if (scriptEngineManager == null) {
scriptEngineManager = new ScriptEngineManager();
}
}
}
}
}