Search in sources :

Example 1 with ScriptEngineDefinition

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;
}
Also used : Invocable(javax.script.Invocable) ScriptInterfaceException(com.infiniteautomation.mango.spring.script.MangoScriptException.ScriptInterfaceException) EngineNotInvocableException(com.infiniteautomation.mango.spring.script.MangoScriptException.EngineNotInvocableException) ScriptEngine(javax.script.ScriptEngine) ScriptEngineDefinition(com.serotonin.m2m2.module.ScriptEngineDefinition)

Example 2 with ScriptEngineDefinition

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);
        }
    });
}
Also used : ScriptIOException(com.infiniteautomation.mango.spring.script.MangoScriptException.ScriptIOException) ScriptContext(javax.script.ScriptContext) Reader(java.io.Reader) ScriptIOException(com.infiniteautomation.mango.spring.script.MangoScriptException.ScriptIOException) IOException(java.io.IOException) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine) ScriptBindingsDefinition(com.serotonin.m2m2.module.ScriptBindingsDefinition) ScriptException(javax.script.ScriptException) ScriptEngineDefinition(com.serotonin.m2m2.module.ScriptEngineDefinition) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) ScriptEvalException(com.infiniteautomation.mango.spring.script.MangoScriptException.ScriptEvalException)

Example 3 with ScriptEngineDefinition

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);
        }
    });
}
Also used : ScriptIOException(com.infiniteautomation.mango.spring.script.MangoScriptException.ScriptIOException) ScriptContext(javax.script.ScriptContext) Reader(java.io.Reader) ScriptIOException(com.infiniteautomation.mango.spring.script.MangoScriptException.ScriptIOException) IOException(java.io.IOException) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine) ScriptBindingsDefinition(com.serotonin.m2m2.module.ScriptBindingsDefinition) ScriptException(javax.script.ScriptException) ScriptEngineDefinition(com.serotonin.m2m2.module.ScriptEngineDefinition) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) ScriptEvalException(com.infiniteautomation.mango.spring.script.MangoScriptException.ScriptEvalException)

Example 4 with ScriptEngineDefinition

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;
}
Also used : Invocable(javax.script.Invocable) ScriptInterfaceException(com.infiniteautomation.mango.spring.script.MangoScriptException.ScriptInterfaceException) EngineNotInvocableException(com.infiniteautomation.mango.spring.script.MangoScriptException.EngineNotInvocableException) ScriptEngine(javax.script.ScriptEngine) ScriptEngineDefinition(com.serotonin.m2m2.module.ScriptEngineDefinition)

Example 5 with ScriptEngineDefinition

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);
}
Also used : ScriptEngineFactory(javax.script.ScriptEngineFactory) PermissionHolder(com.serotonin.m2m2.vo.permission.PermissionHolder) ScriptEngine(javax.script.ScriptEngine) ScriptEngineDefinition(com.serotonin.m2m2.module.ScriptEngineDefinition)

Aggregations

ScriptEngineDefinition (com.serotonin.m2m2.module.ScriptEngineDefinition)6 ScriptEngine (javax.script.ScriptEngine)6 MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)2 EngineNotInvocableException (com.infiniteautomation.mango.spring.script.MangoScriptException.EngineNotInvocableException)2 ScriptEvalException (com.infiniteautomation.mango.spring.script.MangoScriptException.ScriptEvalException)2 ScriptIOException (com.infiniteautomation.mango.spring.script.MangoScriptException.ScriptIOException)2 ScriptInterfaceException (com.infiniteautomation.mango.spring.script.MangoScriptException.ScriptInterfaceException)2 ScriptBindingsDefinition (com.serotonin.m2m2.module.ScriptBindingsDefinition)2 PermissionHolder (com.serotonin.m2m2.vo.permission.PermissionHolder)2 IOException (java.io.IOException)2 Reader (java.io.Reader)2 Bindings (javax.script.Bindings)2 Invocable (javax.script.Invocable)2 ScriptContext (javax.script.ScriptContext)2 ScriptEngineFactory (javax.script.ScriptEngineFactory)2 ScriptException (javax.script.ScriptException)2