Search in sources :

Example 41 with ScriptEngine

use of javax.script.ScriptEngine in project java8-tutorial by winterbe.

the class Nashorn4 method main.

public static void main(String[] args) throws Exception {
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
    engine.eval("loadWithNewGlobal('res/nashorn4.js')");
}
Also used : ScriptEngineManager(javax.script.ScriptEngineManager) ScriptEngine(javax.script.ScriptEngine)

Example 42 with ScriptEngine

use of javax.script.ScriptEngine in project java8-tutorial by winterbe.

the class Nashorn5 method main.

public static void main(String[] args) throws Exception {
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
    engine.eval("load('res/nashorn5.js')");
    Invocable invocable = (Invocable) engine;
    Product product = new Product();
    product.setName("Rubber");
    product.setPrice(1.99);
    product.setStock(1037);
    Object result = invocable.invokeFunction("getValueOfGoods", product);
    System.out.println(result);
}
Also used : Invocable(javax.script.Invocable) ScriptEngineManager(javax.script.ScriptEngineManager) ScriptEngine(javax.script.ScriptEngine)

Example 43 with ScriptEngine

use of javax.script.ScriptEngine in project java8-tutorial by winterbe.

the class Nashorn6 method main.

public static void main(String[] args) throws Exception {
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
    engine.eval("load('res/nashorn6.js')");
    Invocable invocable = (Invocable) engine;
    Product product = new Product();
    product.setName("Rubber");
    product.setPrice(1.99);
    product.setStock(1337);
    ScriptObjectMirror result = (ScriptObjectMirror) invocable.invokeFunction("calculate", product);
    System.out.println(result.get("name") + ": " + result.get("valueOfGoods"));
}
Also used : Invocable(javax.script.Invocable) ScriptObjectMirror(jdk.nashorn.api.scripting.ScriptObjectMirror) ScriptEngineManager(javax.script.ScriptEngineManager) ScriptEngine(javax.script.ScriptEngine)

Example 44 with ScriptEngine

use of javax.script.ScriptEngine in project apex-core by apache.

the class StramClientUtils method evalProperties.

public static void evalProperties(Properties target, Configuration vars) {
    ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript");
    Pattern substitutionPattern = Pattern.compile("\\$\\{(.+?)\\}");
    Pattern evalPattern = Pattern.compile("\\{% (.+?) %\\}");
    try {
        engine.eval("var _prop = {}");
        for (Map.Entry<String, String> entry : vars) {
            String evalString = String.format("_prop[\"%s\"] = \"%s\"", StringEscapeUtils.escapeJava(entry.getKey()), StringEscapeUtils.escapeJava(entry.getValue()));
            engine.eval(evalString);
        }
    } catch (ScriptException ex) {
        LOG.warn("Javascript error: {}", ex.getMessage());
    }
    for (Map.Entry<Object, Object> entry : target.entrySet()) {
        String value = entry.getValue().toString();
        Matcher matcher = substitutionPattern.matcher(value);
        if (matcher.find()) {
            StringBuilder newValue = new StringBuilder();
            int cursor = 0;
            do {
                newValue.append(value.substring(cursor, matcher.start()));
                String subst = vars.get(matcher.group(1));
                if (subst != null) {
                    newValue.append(subst);
                }
                cursor = matcher.end();
            } while (matcher.find());
            newValue.append(value.substring(cursor));
            target.put(entry.getKey(), newValue.toString());
        }
        matcher = evalPattern.matcher(value);
        if (matcher.find()) {
            StringBuilder newValue = new StringBuilder();
            int cursor = 0;
            do {
                newValue.append(value.substring(cursor, matcher.start()));
                try {
                    Object result = engine.eval(matcher.group(1));
                    String eval = result.toString();
                    if (eval != null) {
                        newValue.append(eval);
                    }
                } catch (ScriptException ex) {
                    LOG.warn("JavaScript exception {}", ex.getMessage());
                }
                cursor = matcher.end();
            } while (matcher.find());
            newValue.append(value.substring(cursor));
            target.put(entry.getKey(), newValue.toString());
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) ScriptException(javax.script.ScriptException) Matcher(java.util.regex.Matcher) ScriptEngineManager(javax.script.ScriptEngineManager) Map(java.util.Map) HashMap(java.util.HashMap) ScriptEngine(javax.script.ScriptEngine)

Example 45 with ScriptEngine

use of javax.script.ScriptEngine in project adempiere by adempiere.

the class MHRProcess method executeScriptEngine.

private Object executeScriptEngine(MHRConcept concept, MRule rule, String columnType) {
    Object result = null;
    try {
        String text = "";
        if (rule.getScript() != null) {
            text = rule.getScript().trim().replaceAll("\\bget", "process.get").replace(".process.get", ".get");
        }
        String resultType = "double";
        if (MHRAttribute.COLUMNTYPE_Date.equals(columnType))
            resultType = "Timestamp";
        else if (MHRAttribute.COLUMNTYPE_Text.equals(columnType))
            resultType = "String";
        final String script = s_scriptImport.toString() + Env.NL + resultType + " result = 0;" + Env.NL + "String description = null;" + Env.NL + text;
        ScriptEngine engine = rule.getScriptEngine();
        //MRule.setContext(engine, concept.getCtx(), 0);  // no window
        scriptCtx.entrySet().stream().forEach(entry -> engine.put(entry.getKey(), entry.getValue()));
        result = engine.eval(script);
        if (result != null && "@Error@".equals(result.toString())) {
            throw new AdempiereException("@AD_Rule_ID@ @Error@" + result);
        }
        //	
        description = rule.getValue();
    } catch (Exception e) {
        throw new AdempiereException(e.getLocalizedMessage());
    }
    return result;
}
Also used : AdempiereException(org.adempiere.exceptions.AdempiereException) ScriptEngine(javax.script.ScriptEngine) AdempiereException(org.adempiere.exceptions.AdempiereException)

Aggregations

ScriptEngine (javax.script.ScriptEngine)175 ScriptEngineManager (javax.script.ScriptEngineManager)70 ScriptException (javax.script.ScriptException)46 Test (org.junit.Test)29 Bindings (javax.script.Bindings)20 IOException (java.io.IOException)19 Map (java.util.Map)14 Invocable (javax.script.Invocable)13 File (java.io.File)11 FileReader (java.io.FileReader)11 ScriptEngineFactory (javax.script.ScriptEngineFactory)11 HashMap (java.util.HashMap)10 ScriptContext (javax.script.ScriptContext)10 SimpleBindings (javax.script.SimpleBindings)10 InputStreamReader (java.io.InputStreamReader)9 GroovyTest (org.enumerable.lambda.support.groovy.GroovyTest)9 ScalaTest (org.enumerable.lambda.support.scala.ScalaTest)9 PrintWriter (java.io.PrintWriter)7 JavaScriptTest (org.enumerable.lambda.support.javascript.JavaScriptTest)7 JRubyTest (org.enumerable.lambda.support.jruby.JRubyTest)7