Search in sources :

Example 1 with ScriptUtility

use of com.infiniteautomation.mango.util.script.ScriptUtility in project ma-core-public by MangoAutomation.

the class MangoJavaScriptService method initialize.

/**
 * Reset the engine scope of a script and initialize for running
 * @param context - if provided points will be wrapped with script's setter (alternatively use script.addToContext()
 */
public void initialize(CompiledMangoJavaScript script, Map<String, IDataPointValueSource> context) throws ScriptError {
    if (context == null) {
        context = new HashMap<>();
    }
    Bindings engineScope = script.getEngine().getBindings(ScriptContext.ENGINE_SCOPE);
    // TODO Clear engine scope completely?
    engineScope.put(MangoJavaScriptService.UNCHANGED_KEY, MangoJavaScriptService.UNCHANGED);
    Set<String> points = new HashSet<>();
    engineScope.put(MangoJavaScriptService.POINTS_CONTEXT_KEY, points);
    // Holder for modifying timestamps of meta points, in Engine Scope so it can be modified by all
    engineScope.put(MangoJavaScriptService.TIMESTAMP_CONTEXT_KEY, null);
    if (script.getPermissionHolder() != null) {
        script.getUtilities().clear();
        for (MangoJavascriptContextObjectDefinition def : ModuleRegistry.getMangoJavascriptContextObjectDefinitions()) {
            ScriptUtility util = def.initializeContextObject(script);
            util.setScriptLog(script.getLog());
            util.setResult(script.getResult());
            util.takeContext(script.getEngine(), engineScope, script.getSetter(), script.getImportExclusions(), script.isTestRun());
            engineScope.put(util.getContextKey(), util);
            script.getUtilities().add(util);
        }
        // Initialize additional utilities
        for (ScriptUtility util : script.getAdditionalUtilities()) {
            util.setScriptLog(script.getLog());
            util.setResult(script.getResult());
            util.takeContext(script.getEngine(), engineScope, script.getSetter(), script.getImportExclusions(), script.isTestRun());
            engineScope.put(util.getContextKey(), util);
        }
    }
    Set<Entry<String, Object>> entries = script.getAdditionalContext().entrySet();
    for (Entry<String, Object> entry : entries) engineScope.put(entry.getKey(), entry.getValue());
    String selfPointXid = (String) script.getAdditionalContext().get(SELF_POINT_XID_KEY);
    Map<String, AbstractPointWrapper> external = new HashMap<>();
    for (String varName : context.keySet()) {
        IDataPointValueSource point = context.get(varName);
        AbstractPointWrapper wrapped = wrapPoint(script.getEngine(), point, script.getSetter());
        engineScope.put(varName, wrapped);
        points.add(varName);
        if (!point.getVO().getXid().equals(selfPointXid)) {
            external.put(varName, wrapped);
        }
    }
    engineScope.put(EXTERNAL_POINTS_KEY, external);
    engineScope.put(EXTERNAL_POINTS_ARRAY_KEY, external.values());
    engineScope.put(POINTS_MAP_KEY, context);
    // Set the print writer and log
    script.getEngine().getContext().setWriter(script.getLog().getStdOutWriter());
    engineScope.put(ScriptLog.CONTEXT_KEY, script.getLog());
    try {
        script.getEngine().eval(getGlobalFunctions());
    } 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 if (e.getCause() != null)
            throw ScriptError.createFromThrowable(e.getCause());
        else
            throw new ShouldNeverHappenException(e);
    }
}
Also used : MangoJavascriptContextObjectDefinition(com.serotonin.m2m2.module.MangoJavascriptContextObjectDefinition) HashMap(java.util.HashMap) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) ScriptException(javax.script.ScriptException) Entry(java.util.Map.Entry) ScriptPermissionsException(com.serotonin.m2m2.rt.script.ScriptPermissionsException) IDataPointValueSource(com.serotonin.m2m2.rt.dataImage.IDataPointValueSource) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) AbstractPointWrapper(com.serotonin.m2m2.rt.script.AbstractPointWrapper) ScriptUtility(com.infiniteautomation.mango.util.script.ScriptUtility) HashSet(java.util.HashSet)

Example 2 with ScriptUtility

use of com.infiniteautomation.mango.util.script.ScriptUtility 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 3 with ScriptUtility

use of com.infiniteautomation.mango.util.script.ScriptUtility in project ma-core-public by MangoAutomation.

the class MangoJavascriptContextObjectDefinition method initializeContextObject.

public ScriptUtility initializeContextObject(CompiledMangoJavaScript script) {
    Class<? extends ScriptUtility> utilityClass = script.isTestRun() ? getTestUtilityClass() : getUtilityClass();
    // Auto wire this guy
    ScriptUtility utility = Common.getRuntimeContext().getAutowireCapableBeanFactory().createBean(utilityClass);
    utility.setPermissions(script.getPermissionHolder());
    utility.setScriptEngine(script.getEngine());
    return utility;
}
Also used : ScriptUtility(com.infiniteautomation.mango.util.script.ScriptUtility)

Example 4 with ScriptUtility

use of com.infiniteautomation.mango.util.script.ScriptUtility in project ma-core-public by infiniteautomation.

the class MangoJavaScriptService method initialize.

/**
 * Reset the engine scope of a script and initialize for running
 * @param context - if provided points will be wrapped with script's setter (alternatively use script.addToContext()
 */
public void initialize(CompiledMangoJavaScript script, Map<String, IDataPointValueSource> context) throws ScriptError {
    if (context == null) {
        context = new HashMap<>();
    }
    Bindings engineScope = script.getEngine().getBindings(ScriptContext.ENGINE_SCOPE);
    // TODO Clear engine scope completely?
    engineScope.put(MangoJavaScriptService.UNCHANGED_KEY, MangoJavaScriptService.UNCHANGED);
    Set<String> points = new HashSet<>();
    engineScope.put(MangoJavaScriptService.POINTS_CONTEXT_KEY, points);
    // Holder for modifying timestamps of meta points, in Engine Scope so it can be modified by all
    engineScope.put(MangoJavaScriptService.TIMESTAMP_CONTEXT_KEY, null);
    if (script.getPermissionHolder() != null) {
        script.getUtilities().clear();
        for (MangoJavascriptContextObjectDefinition def : ModuleRegistry.getMangoJavascriptContextObjectDefinitions()) {
            ScriptUtility util = def.initializeContextObject(script);
            util.setScriptLog(script.getLog());
            util.setResult(script.getResult());
            util.takeContext(script.getEngine(), engineScope, script.getSetter(), script.getImportExclusions(), script.isTestRun());
            engineScope.put(util.getContextKey(), util);
            script.getUtilities().add(util);
        }
        // Initialize additional utilities
        for (ScriptUtility util : script.getAdditionalUtilities()) {
            util.setScriptLog(script.getLog());
            util.setResult(script.getResult());
            util.takeContext(script.getEngine(), engineScope, script.getSetter(), script.getImportExclusions(), script.isTestRun());
            engineScope.put(util.getContextKey(), util);
        }
    }
    Set<Entry<String, Object>> entries = script.getAdditionalContext().entrySet();
    for (Entry<String, Object> entry : entries) engineScope.put(entry.getKey(), entry.getValue());
    String selfPointXid = (String) script.getAdditionalContext().get(SELF_POINT_XID_KEY);
    Map<String, AbstractPointWrapper> external = new HashMap<>();
    for (String varName : context.keySet()) {
        IDataPointValueSource point = context.get(varName);
        AbstractPointWrapper wrapped = wrapPoint(script.getEngine(), point, script.getSetter());
        engineScope.put(varName, wrapped);
        points.add(varName);
        if (!point.getVO().getXid().equals(selfPointXid)) {
            external.put(varName, wrapped);
        }
    }
    engineScope.put(EXTERNAL_POINTS_KEY, external);
    engineScope.put(EXTERNAL_POINTS_ARRAY_KEY, external.values());
    engineScope.put(POINTS_MAP_KEY, context);
    // Set the print writer and log
    script.getEngine().getContext().setWriter(script.getLog().getStdOutWriter());
    engineScope.put(ScriptLog.CONTEXT_KEY, script.getLog());
    try {
        script.getEngine().eval(getGlobalFunctions());
    } 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 if (e.getCause() != null)
            throw ScriptError.createFromThrowable(e.getCause());
        else
            throw new ShouldNeverHappenException(e);
    }
}
Also used : MangoJavascriptContextObjectDefinition(com.serotonin.m2m2.module.MangoJavascriptContextObjectDefinition) HashMap(java.util.HashMap) SimpleBindings(javax.script.SimpleBindings) Bindings(javax.script.Bindings) ScriptException(javax.script.ScriptException) Entry(java.util.Map.Entry) ScriptPermissionsException(com.serotonin.m2m2.rt.script.ScriptPermissionsException) IDataPointValueSource(com.serotonin.m2m2.rt.dataImage.IDataPointValueSource) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) AbstractPointWrapper(com.serotonin.m2m2.rt.script.AbstractPointWrapper) ScriptUtility(com.infiniteautomation.mango.util.script.ScriptUtility) HashSet(java.util.HashSet)

Example 5 with ScriptUtility

use of com.infiniteautomation.mango.util.script.ScriptUtility in project ma-core-public by infiniteautomation.

the class MangoJavascriptContextObjectDefinition method initializeContextObject.

public ScriptUtility initializeContextObject(CompiledMangoJavaScript script) {
    Class<? extends ScriptUtility> utilityClass = script.isTestRun() ? getTestUtilityClass() : getUtilityClass();
    // Auto wire this guy
    ScriptUtility utility = Common.getRuntimeContext().getAutowireCapableBeanFactory().createBean(utilityClass);
    utility.setPermissions(script.getPermissionHolder());
    utility.setScriptEngine(script.getEngine());
    return utility;
}
Also used : ScriptUtility(com.infiniteautomation.mango.util.script.ScriptUtility)

Aggregations

ScriptUtility (com.infiniteautomation.mango.util.script.ScriptUtility)6 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)4 ScriptPermissionsException (com.serotonin.m2m2.rt.script.ScriptPermissionsException)4 Bindings (javax.script.Bindings)4 ScriptException (javax.script.ScriptException)4 SimpleBindings (javax.script.SimpleBindings)4 ValidationException (com.infiniteautomation.mango.util.exception.ValidationException)2 MangoJavascriptContextObjectDefinition (com.serotonin.m2m2.module.MangoJavascriptContextObjectDefinition)2 IDataPointValueSource (com.serotonin.m2m2.rt.dataImage.IDataPointValueSource)2 AbstractPointWrapper (com.serotonin.m2m2.rt.script.AbstractPointWrapper)2 DataPointStateException (com.serotonin.m2m2.rt.script.DataPointStateException)2 ResultTypeException (com.serotonin.m2m2.rt.script.ResultTypeException)2 WrapperContext (com.serotonin.m2m2.rt.script.WrapperContext)2 PermissionException (com.serotonin.m2m2.vo.permission.PermissionException)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Entry (java.util.Map.Entry)2