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 a2dd3b44732720f52d7c242cf5c01de237ac0a08 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Sat Nov 19 10:48:48 2022 +0100 CAMEL-18718: camel-javascript --- .../camel/language/js/JavaScriptExpression.java | 25 +++++++++++----------- .../camel/language/js/JavaScriptLanguage.java | 15 ++++++------- 2 files changed, 20 insertions(+), 20 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 4a9b8469b8a..4c0850c2e84 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,20 +42,21 @@ public class JavaScriptExpression extends ExpressionSupport { @Override public <T> T evaluate(Exchange exchange, Class<T> type) { - Context cx = JavaScriptHelper.newContext(); - Value b = cx.getBindings("js"); + try (Context cx = JavaScriptHelper.newContext()) { + Value b = cx.getBindings("js"); - b.putMember("exchange", exchange); - b.putMember("context", exchange.getContext()); - b.putMember("exchangeId", exchange.getExchangeId()); - b.putMember("message", exchange.getMessage()); - b.putMember("headers", exchange.getMessage().getHeaders()); - b.putMember("properties", exchange.getAllProperties()); - b.putMember("body", exchange.getMessage().getBody()); + b.putMember("exchange", exchange); + b.putMember("context", exchange.getContext()); + b.putMember("exchangeId", exchange.getExchangeId()); + b.putMember("message", exchange.getMessage()); + b.putMember("headers", exchange.getMessage().getHeaders()); + b.putMember("properties", exchange.getAllProperties()); + b.putMember("body", exchange.getMessage().getBody()); - Value o = cx.eval("js", expressionString); - Object answer = o != null ? o.as(type) : null; - return type.cast(answer); + Value o = cx.eval("js", expressionString); + Object answer = o != null ? o.as(type) : null; + return type.cast(answer); + } } public Class<?> getType() { 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 8c6d799b194..0a125359144 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 @@ -44,13 +44,12 @@ public class JavaScriptLanguage extends LanguageSupport implements ScriptingLang @Override public <T> T evaluate(String script, Map<String, Object> bindings, Class<T> resultType) { script = loadResource(script); - - Context cx = JavaScriptHelper.newContext(); - Value b = cx.getBindings("js"); - - bindings.forEach(b::putMember); - Value o = cx.eval("js", script); - Object answer = o != null ? o.as(resultType) : null; - return resultType.cast(answer); + try (Context cx = JavaScriptHelper.newContext()) { + Value b = cx.getBindings("js"); + bindings.forEach(b::putMember); + Value o = cx.eval("js", script); + Object answer = o != null ? o.as(resultType) : null; + return resultType.cast(answer); + } } }