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}. * Looks-up a {@link ScriptEngine} based on the MIME-type provided by the given {@link Script}.
*/ */
private ScriptEngine lookupScriptEngineFor(ScriptModel 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 @Override

View file

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