use of com.infiniteautomation.mango.spring.script.MangoScriptException.ScriptEvalException in project ma-core-public by MangoAutomation.
the class ScriptService method evalScript.
private EvalResult evalScript(ScriptAndEngine scriptAndEngine, EvalContext evalContext) {
MangoScript script = scriptAndEngine.script;
ScriptEngine engine = scriptAndEngine.engine;
ScriptEngineDefinition engineDefinition = scriptAndEngine.engineDefinition;
ScriptContext context = engine.getContext();
Bindings engineBindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
engineBindings.putAll(evalContext.bindings);
context.setWriter(evalContext.writer);
context.setErrorWriter(evalContext.errorWriter);
context.setReader(evalContext.reader);
String scriptFilename = script.getScriptFilename();
if (scriptFilename != null) {
engineBindings.put(ScriptEngine.FILENAME, scriptFilename);
} else {
String scriptName = script.getScriptName();
String fileName = scriptName;
List<String> extensions = engine.getFactory().getExtensions();
boolean hasExtension = extensions.stream().anyMatch(e -> scriptName.endsWith("." + e));
if (!hasExtension && !extensions.isEmpty()) {
fileName += "." + extensions.get(0);
}
engineBindings.put(ScriptEngine.FILENAME, fileName);
}
for (ScriptBindingsDefinition bindingsDef : bindingsDefinitions) {
MangoPermission permission = bindingsDef.requiredPermission();
if (permissionService.hasPermission(script, permission)) {
bindingsDef.addBindings(script, engineBindings, scriptAndEngine.synchronizationObject, engineDefinition);
}
}
return this.runAs.runAs(script, () -> {
try {
Object value;
if (script instanceof CompiledMangoScript) {
value = ((CompiledMangoScript) script).compiled.eval();
} else {
try (Reader reader = script.readScript()) {
value = engine.eval(reader);
}
}
return new EvalResult(value, engineBindings);
} catch (ScriptException e) {
throw new ScriptEvalException(e, engineDefinition.extractSourceLocation(e));
} catch (IOException e) {
throw new ScriptIOException(e);
}
});
}
use of com.infiniteautomation.mango.spring.script.MangoScriptException.ScriptEvalException in project ma-core-public by infiniteautomation.
the class ScriptService method evalScript.
private EvalResult evalScript(ScriptAndEngine scriptAndEngine, EvalContext evalContext) {
MangoScript script = scriptAndEngine.script;
ScriptEngine engine = scriptAndEngine.engine;
ScriptEngineDefinition engineDefinition = scriptAndEngine.engineDefinition;
ScriptContext context = engine.getContext();
Bindings engineBindings = context.getBindings(ScriptContext.ENGINE_SCOPE);
engineBindings.putAll(evalContext.bindings);
context.setWriter(evalContext.writer);
context.setErrorWriter(evalContext.errorWriter);
context.setReader(evalContext.reader);
String scriptFilename = script.getScriptFilename();
if (scriptFilename != null) {
engineBindings.put(ScriptEngine.FILENAME, scriptFilename);
} else {
String scriptName = script.getScriptName();
String fileName = scriptName;
List<String> extensions = engine.getFactory().getExtensions();
boolean hasExtension = extensions.stream().anyMatch(e -> scriptName.endsWith("." + e));
if (!hasExtension && !extensions.isEmpty()) {
fileName += "." + extensions.get(0);
}
engineBindings.put(ScriptEngine.FILENAME, fileName);
}
for (ScriptBindingsDefinition bindingsDef : bindingsDefinitions) {
MangoPermission permission = bindingsDef.requiredPermission();
if (permissionService.hasPermission(script, permission)) {
bindingsDef.addBindings(script, engineBindings, scriptAndEngine.synchronizationObject, engineDefinition);
}
}
return this.runAs.runAs(script, () -> {
try {
Object value;
if (script instanceof CompiledMangoScript) {
value = ((CompiledMangoScript) script).compiled.eval();
} else {
try (Reader reader = script.readScript()) {
value = engine.eval(reader);
}
}
return new EvalResult(value, engineBindings);
} catch (ScriptException e) {
throw new ScriptEvalException(e, engineDefinition.extractSourceLocation(e));
} catch (IOException e) {
throw new ScriptIOException(e);
}
});
}
use of com.infiniteautomation.mango.spring.script.MangoScriptException.ScriptEvalException in project ma-core-public by infiniteautomation.
the class ScriptService method compile.
public CompiledMangoScript compile(MangoScript script) {
if (script instanceof CompiledMangoScript) {
return (CompiledMangoScript) script;
}
ScriptAndEngine scriptAndEngine = getScriptEngine(script);
ScriptEngine engine = scriptAndEngine.engine;
try (Reader reader = script.readScript()) {
return new CompiledMangoScript(((Compilable) engine).compile(reader), script);
} catch (ScriptException e) {
throw new ScriptEvalException(e, scriptAndEngine.engineDefinition.extractSourceLocation(e));
} catch (IOException e) {
throw new ScriptIOException(e);
}
}
use of com.infiniteautomation.mango.spring.script.MangoScriptException.ScriptEvalException in project ma-core-public by MangoAutomation.
the class ScriptService method compile.
public CompiledMangoScript compile(MangoScript script) {
if (script instanceof CompiledMangoScript) {
return (CompiledMangoScript) script;
}
ScriptAndEngine scriptAndEngine = getScriptEngine(script);
ScriptEngine engine = scriptAndEngine.engine;
try (Reader reader = script.readScript()) {
return new CompiledMangoScript(((Compilable) engine).compile(reader), script);
} catch (ScriptException e) {
throw new ScriptEvalException(e, scriptAndEngine.engineDefinition.extractSourceLocation(e));
} catch (IOException e) {
throw new ScriptIOException(e);
}
}
Aggregations