use of com.serotonin.m2m2.module.ScriptEngineDefinition in project ma-core-public by MangoAutomation.
the class ScriptService method getInterface.
public <T> T getInterface(MangoScript script, Class<T> clazz, EvalContext evalContext) {
ScriptAndEngine scriptAndEngine = getScriptEngine(script);
ScriptEngine engine = scriptAndEngine.engine;
if (!(engine instanceof Invocable)) {
throw new EngineNotInvocableException(engine);
}
evalScript(scriptAndEngine, evalContext);
T instance = ((Invocable) engine).getInterface(clazz);
if (instance == null) {
throw new ScriptInterfaceException(clazz);
}
ScriptEngineDefinition engineDefinition = scriptAndEngine.engineDefinition;
T runAsInstance = runAs.runAsProxy(script, instance);
if (engineDefinition.singleThreadedAccess()) {
return synchronizedProxy(scriptAndEngine.synchronizationObject, runAsInstance);
}
return runAsInstance;
}
use of com.serotonin.m2m2.module.ScriptEngineDefinition 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.serotonin.m2m2.module.ScriptEngineDefinition 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.serotonin.m2m2.module.ScriptEngineDefinition in project ma-core-public by infiniteautomation.
the class ScriptService method getInterface.
public <T> T getInterface(MangoScript script, Class<T> clazz, EvalContext evalContext) {
ScriptAndEngine scriptAndEngine = getScriptEngine(script);
ScriptEngine engine = scriptAndEngine.engine;
if (!(engine instanceof Invocable)) {
throw new EngineNotInvocableException(engine);
}
evalScript(scriptAndEngine, evalContext);
T instance = ((Invocable) engine).getInterface(clazz);
if (instance == null) {
throw new ScriptInterfaceException(clazz);
}
ScriptEngineDefinition engineDefinition = scriptAndEngine.engineDefinition;
T runAsInstance = runAs.runAsProxy(script, instance);
if (engineDefinition.singleThreadedAccess()) {
return synchronizedProxy(scriptAndEngine.synchronizationObject, runAsInstance);
}
return runAsInstance;
}
use of com.serotonin.m2m2.module.ScriptEngineDefinition in project ma-core-public by infiniteautomation.
the class ScriptService method getScriptEngine.
private ScriptAndEngine getScriptEngine(MangoScript script) {
ScriptEngine engine;
ScriptEngineDefinition definition;
if (script instanceof CompiledMangoScript) {
engine = ((CompiledMangoScript) script).compiled.getEngine();
definition = definitionForFactory(engine.getFactory());
} else {
ScriptEngineFactory factory = getFactoryByName(script.getEngineName());
definition = definitionForFactory(factory);
engine = definition.createEngine(factory, script);
}
PermissionHolder user = Common.getUser();
permissionService.ensurePermission(user, definition.requiredPermission());
permissionService.hasSupersetOfRoles(user, script);
return new ScriptAndEngine(script, definition, engine);
}
Aggregations