BeanLookupBindings for simpler script access to beans#19
BeanLookupBindings for simpler script access to beans#19nlevitt merged 4 commits intointernetarchive:masterfrom
Conversation
The new class BeanLookupBindings allows scripts to skip getBean calls.
Lines like 'beanname = appCtx.getBean("beanname")' can be left out.
Past scripts remain compatible.
This change effects action directory scripts and rest console scripts.
|
Just scratching an itch. |
|
What happens if you have a script with a line like |
|
Very interesting! I also wonder if the collision (assigning into a bean name) or surprise could cause problems for scripts/habits already developed... but even if it did, it might be worth the convenience, with fair warning/documentation. For a particularly hard case, consider: what if a context had a bean already named 'rawOut' or 'appCtx'? Might this be something a script could opt into with an early line... like the way in Beanshell the setAccessibility(true) invocation can make all protected/private members accessible from that point on? |
bean lookup may be enabled by setting a variable named beanBindings to true in the Bindings or in the script.
|
Good point Noah, Gordon, I took your idea of enabling bean lookup. A line like This should even work with a config that has a bean named 'beanBindings', though once beanBindings is set to true in the script it would be impossible to unset. |
|
One other thought, just because requiring "beanBindings=true" makes the whole idea a little less appealing, since it conflicts with the goal of typing less hard to remember stuff. What if you didn't override put() at all but let it set a variable in the namespace even if it conflicted with a bean name. And then get() would first look in the explicit bindings (call super.get() first) and only if nothing is found, look for a bean by that name. Then In other words looking up variables as bean names would be a read-only fallback. |
|
Noah, that's what I originally considered doing, but if you set beanname to something other than appCtx.getBean("beanname") like maybe appCtx.getBean("beanname").getSomeProperty() then the script would still be broken. What's worse: silent enigmatic failure in a small number of cases or obvious documentented failure in a large number of cases? An argument for having beanBindings enabled by default is that there isn't much systemic use of the script console that I'm aware of yet. Netarchive, anyway, is on H1. Our own 'useful scripts' page is probably the main corpus to support. Now's the time to make the change. One can always disable it too, but it's probably better for disabling the feature to be the obscure useful tip to be discovered on the mailing list vs enabling the feature to be rarely used. |
|
On Jan 11, 2013 4:48 PM, "Travis Wellman" notifications@github.com wrote:
How so? |
|
When you try to use beanname it would not be appCtx.getBean("beanname"). It would be something else... |
|
If you set it explicitly to something else then yeah it will be that something else. Seems to me that's exactly what we want to maintain backward compatibility. |
|
Ok so you're suggesting do the bindings lookup first and if unset then do the bean name lookup? That makes sense... |
|
Yeah |
|
I like that it's a shorter class now. Should we merge? |
BeanLookupBindings for simpler script access to beans
The new class BeanLookupBindings allows scripts to skip getBean calls.
Lines like 'beanname = appCtx.getBean("beanname")' can be left out.
Past scripts remain compatible.
This change effects action directory scripts and rest console scripts.