This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch js in repository https://gitbox.apache.org/repos/asf/camel.git
commit cb81d59c0a92e0a0f613ed6c6a28aa1e15fa60f7 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Sat Nov 19 10:43:01 2022 +0100 CAMEL-18718: camel-javascript --- .../camel/language/js/JavaScriptExpression.java | 4 +-- .../apache/camel/language/js/JavaScriptHelper.java | 37 ++++++++++++++++++++++ .../camel/language/js/JavaScriptLanguage.java | 4 +-- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/components/camel-javascript/src/main/java/org/apache/camel/language/js/JavaScriptExpression.java b/components/camel-javascript/src/main/java/org/apache/camel/language/js/JavaScriptExpression.java index 226a9993492..4a9b8469b8a 100644 --- a/components/camel-javascript/src/main/java/org/apache/camel/language/js/JavaScriptExpression.java +++ b/components/camel-javascript/src/main/java/org/apache/camel/language/js/JavaScriptExpression.java @@ -42,9 +42,7 @@ public class JavaScriptExpression extends ExpressionSupport { @Override public <T> T evaluate(Exchange exchange, Class<T> type) { - Context cx = Context.newBuilder("js") - .allowIO(true) - .build(); + Context cx = JavaScriptHelper.newContext(); Value b = cx.getBindings("js"); b.putMember("exchange", exchange); diff --git a/components/camel-javascript/src/main/java/org/apache/camel/language/js/JavaScriptHelper.java b/components/camel-javascript/src/main/java/org/apache/camel/language/js/JavaScriptHelper.java new file mode 100644 index 00000000000..207bbc3a686 --- /dev/null +++ b/components/camel-javascript/src/main/java/org/apache/camel/language/js/JavaScriptHelper.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.language.js; + +import org.graalvm.polyglot.Context; +import org.graalvm.polyglot.HostAccess; +import org.graalvm.polyglot.PolyglotAccess; + +public final class JavaScriptHelper { + + private JavaScriptHelper() { + } + + public static Context newContext() { + final Context.Builder contextBuilder = Context.newBuilder("js") + .allowIO(true) + .allowHostAccess(HostAccess.ALL) + .allowHostClassLookup(s -> true) + .allowPolyglotAccess(PolyglotAccess.NONE) + .option("engine.WarnInterpreterOnly", "false"); + return contextBuilder.build(); + } +} diff --git a/components/camel-javascript/src/main/java/org/apache/camel/language/js/JavaScriptLanguage.java b/components/camel-javascript/src/main/java/org/apache/camel/language/js/JavaScriptLanguage.java index 3ca43ae5de8..8c6d799b194 100644 --- a/components/camel-javascript/src/main/java/org/apache/camel/language/js/JavaScriptLanguage.java +++ b/components/camel-javascript/src/main/java/org/apache/camel/language/js/JavaScriptLanguage.java @@ -45,9 +45,7 @@ public class JavaScriptLanguage extends LanguageSupport implements ScriptingLang public <T> T evaluate(String script, Map<String, Object> bindings, Class<T> resultType) { script = loadResource(script); - Context cx = Context.newBuilder("js") - .allowIO(true) - .build(); + Context cx = JavaScriptHelper.newContext(); Value b = cx.getBindings("js"); bindings.forEach(b::putMember);