I have recently started working on this new project where we are decoding our message using ScriptEngine & ScriptEngineManager. Here we have used ScriptObjectMirror which is deprecated from JDK 11 (I believe) and now I am looking for another approach for this. Basically I want to remove nashorn API from my project.
Below is my method which I am using right now , I read it can be acheived using binding. Can anyone suggest how to do that?
import java.io.File;
import java.nio.file.Files;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import org.springframework.core.io.ClassPathResource;
import jdk.nashorn.api.scripting.ScriptObjectMirror;
public ScriptObjectMirror DecodeString (String decodeString){
File resource = new ClassPathResource("script/decode.js").getFile();
ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
engine.eval(new String(Files.readAllBytes(resource.toPath())));
Invocable inv = (Invocable) engine;
ScriptObjectMirror data = (ScriptObjectMirror)
inv.invokeFunction("DecodeElsysPayload", inv.invokeFunction("hexToBytes", decodeString));
System.out.println(data.entrySet().toString());
System.out.println("temperature:"+data.get("temperature"));
System.out.println("humidity:"+data.get("humidity"));
return data;
}
Simply replaced it with Bindings
bindings = (Bindings) inv.invokeFunction("DecodeElsysPayload", inv.invokeFunction("hexToBytes", encodeString));