Search in sources :

Example 1 with PackageConfiguration

use of org.xwiki.extension.xar.internal.handler.packager.PackageConfiguration in project xwiki-platform by xwiki.

the class XarExtensionHandler method initJobPackageConfiguration.

private void initJobPackageConfiguration(Request request, boolean defaultConflict) throws InterruptedException {
    ExecutionContext context = this.execution.getContext();
    if (context != null && context.getProperty(CONTEXTKEY_PACKAGECONFIGURATION) == null) {
        Job currentJob = null;
        try {
            currentJob = this.componentManager.<JobContext>getInstance(JobContext.class).getCurrentJob();
        } catch (Exception e) {
            this.logger.error("Failed to lookup JobContext, it will be impossible to do interactive install");
        }
        if (currentJob != null) {
            PackageConfiguration configuration = new PackageConfiguration();
            context.setProperty(CONTEXTKEY_PACKAGECONFIGURATION, configuration);
            DocumentReference userReference = getRequestUserReference(AbstractExtensionValidator.PROPERTY_USERREFERENCE, request);
            configuration.setInteractive(request.isInteractive());
            configuration.setUser(userReference);
            configuration.setVerbose(request.isVerbose());
            configuration.setSkipMandatorytDocuments(true);
            configuration.setXarExtensionPlan(getXARExtensionPlan());
            configuration.setJobStatus(currentJob.getStatus());
            // Non blocker conflicts
            configuration.setConflictAction(ConflictType.CURRENT_DELETED, request.getProperty(ConflictQuestion.REQUEST_CONFLICT_DEFAULTANSWER_CURRENT_DELETED), GlobalAction.CURRENT);
            configuration.setConflictAction(ConflictType.MERGE_SUCCESS, request.getProperty(ConflictQuestion.REQUEST_CONFLICT_DEFAULTANSWER_MERGE_SUCCESS), GlobalAction.MERGED);
            // Blocker conflicts
            configuration.setConflictAction(ConflictType.CURRENT_EXIST, request.getProperty(ConflictQuestion.REQUEST_CONFLICT_DEFAULTANSWER_CURRENT_EXIST), configuration.isInteractive() ? GlobalAction.ASK : GlobalAction.NEXT);
            configuration.setConflictAction(ConflictType.MERGE_FAILURE, request.getProperty(ConflictQuestion.REQUEST_CONFLICT_DEFAULTANSWER_MERGE_FAILURE), configuration.isInteractive() ? GlobalAction.ASK : GlobalAction.MERGED);
            // If user asked to be asked about conflict behavior
            if (defaultConflict && currentJob.getStatus().getRequest().isInteractive()) {
                XWikiContext xcontext = xcontextProvider.get();
                // Make sure the context has the right user
                xcontext.setUserReference(userReference);
                int extensionConflictSetup = NumberUtils.toInt(xcontext.getWiki().getUserPreference("extensionConflictSetup", xcontext), 0);
                if (extensionConflictSetup == 1) {
                    DefaultConflictActionQuestion question = new DefaultConflictActionQuestion(configuration);
                    currentJob.getStatus().ask(question, 1, TimeUnit.HOURS);
                }
            }
        }
    }
}
Also used : ExecutionContext(org.xwiki.context.ExecutionContext) DefaultConflictActionQuestion(org.xwiki.extension.xar.question.DefaultConflictActionQuestion) XWikiContext(com.xpn.xwiki.XWikiContext) JobContext(org.xwiki.job.JobContext) Job(org.xwiki.job.Job) AbstractExtensionJob(org.xwiki.extension.job.internal.AbstractExtensionJob) PackageConfiguration(org.xwiki.extension.xar.internal.handler.packager.PackageConfiguration) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) ExtensionException(org.xwiki.extension.ExtensionException) IOException(java.io.IOException) UninstallException(org.xwiki.extension.UninstallException) InstallException(org.xwiki.extension.InstallException) XarException(org.xwiki.xar.XarException) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 2 with PackageConfiguration

use of org.xwiki.extension.xar.internal.handler.packager.PackageConfiguration in project xwiki-platform by xwiki.

the class XarExtensionHandler method uninstall.

@Override
public void uninstall(InstalledExtension installedExtension, String namespace, Request request) throws UninstallException {
    try {
        initializePagesIndex(request);
        initJobPackageConfiguration(request, false);
    } catch (Exception e) {
        throw new UninstallException("Failed to initialize extension plan index", e);
    }
    // probably not be in an expected state)
    if (!request.isRemote()) {
        Job currentJob;
        try {
            currentJob = this.componentManager.<JobContext>getInstance(JobContext.class).getCurrentJob();
        } catch (ComponentLookupException e) {
            currentJob = null;
        }
        if (currentJob == null) {
            String wiki;
            try {
                wiki = XarHandlerUtils.getWikiFromNamespace(namespace);
            } catch (UnsupportedNamespaceException e) {
                throw new UninstallException("Failed to extract wiki id from namespace", e);
            }
            PackageConfiguration configuration = createPackageConfiguration(null, request, wiki);
            try {
                XarInstalledExtension xarLocalExtension = (XarInstalledExtension) this.xarRepository.resolve(installedExtension.getId());
                Collection<XarEntry> pages = xarLocalExtension.getXarPackage().getEntries();
                this.packager.unimportPages(pages, configuration);
            } catch (Exception e) {
                // Not supposed to be possible
                throw new UninstallException("Failed to get xar extension [" + installedExtension.getId() + "] from xar repository", e);
            }
        } else {
        // The actual delete of pages is done in XarExtensionJobFinishedListener
        }
    }
}
Also used : XarInstalledExtension(org.xwiki.extension.xar.internal.repository.XarInstalledExtension) XarEntry(org.xwiki.xar.XarEntry) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) JobContext(org.xwiki.job.JobContext) Job(org.xwiki.job.Job) AbstractExtensionJob(org.xwiki.extension.job.internal.AbstractExtensionJob) PackageConfiguration(org.xwiki.extension.xar.internal.handler.packager.PackageConfiguration) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) ExtensionException(org.xwiki.extension.ExtensionException) IOException(java.io.IOException) UninstallException(org.xwiki.extension.UninstallException) InstallException(org.xwiki.extension.InstallException) XarException(org.xwiki.xar.XarException) UninstallException(org.xwiki.extension.UninstallException)

Example 3 with PackageConfiguration

use of org.xwiki.extension.xar.internal.handler.packager.PackageConfiguration in project xwiki-platform by xwiki.

the class DefaultDocumentMergeImporterTest method setUp.

@Before
public void setUp() throws Exception {
    this.xwiki = mock(XWiki.class);
    when(this.xcontext.getWiki()).thenReturn(this.xwiki);
    // documents
    this.previousDocument = mock(XWikiDocument.class, "previous");
    when(this.previousDocument.isNew()).thenReturn(false);
    when(this.previousDocument.getDocumentReferenceWithLocale()).thenReturn(this.documentReference);
    this.currentDocument = mock(XWikiDocument.class, "current");
    when(this.currentDocument.isNew()).thenReturn(false);
    when(this.currentDocument.getDocumentReferenceWithLocale()).thenReturn(this.documentReference);
    when(this.xwiki.getDocument(same(this.documentReference), same(xcontext))).thenReturn(this.currentDocument);
    this.nextDocument = mock(XWikiDocument.class, "next");
    when(this.nextDocument.isNew()).thenReturn(false);
    when(this.nextDocument.getDocumentReferenceWithLocale()).thenReturn(this.documentReference);
    this.mergedDocument = mock(XWikiDocument.class, "merged");
    when(this.mergedDocument.isNew()).thenReturn(false);
    when(this.mergedDocument.getDocumentReferenceWithLocale()).thenReturn(this.documentReference);
    when(this.currentDocument.clone()).thenReturn(this.mergedDocument);
    // merge
    this.configuration = new PackageConfiguration();
    this.mergeResult = new MergeResult();
    when(this.mergedDocument.merge(same(this.previousDocument), same(this.nextDocument), any(MergeConfiguration.class), any(XWikiContext.class))).thenReturn(this.mergeResult);
    // job status
    this.jobStatus = mock(JobStatus.class);
    this.configuration.setJobStatus(this.jobStatus);
    // execution
    this.econtext = new ExecutionContext();
    this.execution = this.mocker.getInstance(Execution.class);
    when(this.execution.getContext()).thenReturn(this.econtext);
}
Also used : JobStatus(org.xwiki.job.event.status.JobStatus) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ExecutionContext(org.xwiki.context.ExecutionContext) Execution(org.xwiki.context.Execution) MergeResult(com.xpn.xwiki.doc.merge.MergeResult) XWiki(com.xpn.xwiki.XWiki) XWikiContext(com.xpn.xwiki.XWikiContext) MergeConfiguration(com.xpn.xwiki.doc.merge.MergeConfiguration) PackageConfiguration(org.xwiki.extension.xar.internal.handler.packager.PackageConfiguration) Before(org.junit.Before)

Example 4 with PackageConfiguration

use of org.xwiki.extension.xar.internal.handler.packager.PackageConfiguration 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 5 with PackageConfiguration

use of org.xwiki.extension.xar.internal.handler.packager.PackageConfiguration in project xwiki-platform by xwiki.

the class XarExtensionHandler method installInternal.

private void installInternal(LocalExtension newLocalExtension, String wiki, Request request) throws InstallException {
    try {
        initializePagesIndex(request);
        initJobPackageConfiguration(request, true);
    } catch (Exception e) {
        throw new InstallException("Failed to initialize extension plan index", e);
    }
    // import xar into wiki (add new version when the page already exists)
    PackageConfiguration configuration = createPackageConfiguration(newLocalExtension, request, wiki);
    try {
        this.packager.importXAR("Install extension [" + newLocalExtension + "]", new File(newLocalExtension.getFile().getAbsolutePath()), configuration);
    } catch (Exception e) {
        throw new InstallException("Failed to import xar for extension [" + newLocalExtension + "]", e);
    }
}
Also used : PackageConfiguration(org.xwiki.extension.xar.internal.handler.packager.PackageConfiguration) InstallException(org.xwiki.extension.InstallException) File(java.io.File) ComponentLookupException(org.xwiki.component.manager.ComponentLookupException) ExtensionException(org.xwiki.extension.ExtensionException) IOException(java.io.IOException) UninstallException(org.xwiki.extension.UninstallException) InstallException(org.xwiki.extension.InstallException) XarException(org.xwiki.xar.XarException)

Aggregations

PackageConfiguration (org.xwiki.extension.xar.internal.handler.packager.PackageConfiguration)7 IOException (java.io.IOException)4 ExecutionContext (org.xwiki.context.ExecutionContext)4 XWikiContext (com.xpn.xwiki.XWikiContext)3 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)3 ExtensionException (org.xwiki.extension.ExtensionException)3 InstallException (org.xwiki.extension.InstallException)3 UninstallException (org.xwiki.extension.UninstallException)3 DocumentReference (org.xwiki.model.reference.DocumentReference)3 XarEntry (org.xwiki.xar.XarEntry)3 XarException (org.xwiki.xar.XarException)3 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 LocalExtension (org.xwiki.extension.LocalExtension)2 AbstractExtensionJob (org.xwiki.extension.job.internal.AbstractExtensionJob)2 Job (org.xwiki.job.Job)2 JobContext (org.xwiki.job.JobContext)2 XWiki (com.xpn.xwiki.XWiki)1 MergeConfiguration (com.xpn.xwiki.doc.merge.MergeConfiguration)1