[ 
https://issues.apache.org/activemq/browse/CAMEL-655?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=43845#action_43845
 ] 

Aaron Mulder commented on CAMEL-655:
------------------------------------

A follow-up from Dave, my Jython guy.  We'd obviously need to massage this a 
bit if we were going to go this way, but it's an option.

-----------

It figures that I found Invocable right after I sent that out.  This works:

args[0] should be the value you want for request.headers['foo']
args[1] should be the name of the script file

{code:java|title=JyScript.java}
import java.io.*;
import java.util.*;
import javax.script.*;

public class JyScript {
    public static void main(String[] args) throws Exception {
        final ScriptEngineManager manager = new ScriptEngineManager();
        final ScriptEngine engine = manager.getEngineByName("jython");
        final MyRequest request = new MyRequest(args[0]);
        engine.put("request", request);
        engine.eval(new BufferedReader(new FileReader(args[1])));
        final Invocable invocable = (Invocable) engine;
        Object result = invocable.invokeFunction("is_console_header_value", 
"bar");
        System.out.printf("Result is: %s", result);
    }
}

class MyRequest {
    private Map<String, String> headers;

    public MyRequest(final String value) {
        headers = new HashMap<String, String>();
        headers.put("foo", value);
        headers.put("other_key", "other_value");
    }

    public Map<String, String> getHeaders() {
        return headers;
    }
}
{code}

{code:title=my_func.py}
def is_console_header_value(val):
    return request.headers['foo'] == val
{code}

> Jython Expressions always return null/false
> -------------------------------------------
>
>                 Key: CAMEL-655
>                 URL: https://issues.apache.org/activemq/browse/CAMEL-655
>             Project: Apache Camel
>          Issue Type: Bug
>          Components: camel-script
>    Affects Versions: 1.3.0, 1.4.0
>         Environment: Jython 2.2.1, camel-script 1.3.0/1.4.0-SNAPSHOT, jython 
> integration lib from java.net or logicblaze.com repo
>            Reporter: Aaron Mulder
>
> A Jython expression used for example like this always returns null, which is 
> now translated to false:
> from("...").choice().when().jython("some expression").to(...)
> For example, here's an expression that always evaluates to null/false:
> request.headers['foo'] == 'bar'
> To see this, look at the test PythonExpressionTest in the camel-script module.

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.

Reply via email to