Search in sources :

Example 6 with LocalExtension

use of org.xwiki.extension.LocalExtension 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 7 with LocalExtension

use of org.xwiki.extension.LocalExtension in project xwiki-platform by xwiki.

the class XarExtensionPlan method getNextXarExtension.

public LocalExtension getNextXarExtension(String wiki, LocalDocumentReference localDocumentReference) {
    XarEntry xarEntry = new XarEntry(localDocumentReference);
    LocalExtension nextExtension = null;
    Map<XarEntry, LocalExtension> wikiEntry = this.nextXAREntries.get(wiki);
    if (wikiEntry != null) {
        nextExtension = wikiEntry.get(xarEntry);
    }
    if (nextExtension == null) {
        wikiEntry = this.nextXAREntries.get(null);
        if (wikiEntry != null) {
            nextExtension = wikiEntry.get(xarEntry);
        }
    }
    return nextExtension;
}
Also used : XarEntry(org.xwiki.xar.XarEntry) LocalExtension(org.xwiki.extension.LocalExtension)

Example 8 with LocalExtension

use of org.xwiki.extension.LocalExtension in project xwiki-platform by xwiki.

the class Package method registerExtension.

private void registerExtension(XWikiContext context) {
    // Register the package as extension if it's one
    if (isInstallExtension() && StringUtils.isNotEmpty(getExtensionId()) && StringUtils.isNotEmpty(getVersion())) {
        ExtensionId extensionId = new ExtensionId(getExtensionId(), getVersion());
        try {
            LocalExtensionRepository localRepository = Utils.getComponent(LocalExtensionRepository.class);
            LocalExtension localExtension = localRepository.getLocalExtension(extensionId);
            if (localExtension == null) {
                Extension extension;
                try {
                    // Try to find and download the extension from a repository
                    extension = Utils.getComponent(ExtensionRepositoryManager.class).resolve(extensionId);
                } catch (ResolveException e) {
                    LOGGER.debug("Can't find extension [{}]", extensionId, e);
                    // FIXME: Create a dummy extension. Need support for partial/lazy extension.
                    return;
                }
                localExtension = localRepository.storeExtension(extension);
            }
            InstalledExtensionRepository installedRepository = Utils.getComponent(InstalledExtensionRepository.class);
            String namespace = "wiki:" + context.getWikiId();
            // Make sure it's not already there
            if (installedRepository.getInstalledExtension(localExtension.getId().getId(), namespace) == null) {
                for (ExtensionId feature : localExtension.getExtensionFeatures()) {
                    if (installedRepository.getInstalledExtension(feature.getId(), namespace) != null) {
                        // Already exist so don't register it or it could create a mess
                        return;
                    }
                }
            } else {
                return;
            }
            // Register the extension as installed
            InstalledExtension installedExtension = installedRepository.installExtension(localExtension, namespace, false);
            // Tell the world about it
            Utils.getComponent(ObservationManager.class).notify(new ExtensionInstalledEvent(installedExtension.getId(), namespace), installedExtension);
        } catch (Exception e) {
            LOGGER.error("Failed to register extenion [{}] from the XAR", extensionId, e);
        }
    }
}
Also used : InstalledExtension(org.xwiki.extension.InstalledExtension) Extension(org.xwiki.extension.Extension) LocalExtension(org.xwiki.extension.LocalExtension) ResolveException(org.xwiki.extension.ResolveException) InstalledExtension(org.xwiki.extension.InstalledExtension) LocalExtensionRepository(org.xwiki.extension.repository.LocalExtensionRepository) LocalExtension(org.xwiki.extension.LocalExtension) ExtensionId(org.xwiki.extension.ExtensionId) ObservationManager(org.xwiki.observation.ObservationManager) InstalledExtensionRepository(org.xwiki.extension.repository.InstalledExtensionRepository) XWikiException(com.xpn.xwiki.XWikiException) QueryException(org.xwiki.query.QueryException) DocumentException(org.dom4j.DocumentException) IOException(java.io.IOException) ResolveException(org.xwiki.extension.ResolveException) ExtensionInstalledEvent(org.xwiki.extension.event.ExtensionInstalledEvent)

Aggregations

LocalExtension (org.xwiki.extension.LocalExtension)8 IOException (java.io.IOException)3 ExecutionContext (org.xwiki.context.ExecutionContext)3 Extension (org.xwiki.extension.Extension)3 ResolveException (org.xwiki.extension.ResolveException)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 XarEntry (org.xwiki.xar.XarEntry)3 HashSet (java.util.HashSet)2 Map (java.util.Map)2 ExtensionId (org.xwiki.extension.ExtensionId)2 InstalledExtension (org.xwiki.extension.InstalledExtension)2 PackageConfiguration (org.xwiki.extension.xar.internal.handler.packager.PackageConfiguration)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiException (com.xpn.xwiki.XWikiException)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 DocumentException (org.dom4j.DocumentException)1 InstallException (org.xwiki.extension.InstallException)1 Action (org.xwiki.extension.distribution.internal.DocumentsModifiedDuringDistributionListener.DocumentStatus.Action)1 ExtensionInstalledEvent (org.xwiki.extension.event.ExtensionInstalledEvent)1 InstalledExtensionRepository (org.xwiki.extension.repository.InstalledExtensionRepository)1