Context Module

Last modified by Manuel Leduc on 2024/07/05 14:26

cogShare contextual information between components
TypeJAR
Category
Developed by

XWiki Development Team

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1
Bundled With

XWiki Standard

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:

@Inject
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

Since 10.10RC1.

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

    @Inject
   private ContextStoreManager contextStore;

   private void addTask() throws ComponentLookupException
   {
        Map<String, Serializable> storedContext = this.contextStore.save(Arrays.asList("wiki", "user", "locale"));

       [...]
   }

Restore

    @Inject
   private ContextStoreManager contextStore;

   private void startTask(Map<String, Serializable> storedContext) throws ComponentLookupException
   {
       this.contextStore.restore(storedContext);

       [...]
   }

Get Connected