Context Module
Share contextual information between components |
Type | JAR |
Category | |
Developed by | |
Rating | |
License | GNU Lesser General Public License 2.1 |
Bundled With | XWiki Standard |
Table of contents
Description
This module control the ExecutionContext and related common tools.
It was first introduced as a replaced for the XWikiContext which used to be passed as parameter in most XWiki classes methods. The ExecutionContext context is stored in a ThreadLocal and as such as be reached from anywhere should it be needed, the main entry point to access it is the component Execution.
Push/pop ExecutionContext
It's possible to push (and put) a new ExecutionContext in the current thread for various isolation needs.
Since 11.9 it's recommended to do that trough the ExecutionContextManager component:
private ExecutionContextManager contextManager;
private void foo() throws ExecutionContextException
{
this.contextManager.pushContext(new ExecutionContext(), true);
try {
[...]
} finally {
this.contextManager.popContext();
}
}
Before 11.9 you can will use the APIs provided by Execution component.
Save and restore contextual information
Saving and restoring specific contextual information is useful for example when execution asynchronous code which behavior still depends on various information located in the initial context.
This is done trough org.xwiki.context.concurrent.ContextStoreManager.
Save
private ContextStoreManager contextStore;
private void addTask() throws ComponentLookupException
{
Map<String, Serializable> storedContext = this.contextStore.save(Arrays.asList("wiki", "user", "locale"));
[...]
}
Restore
private ContextStoreManager contextStore;
private void startTask(Map<String, Serializable> storedContext) throws ComponentLookupException
{
this.contextStore.restore(storedContext);
[...]
}