use of javax.script.ScriptContext in project OpenAM by OpenRock.
the class StandardScriptEvaluator method buildScriptContext.
/**
* Build the script context for evaluating a script, using the given set of variables for the engine scope.
*
* @param engineScope the variable bindings to use for the engine scope.
* @return the configured script context.
*/
private ScriptContext buildScriptContext(Bindings engineScope) {
final ScriptContext context = new SimpleScriptContext();
context.setBindings(engineScope, ScriptContext.ENGINE_SCOPE);
context.setBindings(scriptEngineManager.getBindings(), ScriptContext.GLOBAL_SCOPE);
// Replace reader/writer instances with null versions
context.setReader(null);
// Groovy expects these writers to be non-null, so use the Groovy-supplied NullWriter instance
context.setWriter(NullWriter.DEFAULT);
context.setErrorWriter(NullWriter.DEFAULT);
return context;
}
use of javax.script.ScriptContext in project OpenAM by OpenRock.
the class StandardScriptEvaluator method evaluateScript.
/**
* Evaluates scripts immediately using the configured JSR-223 script engine manager. This implementation should
* be wrapped with a {@link org.forgerock.openam.scripting.ThreadPoolScriptEvaluator} if script interruption or
* timeouts are required.
*
* @param script the script to evaluate.
* @param bindings any additional variable bindings to set before running the script.
* @param <T> the type of result returned from the script.
* @return the result of evaluating the script.
* @throws ScriptException if an error occurs in script execution.
*/
@Override
@SuppressWarnings("unchecked")
public <T> T evaluateScript(final ScriptObject script, final Bindings bindings) throws ScriptException {
Reject.ifNull(script);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Evaluating script: " + script);
}
final ScriptEngine engine = getScriptEngineFor(script);
final Bindings variableBindings = mergeBindings(script.getBindings(), bindings);
final ScriptContext context = buildScriptContext(variableBindings);
return (T) engine.eval(script.getScript(), context);
}
use of javax.script.ScriptContext in project OpenAM by OpenRock.
the class SandboxedGroovyScriptEngineTest method shouldUnregisterEngineAfterException.
@Test(expectedExceptions = ScriptException.class)
public void shouldUnregisterEngineAfterException() throws Exception {
// Given
String script = "1 + 1";
ScriptContext context = new SimpleScriptContext();
given(mockScriptEngine.eval(script, context)).willThrow(new ScriptException("test"));
// When
try {
testEngine.eval(script, context);
} finally {
// Then
verify(mockValueFilter).unregister();
}
}
use of javax.script.ScriptContext in project OpenAM by OpenRock.
the class RhinoCompiledScriptTest method shouldDelegateToScriptEngine.
@Test
public void shouldDelegateToScriptEngine() throws ScriptException {
// Given
ScriptContext context = mock(ScriptContext.class);
Object expectedResult = "a result";
given(mockEngine.evalCompiled(mockScript, context)).willReturn(expectedResult);
// When
Object result = testScript.eval(context);
// Then
verify(mockEngine).evalCompiled(mockScript, context);
assertThat(result).isEqualTo(expectedResult);
}
use of javax.script.ScriptContext in project OpenAM by OpenRock.
the class SandboxedGroovyScriptEngineTest method shouldApplySandboxToCompiledScripts.
@Test
public void shouldApplySandboxToCompiledScripts() throws Exception {
// Given
String script = "1 + 1";
ScriptContext context = new SimpleScriptContext();
CompiledScript mockCompiledScript = mock(CompiledScript.class);
given(mockScriptEngine.compile(script)).willReturn(mockCompiledScript);
// When
CompiledScript compiledScript = testEngine.compile(script);
compiledScript.eval(context);
// Then
verify(mockValueFilter).register();
verify(mockCompiledScript).eval(context);
verify(mockValueFilter).unregister();
}
Aggregations