List of usage examples for javax.script Invocable invokeFunction
public Object invokeFunction(String name, Object... args) throws ScriptException, NoSuchMethodException;
From source file:Main.java
public static void main(String[] args) throws Exception { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("js"); engine.eval("function add (a, b) {c = a + b; return c; }"); Invocable jsInvoke = (Invocable) engine; Object result1 = jsInvoke.invokeFunction("add", new Object[] { 10, 5 }); System.out.println(result1);/* w w w . j a va2 s . c om*/ Adder adder = jsInvoke.getInterface(Adder.class); int result2 = adder.add(10, 5); System.out.println(result2); }
From source file:InvocableTest.java
public static void main(String args[]) { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("javascript"); try {// w ww .j a va 2 s . c o m engine.eval("function myFunction(name){var output = '';" + " for (i = 0; i <= name.length; i++) {output = name.charAt(i)+'-'+ output" + " } return output;}"); Invocable invokeEngine = (Invocable) engine; Object o = invokeEngine.invokeFunction("myFunction", "abcde"); System.out.println(o); } catch (NoSuchMethodException e) { System.err.println(e); } catch (ScriptException e) { System.err.println(e); } }
From source file:Main.java
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);/*from www. java2s .co m*/ product.setStock(1037); Object result = invocable.invokeFunction("getValueOfGoods", product); System.out.println(result); }
From source file:Main.java
public static Object invokeFunction(String filepath, String funcname, Object... params) { try {/*from w w w . j a v a 2 s . c om*/ ScriptEngineManager sem = new ScriptEngineManager(); ScriptEngine jsEngine = sem.getEngineByName("js"); jsEngine.eval(new FileReader(filepath)); Invocable invocable = (Invocable) jsEngine; return invocable.invokeFunction(funcname, params); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
From source file:com.intbit.util.ServletUtil.java
public static String getServerName(ServletContext context) { try {//w w w .j a va 2s. c o m ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); String path = context.getRealPath("") + "/js/configurations.js"; // read script file engine.eval(Files.newBufferedReader(Paths.get(path), StandardCharsets.UTF_8)); Invocable inv = (Invocable) engine; // call function from script file return inv.invokeFunction("getHost", "").toString(); } catch (Exception ex) { return "http://clients.brndbot.com/BrndBot/"; } }
From source file:org.eclairjs.nashorn.Utils.java
public static Object createJavaScriptObject(Object o) { /*// w ww . j av a2 s.c o m FIXME we should be able to remove this once all the Spark objects are wrappered by Java */ Object obj = null; try { Invocable invocable = (Invocable) NashornEngineSingleton.getEngine(); obj = invocable.invokeFunction("createJavaScriptSparkObject", o); } catch (java.lang.Exception e) { e.printStackTrace(); } return obj; }
From source file:org.eclairjs.nashorn.Utils.java
public static String JsonStringify(Object o) { String jsonStr = null;//from w w w .j a v a 2s . c o m if (o == null) { return ""; } try { Invocable invocable = (Invocable) NashornEngineSingleton.getEngine(); jsonStr = (String) invocable.invokeFunction("objectToJsonString", o); } catch (java.lang.Exception e) { e.printStackTrace(); } return jsonStr; }
From source file:org.eclairjs.nashorn.Utils.java
public static Object createLambdaFunction(Object func, Object clazz, Object bindArgs) { /*/*from w ww.ja va 2s .c o m*/ FIXME we might be able to move this from JavaScript to Java once we have converted all the wrappper objects to Java */ Object obj = null; try { Invocable invocable = (Invocable) NashornEngineSingleton.getEngine(); Object params[] = { func, clazz, null, bindArgs }; obj = invocable.invokeFunction("createLambdaFunction", params); } catch (java.lang.Exception e) { e.printStackTrace(); } return obj; }
From source file:org.eclairjs.nashorn.Utils.java
public static Object createLambdaFunction(Object func, Object clazz, Object context, Object bindArgs) { /*/*from www . j a v a 2s . c om*/ FIXME we might be able to move this from JavaScript to Java once we have converted all the wrappper objects to Java */ Object obj = null; try { Invocable invocable = (Invocable) NashornEngineSingleton.getEngine(); Object params[] = { func, clazz, context, bindArgs }; obj = invocable.invokeFunction("createLambdaFunction", params); } catch (java.lang.Exception e) { e.printStackTrace(); } return obj; }
From source file:org.eclairjs.nashorn.Utils.java
public static Object createLambdaFunction(Object func, Object clazz, Object context, Object bindArgs, org.apache.spark.sql.Encoder encoder) { /*//from ww w . j ava 2 s .com FIXME we might be able to move this from JavaScript to Java once we have converted all the wrappper objects to Java */ Object obj = null; try { Invocable invocable = (Invocable) NashornEngineSingleton.getEngine(); Object params[] = { func, clazz, context, bindArgs, encoder }; obj = invocable.invokeFunction("createLambdaFunction", params); } catch (java.lang.Exception e) { e.printStackTrace(); } return obj; }