-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Notify the administrators when one of the installed licensed applica…
…tions has a new version available #110 (#126) * add NewExtensionVersionAvailable event and a job scheduler to send this event every week * avoid multiplying notifications by adding an object of NewVersionNotificationClass each time a new notification is fired * add possibility for users to access directly the upgrade page corresponding to a given notification * update unit tests
- Loading branch information
1 parent
ca84155
commit 53cf696
Showing
20 changed files
with
1,420 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
.../src/main/java/com/xwiki/licensing/internal/upgrades/NewExtensionVersionAvailableJob.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* See the NOTICE file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
package com.xwiki.licensing.internal.upgrades; | ||
|
||
import org.quartz.Job; | ||
import org.quartz.JobExecutionContext; | ||
import org.quartz.JobExecutionException; | ||
|
||
import com.xpn.xwiki.plugin.scheduler.AbstractJob; | ||
import com.xpn.xwiki.web.Utils; | ||
|
||
/** | ||
* Scheduler job that sends a notification when a new version is available for a licensed extension. | ||
* | ||
* @since 1.23 | ||
* @version $Id$ | ||
*/ | ||
public class NewExtensionVersionAvailableJob extends AbstractJob implements Job | ||
{ | ||
@SuppressWarnings("deprecation") | ||
@Override | ||
protected void executeJob(JobExecutionContext jobContext) throws JobExecutionException | ||
{ | ||
NewExtensionVersionAvailableManager newVersionManager = | ||
Utils.getComponent(NewExtensionVersionAvailableManager.class); | ||
newVersionManager.checkLicensedExtensionsAvailableVersions(); | ||
} | ||
} |
133 changes: 133 additions & 0 deletions
133
.../main/java/com/xwiki/licensing/internal/upgrades/NewExtensionVersionAvailableManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
* See the NOTICE file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
package com.xwiki.licensing.internal.upgrades; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
|
||
import org.apache.commons.lang3.exception.ExceptionUtils; | ||
import org.slf4j.Logger; | ||
import org.xwiki.component.annotation.Component; | ||
import org.xwiki.extension.ExtensionId; | ||
import org.xwiki.extension.InstalledExtension; | ||
import org.xwiki.extension.repository.InstalledExtensionRepository; | ||
import org.xwiki.extension.version.Version; | ||
import org.xwiki.observation.ObservationManager; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.xwiki.licensing.LicensedExtensionManager; | ||
import com.xwiki.licensing.LicensingConfiguration; | ||
import com.xwiki.licensing.internal.upgrades.notifications.newVersion.NewExtensionVersionAvailableEvent; | ||
|
||
/** | ||
* Check licensed extensions for new available versions and send a notification, without sending multiple notifications | ||
* for the same version. | ||
* | ||
* @version $Id$ | ||
* @since 1.23 | ||
*/ | ||
@Component(roles = NewExtensionVersionAvailableManager.class) | ||
@Singleton | ||
public class NewExtensionVersionAvailableManager | ||
{ | ||
@Inject | ||
private InstalledExtensionRepository installedRepository; | ||
|
||
@Inject | ||
private UpgradeExtensionHandler upgradeExtensionHandler; | ||
|
||
@Inject | ||
private LicensedExtensionManager licensedExtensionManager; | ||
|
||
@Inject | ||
private ObservationManager observationManager; | ||
|
||
@Inject | ||
private LicensingConfiguration licensingConfig; | ||
|
||
@Inject | ||
private Logger logger; | ||
|
||
@Inject | ||
private NewVersionNotificationManager newVersionNotificationManager; | ||
|
||
/** | ||
* Notify the administrators when one of the installed licensed applications has a new version available. Do nothing | ||
* for extensions that have auto upgrades enabled. | ||
*/ | ||
public void checkLicensedExtensionsAvailableVersions() | ||
{ | ||
List<String> allowlist = licensingConfig.getAutoUpgradeAllowList(); | ||
|
||
for (ExtensionId extensionId : licensedExtensionManager.getLicensedExtensions()) { | ||
if (allowlist.contains(extensionId.getId())) { | ||
continue; | ||
} | ||
|
||
InstalledExtension installedExtension = installedRepository.getInstalledExtension(extensionId); | ||
Collection<String> namespaces = installedExtension.getNamespaces(); | ||
if (namespaces == null) { | ||
notifyExtensionVersionAvailable(installedExtension.getId(), null); | ||
} else { | ||
for (String namespace : installedExtension.getNamespaces()) { | ||
notifyExtensionVersionAvailable(installedExtension.getId(), namespace); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void notifyExtensionVersionAvailable(ExtensionId extensionId, String namespace) | ||
{ | ||
InstalledExtension installedExtension = | ||
installedRepository.getInstalledExtension(extensionId.getId(), namespace); | ||
// Get the list of versions that can be installed, with the first one being the most recent. | ||
List<Version> installableVersions = upgradeExtensionHandler.getInstallableVersions(installedExtension.getId()); | ||
if (installableVersions.isEmpty()) { | ||
return; | ||
} | ||
|
||
try { | ||
String namespaceName = namespace != null ? namespace : "root"; | ||
if (!this.newVersionNotificationManager.isNotificationAlreadySent(extensionId.getId(), namespaceName, | ||
installableVersions.get(0).getValue())) | ||
{ | ||
Map<String, String> extensionInfo = new HashMap<>(); | ||
extensionInfo.put("extensionName", installedExtension.getName()); | ||
extensionInfo.put("namespace", namespaceName); | ||
extensionInfo.put("version", installableVersions.get(0).getValue()); | ||
|
||
this.observationManager.notify(new NewExtensionVersionAvailableEvent( | ||
new ExtensionId(extensionId.getId(), installableVersions.get(0)), namespace), | ||
extensionId.getId(), (new ObjectMapper()).writeValueAsString(extensionInfo)); | ||
this.newVersionNotificationManager.markNotificationAsSent(extensionId.getId(), namespaceName, | ||
installableVersions.get(0).getValue()); | ||
} | ||
} catch (JsonProcessingException e) { | ||
this.logger.warn("Failed to send a NewExtensionVersionAvailableEvent for [{}]. Root cause is [{}]", | ||
extensionId.getId(), ExceptionUtils.getRootCauseMessage(e)); | ||
} | ||
} | ||
} |
Oops, something went wrong.