Improve marketplace remote handling

Signed-off-by: Jan N. Klug <github@klug.nrw>
This commit is contained in:
Jan N. Klug 2024-10-16 08:45:51 +02:00 committed by Wouter Born
parent af35487155
commit 6d0a3b330c
2 changed files with 14 additions and 14 deletions

View File

@ -15,7 +15,6 @@ package org.openhab.core.addon.marketplace;
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<Addon> cachedAddons = List.of();
protected List<String> installedAddons = List.of();
protected List<String> 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 abstract class AbstractRemoteAddonService implements AddonService {
// 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 abstract class AbstractRemoteAddonService implements AddonService {
addons.removeIf(addon -> missingAddons.contains(addon.getUid()));
// create lookup list to make sure installed addons take precedence
List<String> installedAddons = addons.stream().map(Addon::getUid).toList();
List<String> currentAddonIds = addons.stream().map(Addon::getUid).toList();
// get the remote addons
if (remoteEnabled()) {
List<Addon> 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 abstract class AbstractRemoteAddonService implements AddonService {
}
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 abstract class AbstractRemoteAddonService implements AddonService {
return cachedAddons;
}
@Override
public abstract @Nullable Addon getAddon(String id, @Nullable Locale locale);
@Override
public List<AddonType> getTypes(@Nullable Locale locale) {
return AddonType.DEFAULT_TYPES;
@ -244,6 +245,7 @@ public abstract class AbstractRemoteAddonService implements AddonService {
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 abstract class AbstractRemoteAddonService implements AddonService {
try {
handler.uninstall(addon);
installedAddonStorage.remove(id);
cachedRemoteAddons.invalidateValue();
refreshSource();
postUninstalledEvent(addon.getUid());
} catch (MarketplaceHandlerException e) {
@ -286,9 +289,6 @@ public abstract class AbstractRemoteAddonService implements AddonService {
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
*

View File

@ -205,7 +205,7 @@ public class CommunityMarketplaceAddonService extends AbstractRemoteAddonService
// 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);
}