Search in sources :

Example 76 with ScriptContext

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;
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext)

Example 77 with ScriptContext

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);
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine)

Example 78 with ScriptContext

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();
    }
}
Also used : ScriptException(javax.script.ScriptException) SimpleScriptContext(javax.script.SimpleScriptContext) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Test(org.testng.annotations.Test)

Example 79 with ScriptContext

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);
}
Also used : ScriptContext(javax.script.ScriptContext) Test(org.testng.annotations.Test)

Example 80 with ScriptContext

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();
}
Also used : CompiledScript(javax.script.CompiledScript) SimpleScriptContext(javax.script.SimpleScriptContext) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Test(org.testng.annotations.Test)

Aggregations

ScriptContext (javax.script.ScriptContext)126 SimpleScriptContext (javax.script.SimpleScriptContext)83 Bindings (javax.script.Bindings)33 Test (org.junit.Test)30 SimpleBindings (javax.script.SimpleBindings)28 Test (org.junit.jupiter.api.Test)19 ScriptEngine (javax.script.ScriptEngine)18 ScriptException (javax.script.ScriptException)18 HashMap (java.util.HashMap)13 CompiledScript (javax.script.CompiledScript)11 IOException (java.io.IOException)9 Map (java.util.Map)8 ScriptEngineManager (javax.script.ScriptEngineManager)8 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)7 Test (org.testng.annotations.Test)7 StringWriter (java.io.StringWriter)6 NashornScriptEngine (jdk.nashorn.api.scripting.NashornScriptEngine)6 XWikiException (com.xpn.xwiki.XWikiException)5 Reader (java.io.Reader)5 StringReader (java.io.StringReader)5