Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lambda processor doesn't work with JS routes #116

Merged
merged 1 commit into from
Jul 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions camel-k-loader-js/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@
<artifactId>camel-main</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-mock</artifactId>
<scope>test</scope>
</dependency>

<!-- ******************************* -->
<!-- test deps -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.camel.k.Source;
import org.apache.camel.k.loader.js.dsl.IntegrationConfiguration;
import org.apache.camel.k.support.URIResolver;
import org.apache.camel.support.LifecycleStrategySupport;
import org.apache.commons.io.IOUtils;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Value;
Expand All @@ -44,24 +45,30 @@ public RouteBuilder load(CamelContext camelContext, Source source) throws Except
return new RouteBuilder() {
@Override
public void configure() throws Exception {
final CamelContext context = getContext();
final Context context = Context.newBuilder("js").allowAllAccess(true).build();

try (Context ctx = createPolyglotContext(); InputStream is = URIResolver.resolve(context, source)) {
Value bindings = ctx.getBindings(LANGUAGE_ID);
try (InputStream is = URIResolver.resolve(camelContext, source)) {
Value bindings = context.getBindings(LANGUAGE_ID);

// configure bindings
bindings.putMember("dsl", new IntegrationConfiguration(this));
bindings.putMember("__dsl", new IntegrationConfiguration(this));

final String script = IOUtils.toString(is, StandardCharsets.UTF_8);
final String wrappedScript = "with (dsl) { " + script + " }";
final String wrappedScript = "with (__dsl) { " + script + " }";

ctx.eval(LANGUAGE_ID, wrappedScript);
context.eval(LANGUAGE_ID, wrappedScript);

//
// Close the polyglot context when the camel context stops
//
getContext().addLifecycleStrategy(new LifecycleStrategySupport() {
@Override
public void onContextStop(CamelContext camelContext) {
context.close(true);
}
});
}
}
};
}

private static Context createPolyglotContext() {
return Context.newBuilder("js").allowAllAccess(true).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.List;

import org.apache.camel.ProducerTemplate;
import org.apache.camel.component.seda.SedaComponent;
import org.apache.camel.k.Runtime;
import org.apache.camel.k.listener.RoutesConfigurer;
Expand Down Expand Up @@ -99,4 +100,23 @@ public void testRestDSL() throws Exception {

runtime.run();
}

@Test
public void testProcessors() throws Exception {
ApplicationRuntime runtime = new ApplicationRuntime();
runtime.addListener(RoutesConfigurer.forRoutes("classpath:routes-with-processors.js"));
runtime.addListener(Runtime.Phase.Started, r -> {
ProducerTemplate template = r.getCamelContext().createProducerTemplate();

String a = template.requestBody("direct:arrow", "", String.class);
assertThat(a).isEqualTo("arrow");

String f = template.requestBody("direct:function", "", String.class);
assertThat(f).isEqualTo("function");

runtime.stop();
});

runtime.run();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

from('direct:function')
.process(function(e) { e.getMessage().setBody('function') });

from('direct:arrow')
.process(e => { e.getMessage().setBody('arrow') });