Interface Control
JavaBox.
Script controls are allowed to modify a script's generated bytecode, for example, to prevent linking to certain classes, weave in periodic checsks, etc.
Control Contexts
Controls are given per-container and per-execution contexts, to which they may create and attach
their own private state. The lifecycle methods initialize() and shutdown() are for managing per-container context, and startExecution() and
finishExecution() are for per-execution context. An "execution" happens with
script snippets that contain statements or expressions; snippets that simply declare classes, etc.,
do not execute (not immediately, anyway).
If the return value from a script execution is an invokable object, then any subsequent invocations into that object's methods will not have any per-container or per-execution context, because that execution will be happening outside of the container. However, a control could use other tricks to regain context outside of the container, e.g., wrapping return values in a proxy interface, static method invocations that provide a unique symbolic key, etc.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final recordstatic final recordThe per-execution context associated with aControl. -
Method Summary
Modifier and TypeMethodDescriptiondefault voidfinishExecution(Control.ExecutionContext context, Object result, Throwable error) Notification that the execution of a script snippet has finished, either successfully or by throwing an exception.default Objectinitialize(JavaBox box) Initialize this control for the givenJavaBoxand return any associated private context.default byte[]modifyBytecode(ClassDesc name, byte[] bytecode) Apply this control to the given class which was generated from a script.default voidshutdown(Control.ContainerContext context) Shutdown this control for the givenJavaBox.default ObjectstartExecution(Control.ContainerContext context) Notification that the execution of a script snippet has started.
-
Method Details
-
initialize
Initialize this control for the givenJavaBoxand return any associated private context.Controls may create their own private context on a per-
JavaBoxbasis using this method. If no private context is needed, this method may return null.This method should also perform any other required per-container initialization, for example, loading custom support classes.
When the given
JavaBoxis closed,shutdown()will be invoked with aControl.ContainerContextreferencing the returned context object.The default implementation in
Controlreturns null.- Parameters:
box- theJavaBoxinstance- Returns:
- this control's private context for the given
JavaBox, or null if none is needed - Throws:
JavaBoxException- if some error occurs
-
shutdown
- Parameters:
context- the container context for this control
-
modifyBytecode
Apply this control to the given class which was generated from a script.The default implementation in
Controljust returnsbytecode.- Parameters:
name- the name of the class being addedbytecode- the Java bytecode of the class being added- Returns:
- replacement java bytecode; must not be null
- Throws:
ControlViolationException- if the class contains something rejected by this controlJavaBoxException- if some other error occurs
-
startExecution
Notification that the execution of a script snippet has started.This method should initialize this control for the new execution and return any associated private context.
This method should also perform any required per-execution initialization, for example, initializing resource counters, etc.
The current thread will be the thread that is actually executing the script snippet. A
Control.ExecutionContextwill be created using the returned private context and made available in this thread viaJavaBox.executionContextFor(), and also provided tofinishExecution().The default implementation in
Controlreturns null.- Parameters:
context- the container context for this control
-
finishExecution
Notification that the execution of a script snippet has finished, either successfully or by throwing an exception.The current thread will be the thread that actually executed the snippet.
The default implementation in
Controldoes nothing.- Parameters:
context- the execution context for this controlresult- the return value from the successful execution of an expression snippet, otherwise nullerror- the exception thrown by snippet execution if there was an error, otherwise null
-