nashorn

Differ null and undefined values in Nashorn

不羁岁月 提交于 2019-12-04 06:56:00
I'm running this code ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); engine.eval("var out;"); engine.eval("var out1 = null;"); Object m = engine.get("out"); Object m1 = engine.get("out1"); And getting m == null and m1 == null. How to determine if value is undefined or null? Java doesn't have a concept of "undefined", so understanding the distinction will require expressing it in the script's language. I suggest using this expression: Boolean isUndefined = engine.eval("out === undefined"); Actually, the correct way to know if the Object returned by the script is

Programmatically provide NiFi InvokeHTTP different certificates

北城余情 提交于 2019-12-04 02:09:32
问题 I have a requirement in Nifi where I have cycle through different HTTP S REST Endpoints and provide different certificates for some endpoints and different username / password for some other endpoints. I used InvokeHTTP processor to send the requests, although URL takes an expression language, I cannot setup SSLContextService with an expression. Alternatively, I thought on using ExecuteScript to call those Endpoints, however as listed here in StackOverflow post; I still don't know how to

Seamlessly pass Arrays and Lists to and from Nashorn

拜拜、爱过 提交于 2019-12-03 13:05:00
I know you can work with Java arrays in Nashorn and there are plenty of examples of how to do this. The problem for me with the standard approach is that it makes the javascript code explicitly aware of it's runtime environment. Currently I have a solution that makes use of Rhino and it seamlessly converts between Java type and Native javascript types. For Rhino I accomplished this by implementing org.mozilla.javascript.ContextFactory and org.mozilla.javascript.WrapFActory and setting WrapFactory on the Context when makeContext is called. This WrapFactory implementation takes care of

Effective way to pass JSON between java and javascript

江枫思渺然 提交于 2019-12-03 07:13:01
I'm fairly new to Nashorn and scripting on top of the JVM and wanted to know if I can get my java code and javascripts to communicate more effectively. I'm using a 3rd party JS lib that works with JS objects, and in my java code I have the data I want to pass as a Map<String, Object> data . Because that 3rd party JS expects to work with plain JS objects I can't pass my data as is, although the script engine allows you to access a Map as if it was a plain JS object. The script i'm using uses 'hasOwnProperty' on the data argument and fails when invoked on an Java object. When I tried using

Java 8 Javascript Engine backwards compatibility

老子叫甜甜 提交于 2019-12-03 06:28:45
问题 I am trying out Java 8 in my project and I am stuck in an error related to my build process. I am using ANT scripts and at some point i am using some javascript (embeded into ANT) to do some build specific operations. The part of the script that is causing the error looks like below: <script language="javascript"> <![CDATA[ importClass(java.io.File); importClass(java.io.FileReader); ... ]]> </script> The project is building fine with Java 7 or Java 6, but it gives me some errors when i am

Nashorn inefficiency

…衆ロ難τιáo~ 提交于 2019-12-03 06:01:03
问题 I'm implementing some performance-sensitive code using Nashorn. I do it like this: private ScriptEngine engine = new NashornScriptEngineFactory().getScriptEngine(new String[] { "--no-java" }); String someExpression = "someFunction() + someVariable"; // this compiled script gets cached, caching code omitted CompiledScript script = ((Compilable)engine).compile(expr); MyScriptContext context = new MyScriptContext(); Object output = script.eval(context); At runtime, Nashorn insists on making a

Java 8 Nashorn: Console pretty unusable

泪湿孤枕 提交于 2019-12-03 01:51:58
I am using Java 8 build 1.8.0-b132 on OSX 10.9.2. I am playing with Nashorn the new JavaScript runtime in Java 8. I am using Library/Java/JavaVirtualMachines/jdk1.8.0.jdk/Contents/Home/bin/jjs but unfortunately jjs is pretty unusable in interactive mode since cursor left/right does not work: jjs jjs> var x =^[[D The above output (^[[D) is the result of pressing 'cursor left'. Is there a way to fix that behavior and using cursor left/right in the jjs console? jbandi Thanks to @assylias for pointing me to rlwrap . I installed rlwrap via homebrew : brew install rlwrap Then I defined an alias in

Interrupt java thread running nashorn script

六月ゝ 毕业季﹏ 提交于 2019-12-03 00:25:37
In the code below i have javascript running in a separate thread from the main one. That script is an infinite loop, so it needs to be terminated somehow. How? Calling .cancel() is not working AFTER the script begins running. But if i call .cancel() just after the thread initialization, it will terminate it (the commented out line). package testscriptterminate; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future;

Java 8 Javascript Engine backwards compatibility

两盒软妹~` 提交于 2019-12-02 20:18:08
I am trying out Java 8 in my project and I am stuck in an error related to my build process. I am using ANT scripts and at some point i am using some javascript (embeded into ANT) to do some build specific operations. The part of the script that is causing the error looks like below: <script language="javascript"> <![CDATA[ importClass(java.io.File); importClass(java.io.FileReader); ... ]]> </script> The project is building fine with Java 7 or Java 6, but it gives me some errors when i am using Java 8. These errors are related to the upgrade of the JS engine. In particular i am getting the

Nashorn inefficiency

此生再无相见时 提交于 2019-12-02 18:32:36
I'm implementing some performance-sensitive code using Nashorn. I do it like this: private ScriptEngine engine = new NashornScriptEngineFactory().getScriptEngine(new String[] { "--no-java" }); String someExpression = "someFunction() + someVariable"; // this compiled script gets cached, caching code omitted CompiledScript script = ((Compilable)engine).compile(expr); MyScriptContext context = new MyScriptContext(); Object output = script.eval(context); At runtime, Nashorn insists on making a lot of necessary calls to MyScriptContext. It insists on calling MyScriptContext.getBindings().put(