use of org.xwiki.context.ExecutionContext in project xwiki-platform by xwiki.
the class ClassPropertyValuesResourceImplTest method configure.
@Before
public void configure() throws Exception {
this.resolver = this.mocker.getInstance(DocumentReferenceResolver.TYPE_STRING);
this.authorization = this.mocker.getInstance(ContextualAuthorizationManager.class);
when(this.resolver.resolve("Path.To.Class", propertyReference.extractReference(EntityType.WIKI))).thenReturn((DocumentReference) propertyReference.getParent());
XWikiContext xcontext = mock(XWikiContext.class);
XWiki xwiki = mock(XWiki.class);
XWikiDocument classDocument = mock(XWikiDocument.class);
when(xcontext.getWiki()).thenReturn(xwiki);
when(xwiki.getDocument(propertyReference, xcontext)).thenReturn(classDocument);
when(classDocument.getXClass()).thenReturn(this.xclass);
ExecutionContext executionContext = new ExecutionContext();
executionContext.setProperty("xwikicontext", xcontext);
Execution execution = mock(Execution.class);
ComponentManager componentManager = this.mocker.getInstance(ComponentManager.class, "context");
when(componentManager.getInstance(Execution.class)).thenReturn(execution);
when(execution.getContext()).thenReturn(executionContext);
Provider<XWikiContext> xcontextProvider = this.mocker.getInstance(XWikiContext.TYPE_PROVIDER);
when(xcontextProvider.get()).thenReturn(xcontext);
UriInfo uriInfo = mock(UriInfo.class);
when(uriInfo.getBaseUri()).thenReturn(new URI("/xwiki/rest"));
this.resource = this.mocker.getComponentUnderTest();
ReflectionUtils.setFieldValue(resource, "uriInfo", uriInfo);
}
use of org.xwiki.context.ExecutionContext in project xwiki-platform by xwiki.
the class AbstractJob method execute.
@Override
public final void execute(JobExecutionContext jobContext) throws JobExecutionException {
JobDataMap data = jobContext.getJobDetail().getJobDataMap();
// The XWiki context was saved in the Job execution data map. Get it as we'll retrieve
// the script to execute from it.
this.xcontext = (XWikiContext) data.get("context");
// Clone the XWikiContex to have a new one for each run
this.xcontext = this.xcontext.clone();
// Init execution context
Execution execution;
try {
ExecutionContextManager ecim = Utils.getComponent(ExecutionContextManager.class);
execution = Utils.getComponent(Execution.class);
ExecutionContext context = new ExecutionContext();
// Bridge with old XWiki Context, required for old code
this.xcontext.declareInExecutionContext(context);
ecim.initialize(context);
} catch (ExecutionContextException e) {
throw new JobExecutionException("Fail to initialize execution context", e);
}
try {
// Execute the job
executeJob(jobContext);
} finally {
// We must ensure we clean the ThreadLocal variables located in the Execution
// component as otherwise we will have a potential memory leak.
execution.removeContext();
}
}
use of org.xwiki.context.ExecutionContext in project xwiki-platform by xwiki.
the class DefaultSolrIndexer method processBatch.
/**
* Process a batch of operations that were just read from the index operations queue. This method also commits the
* batch when it finishes to process it.
*
* @param queueEntry the batch to process
* @return {@code true} to wait for another batch, {@code false} to stop the indexing thread
*/
private boolean processBatch(IndexQueueEntry queueEntry) {
SolrInstance solrInstance = this.solrInstanceProvider.get();
int length = 0;
for (IndexQueueEntry batchEntry = queueEntry; batchEntry != null; batchEntry = this.indexQueue.poll()) {
if (batchEntry == INDEX_QUEUE_ENTRY_STOP) {
// Discard the current batch and stop the indexing thread.
return false;
}
IndexOperation operation = batchEntry.operation;
// For the current contiguous operations queue, group the changes
try {
this.ecim.initialize(new ExecutionContext());
if (IndexOperation.INDEX.equals(operation)) {
LengthSolrInputDocument solrDocument = getSolrDocument(batchEntry.reference);
if (solrDocument != null) {
solrInstance.add(solrDocument);
length += solrDocument.getLength();
++this.batchSize;
}
} else if (IndexOperation.DELETE.equals(operation)) {
if (batchEntry.reference == null) {
solrInstance.deleteByQuery(batchEntry.deleteQuery);
} else {
solrInstance.delete(this.solrRefereceResolver.getId(batchEntry.reference));
}
++this.batchSize;
}
} catch (Throwable e) {
this.logger.error("Failed to process entry [{}]", batchEntry, e);
} finally {
this.execution.removeContext();
}
// the reason why we perform it at the end of the batch.
if (shouldCommit(length, this.batchSize)) {
commit();
length = 0;
}
}
// Commit what's left
if (this.batchSize > 0) {
commit();
}
return true;
}
use of org.xwiki.context.ExecutionContext in project xwiki-platform by xwiki.
the class PageTest method configureComponentsBeforeOldcoreRuleForPageTest.
/**
* Set up of Components after the Components declared in {@link org.xwiki.test.annotation.ComponentList} have been
* handled but before {@link MockitoOldcoreRule#before(Class)} has been called (i.e. before it has created Mocks
* and configured Components).
*
* @throws Exception in case of errors
*/
@AfterComponent
public void configureComponentsBeforeOldcoreRuleForPageTest() throws Exception {
// Configure the Execution Context
ExecutionContext ec = new ExecutionContext();
mocker.<Execution>getInstance(Execution.class).setContext(ec);
}
use of org.xwiki.context.ExecutionContext in project xwiki-platform by xwiki.
the class WikiCreationJobTest method setUp.
@Before
public void setUp() throws Exception {
observationManager = mocker.getInstance(ObservationManager.class);
loggerManager = mocker.getInstance(LoggerManager.class);
store = mocker.getInstance(JobStatusStore.class);
executionProvider = mock(Provider.class);
mocker.registerComponent(new DefaultParameterizedType(null, Provider.class, Execution.class), executionProvider);
when(executionProvider.get()).thenReturn(execution);
executionContextManagerProvider = mock(Provider.class);
mocker.registerComponent(new DefaultParameterizedType(null, Provider.class, ExecutionContextManager.class), executionContextManagerProvider);
executionContextManager = mock(ExecutionContextManager.class);
when(executionContextManagerProvider.get()).thenReturn(executionContextManager);
jobContext = mocker.getInstance(JobContext.class);
progressManager = mocker.getInstance(JobProgressManager.class);
execution.pushContext(new ExecutionContext());
}
Aggregations