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

Use FeatureInstaller for suggestion finder installation #4188

Merged
merged 2 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,30 @@
public class AddonFinderConstants {

public static final String ADDON_SUGGESTION_FINDER = "-addon-suggestion-finder";
private static final String ADDON_SUGGESTION_FINDER_FEATURE = "openhab-core-config-discovery-addon-";

public static final String SERVICE_TYPE_IP = "ip";
public static final String CFG_FINDER_IP = "suggestionFinderIp";
public static final String SERVICE_NAME_IP = SERVICE_TYPE_IP + ADDON_SUGGESTION_FINDER;
public static final String FEATURE_IP = ADDON_SUGGESTION_FINDER_FEATURE + SERVICE_TYPE_IP;

public static final String SERVICE_TYPE_MDNS = "mdns";
public static final String CFG_FINDER_MDNS = "suggestionFinderMdns";
public static final String SERVICE_NAME_MDNS = SERVICE_TYPE_MDNS + ADDON_SUGGESTION_FINDER;
public static final String FEATURE_MDNS = ADDON_SUGGESTION_FINDER_FEATURE + SERVICE_TYPE_MDNS;

public static final String SERVICE_TYPE_UPNP = "upnp";
public static final String CFG_FINDER_UPNP = "suggestionFinderUpnp";
public static final String SERVICE_NAME_UPNP = SERVICE_TYPE_UPNP + ADDON_SUGGESTION_FINDER;
public static final String FEATURE_UPNP = ADDON_SUGGESTION_FINDER_FEATURE + SERVICE_TYPE_UPNP;

public static final String SERVICE_TYPE_USB = "usb";
public static final String CFG_FINDER_USB = "suggestionFinderUsb";
public static final String SERVICE_NAME_USB = SERVICE_TYPE_USB + ADDON_SUGGESTION_FINDER;
public static final String FEATURE_USB = ADDON_SUGGESTION_FINDER_FEATURE + SERVICE_TYPE_USB;

public static final List<String> SUGGESTION_FINDERS = List.of(SERVICE_NAME_IP, SERVICE_NAME_MDNS, SERVICE_NAME_UPNP,
SERVICE_NAME_USB);

public static final Map<String, String> SUGGESTION_FINDER_TYPES = Map.of(SERVICE_NAME_IP, SERVICE_TYPE_IP,
SERVICE_NAME_MDNS, SERVICE_TYPE_MDNS, SERVICE_NAME_UPNP, SERVICE_TYPE_UPNP, SERVICE_NAME_USB,
SERVICE_TYPE_USB);

public static final Map<String, String> SUGGESTION_FINDER_CONFIGS = Map.of(SERVICE_NAME_IP, CFG_FINDER_IP,
SERVICE_NAME_MDNS, CFG_FINDER_MDNS, SERVICE_NAME_UPNP, CFG_FINDER_UPNP, SERVICE_NAME_USB, CFG_FINDER_USB);

public static final Map<String, String> SUGGESTION_FINDER_FEATURES = Map.of(SERVICE_NAME_IP, FEATURE_IP,
SERVICE_NAME_MDNS, FEATURE_MDNS, SERVICE_NAME_UPNP, FEATURE_UPNP, SERVICE_NAME_USB, FEATURE_USB);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,15 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.eclipse.jdt.annotation.NonNullByDefault;
Expand Down Expand Up @@ -74,7 +72,7 @@ public class AddonSuggestionService implements AutoCloseable {
private @Nullable Map<String, Object> config;
private final ScheduledExecutorService scheduler;
private final Map<String, Boolean> baseFinderConfig = new ConcurrentHashMap<>();
private final List<Future<?>> tasks = new CopyOnWriteArrayList<>();
private final ScheduledFuture<?> cfgRefreshTask;

@Activate
public AddonSuggestionService(final @Reference ConfigurationAdmin configurationAdmin,
Expand All @@ -83,20 +81,18 @@ public AddonSuggestionService(final @Reference ConfigurationAdmin configurationA
this.localeProvider = localeProvider;

SUGGESTION_FINDERS.forEach(f -> baseFinderConfig.put(f, true));
modified(config);
changed();

// Changes to the configuration are expected to call the {@link modified} method. This works well when running
// in Eclipse. Running in Karaf, the method was not consistently called. Therefore regularly check for changes
// in configuration.
// This pattern and code was re-used from {@link org.openhab.core.karaf.internal.FeatureInstaller}
scheduler = ThreadPoolManager.getScheduledPool(ThreadPoolManager.THREAD_POOL_NAME_COMMON);
tasks.add(scheduler.scheduleWithFixedDelay(this::syncConfiguration, 1, 1, TimeUnit.MINUTES));
cfgRefreshTask = scheduler.scheduleWithFixedDelay(this::syncConfiguration, 1, 1, TimeUnit.MINUTES);
}

@Deactivate
protected void deactivate() {
tasks.forEach(task -> task.cancel(true));
cfgRefreshTask.cancel(true);
}

@Reference(cardinality = ReferenceCardinality.OPTIONAL, policy = ReferencePolicy.DYNAMIC, policyOption = ReferencePolicyOption.GREEDY)
Expand All @@ -120,14 +116,16 @@ public void modified(@Nullable final Map<String, Object> config) {
boolean enabled = (config != null)
? ConfigParser.valueAsOrElse(config.get(cfgParam), Boolean.class, cfg)
: cfg;
baseFinderConfig.put(finder, enabled);
String feature = SUGGESTION_FINDER_FEATURES.get(finder);
AddonFinderService finderService = addonFinderService;
if (feature != null && finderService != null) {
if (enabled) {
tasks.add(scheduler.submit(() -> finderService.install(feature)));
} else {
tasks.add(scheduler.submit(() -> finderService.uninstall(feature)));
if (cfg != enabled) {
baseFinderConfig.put(finder, enabled);
String type = SUGGESTION_FINDER_TYPES.get(finder);
AddonFinderService finderService = addonFinderService;
if (type != null && finderService != null) {
if (enabled) {
finderService.install(type);
} else {
finderService.uninstall(type);
}
}
}
}
Expand All @@ -141,12 +139,8 @@ private void syncConfiguration() {
if (cfg == null) {
return;
}
final Map<String, Object> cfgMap = new HashMap<>();
final Enumeration<String> enumeration = cfg.keys();
while (enumeration.hasMoreElements()) {
final String key = enumeration.nextElement();
cfgMap.put(key, cfg.get(key));
}
List<String> keys = Collections.list(cfg.keys());
final Map<String, Object> cfgMap = keys.stream().collect(Collectors.toMap(Function.identity(), cfg::get));
if (!cfgMap.equals(config)) {
modified(cfgMap);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ public class FeatureInstaller implements ConfigurationListener {
private static final String ADDONS_PID = "org.openhab.addons";
private static final String PROPERTY_MVN_REPOS = "org.ops4j.pax.url.mvn.repositories";

public static final List<String> ADDON_TYPES = AddonType.DEFAULT_TYPES.stream().map(AddonType::getId).toList();
public static final String FINDER_ADDON_TYPE = "core-config-discovery-addon";
public static final List<String> ADDON_TYPES = Stream
.concat(AddonType.DEFAULT_TYPES.stream().map(AddonType::getId), Stream.of(FINDER_ADDON_TYPE)).toList();

private final Logger logger = LoggerFactory.getLogger(FeatureInstaller.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@
*/
package org.openhab.core.karaf.internal;

import org.apache.karaf.features.FeaturesService;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.config.discovery.addon.AddonFinderService;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This service is an implementation of an openHAB {@link AddonFinderService} using the Karaf features
Expand All @@ -31,14 +28,12 @@
@Component(name = "org.openhab.core.karafaddonfinders", immediate = true)
@NonNullByDefault
public class KarafAddonFinderService implements AddonFinderService {
private final Logger logger = LoggerFactory.getLogger(KarafAddonFinderService.class);

private final FeaturesService featuresService;
private final FeatureInstaller featureInstaller;
private boolean deactivated;

@Activate
public KarafAddonFinderService(final @Reference FeaturesService featuresService) {
this.featuresService = featuresService;
public KarafAddonFinderService(final @Reference FeatureInstaller featureInstaller) {
this.featureInstaller = featureInstaller;
}

@Deactivate
Expand All @@ -49,30 +44,14 @@ protected void deactivate() {
@Override
public void install(String id) {
if (!deactivated) {
try {
if (!featuresService.isInstalled(featuresService.getFeature(id))) {
featuresService.installFeature(id);
}
} catch (Exception e) {
if (!deactivated) {
logger.error("Failed to install add-on suggestion finder {}", id, e);
}
}
featureInstaller.addAddon(FeatureInstaller.FINDER_ADDON_TYPE, id);
}
}

@Override
public void uninstall(String id) {
if (!deactivated) {
try {
if (featuresService.isInstalled(featuresService.getFeature(id))) {
featuresService.uninstallFeature(id);
}
} catch (Exception e) {
if (!deactivated) {
logger.error("Failed to uninstall add-on suggestion finder {}", id, e);
}
}
featureInstaller.removeAddon(FeatureInstaller.FINDER_ADDON_TYPE, id);
}
}
}