Search in sources :

Example 1 with Extension

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

the class RepositoryManager method importExtension.

public DocumentReference importExtension(String extensionId, ExtensionRepository repository, Type type) throws QueryException, XWikiException, ResolveException {
    TreeMap<Version, String> versions = new TreeMap<Version, String>();
    Version lastVersion = getVersions(extensionId, repository, type, versions);
    if (lastVersion == null) {
        throw new ExtensionNotFoundException("Can't find any version for the extension [" + extensionId + "] on repository [" + repository + "]");
    } else if (versions.isEmpty()) {
        // If no valid version import the last version
        versions.put(lastVersion, extensionId);
    } else {
        // Select the last valid version
        lastVersion = versions.lastKey();
    }
    Extension extension = repository.resolve(new ExtensionId(extensionId, lastVersion));
    // Get former ids versions
    Collection<ExtensionId> features = extension.getExtensionFeatures();
    for (ExtensionId feature : features) {
        try {
            getVersions(feature.getId(), repository, type, versions);
        } catch (ResolveException e) {
        // Ignore
        }
    }
    XWikiContext xcontext = this.xcontextProvider.get();
    boolean needSave = false;
    XWikiDocument document = getExistingExtensionDocumentById(extensionId);
    if (document == null) {
        // Create document
        document = xcontext.getWiki().getDocument(new DocumentReference(xcontext.getWikiId(), Arrays.asList("Extension", extension.getName()), "WebHome"), xcontext);
        for (int i = 1; !document.isNew(); ++i) {
            document = xcontext.getWiki().getDocument(new DocumentReference(xcontext.getWikiId(), Arrays.asList("Extension", extension.getName() + ' ' + i), "WebHome"), xcontext);
        }
        document.readFromTemplate(this.currentResolver.resolve(XWikiRepositoryModel.EXTENSION_TEMPLATEREFERENCE), xcontext);
        needSave = true;
    }
    // Update document
    BaseObject extensionObject = document.getXObject(XWikiRepositoryModel.EXTENSION_CLASSREFERENCE);
    if (extensionObject == null) {
        extensionObject = document.newXObject(XWikiRepositoryModel.EXTENSION_CLASSREFERENCE, xcontext);
        needSave = true;
    }
    if (!StringUtils.equals(extensionId, getValue(extensionObject, XWikiRepositoryModel.PROP_EXTENSION_ID, (String) null))) {
        extensionObject.set(XWikiRepositoryModel.PROP_EXTENSION_ID, extensionId, xcontext);
        needSave = true;
    }
    // Update extension informations
    needSave |= updateExtension(extension, extensionObject, xcontext);
    // Proxy marker
    BaseObject extensionProxyObject = document.getXObject(XWikiRepositoryModel.EXTENSIONPROXY_CLASSREFERENCE);
    if (extensionProxyObject == null) {
        extensionProxyObject = document.newXObject(XWikiRepositoryModel.EXTENSIONPROXY_CLASSREFERENCE, xcontext);
        extensionProxyObject.setIntValue(XWikiRepositoryModel.PROP_PROXY_AUTOUPDATE, 1);
        needSave = true;
    }
    needSave |= update(extensionProxyObject, XWikiRepositoryModel.PROP_PROXY_REPOSITORYID, repository.getDescriptor().getId());
    needSave |= update(extensionProxyObject, XWikiRepositoryModel.PROP_PROXY_REPOSITORYTYPE, repository.getDescriptor().getType());
    needSave |= update(extensionProxyObject, XWikiRepositoryModel.PROP_PROXY_REPOSITORYURI, repository.getDescriptor().getURI().toString());
    // Remove unexisting versions
    Set<String> validVersions = new HashSet<String>();
    List<BaseObject> versionObjects = document.getXObjects(XWikiRepositoryModel.EXTENSIONVERSION_CLASSREFERENCE);
    if (versionObjects != null) {
        for (BaseObject versionObject : versionObjects) {
            if (versionObject != null) {
                String version = getValue(versionObject, XWikiRepositoryModel.PROP_VERSION_VERSION);
                if (StringUtils.isBlank(version) || (isVersionProxyingEnabled(document) && !new DefaultVersion(version).equals(extension.getId().getVersion()))) {
                    // Empty version OR old versions should be proxied
                    document.removeXObject(versionObject);
                    needSave = true;
                } else {
                    if (!versions.containsKey(new DefaultVersion(version))) {
                        // The version does not exist on remote repository
                        if (!isVersionValid(document, versionObject, xcontext)) {
                            // The version is invalid, removing it to not make the whole extension invalid
                            document.removeXObject(versionObject);
                            needSave = true;
                        } else {
                            // The version is valid, lets keep it
                            validVersions.add(version);
                        }
                    } else {
                        // This version exist on remote repository
                        validVersions.add(version);
                    }
                }
            }
        }
    }
    List<BaseObject> dependencyObjects = document.getXObjects(XWikiRepositoryModel.EXTENSIONDEPENDENCY_CLASSREFERENCE);
    if (dependencyObjects != null) {
        for (BaseObject dependencyObject : dependencyObjects) {
            if (dependencyObject != null) {
                String version = getValue(dependencyObject, XWikiRepositoryModel.PROP_DEPENDENCY_EXTENSIONVERSION);
                if (!validVersions.contains(version)) {
                    // The version is invalid, removing it to not make the whole extension invalid
                    document.removeXObject(dependencyObject);
                    needSave = true;
                }
            }
        }
    }
    for (Map.Entry<Version, String> entry : versions.entrySet()) {
        Version version = entry.getKey();
        String id = entry.getValue();
        try {
            Extension versionExtension;
            if (version.equals(extension.getId().getVersion())) {
                versionExtension = extension;
            } else if (isVersionProxyingEnabled(document)) {
                continue;
            } else {
                versionExtension = repository.resolve(new ExtensionId(id, version));
            }
            // Update version related informations
            needSave |= updateExtensionVersion(document, versionExtension);
        } catch (Exception e) {
            this.logger.error("Failed to resolve extension with id [" + id + "] and version [" + version + "] on repository [" + repository + "]", e);
        }
    }
    if (needSave) {
        document.setAuthorReference(xcontext.getUserReference());
        if (document.isNew()) {
            document.setContentAuthorReference(xcontext.getUserReference());
            document.setCreatorReference(xcontext.getUserReference());
        }
        xcontext.getWiki().saveDocument(document, "Imported extension [" + extensionId + "] from repository [" + repository.getDescriptor() + "]", true, xcontext);
    }
    return document.getDocumentReference();
}
Also used : ExtensionNotFoundException(org.xwiki.extension.ExtensionNotFoundException) DefaultVersion(org.xwiki.extension.version.internal.DefaultVersion) XWikiContext(com.xpn.xwiki.XWikiContext) ExtensionId(org.xwiki.extension.ExtensionId) TreeMap(java.util.TreeMap) DefaultVersionConstraint(org.xwiki.extension.version.internal.DefaultVersionConstraint) XWikiException(com.xpn.xwiki.XWikiException) ComponentLifecycleException(org.xwiki.component.manager.ComponentLifecycleException) InitializationException(org.xwiki.component.phase.InitializationException) ExtensionNotFoundException(org.xwiki.extension.ExtensionNotFoundException) WebApplicationException(javax.ws.rs.WebApplicationException) QueryException(org.xwiki.query.QueryException) ResolveException(org.xwiki.extension.ResolveException) CacheException(org.xwiki.cache.CacheException) BaseObject(com.xpn.xwiki.objects.BaseObject) Extension(org.xwiki.extension.Extension) ResolveException(org.xwiki.extension.ResolveException) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) Version(org.xwiki.extension.version.Version) DefaultVersion(org.xwiki.extension.version.internal.DefaultVersion) Map(java.util.Map) TreeMap(java.util.TreeMap) DocumentReference(org.xwiki.model.reference.DocumentReference) HashSet(java.util.HashSet)

Example 2 with Extension

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

the class RepairXarJob method getLocalXARExtension.

/**
 * @param extensionId the extension unique identifier
 * @return the stored local extension
 * @throws InstallException failed to store extension
 */
private LocalExtension getLocalXARExtension(ExtensionId extensionId) throws InstallException {
    LocalExtension localExtension = this.localRepository.getLocalExtension(extensionId);
    if (localExtension == null) {
        this.progressManager.pushLevelProgress(2, this);
        try {
            this.progressManager.startStep(this);
            Extension extension = this.repositoryManager.resolve(extensionId);
            this.progressManager.endStep(this);
            this.progressManager.startStep(this);
            if (extension.getType().equals(XarExtensionHandler.TYPE)) {
                localExtension = this.localExtensionRepository.storeExtension(extension);
            }
            this.progressManager.endStep(this);
        } catch (ResolveException e) {
            throw new InstallException("Failed to find extension", e);
        } catch (LocalExtensionRepositoryException e) {
            throw new InstallException("Failed save extension in local repository", e);
        } finally {
            this.progressManager.popLevelProgress(this);
        }
    } else if (!localExtension.getType().equals(XarExtensionHandler.TYPE)) {
        localExtension = null;
    }
    return localExtension;
}
Also used : Extension(org.xwiki.extension.Extension) InstalledExtension(org.xwiki.extension.InstalledExtension) LocalExtension(org.xwiki.extension.LocalExtension) ResolveException(org.xwiki.extension.ResolveException) LocalExtensionRepositoryException(org.xwiki.extension.repository.LocalExtensionRepositoryException) LocalExtension(org.xwiki.extension.LocalExtension) InstallException(org.xwiki.extension.InstallException)

Example 3 with Extension

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

the class ExtensionManagerScriptService method resolve.

/**
 * Get the extension handler corresponding to the given extension ID and version. The returned handler can be used
 * to get more information about the extension, such as the authors, an extension description, its license...
 *
 * @param id the extension id or provided feature (virtual extension) of the extension to resolve
 * @param version the specific version to resolve
 * @return the read-only handler corresponding to the requested extension, or {@code null} if the extension couldn't
 *         be resolved, in which case {@link #getLastError()} contains the failure reason
 */
public Extension resolve(String id, String version) {
    setError(null);
    Extension extension = null;
    try {
        extension = safe(this.extensionManager.resolveExtension(new ExtensionId(id, version)));
    } catch (Exception e) {
        setError(e);
    }
    return extension;
}
Also used : CoreExtension(org.xwiki.extension.CoreExtension) Extension(org.xwiki.extension.Extension) InstalledExtension(org.xwiki.extension.InstalledExtension) LocalExtension(org.xwiki.extension.LocalExtension) ExtensionId(org.xwiki.extension.ExtensionId) SearchException(org.xwiki.extension.repository.search.SearchException) JobException(org.xwiki.job.JobException)

Example 4 with Extension

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

the class ExtensionManagerScriptService method resolve.

/**
 * Search the provided extension as a dependency of another extension among all repositories including core and
 * local repositories.
 * <p>
 * The search is done in the following order:
 * <ul>
 * <li>Is it a core extension ?</li>
 * <li>Is it a local extension ?</li>
 * <li>Is this feature installed in current namespace or parent ?</li>
 * <li>Is it a remote extension in one of the configured remote repositories ?</li>
 * </ul>
 * The first one found is returned.
 *
 * @param extensionDependency the extension dependency to resolve
 * @param namespace the namespace where to search for the dependency
 * @return the read-only handler corresponding to the requested extension, or {@code null} if the extension couldn't
 *         be resolved, in which case {@link #getLastError()} contains the failure reason
 * @since 5.3M1
 */
public Extension resolve(ExtensionDependency extensionDependency, String namespace) {
    setError(null);
    Extension extension = null;
    try {
        extension = safe(this.extensionManager.resolveExtension(extensionDependency, namespace));
    } catch (Exception e) {
        setError(e);
    }
    return extension;
}
Also used : CoreExtension(org.xwiki.extension.CoreExtension) Extension(org.xwiki.extension.Extension) InstalledExtension(org.xwiki.extension.InstalledExtension) LocalExtension(org.xwiki.extension.LocalExtension) SearchException(org.xwiki.extension.repository.search.SearchException) JobException(org.xwiki.job.JobException)

Example 5 with Extension

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

the class FlavorSearchJob method findValidVersion.

private Extension findValidVersion(String flavorId, String namespace, List<Version> versionList) {
    this.progressManager.pushLevelProgress(versionList.size(), flavorId);
    try {
        for (ListIterator<Version> it = versionList.listIterator(versionList.size()); it.hasPrevious(); ) {
            this.progressManager.startStep(flavorId);
            Version version = it.previous();
            Extension extension = tryInstallExtension(new ExtensionId(flavorId, version), namespace);
            this.progressManager.endStep(flavorId);
            if (extension != null) {
                return extension;
            }
        }
    } finally {
        this.progressManager.popLevelProgress(flavorId);
    }
    return null;
}
Also used : Extension(org.xwiki.extension.Extension) Version(org.xwiki.extension.version.Version) ExtensionId(org.xwiki.extension.ExtensionId)

Aggregations

Extension (org.xwiki.extension.Extension)12 ExtensionId (org.xwiki.extension.ExtensionId)7 LocalExtension (org.xwiki.extension.LocalExtension)6 InstalledExtension (org.xwiki.extension.InstalledExtension)5 ResolveException (org.xwiki.extension.ResolveException)5 XWikiException (com.xpn.xwiki.XWikiException)3 IOException (java.io.IOException)3 CoreExtension (org.xwiki.extension.CoreExtension)3 SearchException (org.xwiki.extension.repository.search.SearchException)3 JobException (org.xwiki.job.JobException)3 QueryException (org.xwiki.query.QueryException)3 HashSet (java.util.HashSet)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 Version (org.xwiki.extension.version.Version)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 URISyntaxException (java.net.URISyntaxException)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1