Refine search
ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); // read script file engine.eval(Files.newBufferedReader(Paths.get("C:/Scripts/Jsfunctions.js"), StandardCharsets.UTF_8)); Invocable inv = (Invocable) engine; // call function from script file inv.invokeFunction("yourFunction", "param");
private SelectorFunction compileSelectorFunction() { final ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript"); try { ((Compilable) engine).compile("var apply = " + function).eval(); return ((Invocable) engine).getInterface(SelectorFunction.class); } catch (ScriptException e) { throw new RuntimeException(e); } }
bindings.putAll(model); model.put("renderingContext", context); html = engine.eval(template, bindings); Object thiz = engine.eval(this.renderObject); html = ((Invocable) engine).invokeMethod(thiz, this.renderFunction, template, model, context); html = ((Invocable) engine).invokeFunction(this.renderFunction, template, model, context);
public <T> T compileScript( Class<T> iface, String script, String engineName) { ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine engine = factory.getEngineByName(engineName); try { engine.eval(script); Invocable inv = (Invocable) engine; return inv.getInterface(iface); } catch (ScriptException e) { throw Util.newError( e, "Error while compiling script to implement " + iface + " SPI"); } }
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); engine.eval(new FileReader("regex.js")); Invocable invocable = (Invocable) engine; Object result = invocable.invokeFunction("check", "\\d+", "1"); System.out.println(result);
public static String md2html() throws ScriptException, FileNotFoundException, NoSuchMethodException { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); File functionscript = new File("public/lib/marked.js"); Reader reader = new FileReader(functionscript); engine.eval(reader); Invocable invocableEngine = (Invocable) engine; Object marked = engine.get("marked"); Object lexer = invocableEngine.invokeMethod(marked, "lexer", "**hello**"); Object result = invocableEngine.invokeMethod(marked, "parser", lexer); return result.toString(); }
} else { scriptEngine.eval(scriptBody); final Object obj = scriptEngine.get("writer"); if (obj != null) { final ComponentLog logger = getLogger(); invocable.invokeMethod(obj, "setLogger", logger); } catch (final NoSuchMethodException nsme) { if (logger.isDebugEnabled()) { try { invocable.invokeMethod(obj, "setConfigurationContext", configurationContext); } catch (final NoSuchMethodException nsme) { if (logger.isDebugEnabled()) { final RecordSetWriterFactory scriptedWriter = invocable.getInterface(obj, RecordSetWriterFactory.class); recordFactory.set(scriptedWriter);
public void testInvokeMethodNoSuchArgs() throws ScriptException, NoSuchMethodException { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine pythonEngine = manager.getEngineByName("python"); Invocable invocableEngine = (Invocable)pythonEngine; Object newStringCapitalize = invocableEngine.invokeMethod("test", "capitalize"); assertEquals(newStringCapitalize, "Test"); }
@Override public <T> T executeTemplate(String template, HttpRequest request, Class<? extends DTO<T>> dtoClass) { T result = null; String script = "function handle(request) {" + indentAndToString(template)[0] + "}"; try { if (engine != null) { engine.eval(script + " function serialise(request) { return JSON.stringify(handle(JSON.parse(request)), null, 2); }"); // HttpResponse handle(HttpRequest httpRequest) - ES5 Object stringifiedResponse = ((Invocable) engine).invokeFunction("serialise", new HttpRequestTemplateObject(request)); logFormatter.info(TEMPLATE_GENERATED, request, "generated output:{}from template:{}for request:{}", stringifiedResponse, script, request); result = httpTemplateOutputDeserializer.deserializer(request, (String) stringifiedResponse, dtoClass); } else { logFormatter.error(request, "JavaScript based templating is only available in a JVM with the \"nashorn\" JavaScript engine, " + "please use a JVM with the \"nashorn\" JavaScript engine, such as Oracle Java 8+", new RuntimeException("\"nashorn\" JavaScript engine not available")); } } catch (Exception e) { throw new RuntimeException(formatLogMessage("Exception transforming template:{}for request:{}", script, request), e); } return result; } }
public ColumnTransformer createFromScript(Reader script) { Invocable invocable = (Invocable) engine; try { if (scriptEngineBindings != null) { manager.getBindings().putAll(scriptEngineBindings); } engine.eval(script); } catch (ScriptException ex) { throw new IllegalStateException("Cannot evaluate script", ex); } return invocable.getInterface(ColumnTransformer.class); }
@Test public void implementJavaInterfaceWithJS() throws Exception { ScriptEngine jse = new NashornScriptEngineFactory().getScriptEngine(new ClassFilter_restrictAll()); jse.eval("var customInterface = new Object(); customInterface.funcA = function() {print('custom funcA() hit!');}"); jse.eval("customInterface.funcB = function(prm) {print('custom funcB() hit! prm='+prm); return 'js_'+prm;}"); CustomInterface customInterfaceInstance = ((Invocable)jse).getInterface(jse.get("customInterface"), CustomInterface.class); customInterfaceInstance.funcA(); String res = customInterfaceInstance.funcB("java"); assertEquals("js_java", res); }
@Override public Object invokeFunction(String name, Object... args) throws ScriptException, NoSuchMethodException { return ((Invocable)engine).invokeFunction(name, args); }
private void invokeFunctionOrMethod(Shell shellState, ScriptEngine engine, CommandLine cl, Object[] args) { try { Invocable inv = (Invocable) engine; if (cl.hasOption(function.getOpt())) { inv.invokeFunction(cl.getOptionValue(function.getOpt()), args); } else if (cl.hasOption(object.getOpt())) { String objectMethod = cl.getOptionValue(object.getOpt()); String[] parts = objectMethod.split(":"); if (!(parts.length == 2)) { shellState.printException(new Exception("Object and Method must be supplied")); return; } String objectName = parts[0]; String methodName = parts[1]; Object obj = engine.get(objectName); inv.invokeMethod(obj, methodName, args); } } catch (Exception e) { shellState.printException(e); } }
@Override public Object executeScriptEngineOperation(ScriptEngine scriptEngine) throws Exception { if (thisObj == null) { return ((Invocable)scriptEngine).invokeFunction(name, args); } else { return ((Invocable)scriptEngine).invokeMethod(thisObj, name, args); } }
@OnDisabled public void onDisabled(final ConfigurationContext context) { // Call an non-interface method onDisabled(context), to allow a scripted LookupService the chance to shut down as necessary final Invocable invocable = (Invocable) scriptEngine; if (configurationContext != null) { try { // Get the actual object from the script engine, versus the proxy stored in lookupService. The object may have additional methods, // where lookupService is a proxied interface final Object obj = scriptEngine.get("lookupService"); if (obj != null) { try { invocable.invokeMethod(obj, "onDisabled", context); } catch (final NoSuchMethodException nsme) { if (getLogger().isDebugEnabled()) { getLogger().debug("Configured script LookupService does not contain an onDisabled() method."); } } } else { throw new ScriptException("No LookupService was defined by the script."); } } catch (ScriptException se) { throw new ProcessException("Error executing onDisabled(context) method", se); } } }
@Override public <T> T getInterface(Object thiz, Class<T> clasz) { return ((Invocable) engine).getInterface(thiz, clasz); }
@Override public Object invokeMethod(Object thiz, String name, Object... args) throws ScriptException, NoSuchMethodException { return ((Invocable) engine).invokeMethod(thiz, name, args); }
public void testGetInterfaceCharSequence2() throws ScriptException, IOException { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine pythonEngine = manager.getEngineByName("python"); Invocable invocableEngine = (Invocable) pythonEngine; assertNull(pythonEngine.eval( "from java.lang import StringBuilder\r\n" + "c = StringBuilder(\"abc\")\r\n")); CharSequence seq = invocableEngine.getInterface(pythonEngine.get("c"), CharSequence.class); assertEquals("abc", seq.toString()); } }
ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); engine.eval(new FileReader("myScript.js")); Invocable invocable = (Invocable) engine; Object result = invocable.invokeFunction("myJSFunc", "Peter Parker"); System.out.println(result);
} else { scriptEngine.eval(scriptBody); final Object obj = scriptEngine.get("reader"); if (obj != null) { final ComponentLog logger = getLogger(); invocable.invokeMethod(obj, "setLogger", logger); } catch (final NoSuchMethodException nsme) { if (logger.isDebugEnabled()) { try { invocable.invokeMethod(obj, "setConfigurationContext", configurationContext); } catch (final NoSuchMethodException nsme) { if (logger.isDebugEnabled()) { final RecordReaderFactory scriptedReader = invocable.getInterface(obj, RecordReaderFactory.class); recordFactory.set(scriptedReader);