use of com.infiniteautomation.mango.util.script.ScriptUtility in project ma-core-public by infiniteautomation.
the class MangoJavaScriptService method execute.
/**
* Reset result and execute script for any type of result
*/
public void execute(CompiledMangoJavaScript script, long runtime, long timestamp) throws ScriptError, ScriptPermissionsException {
try {
runAs.runAsCallable(script.getPermissionHolder(), () -> {
script.getResult().reset();
// Setup the wrapper context
Bindings engineScope = script.getEngine().getBindings(ScriptContext.ENGINE_SCOPE);
engineScope.put(MangoJavaScriptService.WRAPPER_CONTEXT_KEY, new WrapperContext(runtime, timestamp));
// Ensure the result is available to the utilities
for (ScriptUtility util : script.getUtilities()) {
util.setResult(script.getResult());
}
// Initialize additional utilities
for (ScriptUtility util : script.getAdditionalUtilities()) util.setResult(script.getResult());
Object resultObject = script.getCompiledScript().eval();
script.getResult().setResult(resultObject);
return null;
});
} catch (ScriptException e) {
throw ScriptError.create(e, script.isWrapInFunction());
} catch (RuntimeException e) {
// Nashorn seems to like to wrap exceptions in RuntimeException
if (e.getCause() instanceof ScriptPermissionsException)
throw (ScriptPermissionsException) e.getCause();
else
throw new ShouldNeverHappenException(e);
} catch (Exception e) {
throw new ShouldNeverHappenException(e);
}
}
Aggregations