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();
}
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);
}
}
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);
}
}
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);
}
}
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);
}
Aggregations