Skip to content

Commit

Permalink
[marketplace] avoid collisions with multiple AddonProviders (openhab#…
Browse files Browse the repository at this point in the history
…2564)

* [marketplace] avoid collisions with multiple AddonProviders

The current implementation could result in collision of file-path and failed in determining the installation status if other AddonProviders re-use the marketplace addon handlers.

Signed-off-by: Jan N. Klug <[email protected]>
  • Loading branch information
J-N-K authored Nov 23, 2021
1 parent 66c391a commit 3e8b664
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.openhab.core.addon.Addon;
import org.openhab.core.addon.marketplace.MarketplaceAddonHandler;
import org.openhab.core.addon.marketplace.MarketplaceHandlerException;
import org.openhab.core.util.UIDUtils;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
Expand Down Expand Up @@ -169,7 +170,7 @@ private void installFromCache(String addonId) throws MarketplaceHandlerException
private void ensureCachedKarsAreInstalled() {
try {
if (Files.isDirectory(KAR_CACHE_PATH)) {
Files.list(KAR_CACHE_PATH).filter(Files::isDirectory).map(p -> "marketplace:" + p.getFileName())
Files.list(KAR_CACHE_PATH).filter(Files::isDirectory).map(this::addonIdFromPath)
.filter(addonId -> !isInstalled(addonId)).forEach(addonId -> {
logger.info("Reinstalling missing marketplace KAR: {}", addonId);
try {
Expand All @@ -184,7 +185,14 @@ private void ensureCachedKarsAreInstalled() {
}
}

private String addonIdFromPath(Path path) {
String pathName = UIDUtils.decode(path.getFileName().toString());
return pathName.contains(":") ? pathName : "marketplace:";
}

private Path getAddonCacheDirectory(String addonId) {
return KAR_CACHE_PATH.resolve(addonId.replace("marketplace:", ""));
String dir = addonId.startsWith("marketplace:") ? addonId.replace("marketplace:", "")
: UIDUtils.encode(addonId);
return KAR_CACHE_PATH.resolve(dir);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.OpenHAB;
import org.openhab.core.util.UIDUtils;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
Expand Down Expand Up @@ -148,7 +149,7 @@ protected void uninstallBundle(BundleContext bundleContext, String addonId) thro
protected void ensureCachedBundlesAreInstalled(BundleContext bundleContext) {
try {
if (Files.isDirectory(BUNDLE_CACHE_PATH)) {
Files.list(BUNDLE_CACHE_PATH).filter(Files::isDirectory).map(p -> "marketplace:" + p.getFileName())
Files.list(BUNDLE_CACHE_PATH).filter(Files::isDirectory).map(this::addonIdFromPath)
.filter(addonId -> !isBundleInstalled(bundleContext, addonId)).forEach(addonId -> {
logger.info("Reinstalling missing marketplace bundle: {}", addonId);
try {
Expand All @@ -163,7 +164,14 @@ protected void ensureCachedBundlesAreInstalled(BundleContext bundleContext) {
}
}

private String addonIdFromPath(Path path) {
String pathName = UIDUtils.decode(path.getFileName().toString());
return pathName.contains(":") ? pathName : "marketplace:";
}

private Path getAddonCacheDirectory(String addonId) {
return BUNDLE_CACHE_PATH.resolve(addonId.replace("marketplace:", ""));
String dir = addonId.startsWith("marketplace:") ? addonId.replace("marketplace:", "")
: UIDUtils.encode(addonId);
return BUNDLE_CACHE_PATH.resolve(dir);
}
}

0 comments on commit 3e8b664

Please sign in to comment.