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