diff --git a/camel-k-runtime-jvm/pom.xml b/camel-k-runtime-jvm/pom.xml index 4c5caab70..48bcb2e6d 100644 --- a/camel-k-runtime-jvm/pom.xml +++ b/camel-k-runtime-jvm/pom.xml @@ -72,6 +72,12 @@ ${commons-lang.version} + + org.graalvm.js + js + ${graalvm.version} + + diff --git a/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/loader/JavaScriptLoader.java b/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/loader/JavaScriptLoader.java index e638a47be..5b519abde 100644 --- a/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/loader/JavaScriptLoader.java +++ b/camel-k-runtime-jvm/src/main/java/org/apache/camel/k/jvm/loader/JavaScriptLoader.java @@ -20,12 +20,6 @@ import java.io.InputStreamReader; import java.util.Collections; import java.util.List; -import java.util.function.Function; -import java.util.function.Supplier; -import javax.script.Bindings; -import javax.script.ScriptEngine; -import javax.script.ScriptEngineManager; -import javax.script.SimpleBindings; import org.apache.camel.CamelContext; import org.apache.camel.builder.RouteBuilder; @@ -33,9 +27,9 @@ import org.apache.camel.k.Source; import org.apache.camel.k.jvm.dsl.Components; import org.apache.camel.k.support.URIResolver; -import org.apache.camel.model.RouteDefinition; -import org.apache.camel.model.rest.RestConfigurationDefinition; -import org.apache.camel.model.rest.RestDefinition; +import org.graalvm.polyglot.Context; +import org.graalvm.polyglot.Value; +import org.graalvm.polyglot.proxy.ProxyExecutable; public class JavaScriptLoader implements RoutesLoader { @Override @@ -49,24 +43,30 @@ public RouteBuilder load(CamelContext camelContext, Source source) throws Except @Override public void configure() throws Exception { final CamelContext context = getContext(); - final ScriptEngineManager manager = new ScriptEngineManager(); - final ScriptEngine engine = manager.getEngineByName("nashorn"); - final Bindings bindings = new SimpleBindings(); - // Exposed to the underlying script, but maybe better to have - // a nice dsl - bindings.put("builder", this); - bindings.put("context", context); - bindings.put("components", new Components(context)); - bindings.put("registry", camelContext.getRegistry()); - bindings.put("from", (Function) uri -> from(uri)); - bindings.put("rest", (Supplier) () -> rest()); - bindings.put("restConfiguration", (Supplier) () -> restConfiguration()); + try (Context ctx = createPolyglotContext(); InputStream is = URIResolver.resolve(context, source)) { + Value bindings = ctx.getBindings("js"); + bindings.putMember("builder", this); + bindings.putMember("context", context); + bindings.putMember("components", new Components(context)); + bindings.putMember("registry", context.getRegistry()); - try (InputStream is = URIResolver.resolve(context, source)) { - engine.eval(new InputStreamReader(is), bindings); + bindings.putMember("from", (ProxyExecutable) args -> from(args[0].asString())); + bindings.putMember("rest", (ProxyExecutable) args -> rest()); + bindings.putMember("restConfiguration", (ProxyExecutable) args -> restConfiguration()); + + ctx.eval( + org.graalvm.polyglot.Source.newBuilder( + "js", + new InputStreamReader(is), source.getName() + ).build() + ); } } }; } + + private static Context createPolyglotContext() { + return Context.newBuilder("js").allowAllAccess(true).build(); + } } diff --git a/pom.xml b/pom.xml index 00984530d..a92cbc806 100644 --- a/pom.xml +++ b/pom.xml @@ -53,6 +53,7 @@ 2.7.5 2.2.0 1.4.26.Final + 19.0.2 1.7.1 3.8.1 2.22.2