DevOps / BeanShell Interview questions
Explain the lifecycle of a BeanShell Interpreter instance embedded in a Java application?
The lifecycle begins when the host application constructs a bsh.Interpreter instance, at which point BeanShell initializes a fresh top-level NameSpace for that instance, along with its default set of built-in commands and imports ready to be used.
The host application then typically populates that namespace with relevant data via set(), giving the script access to whatever Java objects or values it needs from the host's own state before any script logic actually runs.
The host calls eval(), for a script string, or source(), for a script file, one or more times against this same Interpreter instance, and because the namespace persists across these calls on the same instance, variables and methods defined by one call remain available to subsequent calls on that same Interpreter - this persistence is what lets a host application source a utility script once and then repeatedly eval() smaller scripts that rely on functions the utility script defined.
After script execution, the host retrieves any results it needs via get(), reading final variable values back out of the persisted namespace.
The Interpreter instance itself then either continues to be reused for further script execution later, preserving its accumulated namespace state across calls, or is discarded, eligible for garbage collection, once the host application no longer needs it, at which point any state that existed only within that Interpreter's namespace, and wasn't otherwise retrieved via get(), is simply lost.
More Related questions...