Example usage for jdk.nashorn.api.scripting ScriptObjectMirror to

List of usage examples for jdk.nashorn.api.scripting ScriptObjectMirror to

Introduction

In this page you can find the example usage for jdk.nashorn.api.scripting ScriptObjectMirror to.

Prototype

public <T> T to(final Class<T> type) 

Source Link

Document

Utility to convert this script object to the given type.

Usage

From source file:org.openremote.manager.rules.RulesetDeployment.java

License:Open Source License

/**
 * Marshal the JavaScript rules array into {@link Rule} instances.
 *///from ww  w  .  j a va 2  s . c  o  m
protected void registerRulesJavascript(ScriptObjectMirror scriptRules) {
    if (scriptRules == null || !scriptRules.isArray()) {
        throw new IllegalArgumentException("No 'rules' array defined in ruleset");
    }
    Collection<Object> rulesObjects = scriptRules.values();
    for (Object rulesObject : rulesObjects) {
        ScriptObjectMirror rule = (ScriptObjectMirror) rulesObject;

        String name;
        if (!rule.containsKey("name")) {
            throw new IllegalArgumentException("Missing 'name' in rule definition");
        }
        try {
            name = (String) rule.getMember("name");
        } catch (ClassCastException ex) {
            throw new IllegalArgumentException("Defined 'name' of rule is not a string");
        }

        String description;
        try {
            description = rule.containsKey("description") ? (String) rule.getMember("description") : null;
        } catch (ClassCastException ex) {
            throw new IllegalArgumentException("Defined 'description' is not a string in rule: " + name);
        }

        int priority;
        try {
            priority = rule.containsKey("priority") ? (int) rule.getMember("priority") : DEFAULT_RULE_PRIORITY;
        } catch (ClassCastException ex) {
            throw new IllegalArgumentException("Defined 'priority' is not a number in rule: " + name);
        }

        if (!rule.containsKey("when")) {
            throw new IllegalArgumentException("Missing 'when' function in rule: " + name);
        }

        Condition when;
        try {
            ScriptObjectMirror whenMirror = (ScriptObjectMirror) rule.getMember("when");
            if (!whenMirror.isFunction()) {
                throw new IllegalArgumentException("Defined 'when' is not a function in rule: " + name);
            }
            when = whenMirror.to(Condition.class);
        } catch (ClassCastException ex) {
            throw new IllegalArgumentException("Defined 'when' is not a function in rule: " + name);
        }

        Action then;
        try {
            ScriptObjectMirror thenMirror = (ScriptObjectMirror) rule.getMember("then");
            if (!thenMirror.isFunction()) {
                throw new IllegalArgumentException("Defined 'then' is not a function in rule: " + name);
            }
            then = thenMirror.to(Action.class);
        } catch (ClassCastException ex) {
            throw new IllegalArgumentException("Defined 'then' is not a function in rule: " + name);
        }

        RulesEngine.LOG.info("Registering rule: " + name);

        rules.register(new RuleBuilder().name(name).description(description).priority(priority).when(when)
                .then(then).build());
    }
}

From source file:org.siphon.common.js.JsDateUtil.java

License:Open Source License

public Object toNativeDate(double time) throws ScriptException {
    ScriptObjectMirror m = (ScriptObjectMirror) newDate.eval();
    NativeDate.setTime(m.to(NativeDate.class), time);
    return m;/* w ww  .j av  a 2  s . co m*/
}

From source file:org.siphon.common.js.JsEngineUtil.java

License:Open Source License

public static Object eval(ScriptEngine jsEngine, String srcFile, boolean onlyOnce, boolean preservePathInStack)
        throws Exception {
    ScriptObjectMirror importedFiles = (ScriptObjectMirror) jsEngine.get("IMPORTED_FILES");
    if (importedFiles.containsKey(srcFile)) {
        if (onlyOnce)
            return null;
    } else {//from  w  w w  .  j a  v  a 2  s .c om
        importedFiles.put(srcFile, true);
    }
    ScriptObjectMirror stk = (ScriptObjectMirror) jsEngine.get("IMPORTS_PATH_STACK");
    // NativeArray.pushObject(stk.to(NativeArray.class), srcFile);
    //stk.callMember("push", srcFile);

    try {
        String code = FileUtils.readFileToString(new File(srcFile), "utf-8");
        return eval(jsEngine, srcFile, code);
    } catch (ScriptException | FileNotFoundException e) {
        throw e;
    } finally {
        if (!preservePathInStack)
            NativeArray.pop(stk.to(NativeArray.class));
    }
}

From source file:org.siphon.common.js.JsEngineUtil.java

License:Open Source License

public static Object eval(ScriptEngine jsEngine, String srcFile, String aliasPath, String script,
        boolean onlyOnce, boolean preservePathInStack) throws NoSuchMethodException, ScriptException {
    ScriptObjectMirror importedFiles = (ScriptObjectMirror) jsEngine.get("IMPORTED_FILES");
    if (importedFiles.containsKey(srcFile)) {
        if (onlyOnce)
            return null;
    } else {/*from   w w w  . j a v  a  2 s  .  com*/
        importedFiles.put(srcFile, true);
    }
    ScriptObjectMirror stk = (ScriptObjectMirror) jsEngine.get("IMPORTS_PATH_STACK");
    //NativeArray.pushObject((Object)(stk.to(NativeArray.class)), (Object)srcFile);
    stk.callMember("push", srcFile);

    try {
        return eval(jsEngine, aliasPath, script);
    } catch (ScriptException e) {
        throw e;
    } finally {
        if (!preservePathInStack)
            NativeArray.pop(stk.to(NativeArray.class));
    }
}

From source file:org.siphon.common.js.JsTypeUtil.java

License:Open Source License

public ScriptObjectMirror newArray(Object... objects) throws ScriptException {
    ScriptObjectMirror arrayMirror = (ScriptObjectMirror) this.arrayConstructor.newObject();
    NativeArray array = arrayMirror.to(NativeArray.class);
    for (int i = 0; i < objects.length; i++) {
        // NativeArray.push(array, objects[i]);
        arrayMirror.callMember("push", objects[i]);
    }//from  w w  w .ja v  a  2s .c om
    return arrayMirror;
}

From source file:org.siphon.common.js.JsTypeUtil.java

License:Open Source License

public static Object getSealed(ScriptObjectMirror scriptObjectMirror) {
    try {//from ww  w.  ja v a2  s .c om
        return scriptObjectMirror.to(Object.class);
    } catch (Exception e) {
        return null;
    }
}

From source file:org.siphon.common.js.JsTypeUtil.java

License:Open Source License

public ScriptObjectMirror bytesToNativeArray(byte[] arr) throws ScriptException {
    ScriptObjectMirror result = this.newArray();
    NativeArray array = result.to(NativeArray.class);
    for (int i = 0; i < arr.length; i++) {
        NativeArray.push(array, arr[i]);
        // result.callMember("push", arr[i]);
    }//from  w  ww  .  j a  v a 2s . co m
    return result;
}

From source file:org.siphon.common.js.JsTypeUtil.java

License:Open Source License

private static Object jsObjectToJava(ScriptObjectMirror object) throws UnsupportedConversionException {
    if (!object.containsKey("_d2js_type")) {
        if (object.isArray()) {
            return jsObjectToJava(object.to(NativeArray.class));
        }/* w  w w .  j  a v  a2  s  .  co  m*/
        Object sealed = object.to(Object.class);
        if (sealed instanceof ScriptObject) {
            return jsObjectToJava((ScriptObject) sealed);
        } else {
            return jsObjectToJava(sealed);
        }
    } else {
        String type = object.get("_d2js_type").toString();
        Object value = object.get("value");
        return jsObjectToJava(type, value);
    }
}

From source file:org.siphon.common.js.JsTypeUtil.java

License:Open Source License

public static Long parseTime(Object value) throws UnsupportedConversionException {
    if (value instanceof Double) {
        return ((Double) value).longValue();
    } else if (value instanceof String) {
        try {//from www  .j a v  a  2  s .c  o m
            return sdfTime.parse((String) value).getTime();
        } catch (ParseException e) {
            throw new UnsupportedConversionException("unmatched datetime format " + value, e);
        }
    } else if (value instanceof NativeDate) {
        return getTime((NativeDate) value);
    } else if (value instanceof ZonedDateTime) {
        return ((ZonedDateTime) value).toInstant().toEpochMilli();
    } else if (value instanceof ScriptObjectMirror) {
        ScriptObjectMirror m = (ScriptObjectMirror) value;
        Object o = m.to(Object.class);
        if (o instanceof NativeDate) {
            return getTime(m.to(NativeDate.class));
        } else if (o instanceof ZonedDateTime) {
            return ((ZonedDateTime) o).toInstant().toEpochMilli();
        } else {
            throw new UnsupportedConversionException("unknown date format " + value + " " + m.getClassName(),
                    null);
        }
    } else {
        throw new UnsupportedConversionException("unknown date format " + value + " " + value.getClass(), null);
    }
}

From source file:org.siphon.common.js.JsTypeUtil.java

License:Open Source License

public static Long parseDate(Object value) throws UnsupportedConversionException {
    if (JsTypeUtil.isNull(value)) {
        return null;
    }/*from  w w  w . j ava  2  s  .  c o  m*/
    if (value instanceof Double) {
        return ((Double) value).longValue();
    } else if (value instanceof String) {
        try {
            return sdfDate.parse((String) value).getTime();
        } catch (ParseException e) {
            throw new UnsupportedConversionException("unmatched datetime format " + value, e);
        }
    } else if (value instanceof NativeDate) {
        return getTime((NativeDate) value);
    } else if (value instanceof ScriptObjectMirror
            && ((ScriptObjectMirror) value).to(Object.class) instanceof NativeDate) {
        ScriptObjectMirror m = (ScriptObjectMirror) value;
        Object o = m.to(Object.class);
        if (o instanceof NativeDate) {
            return getTime(m.to(NativeDate.class));
        } else if (o instanceof ZonedDateTime) {
            return ((ZonedDateTime) o).toInstant().toEpochMilli();
        } else {
            throw new UnsupportedConversionException("unknown date format " + value + " " + m.getClassName(),
                    null);
        }
    } else if (value instanceof ZonedDateTime) {
        return ((ZonedDateTime) value).toInstant().toEpochMilli();
    } else {
        throw new UnsupportedConversionException("unknown date format " + value + " " + value.getClass(), null);
    }
}