Search in sources :

Example 1 with ExtensionInstalledEvent

use of org.xwiki.extension.event.ExtensionInstalledEvent 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 2 with ExtensionInstalledEvent

use of org.xwiki.extension.event.ExtensionInstalledEvent in project xwiki-platform by xwiki.

the class ImportTest method testImportExtension.

/**
 * Test the regular document import when the XAR is tagged as extension.
 *
 * @throws Exception
 */
public void testImportExtension() throws Exception {
    ExtensionId extensionId = new ExtensionId("test", "1.0");
    XWikiDocument doc1 = new XWikiDocument(new DocumentReference("Test", "Test", "DocImport"));
    doc1.setDefaultLanguage("en");
    byte[] zipFile = this.createZipFile(new XWikiDocument[] { doc1 }, new String[] { "ISO-8859-1" }, extensionId);
    // Store the extension in the local repository
    DefaultLocalExtension localExtension = new DefaultLocalExtension(null, extensionId, "xar");
    File file = File.createTempFile("temp", ".xar");
    FileUtils.writeByteArrayToFile(file, zipFile);
    localExtension.setFile(file);
    LocalExtensionRepository localeRepository = getComponentManager().getInstance(LocalExtensionRepository.class);
    localeRepository.storeExtension(localExtension);
    // Listen to extension installed event
    Mock extensionListener = mock(EventListener.class);
    extensionListener.stubs().method("getEvents").will(returnValue(Arrays.asList(new ExtensionInstalledEvent())));
    extensionListener.stubs().method("getName").will(returnValue("extension installed listener"));
    extensionListener.expects(once()).method("onEvent");
    ObservationManager observationManager = getComponentManager().getInstance(ObservationManager.class);
    observationManager.addListener((EventListener) extensionListener.proxy());
    // make sure no data is in the packager from the other tests run
    this.pack = new Package();
    // import and install this document
    this.pack.Import(zipFile, getContext());
    this.pack.install(getContext());
    // check if it is there
    XWikiDocument foundDocument = this.xwiki.getDocument(new DocumentReference("Test", "Test", "DocImport"), getContext());
    assertFalse(foundDocument.isNew());
    XWikiDocument nonExistingDocument = this.xwiki.getDocument(new DocumentReference("Test", "Test", "DocImportNonexisting"), getContext());
    assertTrue(nonExistingDocument.isNew());
    XWikiDocument foundTranslationDocument = foundDocument.getTranslatedDocument("fr", getContext());
    assertSame(foundDocument, foundTranslationDocument);
    XWikiDocument doc1Translation = new XWikiDocument(new DocumentReference("Test", "Test", "DocImport"));
    doc1Translation.setLanguage("fr");
    doc1Translation.setDefaultLanguage("en");
    this.xwiki.saveDocument(doc1Translation, getContext());
    foundTranslationDocument = foundDocument.getTranslatedDocument("fr", getContext());
    assertNotSame(foundDocument, foundTranslationDocument);
    // Check that the extension has been registered
    InstalledExtensionRepository installedExtensionRepository = getComponentManager().getInstance(InstalledExtensionRepository.class);
    assertNotNull(installedExtensionRepository.getInstalledExtension(extensionId));
    assertNotNull(installedExtensionRepository.getInstalledExtension(extensionId.getId(), "wiki:" + getContext().getWikiId()));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) LocalExtensionRepository(org.xwiki.extension.repository.LocalExtensionRepository) ExtensionId(org.xwiki.extension.ExtensionId) ObservationManager(org.xwiki.observation.ObservationManager) DefaultLocalExtension(org.xwiki.extension.repository.internal.local.DefaultLocalExtension) File(java.io.File) DocumentReference(org.xwiki.model.reference.DocumentReference) Mock(org.jmock.Mock) InstalledExtensionRepository(org.xwiki.extension.repository.InstalledExtensionRepository) ExtensionInstalledEvent(org.xwiki.extension.event.ExtensionInstalledEvent)

Example 3 with ExtensionInstalledEvent

use of org.xwiki.extension.event.ExtensionInstalledEvent 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)

Example 4 with ExtensionInstalledEvent

use of org.xwiki.extension.event.ExtensionInstalledEvent in project xwiki-platform by xwiki.

the class JARTranslationBundleFactoryTest method mockInstallExtension.

private void mockInstallExtension(ExtensionId extensionId, String namespace) throws ComponentLookupException {
    DefaultInstalledExtension installedExtension = mockInstalledExtension(extensionId, namespace);
    getObservationManager().notify(new ExtensionInstalledEvent(extensionId, namespace), installedExtension);
}
Also used : DefaultInstalledExtension(org.xwiki.extension.repository.internal.installed.DefaultInstalledExtension) ExtensionInstalledEvent(org.xwiki.extension.event.ExtensionInstalledEvent)

Example 5 with ExtensionInstalledEvent

use of org.xwiki.extension.event.ExtensionInstalledEvent in project xwiki-platform by xwiki.

the class JARTranslationBundleFactoryListener method onEvent.

@Override
public void onEvent(Event event, Object source, Object data) {
    ExtensionEvent extensionEvent = (ExtensionEvent) event;
    InstalledExtension extension = (InstalledExtension) source;
    if (extension.getType().equals(EXTENSION_TYPE)) {
        if (event instanceof ExtensionInstalledEvent) {
            extensionAdded(extension, extensionEvent.getNamespace());
        } else if (event instanceof ExtensionUninstalledEvent) {
            extensionDeleted(extension, extensionEvent.getNamespace());
        } else {
            extensionUpgraded(extension, (Collection<InstalledExtension>) data, extensionEvent.getNamespace());
        }
    }
}
Also used : InstalledExtension(org.xwiki.extension.InstalledExtension) ExtensionUninstalledEvent(org.xwiki.extension.event.ExtensionUninstalledEvent) Collection(java.util.Collection) ExtensionEvent(org.xwiki.extension.event.ExtensionEvent) ExtensionInstalledEvent(org.xwiki.extension.event.ExtensionInstalledEvent)

Aggregations

ExtensionInstalledEvent (org.xwiki.extension.event.ExtensionInstalledEvent)5 InstalledExtension (org.xwiki.extension.InstalledExtension)3 ExtensionId (org.xwiki.extension.ExtensionId)2 InstalledExtensionRepository (org.xwiki.extension.repository.InstalledExtensionRepository)2 LocalExtensionRepository (org.xwiki.extension.repository.LocalExtensionRepository)2 ObservationManager (org.xwiki.observation.ObservationManager)2 XWikiException (com.xpn.xwiki.XWikiException)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 File (java.io.File)1 IOException (java.io.IOException)1 Collection (java.util.Collection)1 DocumentException (org.dom4j.DocumentException)1 Mock (org.jmock.Mock)1 Extension (org.xwiki.extension.Extension)1 ExtensionDependency (org.xwiki.extension.ExtensionDependency)1 LocalExtension (org.xwiki.extension.LocalExtension)1 ResolveException (org.xwiki.extension.ResolveException)1 ExtensionEvent (org.xwiki.extension.event.ExtensionEvent)1 ExtensionInstallFailedEvent (org.xwiki.extension.event.ExtensionInstallFailedEvent)1 ExtensionInstallingEvent (org.xwiki.extension.event.ExtensionInstallingEvent)1