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

[addonsinfoprovider] Addon for providing addon-info of other addons #15780

Closed
wants to merge 16 commits into from
Closed
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
5 changes: 5 additions & 0 deletions bom/openhab-addons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2141,6 +2141,11 @@
<artifactId>org.openhab.voice.watsonstt</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.misc.addonsinfoprovider</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>

</project>
1 change: 1 addition & 0 deletions bundles/org.openhab.misc.addonsinfoprovider/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/src/main/resources/addons.xml
13 changes: 13 additions & 0 deletions bundles/org.openhab.misc.addonsinfoprovider/NOTICE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
This content is produced and maintained by the openHAB project.

* Project home: https://www.openhab.org

== Declared Project Licenses

This program and the accompanying materials are made available under the terms
of the Eclipse Public License 2.0 which is available at
https://www.eclipse.org/legal/epl-2.0/.

== Source Code

https://github.com/openhab/openhab-addons
6 changes: 6 additions & 0 deletions bundles/org.openhab.misc.addonsinfoprovider/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# AddonsInfoProvider Addon

This is a special addon that implements an `AddonInfoProvider` service containing information about suggested Addons that could potentially be installed.
It allows developers to include information in their own addons so that the system can scan the user's network to discover potential addons that can automatically be installed.

See [this page](https://www.openhab.org/docs/developer/addons/addon.html) for instructions how to add the respective information to your addon.
53 changes: 53 additions & 0 deletions bundles/org.openhab.misc.addonsinfoprovider/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.openhab.addons.bundles</groupId>
<artifactId>org.openhab.addons.reactor.bundles</artifactId>
<version>4.1.0-SNAPSHOT</version>
</parent>

<artifactId>org.openhab.misc.addonsinfoprovider</artifactId>

<name>openHAB Add-ons :: Bundles :: Addons Information Provider</name>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>3.1.0</version>
<inherited>false</inherited>
<executions>
<execution>
<id>create-addonsinfo</id>
<goals>
<goal>run</goal>
</goals>
<phase>generate-sources</phase>
<configuration>
<target>
<concat destfile="src/main/resources/addons.xml">
<header file="src/main/resources/header.xml" filtering="no"/>
<fileset dir="${basedirRoot}/bundles">
<include name="*/src/main/resources/OH-INF/addon/addon.xml"/>
</fileset>
<filterchain>
<linecontainsRegExp negate="true">
<regexp pattern="&lt;\?xml"/>
</linecontainsRegExp>
</filterchain>
<footer file="src/main/resources/footer.xml" filtering="no"/>
</concat>
</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<features name="org.openhab.misc.addonsinfoprovider-${project.version}" xmlns="http://karaf.apache.org/xmlns/features/v1.4.0">
<repository>mvn:org.openhab.core.features.karaf/org.openhab.core.features.karaf.openhab-core/${ohc.version}/xml/features</repository>

<feature name="openhab-misc-addonsinfoprovider" description="Addons Information Provider" version="${project.version}">
<feature>openhab-runtime-base</feature>
<bundle start-level="80">mvn:org.openhab.addons.bundles/org.openhab.misc.addonsinfoprovider/${project.version}</bundle>
</feature>
</features>
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.misc.addonsinfoprovider;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.regex.PatternSyntaxException;
import java.util.stream.Collectors;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.addon.AddonInfo;
import org.openhab.core.addon.AddonInfoList;

Check failure on line 27 in bundles/org.openhab.misc.addonsinfoprovider/src/main/java/org/openhab/misc/addonsinfoprovider/AddonsInfoProvider.java

View workflow job for this annotation

GitHub Actions / Build (Java 17, ubuntu-22.04)

The import org.openhab.core.addon.AddonInfoList cannot be resolved
import org.openhab.core.addon.AddonInfoListReader;

Check failure on line 28 in bundles/org.openhab.misc.addonsinfoprovider/src/main/java/org/openhab/misc/addonsinfoprovider/AddonsInfoProvider.java

View workflow job for this annotation

GitHub Actions / Build (Java 17, ubuntu-22.04)

The import org.openhab.core.addon.AddonInfoListReader cannot be resolved
import org.openhab.core.addon.AddonInfoProvider;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* The {@link AddonsInfoProvider} provides information about other addons in the openhab-addons .kar file.
*
* @author Andrew Fiddian-Green - Initial contribution
*/
@NonNullByDefault
@Component(name = "addons-info-provider", service = AddonInfoProvider.class)
public class AddonsInfoProvider implements AddonInfoProvider {

private final Logger logger = LoggerFactory.getLogger(AddonsInfoProvider.class);
private final Set<AddonInfo> candidateAddonInfos = new HashSet<>();

@Activate
public AddonsInfoProvider() {
setCandidates(getResourceXml());
}

@Override
public @Nullable AddonInfo getAddonInfo(@Nullable String uid, @Nullable Locale locale) {
return candidateAddonInfos.stream().filter(a -> a.getUID().equals(uid)).findFirst().orElse(null);
}

@Override
public Set<AddonInfo> getAddonInfos(@Nullable Locale locale) {
return candidateAddonInfos;
}

private String getResourceXml() {
ClassLoader loader = getClass().getClassLoader();
if (loader != null) {
InputStream stream = loader.getResourceAsStream("addons.xml");
if (stream != null) {
try {
return new String(stream.readAllBytes(), StandardCharsets.UTF_8);
} catch (IOException e) {
}
}
}
throw new IllegalStateException("Error loading 'addons.xml' resource");
}

public void setCandidates(String xml) {
candidateAddonInfos.clear();
AddonInfoListReader reader = new AddonInfoListReader();

Check failure on line 78 in bundles/org.openhab.misc.addonsinfoprovider/src/main/java/org/openhab/misc/addonsinfoprovider/AddonsInfoProvider.java

View workflow job for this annotation

GitHub Actions / Build (Java 17, ubuntu-22.04)

AddonInfoListReader cannot be resolved to a type

Check failure on line 78 in bundles/org.openhab.misc.addonsinfoprovider/src/main/java/org/openhab/misc/addonsinfoprovider/AddonsInfoProvider.java

View workflow job for this annotation

GitHub Actions / Build (Java 17, ubuntu-22.04)

AddonInfoListReader cannot be resolved to a type
try {
AddonInfoList addonInfoList = reader.readFromXML(xml);

Check failure on line 80 in bundles/org.openhab.misc.addonsinfoprovider/src/main/java/org/openhab/misc/addonsinfoprovider/AddonsInfoProvider.java

View workflow job for this annotation

GitHub Actions / Build (Java 17, ubuntu-22.04)

AddonInfoList cannot be resolved to a type
if (addonInfoList != null) {
candidateAddonInfos.addAll(addonInfoList.getAddons().stream().collect(Collectors.toSet()));
}
} catch (PatternSyntaxException e) {
logger.warn("PatternSyntaxException: message:{}, description:{}, pattern:{}, index:{}", e.getMessage(),
e.getDescription(), e.getPattern(), e.getIndex(), e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<addon:addon id="addonsinfoprovider" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:addon="https://openhab.org/schemas/addon/v1.0.0"
xsi:schemaLocation="https://openhab.org/schemas/addon/v1.0.0 https://openhab.org/schemas/addon-1.0.0.xsd">

<type>misc</type>
<name>Addons Information Provider</name>
<description>This is a special addon for providing information about other addons to be installed on setup.</description>

</addon:addon>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

</addons>
</addon-info-list>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2010-2023 Contributors to the openHAB project

See the NOTICE file(s) distributed with this work for additional
information.

This program and the accompanying materials are made available under the
terms of the Eclipse Public License 2.0 which is available at
http://www.eclipse.org/legal/epl-2.0

SPDX-License-Identifier: EPL-2.0
-->

<addon-info-list>
<addons>
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* Copyright (c) 2010-2023 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.misc.addonsinfoprovider.test;

import static org.junit.jupiter.api.Assertions.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.regex.PatternSyntaxException;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.Test;
import org.openhab.core.addon.AddonDiscoveryMethod;
import org.openhab.core.addon.AddonInfo;
import org.openhab.core.addon.AddonMatchProperty;
import org.openhab.misc.addonsinfoprovider.AddonsInfoProvider;

/**
* JUnit test for {@link AddonsInfoProvider}.
*
* @author Andrew Fiddian-Green - Initial contribution
*/
@NonNullByDefault
class AddonsInfoProviderTest {

private static final String DELIMITER = "\n\t";

@Test
void testAddonSuggestionInfoProvider() {
AddonsInfoProvider provider = new AddonsInfoProvider();
assertNotNull(provider);
Set<AddonInfo> addonInfos = provider.getAddonInfos(Locale.US);
assertNotNull(addonInfos);

AddonInfo addonInfoBad = provider.getAddonInfo("flying-aardvarks", Locale.US);
assertNull(addonInfoBad);

/*
* At build time we run a regular expression syntax check on all the add-on infos resp. discovery methods and
* match properties in the bundle, in order to help developers identify regex syntax errors in their addon.xml
* files.
*/
List<String> patternErrors = new ArrayList<>();
for (AddonInfo addonInfo : addonInfos) {
for (AddonDiscoveryMethod discoveryMethod : addonInfo.getDiscoveryMethods()) {
for (AddonMatchProperty matchProperty : discoveryMethod.getMatchProperties()) {
try {
matchProperty.getPattern();
} catch (PatternSyntaxException e) {
patternErrors.add(String.format(
"Regex syntax error in org.openhab.%s.%s addon.xml => %s in \"%s\" position %d",
addonInfo.getType(), addonInfo.getId(), e.getDescription(), e.getPattern(),
e.getIndex()));
}
}
}
}
if (!patternErrors.isEmpty()) {
fail(DELIMITER + String.join(DELIMITER, patternErrors));
}
}
}
2 changes: 2 additions & 0 deletions bundles/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,8 @@
<module>org.openhab.voice.voicerss</module>
<module>org.openhab.voice.voskstt</module>
<module>org.openhab.voice.watsonstt</module>
<!-- addonsinfoprovider BUILD THIS LAST -->
<module>org.openhab.misc.addonsinfoprovider</module>
</modules>

<properties>
Expand Down
Loading