Search in sources :

Example 1 with ExtensionDependency

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

the class RepositoryManager method updateExtensionVersionDependencies.

private boolean updateExtensionVersionDependencies(XWikiDocument document, Extension extension) throws XWikiException {
    boolean needSave = false;
    List<ExtensionDependency> dependencies = new ArrayList<>(extension.getDependencies());
    int dependencyIndex = 0;
    // Clean misplaced or bad existing dependencies associated to this extension version
    List<BaseObject> xobjects = document.getXObjects(XWikiRepositoryModel.EXTENSIONDEPENDENCY_CLASSREFERENCE);
    if (xobjects != null) {
        boolean deleteExistingObjects = false;
        // Clone since we are going to modify and parse it at the same time
        xobjects = new ArrayList<>(document.getXObjects(XWikiRepositoryModel.EXTENSIONDEPENDENCY_CLASSREFERENCE));
        for (int i = 0; i < xobjects.size(); ++i) {
            BaseObject dependencyObject = xobjects.get(i);
            if (dependencyObject != null) {
                String extensionVersion = getValue(dependencyObject, XWikiRepositoryModel.PROP_DEPENDENCY_EXTENSIONVERSION, (String) null);
                if (StringUtils.isNotEmpty(extensionVersion) && extension.getId().getVersion().equals(new DefaultVersion(extensionVersion))) {
                    if (deleteExistingObjects) {
                        document.removeXObject(dependencyObject);
                        needSave = true;
                    } else {
                        String xobjectId = getValue(dependencyObject, XWikiRepositoryModel.PROP_DEPENDENCY_ID);
                        String xobjectConstraint = getValue(dependencyObject, XWikiRepositoryModel.PROP_DEPENDENCY_CONSTRAINT);
                        List<String> xobjectRepositories = (List<String>) getValue(dependencyObject, XWikiRepositoryModel.PROP_DEPENDENCY_REPOSITORIES);
                        boolean xobjectOptional = getValue(dependencyObject, XWikiRepositoryModel.PROP_DEPENDENCY_OPTIONAL, 0) == 1;
                        if (dependencies.size() > dependencyIndex) {
                            ExtensionDependency dependency = dependencies.get(dependencyIndex);
                            DefaultExtensionDependency xobjectDependency = new DefaultExtensionDependency(xobjectId, new DefaultVersionConstraint(xobjectConstraint), xobjectOptional, dependency.getProperties());
                            xobjectDependency.setRepositories(XWikiRepositoryModel.toRepositoryDescriptors(xobjectRepositories, this.extensionFactory));
                            if (dependency.equals(xobjectDependency)) {
                                ++dependencyIndex;
                                continue;
                            }
                        }
                        deleteExistingObjects = true;
                        document.removeXObject(dependencyObject);
                        needSave = true;
                    }
                }
            }
        }
    }
    // Add missing dependencies
    if (dependencyIndex < dependencies.size()) {
        XWikiContext xcontext = this.xcontextProvider.get();
        for (; dependencyIndex < dependencies.size(); ++dependencyIndex) {
            ExtensionDependency dependency = dependencies.get(dependencyIndex);
            BaseObject dependencyObject = document.newXObject(XWikiRepositoryModel.EXTENSIONDEPENDENCY_CLASSREFERENCE, xcontext);
            dependencyObject.set(XWikiRepositoryModel.PROP_DEPENDENCY_EXTENSIONVERSION, extension.getId().getVersion().getValue(), xcontext);
            dependencyObject.set(XWikiRepositoryModel.PROP_DEPENDENCY_ID, dependency.getId(), xcontext);
            dependencyObject.set(XWikiRepositoryModel.PROP_DEPENDENCY_CONSTRAINT, dependency.getVersionConstraint().getValue(), xcontext);
            dependencyObject.set(XWikiRepositoryModel.PROP_DEPENDENCY_OPTIONAL, dependency.isOptional() ? 1 : 0, xcontext);
            dependencyObject.set(XWikiRepositoryModel.PROP_DEPENDENCY_REPOSITORIES, XWikiRepositoryModel.toStringList(dependency.getRepositories()), xcontext);
            needSave = true;
        }
    }
    return needSave;
}
Also used : DefaultVersion(org.xwiki.extension.version.internal.DefaultVersion) ArrayList(java.util.ArrayList) XWikiContext(com.xpn.xwiki.XWikiContext) DefaultExtensionDependency(org.xwiki.extension.DefaultExtensionDependency) ExtensionDependency(org.xwiki.extension.ExtensionDependency) DefaultVersionConstraint(org.xwiki.extension.version.internal.DefaultVersionConstraint) BaseObject(com.xpn.xwiki.objects.BaseObject) DefaultVersionConstraint(org.xwiki.extension.version.internal.DefaultVersionConstraint) List(java.util.List) ArrayList(java.util.ArrayList) DefaultExtensionDependency(org.xwiki.extension.DefaultExtensionDependency)

Example 2 with ExtensionDependency

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

the class RepositoryTestUtils method extensionDependencyObjects.

/**
 * @since 7.3M1
 */
public static List<org.xwiki.rest.model.jaxb.Object> extensionDependencyObjects(Extension extension) {
    List<org.xwiki.rest.model.jaxb.Object> dependencies = new ArrayList<>(extension.getDependencies().size());
    int number = 0;
    for (ExtensionDependency dependency : extension.getDependencies()) {
        org.xwiki.rest.model.jaxb.Object dependencyObject = extensionDependencyObject(extension.getId().getVersion(), dependency);
        dependencyObject.setNumber(number++);
        dependencies.add(dependencyObject);
    }
    return dependencies;
}
Also used : ArrayList(java.util.ArrayList) ExtensionDependency(org.xwiki.extension.ExtensionDependency)

Example 3 with ExtensionDependency

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

the class DiffXarJob method diff.

private void diff(String feature, String namespace, Set<LocalDocumentReference> alreadydone) {
    if (this.comparedFeatures.contains(feature)) {
        // We already looked at this feature.
        return;
    }
    this.comparedFeatures.add(feature);
    InstalledExtension installedExtension = this.installedExtensionRepository.getInstalledExtension(feature, namespace);
    if (installedExtension != null) {
        diff(installedExtension, namespace, alreadydone);
        Collection<? extends ExtensionDependency> dependencies = installedExtension.getDependencies();
        this.progressManager.pushLevelProgress(dependencies.size(), this);
        try {
            for (ExtensionDependency dependency : dependencies) {
                this.progressManager.startStep(this);
                diff(dependency.getId(), namespace, new HashSet<>(alreadydone));
            }
        } finally {
            this.progressManager.popLevelProgress(this);
        }
    }
}
Also used : InstalledExtension(org.xwiki.extension.InstalledExtension) ExtensionDependency(org.xwiki.extension.ExtensionDependency)

Example 4 with ExtensionDependency

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

the class RepairXarJob method repairExtension.

/**
 * @param localExtension the local extension to install
 * @param namespace the namespace where to install extension
 * @param dependency indicate of the extension is installed as a dependency of another
 * @param managedDependencies the managed dependencies
 * @throws InstallException failed to repair extension
 */
private void repairExtension(LocalExtension localExtension, String namespace, boolean dependency, Map<String, ExtensionDependency> managedDependencies) throws InstallException {
    this.progressManager.pushLevelProgress(2, this);
    this.observationManager.notify(new ExtensionInstallingEvent(localExtension.getId(), namespace), localExtension);
    InstalledExtension installedExtension = null;
    try {
        this.progressManager.startStep(this);
        Collection<? extends ExtensionDependency> dependencies = localExtension.getDependencies();
        if (!dependencies.isEmpty()) {
            this.progressManager.pushLevelProgress(dependencies.size(), dependencies);
            try {
                for (ExtensionDependency extensionDependency : dependencies) {
                    this.progressManager.startStep(dependencies);
                    // Replace with managed dependency if any
                    ExtensionDependency resolvedDependency = ExtensionUtils.getDependency(extensionDependency, managedDependencies, localExtension);
                    repairDependency(resolvedDependency, namespace, ExtensionUtils.append(managedDependencies, localExtension));
                    this.progressManager.endStep(dependencies);
                }
            } finally {
                this.progressManager.popLevelProgress(dependencies);
            }
        }
        this.progressManager.endStep(this);
        this.progressManager.startStep(this);
        installedExtension = this.installedRepository.installExtension(localExtension, namespace, dependency);
    } finally {
        if (installedExtension != null) {
            this.observationManager.notify(new ExtensionInstalledEvent(installedExtension.getId(), namespace), installedExtension);
        } else {
            this.observationManager.notify(new ExtensionInstallFailedEvent(localExtension.getId(), namespace), localExtension);
        }
        this.progressManager.popLevelProgress(this);
    }
}
Also used : InstalledExtension(org.xwiki.extension.InstalledExtension) ExtensionInstallingEvent(org.xwiki.extension.event.ExtensionInstallingEvent) ExtensionInstallFailedEvent(org.xwiki.extension.event.ExtensionInstallFailedEvent) ExtensionDependency(org.xwiki.extension.ExtensionDependency) ExtensionInstalledEvent(org.xwiki.extension.event.ExtensionInstalledEvent)

Example 5 with ExtensionDependency

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

the class WikiEventListener method copyInstalledExtension.

private void copyInstalledExtension(InstalledExtension installedExtension, String sourceNamespace, String targetNamespace) {
    if (!installedExtension.isInstalled(targetNamespace) && !installedExtension.isInstalled(null)) {
        // Copy dependencies first
        for (ExtensionDependency dependency : installedExtension.getDependencies()) {
            InstalledExtension installedDependency = this.installedRepository.getInstalledExtension(dependency.getId(), sourceNamespace);
            if (installedDependency != null) {
                copyInstalledExtension(installedDependency, sourceNamespace, targetNamespace);
            }
        }
        // Copy extension
        try {
            // Installed extension
            this.extensionHandlerManager.initialize(installedExtension, targetNamespace);
            // Register extension as installed
            this.installedRepository.installExtension(installedExtension, targetNamespace, installedExtension.isDependency(sourceNamespace));
        } catch (ExtensionException e) {
            this.logger.error("Failed to copy extension [{}] from namespace [{}] to namespace [{}]", installedExtension, sourceNamespace, targetNamespace, e);
        }
    }
}
Also used : InstalledExtension(org.xwiki.extension.InstalledExtension) ExtensionException(org.xwiki.extension.ExtensionException) ExtensionDependency(org.xwiki.extension.ExtensionDependency)

Aggregations

ExtensionDependency (org.xwiki.extension.ExtensionDependency)5 InstalledExtension (org.xwiki.extension.InstalledExtension)3 ArrayList (java.util.ArrayList)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 List (java.util.List)1 DefaultExtensionDependency (org.xwiki.extension.DefaultExtensionDependency)1 ExtensionException (org.xwiki.extension.ExtensionException)1 ExtensionInstallFailedEvent (org.xwiki.extension.event.ExtensionInstallFailedEvent)1 ExtensionInstalledEvent (org.xwiki.extension.event.ExtensionInstalledEvent)1 ExtensionInstallingEvent (org.xwiki.extension.event.ExtensionInstallingEvent)1 DefaultVersion (org.xwiki.extension.version.internal.DefaultVersion)1 DefaultVersionConstraint (org.xwiki.extension.version.internal.DefaultVersionConstraint)1