Skip to content

Commit

Permalink
Added context params to make js engine behave less strict
Browse files Browse the repository at this point in the history
  • Loading branch information
northlander committed Nov 17, 2021
1 parent 05809bb commit 92872bf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
1 change: 0 additions & 1 deletion src/main/java/co/nordlander/a/A.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ public enum Protocol {
}

public static void main(String[] args) {
System.setProperty("polyglot.js.nashorn-compat", "true");
System.setProperty("polyglot.engine.WarnInterpreterOnly", "false");
A a = new A();
try { a.run(args); } catch (Exception e) {
Expand Down
23 changes: 16 additions & 7 deletions src/main/java/co/nordlander/a/MessageDumpTransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.function.Predicate;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.*;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -39,9 +38,19 @@
public class MessageDumpTransformer {


protected ScriptEngineManager mgr = new ScriptEngineManager();
protected ScriptEngine engine = mgr.getEngineByName("js");
protected ScriptEngineManager mgr;
protected ScriptEngine engine;
protected Bindings bindings;
protected Map<String, Object> context = new TreeMap<>();

public MessageDumpTransformer(){
mgr = new ScriptEngineManager();
engine = mgr.getEngineByName("js");
bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.put("polyglot.js.nashorn-compat", true);
bindings.put("polyglot.js.allowHostAccess", true);
bindings.put("polyglot.js.allowHostClassLookup", (Predicate<String>) s -> true);
}

public MessageDump transformMessage(MessageDump msg, String script) throws ScriptException, IOException{
if (StringUtils.isBlank(script)) {
Expand Down Expand Up @@ -71,9 +80,9 @@ protected String toScript(final String script) throws IOException {
}

protected MessageDump doTransformMessage(MessageDump msg, String script) throws ScriptException{
engine.put("msg", msg);
bindings.put("msg", msg);
for (Map.Entry<String, Object> entry : context.entrySet() ) {
engine.put(entry.getKey(), entry.getValue());
bindings.put(entry.getKey(), entry.getValue());
}
engine.eval(script);
return msg;
Expand Down
1 change: 0 additions & 1 deletion src/test/java/co/nordlander/a/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ public abstract class BaseTest {

@Before
public void setupJMS() throws Exception {
System.setProperty("polyglot.js.nashorn-compat", "true");
System.setProperty("polyglot.engine.WarnInterpreterOnly", "false");

cf = getConnectionFactory();
Expand Down

0 comments on commit 92872bf

Please sign in to comment.