Search in sources :

Example 6 with ScriptObjectMirror

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);
}
Also used : Files(java.nio.file.Files) ErrorMessage(org.phoenicis.javafx.views.common.ErrorMessage) LoggerFactory(org.slf4j.LoggerFactory) ScriptInterpreter(org.phoenicis.scripts.interpreter.ScriptInterpreter) IOException(java.io.IOException) Localisation.tr(org.phoenicis.configuration.localisation.Localisation.tr) AppsController(org.phoenicis.javafx.controller.apps.AppsController) File(java.io.File) Platform(javafx.application.Platform) Consumer(java.util.function.Consumer) List(java.util.List) EngineDTO(org.phoenicis.engines.dto.EngineDTO) EnginesSource(org.phoenicis.engines.EnginesSource) EngineCategoryDTO(org.phoenicis.engines.dto.EngineCategoryDTO) ViewEngines(org.phoenicis.javafx.views.mainwindow.engines.ViewEngines) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) ConfirmMessage(org.phoenicis.javafx.views.common.ConfirmMessage) URI(java.net.URI) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession) Path(java.nio.file.Path) ThemeManager(org.phoenicis.javafx.views.common.ThemeManager) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession)

Example 7 with ScriptObjectMirror

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;
}
Also used : ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) ActiveException(edu.umass.cs.gnsserver.activecode.prototype.ActiveException) IOException(java.io.IOException) ActiveMessage(edu.umass.cs.gnsserver.activecode.prototype.ActiveMessage)

Example 8 with ScriptObjectMirror

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());
    }
}
Also used : Condition(org.jeasy.rules.api.Condition) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) Action(org.jeasy.rules.api.Action) RuleBuilder(org.jeasy.rules.core.RuleBuilder)

Example 9 with ScriptObjectMirror

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;
}
Also used : ScriptException(javax.script.ScriptException) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror)

Example 10 with ScriptObjectMirror

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);
}
Also used : RepositoryDTO(org.phoenicis.repository.dto.RepositoryDTO) LoggerFactory(org.slf4j.LoggerFactory) AppsController(org.phoenicis.javafx.controller.apps.AppsController) ArrayList(java.util.ArrayList) TypeDTO(org.phoenicis.repository.dto.TypeDTO) EnginesSource(org.phoenicis.engines.EnginesSource) ConfirmMessage(org.phoenicis.javafx.views.common.ConfirmMessage) URI(java.net.URI) Path(java.nio.file.Path) CategoryDTO(org.phoenicis.repository.dto.CategoryDTO) Files(java.nio.file.Files) ErrorMessage(org.phoenicis.javafx.views.common.ErrorMessage) RepositoryManager(org.phoenicis.repository.RepositoryManager) ScriptInterpreter(org.phoenicis.scripts.interpreter.ScriptInterpreter) IOException(java.io.IOException) Localisation.tr(org.phoenicis.configuration.localisation.Localisation.tr) File(java.io.File) Platform(javafx.application.Platform) Consumer(java.util.function.Consumer) List(java.util.List) EngineDTO(org.phoenicis.engines.dto.EngineDTO) Optional(java.util.Optional) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession) ThemeManager(org.phoenicis.javafx.views.common.ThemeManager) EnginesView(org.phoenicis.javafx.views.mainwindow.engines.EnginesView) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) InteractiveScriptSession(org.phoenicis.scripts.interpreter.InteractiveScriptSession)

Aggregations

ScriptObjectMirror (jdk.nashorn.api.scripting.ScriptObjectMirror)76 Test (org.junit.Test)34 InteractiveScriptSession (org.phoenicis.scripts.interpreter.InteractiveScriptSession)28 ScriptInterpreter (org.phoenicis.scripts.interpreter.ScriptInterpreter)28 Consumer (java.util.function.Consumer)27 List (java.util.List)23 LoggerFactory (org.slf4j.LoggerFactory)20 File (java.io.File)17 IOException (java.io.IOException)17 Logger (org.slf4j.Logger)14 WinePrefixContainerDTO (org.phoenicis.containers.dto.WinePrefixContainerDTO)12 Map (java.util.Map)11 ArrayList (java.util.ArrayList)10 RegistryParameter (org.phoenicis.containers.wine.parameters.RegistryParameter)10 LibraryManager (org.phoenicis.library.LibraryManager)10 ShortcutManager (org.phoenicis.library.ShortcutManager)10 ShortcutCategoryDTO (org.phoenicis.library.dto.ShortcutCategoryDTO)10 ShortcutDTO (org.phoenicis.library.dto.ShortcutDTO)10 RegistryWriter (org.phoenicis.win32.registry.RegistryWriter)10 URI (java.net.URI)8