commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henrib <hen...@apache.org>
Subject Re: [jexl] setFunctions and JexlContext
Date Sun, 12 Feb 2012 18:51:25 GMT
Hi,
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.
{code}
    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);
        JEXL.setFunctions(funcs);
        JexlContext jc = new EnhancedContext();
....
        e = JEXL.createExpression("cx:ratio(10) + cx:ratio(20)");
{code}

Hope this helps,
Cheers
Henrib


--
View this message in context: http://apache-commons.680414.n4.nabble.com/jexl-setFunctions-and-JexlContext-tp4346527p4381702.html
Sent from the Commons - User mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message