Hi Everyone,
I would like you to vote on merging the feature-execution-context
branches of commons and platform before the release of 4.3M2:
https://github.com/xwiki/xwiki-commons/compare/master...feature-execution-c…
https://github.com/xwiki/xwiki-platform/compare/master...feature-execution-…
Explanation of the feature:
The execution context is a simple map that binds a string to an object.
This can be compared to how variables are handled by scripting languages
such as bash, where an assignment brings the variable into existance:
$ my_variable="some value"
In the case of the execution context, the syntax is:
context.setProperty("my_variable", "some value");
This feature is about adding property declarations, where a property can
be associated with attributes that controls how the execution context
and execution context manager handles the property. The general idea
can, once again, be compared to bash and how it is possible to declare
variables there:
$ declare -ru my_variable="read only, forced upper case"
Of course, the set of attributes that are interesting to us is different
from bash. Currently the feature branch have support for declaring
properties with these attributes:
* Final - The value may not be updated within the the execution
context. Default: false.
* Inherited - The property will be inherited from the current
context when replacing the execution context within the current scope.
Default: false.
* Cloned value - Also clone the value when the execution context is
cloned. Default: false.
* Type - The class of the value, for typechecking when setting
the value. Default: null (unchecked).
* Non-null - The value may not be null, checked when setting the
value. Default: false.
Example declaration:
ExecutionContextProperty property = new
ExecutionContextProperty("my_variable");
property.setValue("some value");
property.setType(String.class);
property.setInherited(true);
context.declareProperty(property);
The property value may be updated and the property may be removed and
redeclared, unless declared 'final':
context.setProperty("my_variable", "some new value");
context.removeProperty("my_variable");
Additional attributes may be added later. This feature is also
backwards compliant, in that implicit declaration by just setting a
value is allowed. (Although we might want to make explicit declarations
mandatory in the future.)
Within the scope of a request, or the life time of a thread executing
some administrative task (e.g., the lucene index updater) the basic life
cycle of the execution context is the following:
@Inject
Execution execution;
@Inject
ExecutionContextManager ecm;
ExecutionContext context = new ExecutionContext();
ecm.initialize(context);
try {
// Do work
} finally {
ecm.removeContext();
}
Within the life cycle of the "root" execution context, we may push a new
execution context, which may either be a clean context, or a clone of
the current context:
// Pushing a clone
ExecutionContext context = ecm.clone(execution.getContext());
execution.pushContext(context);
try {
// Do work
} finally {
execution.popContext();
}
// Pushing a clean context
ExecutionContext context = new ExecutionContext();
execution.pushContext(context);
try {
// Do work
} finally {
execution.popContext();
}
Component authors that needs to place a value in the execution context
provides an initializer that declares the variable and sets an initial
value.
The attributes 'final', 'cloned value', and 'inherited' lets
component
authors control how the value is managed during the lifecycle of the
root execution context.
The attributes 'type' and 'non-null' provides some runtime assertions to
catch some errors earlier.
So to summarize, this feature:
1. is a convenient mechanism for managing properties in the execution
context,
2. provides some validation of the property values,
3. improves performance slightly by avoiding unnecessary cloning.
For more information, see the proposal thread:
http://xwiki.475771.n2.nabble.com/PROPOSAL-Execution-context-property-decla…
Best Regards,
/Andreas