Search in sources :

Example 26 with ScriptEngineManager

use of javax.script.ScriptEngineManager 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 27 with ScriptEngineManager

use of javax.script.ScriptEngineManager 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 28 with ScriptEngineManager

use of javax.script.ScriptEngineManager 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 29 with ScriptEngineManager

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

the class Nashorn8 method main.

public static void main(String[] args) throws ScriptException, NoSuchMethodException {
    NashornScriptEngine engine = (NashornScriptEngine) new ScriptEngineManager().getEngineByName("nashorn");
    engine.eval("load('res/nashorn8.js')");
    // [object global]
    engine.invokeFunction("evaluate1");
    // [object Object]
    engine.invokeFunction("evaluate2");
    // Foobar
    engine.invokeFunction("evaluate3", "Foobar");
    // [object global] <- ???????
    engine.invokeFunction("evaluate3", new Person("John", "Doe"));
}
Also used : NashornScriptEngine(jdk.nashorn.api.scripting.NashornScriptEngine) ScriptEngineManager(javax.script.ScriptEngineManager) Person(com.winterbe.java8.samples.lambda.Person)

Example 30 with ScriptEngineManager

use of javax.script.ScriptEngineManager 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)

Aggregations

ScriptEngineManager (javax.script.ScriptEngineManager)109 ScriptEngine (javax.script.ScriptEngine)66 Test (org.junit.Test)23 ScriptException (javax.script.ScriptException)20 ScriptEngineFactory (javax.script.ScriptEngineFactory)14 File (java.io.File)12 Invocable (javax.script.Invocable)11 QuickTest (com.hazelcast.test.annotation.QuickTest)10 IOException (java.io.IOException)10 FileReader (java.io.FileReader)9 Bindings (javax.script.Bindings)9 Before (org.junit.Before)9 Map (java.util.Map)8 GroovyScriptEngineFactory (org.codehaus.groovy.jsr223.GroovyScriptEngineFactory)8 Reader (java.io.Reader)7 HashMap (java.util.HashMap)6 SimpleBindings (javax.script.SimpleBindings)6 PrintWriter (java.io.PrintWriter)4 StringWriter (java.io.StringWriter)4 InputStreamReader (java.io.InputStreamReader)3