Search in sources :

Example 1 with SimpleScriptContext

use of javax.script.SimpleScriptContext 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 SimpleScriptContext

use of javax.script.SimpleScriptContext 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)

Example 3 with SimpleScriptContext

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

the class ActiveNonBlockingRunner method updateCache.

/**
	 * Update cache needs to be synchronized, as some code cache may not be evaled before being used.
	 * 
	 * @param codeId
	 * @param code
	 * @throws ScriptException
	 */
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)

Example 4 with SimpleScriptContext

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

the class ActiveTrustedRunner 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)

Example 5 with SimpleScriptContext

use of javax.script.SimpleScriptContext in project sling by apache.

the class JsEnvironment method runResource.

public void runResource(Resource scriptResource, Bindings globalBindings, Bindings arguments, UnaryCallback callback) {
    ScriptContext scriptContext = new SimpleScriptContext();
    CommonJsModule module = new CommonJsModule();
    Bindings scriptBindings = buildBindings(scriptResource, globalBindings, arguments, module);
    scriptContext.setBindings(scriptBindings, ScriptContext.ENGINE_SCOPE);
    scriptContext.setAttribute(ScriptEngine.FILENAME, scriptResource.getPath(), ScriptContext.ENGINE_SCOPE);
    runScript(scriptResource, scriptContext, callback, module);
}
Also used : CommonJsModule(org.apache.sling.scripting.sightly.js.impl.cjs.CommonJsModule) SimpleScriptContext(javax.script.SimpleScriptContext) SimpleScriptContext(javax.script.SimpleScriptContext) ScriptContext(javax.script.ScriptContext) Bindings(javax.script.Bindings) SimpleBindings(javax.script.SimpleBindings)

Aggregations

SimpleScriptContext (javax.script.SimpleScriptContext)73 ScriptContext (javax.script.ScriptContext)59 Bindings (javax.script.Bindings)30 SimpleBindings (javax.script.SimpleBindings)26 Test (org.junit.Test)26 ScriptEngine (javax.script.ScriptEngine)10 ScriptException (javax.script.ScriptException)9 CompiledScript (javax.script.CompiledScript)8 NashornScriptEngine (jdk.nashorn.api.scripting.NashornScriptEngine)7 ScriptEngineManager (javax.script.ScriptEngineManager)6 File (java.io.File)4 HashMap (java.util.HashMap)4 Test (org.testng.annotations.Test)4 Reader (java.io.Reader)3 StringReader (java.io.StringReader)3 Invocable (javax.script.Invocable)3 FileNotFoundException (java.io.FileNotFoundException)2 FileReader (java.io.FileReader)2 IOException (java.io.IOException)2 OutputStreamWriter (java.io.OutputStreamWriter)2