Skip to content

Commit

Permalink
feat(Netflix#2312): replace Nashorn with GraalVM JS Engine
Browse files Browse the repository at this point in the history
this is required for JDK 15+ compatibility
(Nashorn was removed in JDK 15)
  • Loading branch information
BlasiusSecundus committed Sep 11, 2023
1 parent 6359617 commit 55da066
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 20 deletions.
3 changes: 3 additions & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ dependencies {

implementation "com.github.ben-manes.caffeine:caffeine"

implementation "org.graalvm.js:js:${revGraalVM}"
implementation "org.graalvm.js:js-scriptengine:${revGraalVM}"

// JAXB is not bundled with Java 11, dependencies added explicitly
// These are needed by Apache BVAL
implementation "jakarta.xml.bind:jakarta.xml.bind-api:${revJAXB}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@

public class ScriptEvaluator {

private static final ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
private static final ScriptEngine engine =
new ScriptEngineManager().getEngineByName("graal.js");

static {
engine.put("polyglot.js.allowHostAccess", true);
}

private ScriptEvaluator() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ boolean evaluateCondition(WorkflowModel workflow, TaskModel task) throws ScriptE
boolean result = false;
if (condition != null) {
LOGGER.debug("Condition: {} is being evaluated", condition);
// Evaluate the expression by using the Nashorn based script evaluator
// Evaluate the expression by using the GraalVM based script evaluator
result = ScriptEvaluator.evalBool(condition, conditionInput);
}
return result;
Expand Down
1 change: 1 addition & 0 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ ext {
revSpock = '1.3-groovy-2.5'
revSpotifyCompletableFutures = '0.3.3'
revTestContainer = '1.15.3'
revGraalVM = '22.3.3'
}
8 changes: 4 additions & 4 deletions docs/docs/reference-docs/do-while-task.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ Branching inside loopOver task is supported.

### Input Parameters:

| name | type | description |
|---------------|------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| loopCondition | String | Condition to be evaluated after every iteration. This is a Javascript expression, evaluated using the Nashorn engine. If an exception occurs during evaluation, the DO_WHILE task is set to FAILED_WITH_TERMINAL_ERROR. |
| loopOver | List[Task] | List of tasks that needs to be executed as long as the condition is true. |
| name | type | description |
|---------------|------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| loopCondition | String | Condition to be evaluated after every iteration. This is a Javascript expression, evaluated using the GraalVM JS engine. If an exception occurs during evaluation, the DO_WHILE task is set to FAILED_WITH_TERMINAL_ERROR. |
| loopOver | List[Task] | List of tasks that needs to be executed as long as the condition is true. |

### Output Parameters

Expand Down
3 changes: 3 additions & 0 deletions java-sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ dependencies {
implementation "javax.ws.rs:javax.ws.rs-api:${revJAXRS}"
implementation "org.glassfish.jersey.core:jersey-common:${revJerseyCommon}"

implementation "org.graalvm.js:js:${revGraalVM}"
implementation "org.graalvm.js:js-scriptengine:${revGraalVM}"

testImplementation "org.springframework:spring-web"
testImplementation "org.spockframework:spock-core:${revSpock}"
testImplementation "org.spockframework:spock-spring:${revSpock}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,21 @@
*/
package com.netflix.conductor.sdk.workflow.def.tasks;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import com.google.common.base.Strings;
import com.netflix.conductor.common.metadata.tasks.TaskType;
import com.netflix.conductor.common.metadata.workflow.WorkflowTask;
import com.netflix.conductor.sdk.workflow.def.ValidationError;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.netflix.conductor.common.metadata.tasks.TaskType;
import com.netflix.conductor.common.metadata.workflow.WorkflowTask;
import com.netflix.conductor.sdk.workflow.def.ValidationError;

import com.google.common.base.Strings;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
* JQ Transformation task See https://stedolan.github.io/jq/ for how to form the queries to parse
Expand All @@ -43,7 +40,9 @@ public class Javascript extends Task<Javascript> {

private static final String EVALUATOR_TYPE_PARAMETER = "evaluatorType";

private static final String ENGINE = "nashorn";
private static final String ENGINE = "graal.js";

private static final String POLYGLOT_JS_ALLOW_HOST_ACCESS = "polyglot.js.allowHostAccess";

/**
* Javascript tasks are executed on the Conductor server without having to write worker code
Expand Down Expand Up @@ -105,6 +104,7 @@ public Javascript validate() {
LOGGER.error("missing " + ENGINE + " engine. Ensure you are running supported JVM");
return this;
}
scriptEngine.put(POLYGLOT_JS_ALLOW_HOST_ACCESS, true);

try {

Expand Down Expand Up @@ -133,6 +133,7 @@ public Object test(Map<String, Object> input) {
LOGGER.error("missing " + ENGINE + " engine. Ensure you are running supported JVM");
return this;
}
scriptEngine.put(POLYGLOT_JS_ALLOW_HOST_ACCESS, true);

try {

Expand Down

0 comments on commit 55da066

Please sign in to comment.