From fc929c22c15d68b4a03d319b17d6e448e35602f5 Mon Sep 17 00:00:00 2001 From: Christoph Weitkamp Date: Wed, 6 Nov 2019 10:44:51 +0100 Subject: [PATCH] Added links for (available) docs of all Add-Ons (#1186) Signed-off-by: Christoph Weitkamp --- .../core/karaf/internal/FeatureInstaller.java | 49 +++++----- .../karaf/internal/KarafExtensionService.java | 97 ++++++++++--------- 2 files changed, 75 insertions(+), 71 deletions(-) diff --git a/bundles/org.openhab.core.karaf/src/main/java/org/openhab/core/karaf/internal/FeatureInstaller.java b/bundles/org.openhab.core.karaf/src/main/java/org/openhab/core/karaf/internal/FeatureInstaller.java index 46719690629..abca688ae3f 100644 --- a/bundles/org.openhab.core.karaf/src/main/java/org/openhab/core/karaf/internal/FeatureInstaller.java +++ b/bundles/org.openhab.core.karaf/src/main/java/org/openhab/core/karaf/internal/FeatureInstaller.java @@ -39,6 +39,7 @@ import org.apache.karaf.features.Feature; import org.apache.karaf.features.FeaturesService; import org.eclipse.smarthome.config.core.ConfigConstants; +import org.eclipse.smarthome.config.core.ConfigurableService; import org.eclipse.smarthome.core.events.Event; import org.eclipse.smarthome.core.events.EventPublisher; import org.eclipse.smarthome.core.extension.ExtensionEventFactory; @@ -56,19 +57,27 @@ import org.slf4j.LoggerFactory; /** - * This service reads addons.cfg and installs listed addons (= Karaf features) and the selected package. + * This service reads addons.cfg and installs listed add-ons (= Karaf features) and the selected package. * It furthermore allows configuration of the base package through the Paper UI as well as administrating Karaf to * access remote repos and certain feature repos like for legacy or experimental features. * * @author Kai Kreuzer - Initial contribution */ @Component(name = "org.openhab.addons", service = { FeatureInstaller.class, ConfigurationListener.class }, property = { - "service.config.description.uri:String=system:addons", // - "service.config.label:String=Add-on Management", // - "service.config.category:String=system" // -}) + ConfigurableService.SERVICE_PROPERTY_CATEGORY + "=system", + ConfigurableService.SERVICE_PROPERTY_LABEL + "=Add-on Management", + ConfigurableService.SERVICE_PROPERTY_DESCRIPTION_URI + "=" + "system:addons" }) public class FeatureInstaller implements ConfigurationListener { + public static final String EXTENSION_TYPE_ACTION = "action"; + public static final String EXTENSION_TYPE_BINDING = "binding"; + public static final String EXTENSION_TYPE_MISC = "misc"; + public static final String EXTENSION_TYPE_PERSISTENCE = "persistence"; + public static final String EXTENSION_TYPE_TRANSFORMATION = "transformation"; + public static final String EXTENSION_TYPE_UI = "ui"; + public static final String EXTENSION_TYPE_VOICE = "voice"; + + public static final String SIMPLE_PACKAGE = "simple"; public static final String MINIMAL_PACKAGE = "minimal"; private static final String CFG_REMOTE = "remote"; private static final String CFG_LEGACY = "legacy"; @@ -81,16 +90,17 @@ public class FeatureInstaller implements ConfigurationListener { public static final String PREFIX = "openhab-"; public static final String PREFIX_PACKAGE = "package-"; - public static final String[] ADDON_TYPES = new String[] { "binding", "ui", "persistence", "action", "voice", - "transformation", "misc" }; + public static final String[] EXTENSION_TYPES = new String[] { EXTENSION_TYPE_ACTION, EXTENSION_TYPE_BINDING, + EXTENSION_TYPE_MISC, EXTENSION_TYPE_PERSISTENCE, EXTENSION_TYPE_TRANSFORMATION, EXTENSION_TYPE_UI, + EXTENSION_TYPE_VOICE }; private final Logger logger = LoggerFactory.getLogger(FeatureInstaller.class); private String onlineRepoUrl = null; private URI legacyAddonsUrl = null; - private FeaturesService featuresService; - private ConfigurationAdmin configurationAdmin; + private final ConfigurationAdmin configurationAdmin; + private final FeaturesService featuresService; private static EventPublisher eventPublisher; private ScheduledExecutorService scheduler; @@ -100,22 +110,11 @@ public class FeatureInstaller implements ConfigurationListener { private static String currentPackage = null; - @Reference - protected void setFeaturesService(FeaturesService featuresService) { - this.featuresService = featuresService; - } - - protected void unsetFeaturesService(FeaturesService featuresService) { - this.featuresService = null; - } - - @Reference - protected void setConfigurationAdmin(ConfigurationAdmin configurationAdmin) { + @Activate + public FeatureInstaller(final @Reference ConfigurationAdmin configurationAdmin, + final @Reference FeaturesService featuresService) { this.configurationAdmin = configurationAdmin; - } - - protected void unsetConfigurationAdmin(ConfigurationAdmin configurationAdmin) { - this.configurationAdmin = null; + this.featuresService = featuresService; } @Reference @@ -379,7 +378,7 @@ private synchronized void installAddons(final FeaturesService service, final Map final Set targetAddons = new HashSet<>(); // the target we want to have installed afterwards final Set installAddons = new HashSet<>(); // the ones to be installed (the diff) - for (String type : ADDON_TYPES) { + for (String type : EXTENSION_TYPES) { Object install = config.get(type); if (install instanceof String) { String[] entries = ((String) install).split(","); diff --git a/bundles/org.openhab.core.karaf/src/main/java/org/openhab/core/karaf/internal/KarafExtensionService.java b/bundles/org.openhab.core.karaf/src/main/java/org/openhab/core/karaf/internal/KarafExtensionService.java index 6d347147c6e..50d92d897dc 100644 --- a/bundles/org.openhab.core.karaf/src/main/java/org/openhab/core/karaf/internal/KarafExtensionService.java +++ b/bundles/org.openhab.core.karaf/src/main/java/org/openhab/core/karaf/internal/KarafExtensionService.java @@ -27,15 +27,15 @@ import org.eclipse.smarthome.core.extension.Extension; import org.eclipse.smarthome.core.extension.ExtensionService; import org.eclipse.smarthome.core.extension.ExtensionType; +import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** - * This service is an implementation of an ESH {@link ExtensionService} using the Karaf - * features service. This exposes all openHAB addons through the rest api and allows - * UIs to dynamically install and uninstall them. + * This service is an implementation of an openHAB {@link ExtensionService} using the Karaf features service. This + * exposes all openHAB add-ons through the REST API and allows UIs to dynamically install and uninstall them. * * @author Kai Kreuzer - Initial contribution */ @@ -44,25 +44,25 @@ public class KarafExtensionService implements ExtensionService { private final Logger logger = LoggerFactory.getLogger(KarafExtensionService.class); - private FeaturesService featuresService; - private FeatureInstaller featureInstaller; + private final List typeList = new ArrayList<>(FeatureInstaller.EXTENSION_TYPES.length); - @Reference - protected void setFeatureInstaller(FeatureInstaller featureInstaller) { - this.featureInstaller = featureInstaller; - } - - protected void unsetFeatureInstaller(FeatureInstaller featureInstaller) { - this.featureInstaller = null; - } + private final FeaturesService featuresService; + private final FeatureInstaller featureInstaller; - @Reference - protected void setFeaturesService(FeaturesService featuresService) { + @Activate + public KarafExtensionService(final @Reference FeatureInstaller featureInstaller, + final @Reference FeaturesService featuresService) { + this.featureInstaller = featureInstaller; this.featuresService = featuresService; - } - - protected void unsetFeaturesService(FeaturesService featureService) { - this.featuresService = null; + typeList.add(new ExtensionType(FeatureInstaller.EXTENSION_TYPE_BINDING, "Bindings")); + typeList.add(new ExtensionType(FeatureInstaller.EXTENSION_TYPE_MISC, "Misc")); + typeList.add(new ExtensionType(FeatureInstaller.EXTENSION_TYPE_VOICE, "Voice")); + if (!FeatureInstaller.SIMPLE_PACKAGE.equals(featureInstaller.getCurrentPackage())) { + typeList.add(new ExtensionType(FeatureInstaller.EXTENSION_TYPE_ACTION, "Actions")); + typeList.add(new ExtensionType(FeatureInstaller.EXTENSION_TYPE_PERSISTENCE, "Persistence")); + typeList.add(new ExtensionType(FeatureInstaller.EXTENSION_TYPE_TRANSFORMATION, "Transformations")); + typeList.add(new ExtensionType(FeatureInstaller.EXTENSION_TYPE_UI, "User Interfaces")); + } } @Override @@ -71,10 +71,10 @@ public List getExtensions(Locale locale) { try { for (Feature feature : featuresService.listFeatures()) { if (feature.getName().startsWith(FeatureInstaller.PREFIX) - && Arrays.asList(FeatureInstaller.ADDON_TYPES).contains(getType(feature.getName()))) { + && Arrays.asList(FeatureInstaller.EXTENSION_TYPES).contains(getType(feature.getName()))) { Extension extension = getExtension(feature); // for simple packaging, we filter out all openHAB 1 add-ons as they cannot be used through the UI - if (!"simple".equals(featureInstaller.getCurrentPackage()) + if (!FeatureInstaller.SIMPLE_PACKAGE.equals(featureInstaller.getCurrentPackage()) || !extension.getVersion().startsWith("1.")) { extensions.add(extension); } @@ -114,12 +114,30 @@ private Extension getExtension(Feature feature) { String label = feature.getDescription(); String version = feature.getVersion(); String link = null; - if (type.equals("binding")) { - link = "https://www.openhab.org/addons/bindings/" + name + "/"; - } else if (type.equals("action")) { - link = "https://www.openhab.org/addons/actions/" + name + "/"; - } else if (type.equals("persistence")) { - link = "https://www.openhab.org/addons/persistence/" + name + "/"; + switch (type) { + case FeatureInstaller.EXTENSION_TYPE_ACTION: + link = "https://www.openhab.org/addons/actions/" + name + "/"; + break; + case FeatureInstaller.EXTENSION_TYPE_BINDING: + link = "https://www.openhab.org/addons/bindings/" + name + "/"; + break; + case FeatureInstaller.EXTENSION_TYPE_MISC: + // Not possible to define URL + break; + case FeatureInstaller.EXTENSION_TYPE_PERSISTENCE: + link = "https://www.openhab.org/addons/persistence/" + name + "/"; + break; + case FeatureInstaller.EXTENSION_TYPE_TRANSFORMATION: + link = "https://www.openhab.org/addons/transformations/" + name + "/"; + break; + case FeatureInstaller.EXTENSION_TYPE_UI: + // Not possible to define URL + break; + case FeatureInstaller.EXTENSION_TYPE_VOICE: + link = "https://www.openhab.org/addons/voice/" + name + "/"; + break; + default: + break; } boolean installed = featuresService.isInstalled(feature); return new Extension(extId, type, label, version, link, installed); @@ -127,16 +145,6 @@ private Extension getExtension(Feature feature) { @Override public List getTypes(Locale locale) { - List typeList = new ArrayList<>(6); - typeList.add(new ExtensionType("binding", "Bindings")); - if (!"simple".equals(featureInstaller.getCurrentPackage())) { - typeList.add(new ExtensionType("ui", "User Interfaces")); - typeList.add(new ExtensionType("persistence", "Persistence")); - typeList.add(new ExtensionType("action", "Actions")); - typeList.add(new ExtensionType("transformation", "Transformations")); - } - typeList.add(new ExtensionType("voice", "Voice")); - typeList.add(new ExtensionType("misc", "Misc")); return typeList; } @@ -156,17 +164,14 @@ public String getExtensionId(URI extensionURI) { } private String getType(String name) { - if (name.startsWith(FeatureInstaller.PREFIX)) { - name = name.substring(FeatureInstaller.PREFIX.length()); - return StringUtils.substringBefore(name, "-"); - } - return StringUtils.substringBefore(name, "-"); + return StringUtils.substringBefore( + name.startsWith(FeatureInstaller.PREFIX) ? name.substring(FeatureInstaller.PREFIX.length()) : name, + "-"); } private String getName(String name) { - if (name.startsWith(FeatureInstaller.PREFIX)) { - name = name.substring(FeatureInstaller.PREFIX.length()); - } - return StringUtils.substringAfter(name, "-"); + return StringUtils.substringAfter( + name.startsWith(FeatureInstaller.PREFIX) ? name.substring(FeatureInstaller.PREFIX.length()) : name, + "-"); } }