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

[addonservices] allow offline mode #2633

Merged
merged 2 commits into from
Jan 1, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -14,6 +14,7 @@

import static org.openhab.core.addon.Addon.CODE_MATURITY_LEVELS;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;
Expand Down Expand Up @@ -50,6 +51,8 @@
import org.openhab.core.events.Event;
import org.openhab.core.events.EventPublisher;
import org.osgi.framework.Constants;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Modified;
Expand Down Expand Up @@ -114,12 +117,16 @@ public class CommunityMarketplaceAddonService implements AddonService {
private final Set<MarketplaceAddonHandler> addonHandlers = new HashSet<>();

private final EventPublisher eventPublisher;
private final ConfigurationAdmin configurationAdmin;

private @Nullable String apiKey = null;
private boolean showUnpublished = false;

@Activate
public CommunityMarketplaceAddonService(final @Reference EventPublisher eventPublisher) {
public CommunityMarketplaceAddonService(final @Reference EventPublisher eventPublisher,
@Reference ConfigurationAdmin configurationAdmin) {
this.eventPublisher = eventPublisher;
this.configurationAdmin = configurationAdmin;
}

@Activate
Expand Down Expand Up @@ -162,6 +169,11 @@ public void refreshSource() {

@Override
public List<Addon> getAddons(@Nullable Locale locale) {
if (!remoteEnabled()) {

cweitkamp marked this conversation as resolved.
Show resolved Hide resolved
return List.of();
}

try {
List<DiscourseCategoryResponseDTO> pages = new ArrayList<>();

Expand Down Expand Up @@ -201,6 +213,10 @@ public List<Addon> getAddons(@Nullable Locale locale) {

@Override
public @Nullable Addon getAddon(String id, @Nullable Locale locale) {
if (!remoteEnabled()) {
return null;
}

URL url;
try {
url = new URL(String.format("%s%s", COMMUNITY_TOPIC_URL, id.replace(ADDON_ID_PREFIX, "")));
Expand Down Expand Up @@ -460,4 +476,13 @@ private void postFailureEvent(String extensionId, @Nullable String msg) {
Event event = AddonEventFactory.createAddonFailureEvent(extensionId, msg);
eventPublisher.post(event);
}

private boolean remoteEnabled() {
try {
Configuration configuration = configurationAdmin.getConfiguration("org.openhab.addons", null);
return (boolean) Objects.requireNonNullElse(configuration.getProperties().get("remote"), true);
Copy link
Contributor

@cweitkamp cweitkamp Jan 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the getProperties() can return null and we are potentially run into NPEs here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in fc25ddb

Copy link
Contributor

@cweitkamp cweitkamp Jan 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you.

I will try to find a solution for #2581 based on ExpiringCacheMap later.

} catch (IOException e) {
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,16 @@
import org.openhab.core.events.Event;
import org.openhab.core.events.EventPublisher;
import org.osgi.framework.Constants;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Modified;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand All @@ -60,11 +64,13 @@
* @author Yannick Schaus - Initial contribution
* @author Jan N. Klug - Refactored for JSON marketplaces
*/
@Component(immediate = true, configurationPid = "org.openhab.jsonaddonservice", //
@Component(immediate = true, configurationPid = { "org.openhab.jsonaddonservice" }, //
property = Constants.SERVICE_PID + "=org.openhab.jsonaddonservice")
@ConfigurableService(category = "system", label = JsonAddonService.SERVICE_NAME, description_uri = JsonAddonService.CONFIG_URI)
@NonNullByDefault
public class JsonAddonService implements AddonService {
private final Logger logger = LoggerFactory.getLogger(JsonAddonService.class);

static final String SERVICE_NAME = "Json 3rd Party Add-on Service";
static final String CONFIG_URI = "system:jsonaddonservice";

Expand Down Expand Up @@ -92,10 +98,13 @@ public class JsonAddonService implements AddonService {
private boolean showUnstable = false;

private final EventPublisher eventPublisher;
private final ConfigurationAdmin configurationAdmin;

@Activate
public JsonAddonService(@Reference EventPublisher eventPublisher, Map<String, Object> config) {
public JsonAddonService(@Reference EventPublisher eventPublisher, @Reference ConfigurationAdmin configurationAdmin,
Map<String, Object> config) {
this.eventPublisher = eventPublisher;
this.configurationAdmin = configurationAdmin;
modified(config);
}

Expand Down Expand Up @@ -129,6 +138,11 @@ public String getName() {
@Override
@SuppressWarnings("unchecked")
public void refreshSource() {
if (!remoteEnabled()) {
cachedAddons = List.of();
return;
}

cachedAddons = (List<AddonEntryDTO>) addonserviceUrls.stream().map(urlString -> {
try {
URL url = new URL(urlString);
Expand Down Expand Up @@ -251,4 +265,13 @@ private void postFailureEvent(String extensionId, @Nullable String msg) {
Event event = AddonEventFactory.createAddonFailureEvent(extensionId, msg);
eventPublisher.post(event);
}

private boolean remoteEnabled() {
try {
Configuration configuration = configurationAdmin.getConfiguration("org.openhab.addons", null);
return (boolean) Objects.requireNonNullElse(configuration.getProperties().get("remote"), true);
} catch (IOException e) {
return true;
}
}
}