Example usage for javax.script CompiledScript getEngine

List of usage examples for javax.script CompiledScript getEngine

Introduction

In this page you can find the example usage for javax.script CompiledScript getEngine.

Prototype

public abstract ScriptEngine getEngine();

Source Link

Document

Returns the ScriptEngine whose compile method created this CompiledScript.

Usage

From source file:org.openhab.binding.ebus.internal.parser.EBusTelegramParser.java

/**
 * Evaluates the compiled script of a entry.
 * @param entry The configuration entry to evaluate
 * @param scopeValues All known values for script scope
 * @return The computed value/*from  ww w.ja v  a  2 s .c  o m*/
 * @throws ScriptException
 */
private Object evaluateScript(Entry<String, Map<String, Object>> entry, Map<String, Object> scopeValues)
        throws ScriptException {

    Object value = null;

    // executes compiled script
    if (entry.getValue().containsKey("cscript")) {
        CompiledScript cscript = (CompiledScript) entry.getValue().get("cscript");

        // Add global variables thisValue and keyName to JavaScript context
        Bindings bindings = cscript.getEngine().createBindings();
        bindings.putAll(scopeValues);
        value = cscript.eval(bindings);
    }

    // try to convert the returned value to BigDecimal
    value = ObjectUtils.defaultIfNull(NumberUtils.toBigDecimal(value), value);

    // round to two digits, maybe not optimal for any result
    if (value instanceof BigDecimal) {
        ((BigDecimal) value).setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    return value;
}

From source file:org.openhab.binding.ebus.parser.EBusTelegramParser.java

/**
 * @param entry//w  ww.  j ava  2 s .c  o  m
 * @param bindings2
 * @return
 * @throws ScriptException
 */
private Object evaluateScript(Entry<String, Map<String, Object>> entry, Map<String, Object> bindings2)
        throws ScriptException {
    Object value = null;
    if (entry.getValue().containsKey("cscript")) {
        CompiledScript cscript = (CompiledScript) entry.getValue().get("cscript");

        // Add global variables thisValue and keyName to JavaScript context
        Bindings bindings = cscript.getEngine().createBindings();
        bindings.putAll(bindings2);
        value = cscript.eval(bindings);
    }

    value = ObjectUtils.defaultIfNull(NumberUtils.toBigDecimal(value), value);

    // round to two digits, maybe not optimal for any result
    if (value instanceof BigDecimal) {
        ((BigDecimal) value).setScale(2, BigDecimal.ROUND_HALF_UP);
    }

    return value;
}