commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henrib <>
Subject Re: [jexl] setFunctions and JexlContext
Date Sun, 12 Feb 2012 18:51:25 GMT
The idea of a namespace functor is to create the object carrying the method
at usage time, optionally using the context to initialize the instance.
The functor constructor can use members/methods of the context; of course,
it is possible to use variables to expose values to that constructor but in
that case, they are accessible to the whole script which is pollution.

Think about a user (http) session or a database connection that you don't
want to expose as a variable.

>From the tests, the following code hides the 'factor' as a member of a
specialized context and use it at functor creation time.
    public static class EnhancedContext extends MapContext {
        int factor = 6;

    public static class ContextualFunctor {
        private final EnhancedContext context;

        public ContextualFunctor(EnhancedContext theContext) {
            context = theContext;

        public int ratio(int n) {
            context.factor -= 1;
            return n / context.factor;
        funcs.put("cx", ContextualFunctor.class);
        JexlContext jc = new EnhancedContext();
        e = JEXL.createExpression("cx:ratio(10) + cx:ratio(20)");

Hope this helps,

View this message in context:
Sent from the Commons - User mailing list archive at

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message