Search in sources :

Example 41 with ExecutionContext

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);
}
Also used : ContextualAuthorizationManager(org.xwiki.security.authorization.ContextualAuthorizationManager) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ExecutionContext(org.xwiki.context.ExecutionContext) Execution(org.xwiki.context.Execution) ComponentManager(org.xwiki.component.manager.ComponentManager) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) URI(java.net.URI) UriInfo(javax.ws.rs.core.UriInfo) Before(org.junit.Before)

Example 42 with ExecutionContext

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();
    }
}
Also used : JobDataMap(org.quartz.JobDataMap) Execution(org.xwiki.context.Execution) JobExecutionContext(org.quartz.JobExecutionContext) ExecutionContext(org.xwiki.context.ExecutionContext) JobExecutionException(org.quartz.JobExecutionException) ExecutionContextManager(org.xwiki.context.ExecutionContextManager) ExecutionContextException(org.xwiki.context.ExecutionContextException)

Example 43 with ExecutionContext

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;
}
Also used : LengthSolrInputDocument(org.xwiki.search.solr.internal.metadata.LengthSolrInputDocument) ExecutionContext(org.xwiki.context.ExecutionContext) SolrInstance(org.xwiki.search.solr.internal.api.SolrInstance)

Example 44 with ExecutionContext

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);
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) Execution(org.xwiki.context.Execution) AfterComponent(org.xwiki.test.annotation.AfterComponent)

Example 45 with ExecutionContext

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());
}
Also used : Execution(org.xwiki.context.Execution) DefaultExecution(org.xwiki.context.internal.DefaultExecution) ExecutionContext(org.xwiki.context.ExecutionContext) ExecutionContextManager(org.xwiki.context.ExecutionContextManager) LoggerManager(org.xwiki.logging.LoggerManager) ObservationManager(org.xwiki.observation.ObservationManager) JobContext(org.xwiki.job.JobContext) DefaultParameterizedType(org.xwiki.component.util.DefaultParameterizedType) JobProgressManager(org.xwiki.job.event.status.JobProgressManager) JobStatusStore(org.xwiki.job.JobStatusStore) Provider(javax.inject.Provider) Before(org.junit.Before)

Aggregations

ExecutionContext (org.xwiki.context.ExecutionContext)114 Execution (org.xwiki.context.Execution)62 XWikiContext (com.xpn.xwiki.XWikiContext)47 Before (org.junit.Before)31 Test (org.junit.Test)25 XWiki (com.xpn.xwiki.XWiki)19 DocumentReference (org.xwiki.model.reference.DocumentReference)19 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)15 DefaultParameterizedType (org.xwiki.component.util.DefaultParameterizedType)14 ComponentManager (org.xwiki.component.manager.ComponentManager)9 ExecutionContextException (org.xwiki.context.ExecutionContextException)9 WikiDescriptorManager (org.xwiki.wiki.descriptor.WikiDescriptorManager)9 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)8 ExecutionContextManager (org.xwiki.context.ExecutionContextManager)8 List (java.util.List)6 Map (java.util.Map)6 Properties (java.util.Properties)6 Session (javax.mail.Session)6 MimeMessage (javax.mail.internet.MimeMessage)6 XWikiException (com.xpn.xwiki.XWikiException)5