I looked back through an old copy of the pre-ASF repo and found that the
"context" references were added in 2006 and it had something to do with
making MapStack a LocalizedMap for screen/form widget internalization
rendering.  The commit message and a small explanation in the mailing lists
didn't explain that specific addition unfortunately.  Could be that it
isn't used there anymore, at the time freemarker macros weren't used for
widget rendering, so it's possible that a fix in the freemarker render
could remove the need for it.

Regards
Scott

On 21 July 2018 at 17:12, Mathieu Lirzin <[email protected]> wrote:

> Hello Scott,
>
> Scott Gray <[email protected]> writes:
>
> > I think it relates to the "context" variable that is frequently used in
> > groovy data prep scripts for the script output.
> >
> > I'm not in front of a computer, but looking at it in that light may help.
>
> As shown by this snippet from ‘GroovyUtil.java’, the “context” binding
> is added explicitly so it doesn't require the ‘MapStack’ special case
> for the "context" key.  In fact since the context is copied to an
> ‘HashMap’ before being passed to Groovy, the ‘MapStack’ implementation
> is not used at all from Groovy.
>
> --8<---------------cut here---------------start------------->8---
>     public static Binding getBinding(Map<String, Object> context, String
> expression) {
>         Map<String, Object> vars = new HashMap<>();
>         if (context != null) {
>             vars.putAll(context);
>             if (UtilValidate.isNotEmpty(expression)) {
>               ...;
>             }
>             vars.put("context", context);
>             ...;
>         }
>         return new Binding(vars);
>     }
>
>     public static Object runScriptAtLocation(String location, String
> methodName, Map<String, Object> context) throws GeneralException {
>         Script script = InvokerHelper.createScript(
> getScriptClassFromLocation(location), getBinding(context));
>         Object result = null;
>         if (UtilValidate.isEmpty(methodName)) {
>             result = script.run();
>         } else {
>             result = script.invokeMethod(methodName, new Object[] {
> context });
>         }
>         return result;
>     }
> --8<---------------cut here---------------end--------------->8---
>
> What is nice is that the ‘getBinding’ method has a javadoc explaining
> why the “context” binding is added explicitly.  Thanks to Adrian Crum
> (RIP) for providing it.
>
> --8<---------------cut here---------------start------------->8---
> The ‘context’ Map is added to the ‘Binding’ as a variable called
> "context" so that variables can be passed back to the caller.
> --8<---------------cut here---------------end--------------->8---
>
> Thanks for your input.
>
> --
> Mathieu Lirzin
> GPG: F2A3 8D7E EB2B 6640 5761  070D 0ADE E100 9460 4D37
>

Reply via email to