Skip to content

Commit

Permalink
Added links for (available) docs of all Add-Ons (#1186)
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Weitkamp <[email protected]>
  • Loading branch information
cweitkamp authored and kaikreuzer committed Nov 6, 2019
1 parent be37862 commit fc929c2
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -379,7 +378,7 @@ private synchronized void installAddons(final FeaturesService service, final Map
final Set<String> targetAddons = new HashSet<>(); // the target we want to have installed afterwards
final Set<String> 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(",");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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<ExtensionType> 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
Expand All @@ -71,10 +71,10 @@ public List<Extension> 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);
}
Expand Down Expand Up @@ -114,29 +114,37 @@ 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);
}

@Override
public List<ExtensionType> getTypes(Locale locale) {
List<ExtensionType> 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;
}

Expand All @@ -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,
"-");
}
}

0 comments on commit fc929c2

Please sign in to comment.