From ee9cb7d65080deee6ef06e836efbcdeafd397638 Mon Sep 17 00:00:00 2001 From: Arunan Sugunakumar Date: Fri, 14 Jun 2024 14:00:10 +0530 Subject: [PATCH] Integrate graaljs for datamapper engine --- .../pom.xml | 25 +++++++++++++++ .../engine/core/executors/ScriptExecutor.java | 15 +++++++++ .../core/executors/ScriptExecutorFactory.java | 2 +- .../core/executors/ScriptExecutorType.java | 2 +- .../utils/DataMapperEngineConstants.java | 1 + .../pom.xml | 4 +++ .../datamapper/DataMapperMediator.java | 2 +- pom.xml | 32 +++++++++++++++++++ 8 files changed, 80 insertions(+), 3 deletions(-) diff --git a/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/pom.xml b/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/pom.xml index 02513e9d657..411b31ae82a 100644 --- a/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/pom.xml +++ b/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/pom.xml @@ -102,9 +102,34 @@ org.openjdk.nashorn nashorn-core + + + org.graalvm.sdk + graal-sdk + + + org.graalvm.js + js + + + org.graalvm.truffle + truffle-api + + + org.graalvm.regex + regex + + + com.ibm.icu + icu4j + org.apache.bsf.wso2 bsf-all + + org.graalvm.js + js-scriptengine + diff --git a/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/src/main/java/org/wso2/carbon/mediator/datamapper/engine/core/executors/ScriptExecutor.java b/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/src/main/java/org/wso2/carbon/mediator/datamapper/engine/core/executors/ScriptExecutor.java index bad297638a7..d1f06ce2668 100644 --- a/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/src/main/java/org/wso2/carbon/mediator/datamapper/engine/core/executors/ScriptExecutor.java +++ b/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/src/main/java/org/wso2/carbon/mediator/datamapper/engine/core/executors/ScriptExecutor.java @@ -27,6 +27,7 @@ import org.wso2.carbon.mediator.datamapper.engine.core.models.Model; import org.wso2.carbon.mediator.datamapper.engine.core.models.StringModel; import org.wso2.carbon.mediator.datamapper.engine.output.formatters.MapOutputFormatter; +import org.wso2.carbon.mediator.datamapper.engine.utils.DataMapperEngineConstants; import org.wso2.carbon.mediator.datamapper.engine.utils.DataMapperEngineUtils; import org.wso2.carbon.mediator.datamapper.engine.utils.OpenJDKNashornFactoryWrapper; @@ -88,6 +89,20 @@ public ScriptExecutor(ScriptExecutorType scriptExecutorType) { log.debug("Setting Nashorn as Script Engine"); break; } + case GRAALJS: + try { + scriptEngine = new ScriptEngineManager().getEngineByName(DataMapperEngineConstants.GRAALJS_ENGINE_NAME); + bindings = scriptEngine.createBindings(); + log.debug("Setting Graal.js as Script Engine"); + } catch (Exception e) { + log.warn("Could not find Graal.js jar in the lib, switching to rhino js. This may cause performance issue "); + log.debug(e); + scriptEngineManager.registerEngineName(DEFAULT_ENGINE_NAME, new RhinoScriptEngineFactory()); + scriptEngine = scriptEngineManager.getEngineByName(DEFAULT_ENGINE_NAME); + bindings = scriptEngine.createBindings(); + log.debug("Setting Rhino as Script Engine"); + } + break; case RHINO: scriptEngineManager.registerEngineName(DEFAULT_ENGINE_NAME, new RhinoScriptEngineFactory()); scriptEngine = scriptEngineManager.getEngineByName(DEFAULT_ENGINE_NAME); diff --git a/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/src/main/java/org/wso2/carbon/mediator/datamapper/engine/core/executors/ScriptExecutorFactory.java b/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/src/main/java/org/wso2/carbon/mediator/datamapper/engine/core/executors/ScriptExecutorFactory.java index a5332cb272f..02b0dfeac8c 100644 --- a/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/src/main/java/org/wso2/carbon/mediator/datamapper/engine/core/executors/ScriptExecutorFactory.java +++ b/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/src/main/java/org/wso2/carbon/mediator/datamapper/engine/core/executors/ScriptExecutorFactory.java @@ -28,7 +28,7 @@ public class ScriptExecutorFactory { private static ScriptExecutorPool executorPool = null; - private static ScriptExecutorType scriptExecutorType = ScriptExecutorType.NASHORN; + private static ScriptExecutorType scriptExecutorType = ScriptExecutorType.GRAALJS; private static final Log log = LogFactory.getLog(ScriptExecutorFactory.class); /** diff --git a/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/src/main/java/org/wso2/carbon/mediator/datamapper/engine/core/executors/ScriptExecutorType.java b/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/src/main/java/org/wso2/carbon/mediator/datamapper/engine/core/executors/ScriptExecutorType.java index b9e7228440f..7d92b562e81 100644 --- a/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/src/main/java/org/wso2/carbon/mediator/datamapper/engine/core/executors/ScriptExecutorType.java +++ b/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/src/main/java/org/wso2/carbon/mediator/datamapper/engine/core/executors/ScriptExecutorType.java @@ -20,5 +20,5 @@ * This enum type represent the types of executors support in data mapper */ public enum ScriptExecutorType { - RHINO, NASHORN + RHINO, NASHORN, GRAALJS } diff --git a/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/src/main/java/org/wso2/carbon/mediator/datamapper/engine/utils/DataMapperEngineConstants.java b/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/src/main/java/org/wso2/carbon/mediator/datamapper/engine/utils/DataMapperEngineConstants.java index fd762507d76..5917f8928d9 100644 --- a/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/src/main/java/org/wso2/carbon/mediator/datamapper/engine/utils/DataMapperEngineConstants.java +++ b/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper.engine/src/main/java/org/wso2/carbon/mediator/datamapper/engine/utils/DataMapperEngineConstants.java @@ -32,6 +32,7 @@ public class DataMapperEngineConstants { public static final String NULL_ELEMENT_TYPE = "null"; public static final String ARRAY_ELEMENT_FIRST_NAME = "0"; public static final String NASHORN_ENGINE_NAME = "nashorn"; + public static final String GRAALJS_ENGINE_NAME = "graal.js"; public static final String DEFAULT_ENGINE_NAME = "js"; //rhino public static final int DEFAULT_DATAMAPPER_ENGINE_POOL_SIZE = 20; public static final String ORG_APACHE_SYNAPSE_DATAMAPPER_EXECUTOR_POOL_SIZE = diff --git a/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper/pom.xml b/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper/pom.xml index f1cc67470e7..363f166f41b 100644 --- a/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper/pom.xml +++ b/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper/pom.xml @@ -67,5 +67,9 @@ org.wso2.carbon.mediation org.wso2.carbon.mediator.datamapper.engine + + org.graalvm.js + js + diff --git a/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper/src/main/java/org/wso2/carbon/mediator/datamapper/DataMapperMediator.java b/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper/src/main/java/org/wso2/carbon/mediator/datamapper/DataMapperMediator.java index c2f800b471c..5872d3fcd3e 100644 --- a/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper/src/main/java/org/wso2/carbon/mediator/datamapper/DataMapperMediator.java +++ b/components/mediators/datamapper/org.wso2.carbon.mediator.datamapper/src/main/java/org/wso2/carbon/mediator/datamapper/DataMapperMediator.java @@ -755,6 +755,6 @@ private ScriptEngine getScriptExecutor() { if (javaVersion.startsWith("1.7") || javaVersion.startsWith("1.6")) { return new ScriptEngineManager().getEngineByName(DataMapperEngineConstants.DEFAULT_ENGINE_NAME); } - return new ScriptEngineManager().getEngineByName(DataMapperEngineConstants.NASHORN_ENGINE_NAME); + return new ScriptEngineManager().getEngineByName(DataMapperEngineConstants.GRAALJS_ENGINE_NAME); } } diff --git a/pom.xml b/pom.xml index a54ee7e6bb5..f6deee6d1a1 100644 --- a/pom.xml +++ b/pom.xml @@ -2472,6 +2472,36 @@ bsf-all ${bsf.version} + + org.graalvm.sdk + graal-sdk + ${graalvm.version} + + + org.graalvm.js + js-scriptengine + ${graalvm.version} + + + org.graalvm.js + js + ${graalvm.version} + + + org.graalvm.truffle + truffle-api + ${graalvm.version} + + + org.graalvm.regex + regex + ${graalvm.version} + + + com.ibm.icu + icu4j + ${icu.version} + @@ -2665,6 +2695,8 @@ 2.1.18.wso2v1 6.3.50 + 23.0.4 + 72.1