You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by "Kwok, Grace" <gr...@msci.com> on 2012/02/01 02:19:36 UTC

[jexl] setFunctions and JexlContext

Hi,
Could someone explain to me what this mean in actual code?
setFunctions
If the entry value is a class that has one contructor taking a JexlContext as argument, an instance of the namespace will be created at evaluation time. It might be a good idea to derive a JexlContext to carry the information used by the namespace to avoid variable space pollution and strongly type the constructor with this specialized JexlContext.

http://commons.apache.org/jexl/apidocs/index.html

I would like my function to be able to access one of the variable set in the JexlContext.
It sound like I should do something like this.

Public class FunctionClass

{
                Public FunctionClass(JexlContext jexlContext)
                {....}

                Public doSomethingWithVariableA()
                {...}
}

JexlContext jc = new MapContext();
Map<String, Object> funcs = new HashMap<String, Object>();
funcs.put(null, new FunctionClass(jc));
CustomJexlEngine jexl = new JexlEngine();
jexl.setFunctions(funcs);
Jc.put("a", "myPreciousValue");
Expression e11 = jexl.createExpression("doSomethingWithVariableA()");
Object result1 = e11.evaluate(jc);

Then my methods in FunctionClass can access the jexlContext and access variable "a".  However, I don't understand the paragraph about "avoiding variable space pollution".


Thanks, Grace




________________________________
This email message and any attachments are for the sole use of the intended recipients and may contain proprietary and/or confidential information which may be privileged or otherwise protected from disclosure. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not an intended recipient, please contact the sender by reply email and destroy the original message and any copies of the message as well as any attachments to the original message. Local registered entity information: http://www.msci.com/legal/local_registered_entities.html

Re: [jexl] setFunctions and JexlContext

Posted by henrib <he...@apache.org>.
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