gnodet-bot commented on code in PR #29875:
URL: https://github.com/apache/camel/pull/29875#discussion_r4573029562


##########
components/camel-language/src/main/java/org/apache/camel/component/language/LanguageProducer.java:
##########
@@ -135,6 +117,52 @@ public void process(Exchange exchange) throws Exception {
         }
     }
 
+    /**
+     * Evaluates the script of the {@link LanguageConstants#LANGUAGE_SCRIPT} 
header, if there is one. The script is for
+     * this exchange only: it is compiled once, never cached as the script of 
the endpoint, and the header is removed so
+     * a later language endpoint does not run it again.
+     *
+     * @return true if the header had a script, false to use the script of the 
endpoint
+     */
+    private boolean processHeaderScript(Exchange exchange) throws Exception {
+        Expression exp = 
exchange.getIn().getHeader(LanguageConstants.LANGUAGE_SCRIPT, Expression.class);
+        boolean compiled = false;
+        if (exp == null) {
+            String script = 
exchange.getIn().getHeader(LanguageConstants.LANGUAGE_SCRIPT, String.class);
+            if (script == null) {
+                return false;
+            }
+            // the script may be a file: so resolve it before using
+            script = getEndpoint().resolveScript(script);
+            Class<?> type = resultType != Object.class ? resultType : null;
+            exp = getEndpoint().getLanguage().createExpression(script, new 
Object[] { type });
+            exp.init(getEndpoint().getCamelContext());
+            compiled = true;
+        }
+        exchange.getIn().removeHeader(LanguageConstants.LANGUAGE_SCRIPT);
+
+        // the expression compiled from the script is used once, so it is 
always stopped after use
+        Object result = evaluate(exchange, exp, compiled || 
!getEndpoint().isCacheScript());

Review Comment:
   The comment says *"the expression compiled from the script is used once, so 
it is always stopped after use"* — but when the header provides a pre-built 
`Expression` object (not a `String`), `compiled=false`. With 
`cacheScript=true`, `stopAfterUse = false || !true = false`, so the expression 
is **not** stopped after use.
   
   This is intentional and correct (the caller owns the lifecycle of an 
externally-provided `Expression`), but the comment only accurately describes 
the String-compiled path. Consider:
   
   ```suggestion
           // an expression compiled from a String script (compiled=true) is 
always stopped after use;
           // a pre-built Expression from the header is externally managed, so 
it honours the cacheScript flag
           Object result = evaluate(exchange, exp, compiled || 
!getEndpoint().isCacheScript());
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to