Skip to content

Commit

Permalink
Feature - use environment variable to set ES6 support mode for Nashor…
Browse files Browse the repository at this point in the history
…n script evaluator
  • Loading branch information
c4lm committed Feb 23, 2024
1 parent 06bc5fd commit ba8e919
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory;

public class ScriptEvaluator {

private static ScriptEngine engine;
Expand All @@ -25,7 +27,7 @@ private ScriptEvaluator() {}

/**
* Evaluates the script with the help of input provided but converts the result to a boolean
* value.
* value. Set environment variable CONDUCTOR_NASHORN_ES6_ENABLED=true for Nashorn ES6 support.
*
* @param script Script to be evaluated.
* @param input Input parameters.
Expand All @@ -37,24 +39,39 @@ public static Boolean evalBool(String script, Object input) throws ScriptExcepti
}

/**
* Evaluates the script with the help of input provided.
* Evaluates the script with the help of input provided. Set environment variable
* CONDUCTOR_NASHORN_ES6_ENABLED=true for Nashorn ES6 support.
*
* @param script Script to be evaluated.
* @param input Input parameters.
* @throws ScriptException
* @return Generic object, the result of the evaluated expression.
*/
public static Object eval(String script, Object input) throws ScriptException {
if (engine == null) {
engine = new ScriptEngineManager().getEngineByName("Nashorn");
initEngine(false);
Bindings bindings = engine.createBindings();
bindings.put("$", input);
return engine.eval(script, bindings);
}

// to mock in a test
public static String getEnv(String name) {
return System.getenv(name);
}

public static void initEngine(boolean reInit) {
if (engine == null || reInit) {
if ("true".equalsIgnoreCase(getEnv("CONDUCTOR_NASHORN_ES6_ENABLED"))) {
NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
engine = factory.getScriptEngine("--language=es6");
} else {
engine = new ScriptEngineManager().getEngineByName("Nashorn");
}
}
if (engine == null) {
throw new RuntimeException(
"missing nashorn engine. Ensure you are running supported JVM");
}
Bindings bindings = engine.createBindings();
bindings.put("$", input);
return engine.eval(script, bindings);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.util.Map;

import org.junit.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -44,4 +46,43 @@ public void testScript() throws Exception {
assertTrue(ScriptEvaluator.evalBool(script3, payload));
assertFalse(ScriptEvaluator.evalBool(script4, payload));
}

@Test
public void testES6Setting() throws Exception {
Map<String, Object> payload = new HashMap<>();
Map<String, Object> app = new HashMap<>();
app.put("name", "conductor");
app.put("version", 2.0);
app.put("license", "Apache 2.0");

payload.put("app", app);
payload.put("author", "Netflix");
payload.put("oss", true);

String script1 =
"""
(function(){\s
const variable = 1; // const support => es6\s
return $.app.name == 'conductor';})();"""; // true

MockedStatic<ScriptEvaluator> evaluator = Mockito.mockStatic(ScriptEvaluator.class);
evaluator
.when(() -> ScriptEvaluator.getEnv("CONDUCTOR_NASHORN_ES6_ENABLED"))
.thenReturn("true");
evaluator
.when(() -> ScriptEvaluator.eval(Mockito.any(), Mockito.any()))
.thenCallRealMethod();
evaluator
.when(() -> ScriptEvaluator.evalBool(Mockito.any(), Mockito.any()))
.thenCallRealMethod();
evaluator.when(() -> ScriptEvaluator.initEngine(Mockito.anyBoolean())).thenCallRealMethod();
evaluator.when(() -> ScriptEvaluator.toBoolean(Mockito.any())).thenCallRealMethod();
ScriptEvaluator.initEngine(true);
assertTrue(ScriptEvaluator.evalBool(script1, payload));
evaluator
.when(() -> ScriptEvaluator.getEnv("CONDUCTOR_NASHORN_ES6_ENABLED"))
.thenReturn("false");
ScriptEvaluator.initEngine(true);
evaluator.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

import org.openjdk.nashorn.api.scripting.NashornScriptEngineFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -49,7 +50,8 @@ public class Javascript extends Task<Javascript> {
* Javascript tasks are executed on the Conductor server without having to write worker code
*
* <p>Use {@link Javascript#validate()} method to validate the javascript to ensure the script
* is valid.
* is valid. Set environment variable CONDUCTOR_NASHORN_ES6_ENABLED=true for Nashorn ES6 support
* during validation.
*
* @param taskReferenceName
* @param script script to execute
Expand Down Expand Up @@ -100,7 +102,13 @@ public String getExpression() {
* @return
*/
public Javascript validate() {
ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("Nashorn");
ScriptEngine scriptEngine;
if ("true".equalsIgnoreCase(System.getenv("CONDUCTOR_NASHORN_ES6_ENABLED"))) {
NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
scriptEngine = factory.getScriptEngine("--language=es6");
} else {
scriptEngine = new ScriptEngineManager().getEngineByName("Nashorn");
}
if (scriptEngine == null) {
LOGGER.error("missing " + ENGINE + " engine. Ensure you are running supported JVM");
return this;
Expand Down Expand Up @@ -128,7 +136,13 @@ public Javascript validate() {
*/
public Object test(Map<String, Object> input) {

ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("Nashorn");
ScriptEngine scriptEngine;
if ("true".equalsIgnoreCase(System.getenv("CONDUCTOR_NASHORN_ES6_ENABLED"))) {
NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
scriptEngine = factory.getScriptEngine("--language=es6");
} else {
scriptEngine = new ScriptEngineManager().getEngineByName("Nashorn");
}
if (scriptEngine == null) {
LOGGER.error("missing " + ENGINE + " engine. Ensure you are running supported JVM");
return this;
Expand Down

0 comments on commit ba8e919

Please sign in to comment.