use of com.serotonin.m2m2.rt.script.WrapperContext in project ma-core-public by MangoAutomation.
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);
}
}
use of com.serotonin.m2m2.rt.script.WrapperContext in project ma-core-public by infiniteautomation.
the class ScriptExecutor method prepareEngine.
/**
* Prepare the engine by creating the context
* @param engine
* @param context
* @param additionalContext
* @param runtime
* @param permissions
* @param scriptWriter
* @return
*/
protected static Bindings prepareEngine(ScriptEngine engine, Map<String, IDataPointValueSource> context, Map<String, Object> additionalContext, long runtime, long timestamp, ScriptPermissions permissions, PrintWriter scriptWriter, ScriptLog log, ScriptPointValueSetter setter, List<JsonImportExclusion> importExclusions, boolean testRun) {
ScriptUtils.prepareEngine(engine);
ScriptUtils.wrapperContext(engine, new WrapperContext(runtime, timestamp));
Bindings engineScope = engine.getBindings(ScriptContext.ENGINE_SCOPE);
// TODO Bubble PointValueSetter back up to top
if (permissions != null)
ScriptUtils.prepareUtilities(permissions, engine, engineScope, setter, importExclusions, testRun);
if (additionalContext != null) {
Set<Entry<String, Object>> entries = additionalContext.entrySet();
for (Entry<String, Object> entry : entries) engineScope.put(entry.getKey(), entry.getValue());
}
// Put the context variables into the engine with engine scope.
for (String varName : context.keySet()) {
IDataPointValueSource point = context.get(varName);
engineScope.put(varName, ScriptUtils.wrapPoint(engine, point, setter));
}
engineScope.put(ScriptUtils.POINTS_MAP_KEY, context);
// Set the print writer if necessary
if (scriptWriter != null) {
engine.getContext().setWriter(scriptWriter);
engineScope.put(ScriptLog.CONTEXT_KEY, log);
}
return engineScope;
}
use of com.serotonin.m2m2.rt.script.WrapperContext 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