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

[extensionservice] Add more null annotations #7348

Merged
merged 3 commits into from
Apr 13, 2020
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 @@ -12,6 +12,8 @@
*/
package org.openhab.extensionservice.marketplace;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.core.extension.Extension;

/**
Expand All @@ -20,6 +22,7 @@
* @author Kai Kreuzer - Initial contribution and API
*
*/
@NonNullByDefault
public class MarketplaceExtension extends Extension {

// constants used to construct extension IDs
Expand All @@ -39,7 +42,8 @@ public class MarketplaceExtension extends Extension {
private transient String packageFormat;

public MarketplaceExtension(String id, String type, String label, String version, String link, boolean installed,
String description, String backgroundColor, String imageLink, String downloadUrl, String packageFormat) {
String description, @Nullable String backgroundColor, String imageLink, String downloadUrl,
String packageFormat) {
super(id, type, label, version, link, installed, description, backgroundColor, imageLink);
this.downloadUrl = downloadUrl;
this.packageFormat = packageFormat;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
*/
package org.openhab.extensionservice.marketplace;

import org.openhab.extensionservice.marketplace.MarketplaceExtension;
import org.openhab.extensionservice.marketplace.MarketplaceHandlerException;
import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* This interface can be implemented by services that want to register as handlers for specific marketplace extension
Expand All @@ -27,6 +26,7 @@
* @author Kai Kreuzer - Initial contribution and API
*
*/
@NonNullByDefault
public interface MarketplaceExtensionHandler {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
*/
package org.openhab.extensionservice.marketplace;

import org.openhab.extensionservice.marketplace.MarketplaceExtensionHandler;
import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* This is an exception that can be thrown by {@link MarketplaceExtensionHandler}s if some operation fails.
*
* @author Kai Kreuzer - Initial contribution and API
*
*/
@NonNullByDefault
public class MarketplaceHandlerException extends Exception {

private static final long serialVersionUID = -5652014141471618161L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.smarthome.config.xml.util.XmlDocumentReader;
import org.openhab.extensionservice.marketplace.internal.MarketplaceProxy;
import org.openhab.extensionservice.marketplace.internal.MarketplaceXMLReader;
import org.openhab.extensionservice.marketplace.internal.model.Marketplace;
import org.openhab.extensionservice.marketplace.internal.model.Node;
import org.slf4j.Logger;
Expand All @@ -40,27 +40,27 @@
* @author Kai Kreuzer - Initial contribution and API
*
*/
@NonNullByDefault
public class MarketplaceProxy {

private static final String MP_URL = "https://marketplace.eclipse.org/taxonomy/term/4988%2C4396/api/p?client=org.eclipse.smarthome";
private static final long REFRESH_INTERVAL = 3600;
private static final long RETRY_DELAY = 60;

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

private static final String MP_URL = "https://marketplace.eclipse.org/taxonomy/term/4988%2C4396/api/p?client=org.eclipse.smarthome";
private Node[] cachedNodes = new Node[0];
private final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
private final ScheduledFuture<?> refreshJob;
private final URL url;
private Node[] cachedNodes = null;
private long refresh_interval = 3600;
private long retry_delay = 60;
private ScheduledExecutorService executorService;
private ScheduledFuture<?> refreshJob;

/**
* Creates a new instance, which immediately schedules a synchronization with the marketplace content.
*/
public MarketplaceProxy() {
try {
url = new URL(MP_URL);
this.executorService = Executors.newSingleThreadScheduledExecutor();
this.refreshJob = this.executorService.scheduleWithFixedDelay(() -> refresh(), 0, refresh_interval,
TimeUnit.SECONDS);
refreshJob = executorService.scheduleWithFixedDelay(this::refresh, 0, REFRESH_INTERVAL, TimeUnit.SECONDS);
} catch (MalformedURLException e) {
throw new IllegalArgumentException("Something is very wrong - cannot instantiate URL " + MP_URL);
}
Expand All @@ -72,7 +72,7 @@ public MarketplaceProxy() {
* @return list of marketplace nodes
*/
public List<Node> getNodes() {
return cachedNodes != null ? Arrays.asList(cachedNodes) : Collections.emptyList();
return Collections.unmodifiableList(Arrays.asList(cachedNodes));
}

/**
Expand All @@ -81,24 +81,26 @@ public List<Node> getNodes() {
public synchronized void refresh() {
XmlDocumentReader<Marketplace> reader = new MarketplaceXMLReader();
try {
@Nullable
Marketplace result = reader.readFromXML(url);
cachedNodes = result.categories[0].nodes;
if (result != null) {
cachedNodes = result.categories[0].nodes;
}
} catch (Exception e) {
if (cachedNodes == null) {
if (cachedNodes.length == 0) {
logger.warn("Failed downloading Marketplace entries: {}", e.getMessage());
logger.warn("Retrying again in a minute");
this.executorService.schedule(() -> refresh(), retry_delay, TimeUnit.SECONDS);
executorService.schedule(this::refresh, RETRY_DELAY, TimeUnit.SECONDS);
} else {
logger.debug("Cannot access IoT Marketplace - will continue to use cached results: {}", e.getMessage());
}
}
}

public void dispose() {
if (this.refreshJob != null && !this.refreshJob.isCancelled()) {
this.refreshJob.cancel(true);
this.refreshJob = null;
if (!refreshJob.isCancelled()) {
refreshJob.cancel(true);
}
this.executorService.shutdown();
executorService.shutdown();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package org.openhab.extensionservice.marketplace.internal;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.smarthome.config.xml.util.XmlDocumentReader;
import org.openhab.extensionservice.marketplace.internal.model.Category;
import org.openhab.extensionservice.marketplace.internal.model.Marketplace;
Expand All @@ -25,18 +26,19 @@
* @author Kai Kreuzer - Initial contribution and API
*
*/
@NonNullByDefault
public class MarketplaceXMLReader extends XmlDocumentReader<Marketplace> {

public MarketplaceXMLReader() {
super.setClassLoader(Marketplace.class.getClassLoader());
}

@Override
public void registerConverters(XStream xstream) {
public void registerConverters(@NonNullByDefault({}) XStream xstream) {
}

@Override
public void registerAliases(XStream xstream) {
public void registerAliases(@NonNullByDefault({}) XStream xstream) {
Copy link
Member Author

@wborn wborn Apr 12, 2020

Choose a reason for hiding this comment

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

Using @NonNullByDefault({}) is the only way to be compatible with both annotated and non-annotated classes.

Copy link
Contributor

Choose a reason for hiding this comment

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

I have no problems with this, sometimes it is the only way to deal with libraries.

xstream.alias("marketplace", Marketplace.class);
xstream.addImplicitArray(Marketplace.class, "categories");
xstream.alias("category", Category.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
*/
package org.openhab.extensionservice.marketplace.internal.model;

import org.openhab.extensionservice.marketplace.internal.model.Node;

/**
* This is a category that holds the nodes as individual entries of the marketplace.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@
*/
package org.openhab.extensionservice.marketplace.internal.model;

import org.openhab.extensionservice.marketplace.internal.model.Category;
import org.eclipse.jdt.annotation.NonNullByDefault;

/**
* This is the parent object that holds the category of the market.
*
* @author Kai Kreuzer - Initial contribution and API
*/
@NonNullByDefault
public class Marketplace {

/**
* The category of the marketplace
*/
public Category[] categories;
public Category[] categories = new Category[0];
}