use of com.infiniteautomation.mango.util.script.MangoJavaScriptError in project ma-core-public by infiniteautomation.
the class MangoJavaScriptService method executeScript.
/**
* The preferred way to execute a script
*/
public MangoJavaScriptResult executeScript(MangoJavaScript vo, ScriptPointValueSetter setter) throws ValidationException, PermissionException {
PermissionHolder user = Common.getUser();
ensureValid(vo);
MangoJavaScriptResult result = new MangoJavaScriptResult();
final Writer scriptOut;
final PrintWriter scriptWriter;
if (vo.isReturnLogOutput()) {
scriptOut = new StringWriter();
scriptWriter = new PrintWriter(scriptOut);
} else {
NullWriter writer = new NullWriter();
scriptWriter = new NullPrintWriter(writer);
scriptOut = writer;
}
try {
try (ScriptLogExtender scriptLog = new ScriptLogExtender("scriptTest-" + user.getPermissionHolderName(), vo.getLogLevel(), scriptWriter, vo.getLog(), vo.isCloseLog())) {
CompiledMangoJavaScript script = new CompiledMangoJavaScript(vo, setter, scriptLog, result, this, pointValueDao, pointValueCache);
script.compile(vo.getScript(), vo.isWrapInFunction());
script.initialize(vo.getContext());
long time = Common.timer.currentTimeMillis();
runAs.runAsCallable(script.getPermissionHolder(), () -> {
if (vo.getResultDataType() != null) {
script.execute(time, time, vo.getResultDataType());
} else {
script.execute(time, time);
}
return null;
});
}
} catch (ScriptError e) {
// The script exception should be clean as both compile() and execute() clean it
result.addError(new MangoJavaScriptError(e.getTranslatableMessage(), e.getLineNumber(), e.getColumnNumber()));
} catch (ResultTypeException | DataPointStateException e) {
result.addError(new MangoJavaScriptError(e.getTranslatableMessage()));
} catch (Exception e) {
result.addError(new MangoJavaScriptError(e.getMessage()));
} finally {
if (vo.isReturnLogOutput())
result.setScriptOutput(scriptOut.toString());
}
return result;
}
use of com.infiniteautomation.mango.util.script.MangoJavaScriptError in project ma-core-public by infiniteautomation.
the class MangoJavaScriptService method testScript.
/**
*/
public MangoJavaScriptResult testScript(MangoJavaScript vo, BiFunction<MangoJavaScriptResult, PermissionHolder, ScriptPointValueSetter> createSetter, String noChangeKey) {
PermissionHolder user = Common.getUser();
ensureValid(vo);
final StringWriter scriptOut = new StringWriter();
MangoJavaScriptResult result = new MangoJavaScriptResult();
try {
final PrintWriter scriptWriter = new PrintWriter(scriptOut);
try (ScriptLog scriptLog = new ScriptLog("scriptTest-" + user.getPermissionHolderName(), vo.getLogLevel(), scriptWriter)) {
CompiledMangoJavaScript script = new CompiledMangoJavaScript(vo, createSetter.apply(result, vo.getPermissions()), scriptLog, result, this, pointValueDao, pointValueCache);
script.compile(vo.getScript(), vo.isWrapInFunction());
script.initialize(vo.getContext());
long time = Common.timer.currentTimeMillis();
runAs.runAsCallable(vo.getPermissions(), () -> {
if (vo.getResultDataType() != null) {
script.execute(time, time, vo.getResultDataType());
// Convert the UNCHANGED value
Object o = script.getResult().getResult();
if (o instanceof PointValueTime && ((PointValueTime) o).getValue() == UNCHANGED) {
// TODO fix this display hack:
String unchanged = new TranslatableMessage(noChangeKey).translate(Translations.getTranslations(user.getLocaleObject()));
script.getResult().setResult(new PointValueTime(unchanged, ((PointValueTime) o).getTime()));
}
} else {
script.execute(time, time);
}
return null;
});
}
} catch (ScriptError e) {
// The script exception should be clean as both compile() and execute() clean it
result.addError(new MangoJavaScriptError(e.getTranslatableMessage(), e.getLineNumber(), e.getColumnNumber()));
} catch (ResultTypeException e) {
result.addError(new MangoJavaScriptError(e.getTranslatableMessage()));
} catch (Exception e) {
result.addError(new MangoJavaScriptError(e.getMessage()));
} finally {
result.setScriptOutput(scriptOut.toString());
}
return result;
}
Aggregations