Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Commit

Permalink
Nullness fixes (#4290)
Browse files Browse the repository at this point in the history
* fix nullness of MQTT service
* Identifiable should make use of '@NonNullByDefault'

Signed-off-by: Markus Rathgeb <maggu2810@gmail.com>
  • Loading branch information
maggu2810 authored and kaikreuzer committed Nov 3, 2017
1 parent faf88b1 commit 1dc6e04
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
Expand Up @@ -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<T> {

/**
Expand Down
Expand Up @@ -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;
Expand All @@ -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<String, MqttBrokerConnection> brokerConnections = new ConcurrentHashMap<String, MqttBrokerConnection>();
private final List<MqttBrokersObserver> brokersObservers = new CopyOnWriteArrayList<>();

@Deprecated
private EventPublisher eventPublisher;
private @Nullable EventPublisher eventPublisher;

/**
* The expected service configuration looks like this:
Expand Down Expand Up @@ -127,7 +131,12 @@ public void modified(Map<String, Object> config) {

for (Map<String, String> 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) {
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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<String, String> brokerConnectionConfig)
public @Nullable MqttBrokerConnection addBrokerConnection(Map<String, String> 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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 1dc6e04

Please sign in to comment.