Search in sources :

Example 71 with ExecutionContext

use of org.xwiki.context.ExecutionContext in project xwiki-platform by xwiki.

the class UserIterator method getXwikiContext.

protected XWikiContext getXwikiContext() {
    if (execution == null) {
        return this.xwikiContext;
    }
    XWikiContext context = null;
    ExecutionContext executionContext = execution.getContext();
    if (executionContext != null) {
        context = (XWikiContext) executionContext.getProperty(XWikiContext.EXECUTIONCONTEXT_KEY);
    }
    if (executionContext == null || context == null) {
        throw new RuntimeException(String.format("Aborting member extraction from passed references [%s] since " + "no XWiki Context was found", serializeUserAndGroupReferences()));
    }
    return context;
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) XWikiContext(com.xpn.xwiki.XWikiContext)

Example 72 with ExecutionContext

use of org.xwiki.context.ExecutionContext in project xwiki-platform by xwiki.

the class DatabasePingDataProviderTest method provideData.

@Test
public void provideData() throws Exception {
    Execution execution = this.mocker.getInstance(Execution.class);
    ExecutionContext executionContext = mock(ExecutionContext.class);
    when(execution.getContext()).thenReturn(executionContext);
    XWikiContext xwikiContext = mock(XWikiContext.class);
    when(executionContext.getProperty(XWikiContext.EXECUTIONCONTEXT_KEY)).thenReturn(xwikiContext);
    com.xpn.xwiki.XWiki xwiki = mock(com.xpn.xwiki.XWiki.class);
    when(xwikiContext.getWiki()).thenReturn(xwiki);
    XWikiCacheStoreInterface cacheStore = mock(XWikiCacheStoreInterface.class);
    when(xwiki.getStore()).thenReturn(cacheStore);
    XWikiHibernateStore store = mock(XWikiHibernateStore.class);
    when(cacheStore.getStore()).thenReturn(store);
    DatabaseMetaData databaseMetaData = mock(DatabaseMetaData.class);
    when(store.getDatabaseMetaData()).thenReturn(databaseMetaData);
    when(databaseMetaData.getDatabaseProductName()).thenReturn("HSQL Database Engine");
    when(databaseMetaData.getDatabaseProductVersion()).thenReturn("2.2.9");
    Map<String, Object> data = this.mocker.getComponentUnderTest().provideData();
    assertEquals(2, data.size());
    assertEquals("HSQL Database Engine", data.get("dbName"));
    assertEquals("2.2.9", data.get("dbVersion"));
}
Also used : Execution(org.xwiki.context.Execution) ExecutionContext(org.xwiki.context.ExecutionContext) XWikiHibernateStore(com.xpn.xwiki.store.XWikiHibernateStore) XWikiCacheStoreInterface(com.xpn.xwiki.store.XWikiCacheStoreInterface) XWikiContext(com.xpn.xwiki.XWikiContext) DatabaseMetaData(java.sql.DatabaseMetaData) Test(org.junit.Test)

Example 73 with ExecutionContext

use of org.xwiki.context.ExecutionContext in project xwiki-platform by xwiki.

the class XarExtensionHandler method createPackageConfiguration.

private PackageConfiguration createPackageConfiguration(LocalExtension extension, Request request, String wiki) {
    PackageConfiguration configuration;
    // Search job configuration in the context
    ExecutionContext context = this.execution.getContext();
    if (context != null) {
        configuration = (PackageConfiguration) context.getProperty(CONTEXTKEY_PACKAGECONFIGURATION);
    } else {
        configuration = null;
    }
    // Create a configuration for this extension
    if (configuration != null) {
        configuration = configuration.clone();
    } else {
        configuration = new PackageConfiguration();
        DocumentReference userReference = getRequestUserReference(AbstractExtensionValidator.PROPERTY_USERREFERENCE, request);
        configuration.setInteractive(request.isInteractive());
        configuration.setUser(userReference);
        configuration.setVerbose(request.isVerbose());
        configuration.setSkipMandatorytDocuments(true);
    }
    configuration.setWiki(wiki);
    // Filter entries to import if there is a plan
    if (extension != null && configuration.getXarExtensionPlan() != null) {
        Map<String, Map<XarEntry, LocalExtension>> nextXAREntries = configuration.getXarExtensionPlan().nextXAREntries;
        Set<String> entriesToImport = new HashSet<>();
        Map<XarEntry, LocalExtension> nextXAREntriesOnRoot = nextXAREntries.get(null);
        if (nextXAREntriesOnRoot != null) {
            for (Map.Entry<XarEntry, LocalExtension> entry : nextXAREntriesOnRoot.entrySet()) {
                if (entry.getValue() == extension) {
                    entriesToImport.add(entry.getKey().getEntryName());
                }
            }
        }
        Map<XarEntry, LocalExtension> nextXAREntriesOnWiki = nextXAREntries.get(wiki);
        if (nextXAREntriesOnWiki != null) {
            for (Map.Entry<XarEntry, LocalExtension> entry : nextXAREntriesOnWiki.entrySet()) {
                if (entry.getValue() == extension) {
                    entriesToImport.add(entry.getKey().getEntryName());
                }
            }
        }
        configuration.setEntriesToImport(entriesToImport);
    }
    return configuration;
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) XarEntry(org.xwiki.xar.XarEntry) LocalExtension(org.xwiki.extension.LocalExtension) PackageConfiguration(org.xwiki.extension.xar.internal.handler.packager.PackageConfiguration) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference) HashSet(java.util.HashSet)

Example 74 with ExecutionContext

use of org.xwiki.context.ExecutionContext in project xwiki-platform by xwiki.

the class XarExtensionJobFinishedListener method onEvent.

@Override
public void onEvent(Event event, Object source, Object data) {
    JobFinishingEvent jobFinishingEvent = (JobFinishingEvent) event;
    if (!jobFinishingEvent.getRequest().isRemote()) {
        ExecutionContext context = this.execution.getContext();
        if (context != null) {
            XarExtensionPlan xarExtensionPlan = (XarExtensionPlan) context.getProperty(XarExtensionPlan.CONTEXTKEY_XARINSTALLPLAN);
            if (xarExtensionPlan != null) {
                try {
                    Map<String, Map<XarEntry, XarExtensionPlanEntry>> previousXAREntries = xarExtensionPlan.previousXAREntries;
                    Map<String, Map<XarEntry, LocalExtension>> nextXAREntries = xarExtensionPlan.nextXAREntries;
                    Map<XarEntry, LocalExtension> rootNextPages = nextXAREntries.get(null);
                    if (rootNextPages == null) {
                        rootNextPages = Collections.emptyMap();
                    }
                    XWikiContext xcontext = this.xcontextProvider.get();
                    Packager packager = this.packagerProvider.get();
                    // Get pages to delete
                    Set<DocumentReference> pagesToDelete = new HashSet<DocumentReference>();
                    for (Map.Entry<String, Map<XarEntry, XarExtensionPlanEntry>> previousWikiEntry : previousXAREntries.entrySet()) {
                        if (!previousWikiEntry.getValue().isEmpty()) {
                            try {
                                List<DocumentReference> references = packager.getDocumentReferences(previousWikiEntry.getValue().keySet(), createPackageConfiguration(jobFinishingEvent.getRequest(), previousWikiEntry.getKey()));
                                for (DocumentReference reference : references) {
                                    // propose to enable them)
                                    if (((XarInstalledExtensionRepository) this.xarRepository).getXarInstalledExtensions(reference).isEmpty()) {
                                        pagesToDelete.add(reference);
                                    }
                                }
                            } catch (Exception e) {
                                this.logger.warn("Exception when cleaning pages removed since previous xar extension version", e);
                            }
                        }
                    }
                    // Create cleanup question
                    CleanPagesQuestion question = new CleanPagesQuestion(pagesToDelete);
                    Map<DocumentReference, Boolean> pages = question.getPages();
                    // Remove pages which are in the next XAR packages
                    for (DocumentReference previousReference : pagesToDelete) {
                        if (xarExtensionPlan.containsNewPage(previousReference)) {
                            pages.remove(previousReference);
                        }
                    }
                    for (Map.Entry<DocumentReference, Boolean> entry : pages.entrySet()) {
                        DocumentReference reference = entry.getKey();
                        // Get current
                        XWikiDocument currentDocument;
                        try {
                            currentDocument = xcontext.getWiki().getDocument(reference, xcontext);
                        } catch (Exception e) {
                            this.logger.error("Failed to get document [{}]", reference, e);
                            // Lets be safe and skip removing that page
                            pages.put(reference, false);
                            continue;
                        }
                        if (currentDocument.isNew()) {
                            // Current already removed
                            pages.put(reference, false);
                            continue;
                        }
                        // Get previous
                        XWikiDocument previousDocument;
                        try {
                            previousDocument = xarExtensionPlan.getPreviousXWikiDocument(reference, packager);
                        } catch (Exception e) {
                            this.logger.error("Failed to get previous version of document [{}]", reference, e);
                            // Lets be safe and skip removing that page
                            pages.put(reference, false);
                            continue;
                        }
                        // Compare previous and current
                        try {
                            currentDocument.loadAttachmentsContentSafe(xcontext);
                            if (!currentDocument.equalsData(previousDocument)) {
                                // conflict between current and new
                                pages.put(reference, false);
                            }
                        } catch (Exception e) {
                            this.logger.error("Failed to load attachments", e);
                            // Lets be safe and skip removing that page
                            pages.put(reference, false);
                            continue;
                        }
                    }
                    // Ask confirmation
                    if (!pages.isEmpty() && jobFinishingEvent.getRequest().isInteractive()) {
                        try {
                            ((Job) source).getStatus().ask(question);
                        } catch (InterruptedException e) {
                            this.logger.warn("The thread has been interrupted", e);
                            // The thread has been interrupted, do nothing
                            return;
                        }
                    }
                    // Delete pages
                    PackageConfiguration configuration = createPackageConfiguration(jobFinishingEvent.getRequest());
                    for (Map.Entry<DocumentReference, Boolean> entry : pages.entrySet()) {
                        if (entry.getValue()) {
                            packager.deleteDocument(entry.getKey(), configuration);
                        }
                    }
                } finally {
                    // Cleanup extension plan
                    try {
                        xarExtensionPlan.close();
                    } catch (IOException e) {
                        this.logger.error("Failed to close XAR extension plan", e);
                    }
                    context.setProperty(XarExtensionPlan.CONTEXTKEY_XARINSTALLPLAN, null);
                }
            }
        }
    }
}
Also used : XarEntry(org.xwiki.xar.XarEntry) CleanPagesQuestion(org.xwiki.extension.xar.question.CleanPagesQuestion) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Packager(org.xwiki.extension.xar.internal.handler.packager.Packager) LocalExtension(org.xwiki.extension.LocalExtension) PackageConfiguration(org.xwiki.extension.xar.internal.handler.packager.PackageConfiguration) DocumentReference(org.xwiki.model.reference.DocumentReference) JobFinishingEvent(org.xwiki.job.event.JobFinishingEvent) HashSet(java.util.HashSet) XWikiContext(com.xpn.xwiki.XWikiContext) IOException(java.io.IOException) IOException(java.io.IOException) ExecutionContext(org.xwiki.context.ExecutionContext) Map(java.util.Map)

Example 75 with ExecutionContext

use of org.xwiki.context.ExecutionContext in project xwiki-platform by xwiki.

the class DefaultMailSender method sendAsynchronously.

@Override
public MailResult sendAsynchronously(Iterable<? extends MimeMessage> messages, Session session, MailListener listener) {
    // If the session has specified a batch id, then use it! This can be used for example when resending email.
    String batchId = session.getProperty(SESSION_BATCHID_KEY);
    if (batchId == null) {
        batchId = UUID.randomUUID().toString();
    }
    // Pass a clone of the current execution context so that the mail message will be prepared and later sent in the
    // same context, but in read-only mode (i.e. the preparation of the mail will not impact the current thread's
    // context).
    ExecutionContext executionContext = this.execution.getContext();
    ExecutionContext clonedExecutionContext = this.executionContextCloner.copy(executionContext);
    // TODO: Remove once we've found the reason for the functional Mail test flicker: from time to time the wiki
    // is not set in the Mail Status LT
    XWikiContext xcontext = (XWikiContext) clonedExecutionContext.getProperty(XWikiContext.EXECUTIONCONTEXT_KEY);
    if (xcontext.getWikiId() == null) {
        throw new RuntimeException(String.format("Aborting Mail Sending: the Wiki Id must not be null in the " + "XWiki Context. Got [%s] in the original context.", ((XWikiContext) executionContext.getProperty(XWikiContext.EXECUTIONCONTEXT_KEY)).getWikiId()));
    }
    this.prepareMailQueueManager.addToQueue(new PrepareMailQueueItem(messages, session, listener, batchId, clonedExecutionContext));
    return new DefaultMailResult(batchId);
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) XWikiContext(com.xpn.xwiki.XWikiContext) PrepareMailQueueItem(org.xwiki.mail.internal.thread.PrepareMailQueueItem)

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