2016-08-29 16:20:13 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2016 Red Hat, Inc. and/or its affiliates
|
|
|
|
* and other contributors as indicated by the @author tags.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2016-02-09 13:02:53 +00:00
|
|
|
package org.keycloak.scripting;
|
|
|
|
|
|
|
|
import javax.script.Bindings;
|
2017-06-19 13:48:50 +00:00
|
|
|
import javax.script.Compilable;
|
|
|
|
import javax.script.CompiledScript;
|
2016-02-09 13:02:53 +00:00
|
|
|
import javax.script.ScriptEngine;
|
|
|
|
import javax.script.ScriptEngineManager;
|
2017-06-19 13:48:50 +00:00
|
|
|
import javax.script.ScriptException;
|
|
|
|
|
|
|
|
import org.keycloak.models.ScriptModel;
|
2022-03-30 06:30:30 +00:00
|
|
|
import org.keycloak.services.ServicesLogger;
|
2016-02-09 13:02:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A {@link ScriptingProvider} that uses a {@link ScriptEngineManager} to evaluate scripts with a {@link ScriptEngine}.
|
|
|
|
*
|
|
|
|
* @author <a href="mailto:thomas.darimont@gmail.com">Thomas Darimont</a>
|
|
|
|
*/
|
|
|
|
public class DefaultScriptingProvider implements ScriptingProvider {
|
|
|
|
|
2022-03-30 06:30:30 +00:00
|
|
|
private final DefaultScriptingProviderFactory factory;
|
2016-02-09 13:02:53 +00:00
|
|
|
|
2022-03-30 06:30:30 +00:00
|
|
|
DefaultScriptingProvider(DefaultScriptingProviderFactory factory) {
|
|
|
|
this.factory = factory;
|
2016-02-09 13:02:53 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 16:20:13 +00:00
|
|
|
/**
|
|
|
|
* Wraps the provided {@link ScriptModel} in a {@link javax.script.Invocable} instance with bindings configured through the {@link ScriptBindingsConfigurer}.
|
|
|
|
*
|
2017-06-19 13:48:50 +00:00
|
|
|
* @param scriptModel must not be {@literal null}
|
2016-08-29 16:20:13 +00:00
|
|
|
* @param bindingsConfigurer must not be {@literal null}
|
|
|
|
*/
|
2016-02-09 13:02:53 +00:00
|
|
|
@Override
|
2016-08-29 16:20:13 +00:00
|
|
|
public InvocableScriptAdapter prepareInvocableScript(ScriptModel scriptModel, ScriptBindingsConfigurer bindingsConfigurer) {
|
2017-06-19 13:48:50 +00:00
|
|
|
final AbstractEvaluatableScriptAdapter evaluatable = prepareEvaluatableScript(scriptModel);
|
|
|
|
return evaluatable.prepareInvokableScript(bindingsConfigurer);
|
|
|
|
}
|
2016-02-09 13:02:53 +00:00
|
|
|
|
2017-06-19 13:48:50 +00:00
|
|
|
/**
|
|
|
|
* Wraps the provided {@link ScriptModel} in a {@link javax.script.Invocable} instance with bindings configured through the {@link ScriptBindingsConfigurer}.
|
|
|
|
*
|
|
|
|
* @param scriptModel must not be {@literal null}
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public AbstractEvaluatableScriptAdapter prepareEvaluatableScript(ScriptModel scriptModel) {
|
2016-08-29 16:20:13 +00:00
|
|
|
if (scriptModel == null) {
|
|
|
|
throw new IllegalArgumentException("script must not be null");
|
2016-02-09 13:02:53 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 16:20:13 +00:00
|
|
|
if (scriptModel.getCode() == null || scriptModel.getCode().trim().isEmpty()) {
|
2016-02-09 13:02:53 +00:00
|
|
|
throw new IllegalArgumentException("script must not be null or empty");
|
|
|
|
}
|
|
|
|
|
2022-03-30 06:30:30 +00:00
|
|
|
ScriptEngine engine = getPreparedScriptEngine(scriptModel);
|
2016-02-09 13:02:53 +00:00
|
|
|
|
2017-06-19 13:48:50 +00:00
|
|
|
if (engine instanceof Compilable) {
|
2018-01-12 11:20:43 +00:00
|
|
|
return new CompiledEvaluatableScriptAdapter(scriptModel, tryCompile(scriptModel, (Compilable) engine));
|
2017-06-19 13:48:50 +00:00
|
|
|
}
|
2018-01-12 11:20:43 +00:00
|
|
|
|
2017-06-19 13:48:50 +00:00
|
|
|
return new UncompiledEvaluatableScriptAdapter(scriptModel, engine);
|
2016-08-29 16:20:13 +00:00
|
|
|
}
|
|
|
|
|
2018-01-12 11:20:43 +00:00
|
|
|
private CompiledScript tryCompile(ScriptModel scriptModel, Compilable engine) {
|
|
|
|
try {
|
|
|
|
return engine.compile(scriptModel.getCode());
|
|
|
|
} catch (ScriptException e) {
|
|
|
|
throw new ScriptCompilationException(scriptModel, e);
|
|
|
|
}
|
|
|
|
}
|
2016-08-29 16:20:13 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public ScriptModel createScript(String realmId, String mimeType, String scriptName, String scriptCode, String scriptDescription) {
|
2017-06-19 13:48:50 +00:00
|
|
|
return new Script(null /* scriptId */, realmId, scriptName, mimeType, scriptCode, scriptDescription);
|
|
|
|
}
|
2016-08-29 16:20:13 +00:00
|
|
|
|
2017-06-19 13:48:50 +00:00
|
|
|
@Override
|
|
|
|
public void close() {
|
|
|
|
//NOOP
|
2016-08-29 16:20:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Looks-up a {@link ScriptEngine} with prepared {@link Bindings} for the given {@link ScriptModel Script}.
|
|
|
|
*/
|
2022-03-30 06:30:30 +00:00
|
|
|
private ScriptEngine getPreparedScriptEngine(ScriptModel script) {
|
|
|
|
// Try to lookup shared engine in the cache first
|
|
|
|
if (factory.isEnableScriptEngineCache()) {
|
|
|
|
ScriptEngine scriptEngine = factory.getScriptEngineCache().get(script.getMimeType());
|
|
|
|
if (scriptEngine != null) return scriptEngine;
|
|
|
|
}
|
|
|
|
|
2016-08-29 16:20:13 +00:00
|
|
|
ScriptEngine scriptEngine = lookupScriptEngineFor(script);
|
|
|
|
|
|
|
|
if (scriptEngine == null) {
|
2016-02-09 13:02:53 +00:00
|
|
|
throw new IllegalStateException("Could not find ScriptEngine for script: " + script);
|
|
|
|
}
|
|
|
|
|
2022-03-30 06:30:30 +00:00
|
|
|
ServicesLogger.LOGGER.scriptEngineCreated(scriptEngine.getFactory().getEngineName(), scriptEngine.getFactory().getEngineVersion(), script.getMimeType());
|
|
|
|
|
|
|
|
// Nashorn scriptEngine is ok to cache and share across multiple threads
|
|
|
|
if (factory.isEnableScriptEngineCache()) {
|
|
|
|
factory.getScriptEngineCache().put(script.getMimeType(), scriptEngine);
|
|
|
|
}
|
|
|
|
|
2016-08-29 16:20:13 +00:00
|
|
|
return scriptEngine;
|
2016-02-09 13:02:53 +00:00
|
|
|
}
|
|
|
|
|
2016-08-29 16:20:13 +00:00
|
|
|
/**
|
|
|
|
* Looks-up a {@link ScriptEngine} based on the MIME-type provided by the given {@link Script}.
|
|
|
|
*/
|
2016-02-09 13:02:53 +00:00
|
|
|
private ScriptEngine lookupScriptEngineFor(ScriptModel script) {
|
2017-04-20 07:28:46 +00:00
|
|
|
ClassLoader cl = Thread.currentThread().getContextClassLoader();
|
|
|
|
try {
|
|
|
|
Thread.currentThread().setContextClassLoader(DefaultScriptingProvider.class.getClassLoader());
|
2022-03-30 06:30:30 +00:00
|
|
|
return factory.getScriptEngineManager().getEngineByMimeType(script.getMimeType());
|
2017-06-19 13:48:50 +00:00
|
|
|
}
|
|
|
|
finally {
|
2017-04-20 07:28:46 +00:00
|
|
|
Thread.currentThread().setContextClassLoader(cl);
|
|
|
|
}
|
2016-02-09 13:02:53 +00:00
|
|
|
}
|
|
|
|
}
|