Skip to content

Commit

Permalink
Switch to GraalJS #99
Browse files Browse the repository at this point in the history
  • Loading branch information
lburgazzoli committed Jul 16, 2019
1 parent a2f0b60 commit fbeb255
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 23 deletions.
6 changes: 6 additions & 0 deletions camel-k-runtime-jvm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@
<version>${commons-lang.version}</version>
</dependency>

<dependency>
<groupId>org.graalvm.js</groupId>
<artifactId>js</artifactId>
<version>${graalvm.version}</version>
</dependency>

<!-- ****************************** -->
<!-- -->
<!-- TESTS -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,16 @@
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;
import org.apache.camel.k.RoutesLoader;
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
Expand All @@ -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<String, RouteDefinition>) uri -> from(uri));
bindings.put("rest", (Supplier<RestDefinition>) () -> rest());
bindings.put("restConfiguration", (Supplier<RestConfigurationDefinition>) () -> 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();
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<immutables.version>2.7.5</immutables.version>
<semver4j.version>2.2.0</semver4j.version>
<undertow.version>1.4.26.Final</undertow.version>
<graalvm.version>19.0.2</graalvm.version>
<gmavenplus-plugin.version>1.7.1</gmavenplus-plugin.version>
<maven-compiler-plugin.version>3.8.1</maven-compiler-plugin.version>
<maven-surefire-plugin.version>2.22.2</maven-surefire-plugin.version>
Expand Down

0 comments on commit fbeb255

Please sign in to comment.