diff --git a/bundles/core/org.eclipse.smarthome.core/src/main/java/org/eclipse/smarthome/core/common/registry/Identifiable.java b/bundles/core/org.eclipse.smarthome.core/src/main/java/org/eclipse/smarthome/core/common/registry/Identifiable.java index fe54ece18ca..ed4dcae0e55 100644 --- a/bundles/core/org.eclipse.smarthome.core/src/main/java/org/eclipse/smarthome/core/common/registry/Identifiable.java +++ b/bundles/core/org.eclipse.smarthome.core/src/main/java/org/eclipse/smarthome/core/common/registry/Identifiable.java @@ -7,11 +7,14 @@ */ package org.eclipse.smarthome.core.common.registry; +import org.eclipse.jdt.annotation.NonNullByDefault; + /** * Interface for classes that instances provide an identifier. * * @author Markus Rathgeb - Initial contribution */ +@NonNullByDefault public interface Identifiable { /** diff --git a/bundles/io/org.eclipse.smarthome.io.transport.mqtt/src/main/java/org/eclipse/smarthome/io/transport/mqtt/MqttService.java b/bundles/io/org.eclipse.smarthome.io.transport.mqtt/src/main/java/org/eclipse/smarthome/io/transport/mqtt/MqttService.java index 78fde8155db..254dfaa5bd8 100644 --- a/bundles/io/org.eclipse.smarthome.io.transport.mqtt/src/main/java/org/eclipse/smarthome/io/transport/mqtt/MqttService.java +++ b/bundles/io/org.eclipse.smarthome.io.transport.mqtt/src/main/java/org/eclipse/smarthome/io/transport/mqtt/MqttService.java @@ -19,6 +19,8 @@ import javax.naming.ConfigurationException; import org.apache.commons.lang.StringUtils; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.eclipse.smarthome.core.events.EventPublisher; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; @@ -41,13 +43,15 @@ */ @Component(immediate = true, service = { MqttService.class }, configurationPid = { "org.eclipse.smarthome.mqtt" }, property = "service.pid=org.eclipse.smarthome.mqtt") +@NonNullByDefault public class MqttService { private static final String NAME_PROPERTY = "name"; private final Logger logger = LoggerFactory.getLogger(MqttService.class); private final Map brokerConnections = new ConcurrentHashMap(); private final List brokersObservers = new CopyOnWriteArrayList<>(); + @Deprecated - private EventPublisher eventPublisher; + private @Nullable EventPublisher eventPublisher; /** * The expected service configuration looks like this: @@ -127,7 +131,12 @@ public void modified(Map config) { for (Map brokerConfig : brokerConfigs.values()) { try { - addBrokerConnection(brokerConfig).start(); + final MqttBrokerConnection conn = addBrokerConnection(brokerConfig); + if (conn == null) { + logger.warn("MqttBroker connection name already present."); + continue; + } + conn.start(); } catch (ConfigurationException e) { logger.warn("MqttBroker connection configuration faulty: {}", e.getMessage()); } catch (MqttException e) { @@ -182,7 +191,7 @@ public boolean hasBrokerObservers() { * @param brokerName to look for. * @return existing connection or null */ - public MqttBrokerConnection getBrokerConnection(String brokerName) { + public @Nullable MqttBrokerConnection getBrokerConnection(String brokerName) { synchronized (brokerConnections) { return brokerConnections.get(brokerName.toLowerCase()); } @@ -222,17 +231,17 @@ public boolean addBrokerConnection(MqttBrokerConnection connection) { * @throws ConfigurationException Most likely your provided name and url are invalid. * @throws MqttException */ - public MqttBrokerConnection addBrokerConnection(Map brokerConnectionConfig) + public @Nullable MqttBrokerConnection addBrokerConnection(Map brokerConnectionConfig) throws ConfigurationException, MqttException { // Extract mandatory fields String brokerID = brokerConnectionConfig.get(NAME_PROPERTY); - if (StringUtils.isBlank(brokerID)) { + if (brokerID == null || brokerID.isEmpty()) { throw new ConfigurationException("MQTT Broker property 'name' is not provided"); } brokerID = brokerID.toLowerCase(); final String brokerURL = brokerConnectionConfig.get("url"); - if (StringUtils.isBlank(brokerURL)) { + if (brokerURL == null || brokerURL.isEmpty()) { throw new ConfigurationException("MQTT Broker property 'url' is not provided"); } // Add the connection @@ -295,7 +304,7 @@ public void removeBrokerConnection(MqttBrokerConnection connection) { * @param brokerName The broker name * @return Returns the removed broker connection, or null if there was none with the given name. */ - public MqttBrokerConnection removeBrokerConnection(String brokerName) { + public @Nullable MqttBrokerConnection removeBrokerConnection(String brokerName) { synchronized (brokerConnections) { MqttBrokerConnection connection = brokerConnections.remove(brokerName.toLowerCase()); if (connection != null) {