Search in sources :

Example 1 with ScriptContext

use of javax.script.ScriptContext in project OpenAM by OpenRock.

the class SandboxedGroovyScriptEngineTest method shouldApplySandboxToCompiledReaderScripts.

@Test
public void shouldApplySandboxToCompiledReaderScripts() throws Exception {
    // Given
    String script = "1 + 1";
    Reader reader = new StringReader(script);
    ScriptContext context = new SimpleScriptContext();
    CompiledScript mockCompiledScript = mock(CompiledScript.class);
    given(mockScriptEngine.compile(reader)).willReturn(mockCompiledScript);
    // When
    CompiledScript compiledScript = testEngine.compile(reader);
    compiledScript.eval(context);
    // Then
    verify(mockValueFilter).register();
    verify(mockCompiledScript).eval(context);
    verify(mockValueFilter).unregister();
}
Also used : CompiledScript(javax.script.CompiledScript) SimpleScriptContext(javax.script.SimpleScriptContext) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Test(org.testng.annotations.Test)

Example 2 with ScriptContext

use of javax.script.ScriptContext in project OpenAM by OpenRock.

the class RhinoScriptEngine method eval.

/**
     * {@inheritDoc}
     */
@Override
public Object eval(final Reader reader, final ScriptContext scriptContext) throws ScriptException {
    Reject.ifNull(reader, scriptContext);
    Object result = null;
    final Context context = factory.getContext();
    try {
        final Scriptable scope = getScope(context, scriptContext);
        final String filename = getFilename(scriptContext);
        result = context.evaluateReader(scope, reader, filename, 1, null);
    } catch (RhinoException ex) {
        throw convertException(ex);
    } catch (IOException ex) {
        throw new ScriptException(ex);
    } finally {
        factory.releaseContext(context);
    }
    return result;
}
Also used : Context(org.mozilla.javascript.Context) ScriptContext(javax.script.ScriptContext) ScriptException(javax.script.ScriptException) ScriptableObject(org.mozilla.javascript.ScriptableObject) RhinoException(org.mozilla.javascript.RhinoException) IOException(java.io.IOException) Scriptable(org.mozilla.javascript.Scriptable)

Example 3 with ScriptContext

use of javax.script.ScriptContext in project OpenAM by OpenRock.

the class RhinoScriptEngine method evalCompiled.

/**
     * Evaluates a pre-compiled script against this script engine. This should only be called by
     * {@link org.forgerock.openam.scripting.factories.RhinoCompiledScript#eval(javax.script.ScriptContext)}.
     *
     * @param compiledScript The compiled script. Must not be null.
     * @param scriptContext The JSR 223 script context. Must not be null.
     * @return the result of evaluating the compiled script.
     * @throws ScriptException if an error occurs during script execution.
     */
Object evalCompiled(final Script compiledScript, final ScriptContext scriptContext) throws ScriptException {
    Reject.ifNull(compiledScript, scriptContext);
    Object result = null;
    final Context context = factory.getContext();
    try {
        final Scriptable scope = getScope(context, scriptContext);
        result = compiledScript.exec(context, scope);
    } catch (RhinoException ex) {
        throw convertException(ex);
    } finally {
        factory.releaseContext(context);
    }
    return result;
}
Also used : Context(org.mozilla.javascript.Context) ScriptContext(javax.script.ScriptContext) ScriptableObject(org.mozilla.javascript.ScriptableObject) RhinoException(org.mozilla.javascript.RhinoException) Scriptable(org.mozilla.javascript.Scriptable)

Example 4 with ScriptContext

use of javax.script.ScriptContext in project OpenAM by OpenRock.

the class RhinoCompiledScriptTest method shouldPropagateExceptions.

@Test(expectedExceptions = ScriptException.class)
public void shouldPropagateExceptions() throws ScriptException {
    // Given
    ScriptContext context = mock(ScriptContext.class);
    given(mockEngine.evalCompiled(mockScript, context)).willThrow(new ScriptException("test exception"));
    // When
    testScript.eval(context);
// Then - exception
}
Also used : ScriptException(javax.script.ScriptException) ScriptContext(javax.script.ScriptContext) Test(org.testng.annotations.Test)

Example 5 with ScriptContext

use of javax.script.ScriptContext in project GNS by MobilityFirst.

the class ActiveBlockingRunner method updateCache.

private synchronized void updateCache(String codeId, String code) throws ScriptException {
    if (!contexts.containsKey(codeId)) {
        // Create a context if one does not yet exist and eval the code
        ScriptContext sc = new SimpleScriptContext();
        contexts.put(codeId, sc);
        codeHashes.put(codeId, code.hashCode());
        engine.eval(code, sc);
    } else if (codeHashes.get(codeId) != code.hashCode()) {
        // The context exists, but we need to eval the new code
        ScriptContext sc = contexts.get(codeId);
        codeHashes.put(codeId, code.hashCode());
        engine.eval(code, sc);
    }
}
Also used : SimpleScriptContext(javax.script.SimpleScriptContext) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext)

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