Search in sources :

Example 1 with WrapperContext

use of com.serotonin.m2m2.rt.script.WrapperContext in project ma-core-public by MangoAutomation.

the class MangoJavaScriptService method execute.

/**
 * Reset result and execute script for any type of result
 */
public void execute(CompiledMangoJavaScript script, long runtime, long timestamp) throws ScriptError, ScriptPermissionsException {
    try {
        runAs.runAsCallable(script.getPermissionHolder(), () -> {
            script.getResult().reset();
            // Setup the wrapper context
            Bindings engineScope = script.getEngine().getBindings(ScriptContext.ENGINE_SCOPE);
            engineScope.put(MangoJavaScriptService.WRAPPER_CONTEXT_KEY, new WrapperContext(runtime, timestamp));
            // Ensure the result is available to the utilities
            for (ScriptUtility util : script.getUtilities()) {
                util.setResult(script.getResult());
            }
            // Initialize additional utilities
            for (ScriptUtility util : script.getAdditionalUtilities()) util.setResult(script.getResult());
            Object resultObject = script.getCompiledScript().eval();
            script.getResult().setResult(resultObject);
            return null;
        });
    } catch (ScriptException e) {
        throw ScriptError.create(e, script.isWrapInFunction());
    } catch (RuntimeException e) {
        // Nashorn seems to like to wrap exceptions in RuntimeException
        if (e.getCause() instanceof ScriptPermissionsException)
            throw (ScriptPermissionsException) e.getCause();
        else
            throw new ShouldNeverHappenException(e);
    } catch (Exception e) {
        throw new ShouldNeverHappenException(e);
    }
}
Also used : WrapperContext(com.serotonin.m2m2.rt.script.WrapperContext) ScriptException(javax.script.ScriptException) ScriptPermissionsException(com.serotonin.m2m2.rt.script.ScriptPermissionsException) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) ScriptUtility(com.infiniteautomation.mango.util.script.ScriptUtility) ScriptException(javax.script.ScriptException) ResultTypeException(com.serotonin.m2m2.rt.script.ResultTypeException) ScriptPermissionsException(com.serotonin.m2m2.rt.script.ScriptPermissionsException) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) DataPointStateException(com.serotonin.m2m2.rt.script.DataPointStateException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException)

Example 2 with WrapperContext

use of com.serotonin.m2m2.rt.script.WrapperContext in project ma-core-public by infiniteautomation.

the class ScriptExecutor method prepareEngine.

/**
 * Prepare the engine by creating the context
 * @param engine
 * @param context
 * @param additionalContext
 * @param runtime
 * @param permissions
 * @param scriptWriter
 * @return
 */
protected static Bindings prepareEngine(ScriptEngine engine, Map<String, IDataPointValueSource> context, Map<String, Object> additionalContext, long runtime, long timestamp, ScriptPermissions permissions, PrintWriter scriptWriter, ScriptLog log, ScriptPointValueSetter setter, List<JsonImportExclusion> importExclusions, boolean testRun) {
    ScriptUtils.prepareEngine(engine);
    ScriptUtils.wrapperContext(engine, new WrapperContext(runtime, timestamp));
    Bindings engineScope = engine.getBindings(ScriptContext.ENGINE_SCOPE);
    // TODO Bubble PointValueSetter back up to top
    if (permissions != null)
        ScriptUtils.prepareUtilities(permissions, engine, engineScope, setter, importExclusions, testRun);
    if (additionalContext != null) {
        Set<Entry<String, Object>> entries = additionalContext.entrySet();
        for (Entry<String, Object> entry : entries) engineScope.put(entry.getKey(), entry.getValue());
    }
    // Put the context variables into the engine with engine scope.
    for (String varName : context.keySet()) {
        IDataPointValueSource point = context.get(varName);
        engineScope.put(varName, ScriptUtils.wrapPoint(engine, point, setter));
    }
    engineScope.put(ScriptUtils.POINTS_MAP_KEY, context);
    // Set the print writer if necessary
    if (scriptWriter != null) {
        engine.getContext().setWriter(scriptWriter);
        engineScope.put(ScriptLog.CONTEXT_KEY, log);
    }
    return engineScope;
}
Also used : Entry(java.util.Map.Entry) IDataPointValueSource(com.serotonin.m2m2.rt.dataImage.IDataPointValueSource) Bindings(javax.script.Bindings)

Example 3 with WrapperContext

use of com.serotonin.m2m2.rt.script.WrapperContext in project ma-core-public by infiniteautomation.

the class MangoJavaScriptService method execute.

/**
 * Reset result and execute script for any type of result
 */
public void execute(CompiledMangoJavaScript script, long runtime, long timestamp) throws ScriptError, ScriptPermissionsException {
    try {
        runAs.runAsCallable(script.getPermissionHolder(), () -> {
            script.getResult().reset();
            // Setup the wrapper context
            Bindings engineScope = script.getEngine().getBindings(ScriptContext.ENGINE_SCOPE);
            engineScope.put(MangoJavaScriptService.WRAPPER_CONTEXT_KEY, new WrapperContext(runtime, timestamp));
            // Ensure the result is available to the utilities
            for (ScriptUtility util : script.getUtilities()) {
                util.setResult(script.getResult());
            }
            // Initialize additional utilities
            for (ScriptUtility util : script.getAdditionalUtilities()) util.setResult(script.getResult());
            Object resultObject = script.getCompiledScript().eval();
            script.getResult().setResult(resultObject);
            return null;
        });
    } catch (ScriptException e) {
        throw ScriptError.create(e, script.isWrapInFunction());
    } catch (RuntimeException e) {
        // Nashorn seems to like to wrap exceptions in RuntimeException
        if (e.getCause() instanceof ScriptPermissionsException)
            throw (ScriptPermissionsException) e.getCause();
        else
            throw new ShouldNeverHappenException(e);
    } catch (Exception e) {
        throw new ShouldNeverHappenException(e);
    }
}
Also used : WrapperContext(com.serotonin.m2m2.rt.script.WrapperContext) ScriptException(javax.script.ScriptException) ScriptPermissionsException(com.serotonin.m2m2.rt.script.ScriptPermissionsException) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) ScriptUtility(com.infiniteautomation.mango.util.script.ScriptUtility) ScriptException(javax.script.ScriptException) ResultTypeException(com.serotonin.m2m2.rt.script.ResultTypeException) ScriptPermissionsException(com.serotonin.m2m2.rt.script.ScriptPermissionsException) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) ValidationException(com.infiniteautomation.mango.util.exception.ValidationException) DataPointStateException(com.serotonin.m2m2.rt.script.DataPointStateException) PermissionException(com.serotonin.m2m2.vo.permission.PermissionException)

Aggregations

Bindings (javax.script.Bindings)3 ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)2 ScriptUtility (com.infiniteautomation.mango.util.script.ScriptUtility)2 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)2 DataPointStateException (com.serotonin.m2m2.rt.script.DataPointStateException)2 ResultTypeException (com.serotonin.m2m2.rt.script.ResultTypeException)2 ScriptPermissionsException (com.serotonin.m2m2.rt.script.ScriptPermissionsException)2 WrapperContext (com.serotonin.m2m2.rt.script.WrapperContext)2 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)2 ScriptException (javax.script.ScriptException)2 SimpleBindings (javax.script.SimpleBindings)2 IDataPointValueSource (com.serotonin.m2m2.rt.dataImage.IDataPointValueSource)1 Entry (java.util.Map.Entry)1