Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve marketplace add-on handling #4019

Merged
merged 1 commit into from
Jan 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
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;
Expand All @@ -25,6 +27,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
Expand All @@ -36,6 +39,7 @@
import org.openhab.core.addon.AddonService;
import org.openhab.core.addon.AddonType;
import org.openhab.core.cache.ExpiringCache;
import org.openhab.core.common.ThreadPoolManager;
import org.openhab.core.config.core.ConfigParser;
import org.openhab.core.events.Event;
import org.openhab.core.events.EventPublisher;
Expand Down Expand Up @@ -82,6 +86,7 @@ public abstract class AbstractRemoteAddonService implements AddonService {
protected List<String> installedAddons = List.of();

private final Logger logger = LoggerFactory.getLogger(AbstractRemoteAddonService.class);
private final ScheduledExecutorService scheduler = ThreadPoolManager.getScheduledPool(THREAD_POOL_NAME_COMMON);

protected AbstractRemoteAddonService(EventPublisher eventPublisher, ConfigurationAdmin configurationAdmin,
StorageService storageService, AddonInfoRegistry addonInfoRegistry, String servicePid) {
Expand Down Expand Up @@ -113,9 +118,14 @@ public void refreshSource() {
getClass());
return;
}

List<Addon> addons = new ArrayList<>();

// retrieve add-ons that should be available from storage and check if they are really installed
// this is safe, because the {@link AddonHandler}s only report ready when they installed everything from the
// cache
try {
installedAddonStorage.stream().map(this::convertFromStorage).forEach(addons::add);
installedAddonStorage.stream().map(this::convertFromStorage).peek(this::setInstalled).forEach(addons::add);
} catch (JsonSyntaxException e) {
List.copyOf(installedAddonStorage.getKeys()).forEach(installedAddonStorage::remove);
logger.error(
Expand All @@ -124,18 +134,21 @@ public void refreshSource() {
refreshSource();
}

// remove not installed add-ons from the add-ons list, but remember their UIDs to re-install them
List<String> missingAddons = addons.stream().filter(addon -> !addon.isInstalled()).map(Addon::getUid).toList();
missingAddons.forEach(installedAddonStorage::remove);
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();

// get the remote addons
if (remoteEnabled()) {
List<Addon> remoteAddons = Objects.requireNonNullElse(cachedRemoteAddons.getValue(), List.of());
remoteAddons.stream().filter(a -> !installedAddons.contains(a.getUid())).forEach(addons::add);
remoteAddons.stream().filter(a -> !installedAddons.contains(a.getUid())).peek(this::setInstalled)
.forEach(addons::add);
}

// check real installation status based on handlers
addons.forEach(
addon -> addon.setInstalled(addonHandlers.stream().anyMatch(h -> h.isInstalled(addon.getUid()))));

// remove incompatible add-ons if not enabled
boolean showIncompatible = includeIncompatible();
addons.removeIf(addon -> !addon.isInstalled() && !addon.getCompatible() && !showIncompatible);
Expand All @@ -151,6 +164,15 @@ public void refreshSource() {

cachedAddons = addons;
this.installedAddons = installedAddons;

if (!missingAddons.isEmpty()) {
logger.info("Re-installing missing add-ons from remote repository: {}", missingAddons);
scheduler.execute(() -> missingAddons.forEach(this::install));
}
}

private void setInstalled(Addon addon) {
addon.setInstalled(addonHandlers.stream().anyMatch(h -> h.isInstalled(addon.getUid())));
}

/**
Expand Down Expand Up @@ -199,52 +221,57 @@ public List<AddonType> getTypes(@Nullable Locale locale) {
@Override
public void install(String id) {
Addon addon = getAddon(id, null);
if (addon != null) {
for (MarketplaceAddonHandler handler : addonHandlers) {
if (handler.supports(addon.getType(), addon.getContentType())) {
if (!handler.isInstalled(addon.getUid())) {
try {
handler.install(addon);
installedAddonStorage.put(id, gson.toJson(addon));
refreshSource();
postInstalledEvent(addon.getUid());
} catch (MarketplaceHandlerException e) {
postFailureEvent(addon.getUid(), e.getMessage());
}
} else {
postFailureEvent(addon.getUid(), "Add-on is already installed.");
if (addon == null) {
postFailureEvent(id, "Add-on can't be installed because it is not known.");
return;
}
for (MarketplaceAddonHandler handler : addonHandlers) {
if (handler.supports(addon.getType(), addon.getContentType())) {
if (!handler.isInstalled(addon.getUid())) {
try {
handler.install(addon);
addon.setInstalled(true);
installedAddonStorage.put(id, gson.toJson(addon));
refreshSource();
postInstalledEvent(addon.getUid());
} catch (MarketplaceHandlerException e) {
postFailureEvent(addon.getUid(), e.getMessage());
}
return;
} else {
postFailureEvent(addon.getUid(), "Add-on is already installed.");
}
return;
}
}
postFailureEvent(id, "Add-on not known.");
postFailureEvent(id, "Add-on can't be installed because there is no handler for it.");
}

@Override
public void uninstall(String id) {
Addon addon = getAddon(id, null);
if (addon != null) {
for (MarketplaceAddonHandler handler : addonHandlers) {
if (handler.supports(addon.getType(), addon.getContentType())) {
if (handler.isInstalled(addon.getUid())) {
try {
handler.uninstall(addon);
installedAddonStorage.remove(id);
refreshSource();
postUninstalledEvent(addon.getUid());
} catch (MarketplaceHandlerException e) {
postFailureEvent(addon.getUid(), e.getMessage());
}
} else {
if (addon == null) {
postFailureEvent(id, "Add-on can't be uninstalled because it is not known.");
return;
}
for (MarketplaceAddonHandler handler : addonHandlers) {
if (handler.supports(addon.getType(), addon.getContentType())) {
if (handler.isInstalled(addon.getUid())) {
try {
handler.uninstall(addon);
installedAddonStorage.remove(id);
postFailureEvent(addon.getUid(), "Add-on is not installed.");
refreshSource();
postUninstalledEvent(addon.getUid());
} catch (MarketplaceHandlerException e) {
postFailureEvent(addon.getUid(), e.getMessage());
}
return;
} else {
installedAddonStorage.remove(id);
postFailureEvent(addon.getUid(), "Add-on is not installed.");
}
return;
}
}
postFailureEvent(id, "Add-on not known.");
postFailureEvent(id, "Add-on can't be uninstalled because there is no handler for it.");
}

@Override
Expand Down