use of jdk.nashorn.api.scripting.ScriptObjectMirror in project POL-POM-5 by PlayOnLinux.
the class EnginesController method installEngine.
private void installEngine(EngineDTO engineDTO, Consumer<Exception> errorCallback) {
final InteractiveScriptSession interactiveScriptSession = scriptInterpreter.createInteractiveSession();
interactiveScriptSession.eval("include([\"Functions\", \"Engines\", \"" + engineDTO.getCategory() + "\"]);", ignored -> interactiveScriptSession.eval("new Wine()", output -> {
final ScriptObjectMirror wine = (ScriptObjectMirror) output;
wine.callMember("install", engineDTO.getCategory(), engineDTO.getSubCategory(), engineDTO.getVersion(), engineDTO.getUserData());
}, errorCallback), errorCallback);
}
use of jdk.nashorn.api.scripting.ScriptObjectMirror in project GNS by MobilityFirst.
the class ActiveBlockingQuerier method readValueFromField.
/**
*
* @param querierGuid
* @param queriedGuid
* @param fields a JS Array is stringified to this string
* @param ttl
* @return
* @throws ActiveException
*/
private ScriptObjectMirror readValueFromField(String querierGuid, String queriedGuid, String fields, int ttl) throws ActiveException {
ScriptObjectMirror value = null;
try {
ActiveMessage am = new ActiveMessage(ttl, querierGuid, fields, queriedGuid, currentID);
channel.sendMessage(am);
ActiveMessage response = (ActiveMessage) channel.receiveMessage();
if (response == null) {
throw new ActiveException();
}
if (response.getError() != null) {
throw new ActiveException();
}
value = string2JS(response.getValue());
} catch (IOException e) {
throw new ActiveException();
}
return value;
}
use of jdk.nashorn.api.scripting.ScriptObjectMirror in project openremote by openremote.
the class RulesetDeployment method registerRulesJavascript.
/**
* Marshal the JavaScript rules array into {@link Rule} instances.
*/
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());
}
}
use of jdk.nashorn.api.scripting.ScriptObjectMirror in project sling by apache.
the class Module method decorateScript.
/**
*
* @param source
* @return
* @throws ScriptException
*/
private ScriptObjectMirror decorateScript(String source, boolean es6) throws ScriptException {
String filename = (String) get("filename");
if (filename.indexOf("node_modules") == -1 && es6) {
try {
source = factory.getSandboxService().compileSource(source);
} catch (ScriptException e) {
log.error("Could not transpile script", e);
throw new ScriptException("could not load " + get("filename"));
}
}
// TODO: refactor polyfill for window, global and make require outside the wrapper as function parameter
source = "//@sourceURL=" + (String) get("filename") + "\n" + "(function (exports, Require, module, __filename," + " __dirname, currentNode, console, properties, sling, simpleResource) { " + "var window = (this.window == 'undefined' || this.window == null) ? this : this.window;" + "var global = (global == 'undefined') ? this : global;" + "function require(id) { return Require.require(id); } require.resolve = function (id) { return Require.resolve(id, currentNode.resource, 1); };" + source + "})";
// use load + filenane for older JDK versions, @sourceURL is working for latest JDK version
source = "load( { name : \"" + get("filename") + "\"," + " script: \"" + StringEscapeUtils.escapeEcmaScript(source) + "\" } )";
ScriptObjectMirror function = null;
try {
function = (ScriptObjectMirror) factory.getNashornEngine().eval(source);
if (function == null) {
log.error("Function is null !");
}
} catch (ScriptException ex) {
// todo: better handling in future
throw ex;
}
return function;
}
use of jdk.nashorn.api.scripting.ScriptObjectMirror in project POL-POM-5 by PhoenicisOrg.
the class EnginesController method installEngine.
private void installEngine(EngineDTO engineDTO, Consumer<Exception> errorCallback) {
final InteractiveScriptSession interactiveScriptSession = scriptInterpreter.createInteractiveSession();
interactiveScriptSession.eval("include([\"Engines\", \"" + engineDTO.getCategory() + "\", \"Engine\", \"Object\"]);", ignored -> interactiveScriptSession.eval("new Wine()", output -> {
final ScriptObjectMirror wine = (ScriptObjectMirror) output;
wine.callMember("install", engineDTO.getCategory(), engineDTO.getSubCategory(), engineDTO.getVersion(), engineDTO.getUserData());
}, errorCallback), errorCallback);
}
Aggregations