Cannot find ScriptEngine for JDK8 and Wildfly

Closes #12247
This commit is contained in:
mposolda 2022-06-06 21:04:31 +02:00 committed by Marek Posolda
parent 73c3517436
commit 5d2bf6ea33
3 changed files with 34 additions and 7 deletions

View file

@ -23,6 +23,7 @@ import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import org.jboss.logging.Logger;
import org.keycloak.models.ScriptModel;
import org.keycloak.platform.Platform;
import org.keycloak.services.ServicesLogger;
@ -35,6 +36,8 @@ import org.keycloak.utils.ProxyClassLoader;
*/
public class DefaultScriptingProvider implements ScriptingProvider {
private static final Logger logger = Logger.getLogger(DefaultScriptingProvider.class);
private final DefaultScriptingProviderFactory factory;
DefaultScriptingProvider(DefaultScriptingProviderFactory factory) {
@ -136,6 +139,8 @@ public class DefaultScriptingProvider implements ScriptingProvider {
scriptClassLoader = DefaultScriptingProvider.class.getClassLoader();
}
logger.debugf("Using classloader %s to load script engine", scriptClassLoader);
Thread.currentThread().setContextClassLoader(scriptClassLoader);
return new ScriptEngineManager().getEngineByMimeType(script.getMimeType());
}

View file

@ -255,15 +255,26 @@
<version>${mssql.driver.version}</version>
</dependency>
<!-- Nashorn -->
<dependency>
<groupId>org.openjdk.nashorn</groupId>
<artifactId>nashorn-core</artifactId>
</dependency>
</dependencies>
<profiles>
<!-- Nashorn script engine needs to be manually added for the new Java versions as it is not part of the JDK anymore -->
<profile>
<id>jdk15</id>
<activation>
<jdk>[15,)</jdk>
</activation>
<dependencies>
<dependency>
<groupId>org.openjdk.nashorn</groupId>
<artifactId>nashorn-core</artifactId>
</dependency>
</dependencies>
</profile>
<profile>
<id>keycloak-server</id>
<build>

View file

@ -35,6 +35,9 @@ public class WildflyPlatform implements PlatformProvider {
// In this module, the attempt to load script engine will be done by default
private static final String DEFAULT_SCRIPT_ENGINE_MODULE = "org.openjdk.nashorn.nashorn-core";
// Module name for deployment of keycloak server
private static final String DEPLOYMENT_MODULE_NAME = "deployment.keycloak-server.war";
Runnable shutdownHook;
private File tmpDir;
@ -97,10 +100,18 @@ public class WildflyPlatform implements PlatformProvider {
} catch (ModuleLoadException mle) {
if (WildflyUtil.getJavaVersion() >= 15) {
log.warnf("Cannot find script engine in the JBoss module '%s'. Please add JavaScript engine to the specified JBoss Module or make sure it is available on the classpath", engineModule);
return null;
} else {
log.debugf("Cannot find script engine in the JBoss module '%s'. Will fallback to the default script engine", engineModule);
try {
Module module = Module.getContextModuleLoader().loadModule(ModuleIdentifier.fromString(DEPLOYMENT_MODULE_NAME));
log.debugf("Cannot find script engine in the JBoss module '%s'. Will fallback to the default script engine available from the module '%s'", engineModule, DEPLOYMENT_MODULE_NAME);
return module.getClassLoader();
} catch (ModuleLoadException mle2) {
// Should not happen
log.warnf("Cannot find script engine in the JBoss module '%s' and in the module '%s'. Please add JavaScript engine to the specified JBoss Module or make sure it is available on the classpath", engineModule, DEPLOYMENT_MODULE_NAME);
return null;
}
}
return null;
}
}
}