From 6d0a3b330c48cdb1d6112bcf001bed3f11ed4094 Mon Sep 17 00:00:00 2001 From: "Jan N. Klug" Date: Wed, 16 Oct 2024 08:45:51 +0200 Subject: [PATCH] Improve marketplace remote handling Signed-off-by: Jan N. Klug --- .../AbstractRemoteAddonService.java | 26 +++++++++---------- .../CommunityMarketplaceAddonService.java | 2 +- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/AbstractRemoteAddonService.java b/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/AbstractRemoteAddonService.java index da7bffa5c3f..dc4babf0849 100644 --- a/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/AbstractRemoteAddonService.java +++ b/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/AbstractRemoteAddonService.java @@ -15,7 +15,6 @@ import static org.openhab.core.common.ThreadPoolManager.THREAD_POOL_NAME_COMMON; import java.io.IOException; -import java.net.URI; import java.time.Duration; import java.util.ArrayList; import java.util.Comparator; @@ -95,7 +94,7 @@ public abstract class AbstractRemoteAddonService implements AddonService { this::getRemoteAddons); protected final AddonInfoRegistry addonInfoRegistry; protected List cachedAddons = List.of(); - protected List installedAddons = List.of(); + protected List installedAddonIds = List.of(); private final Logger logger = LoggerFactory.getLogger(AbstractRemoteAddonService.class); private final ScheduledExecutorService scheduler = ThreadPoolManager.getScheduledPool(THREAD_POOL_NAME_COMMON); @@ -137,7 +136,10 @@ public void refreshSource() { // this is safe, because the {@link AddonHandler}s only report ready when they installed everything from the // cache try { - installedAddonStorage.stream().map(this::convertFromStorage).peek(this::setInstalled).forEach(addons::add); + installedAddonStorage.stream().map(this::convertFromStorage).forEach(addon -> { + setInstalled(addon); + addons.add(addon); + }); } catch (JsonSyntaxException e) { List.copyOf(installedAddonStorage.getKeys()).forEach(installedAddonStorage::remove); logger.error( @@ -152,13 +154,15 @@ public void refreshSource() { addons.removeIf(addon -> missingAddons.contains(addon.getUid())); // create lookup list to make sure installed addons take precedence - List installedAddons = addons.stream().map(Addon::getUid).toList(); + List currentAddonIds = addons.stream().map(Addon::getUid).toList(); // get the remote addons if (remoteEnabled()) { List remoteAddons = Objects.requireNonNullElse(cachedRemoteAddons.getValue(), List.of()); - remoteAddons.stream().filter(a -> !installedAddons.contains(a.getUid())).peek(this::setInstalled) - .forEach(addons::add); + remoteAddons.stream().filter(a -> !currentAddonIds.contains(a.getUid())).forEach(addon -> { + setInstalled(addon); + addons.add(addon); + }); } // remove incompatible add-ons if not enabled @@ -175,7 +179,7 @@ public void refreshSource() { } cachedAddons = addons; - this.installedAddons = installedAddons; + this.installedAddonIds = currentAddonIds; if (!missingAddons.isEmpty()) { logger.info("Re-installing missing add-ons from remote repository: {}", missingAddons); @@ -222,9 +226,6 @@ public List getAddons(@Nullable Locale locale) { return cachedAddons; } - @Override - public abstract @Nullable Addon getAddon(String id, @Nullable Locale locale); - @Override public List getTypes(@Nullable Locale locale) { return AddonType.DEFAULT_TYPES; @@ -244,6 +245,7 @@ public void install(String id) { handler.install(addon); addon.setInstalled(true); installedAddonStorage.put(id, gson.toJson(addon)); + cachedRemoteAddons.invalidateValue(); refreshSource(); postInstalledEvent(addon.getUid()); } catch (MarketplaceHandlerException e) { @@ -271,6 +273,7 @@ public void uninstall(String id) { try { handler.uninstall(addon); installedAddonStorage.remove(id); + cachedRemoteAddons.invalidateValue(); refreshSource(); postUninstalledEvent(addon.getUid()); } catch (MarketplaceHandlerException e) { @@ -286,9 +289,6 @@ public void uninstall(String id) { postFailureEvent(id, "Add-on can't be uninstalled because there is no handler for it."); } - @Override - public abstract @Nullable String getAddonId(URI addonURI); - /** * check if remote services are enabled * diff --git a/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityMarketplaceAddonService.java b/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityMarketplaceAddonService.java index 4ab130f427f..3d791d447f8 100644 --- a/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityMarketplaceAddonService.java +++ b/bundles/org.openhab.core.addon.marketplace/src/main/java/org/openhab/core/addon/marketplace/internal/community/CommunityMarketplaceAddonService.java @@ -205,7 +205,7 @@ protected List getRemoteAddons() { // check if it is an installed add-on (cachedAddons also contains possibly incomplete results from the remote // side, we need to retrieve them from Discourse) - if (installedAddons.contains(queryId)) { + if (installedAddonIds.contains(queryId)) { return cachedAddons.stream().filter(e -> queryId.equals(e.getUid())).findAny().orElse(null); }