Search in sources :

Example 1 with ScriptIOException

use of com.infiniteautomation.mango.spring.script.MangoScriptException.ScriptIOException 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 2 with ScriptIOException

use of com.infiniteautomation.mango.spring.script.MangoScriptException.ScriptIOException 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 3 with ScriptIOException

use of com.infiniteautomation.mango.spring.script.MangoScriptException.ScriptIOException 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);
    }
}
Also used : ScriptException(javax.script.ScriptException) ScriptIOException(com.infiniteautomation.mango.spring.script.MangoScriptException.ScriptIOException) Reader(java.io.Reader) ScriptIOException(com.infiniteautomation.mango.spring.script.MangoScriptException.ScriptIOException) IOException(java.io.IOException) ScriptEngine(javax.script.ScriptEngine) ScriptEvalException(com.infiniteautomation.mango.spring.script.MangoScriptException.ScriptEvalException)

Example 4 with ScriptIOException

use of com.infiniteautomation.mango.spring.script.MangoScriptException.ScriptIOException 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);
    }
}
Also used : ScriptException(javax.script.ScriptException) ScriptIOException(com.infiniteautomation.mango.spring.script.MangoScriptException.ScriptIOException) Reader(java.io.Reader) ScriptIOException(com.infiniteautomation.mango.spring.script.MangoScriptException.ScriptIOException) IOException(java.io.IOException) ScriptEngine(javax.script.ScriptEngine) ScriptEvalException(com.infiniteautomation.mango.spring.script.MangoScriptException.ScriptEvalException)

Aggregations

ScriptEvalException (com.infiniteautomation.mango.spring.script.MangoScriptException.ScriptEvalException)4 ScriptIOException (com.infiniteautomation.mango.spring.script.MangoScriptException.ScriptIOException)4 IOException (java.io.IOException)4 Reader (java.io.Reader)4 ScriptEngine (javax.script.ScriptEngine)4 ScriptException (javax.script.ScriptException)4 MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)2 ScriptBindingsDefinition (com.serotonin.m2m2.module.ScriptBindingsDefinition)2 ScriptEngineDefinition (com.serotonin.m2m2.module.ScriptEngineDefinition)2 Bindings (javax.script.Bindings)2 ScriptContext (javax.script.ScriptContext)2