From b5d5f095586f8a1ce0b1a5bd0a5d8ba546ce4f51 Mon Sep 17 00:00:00 2001 From: Laurent Garnier Date: Sat, 4 Jul 2020 17:43:15 +0200 Subject: [PATCH 1/2] [kodi] Thing handler factory with null annotations and constructor injection Signed-off-by: Laurent Garnier --- .../kodi/internal/KodiHandlerFactory.java | 64 ++++++------------- 1 file changed, 21 insertions(+), 43 deletions(-) diff --git a/bundles/org.openhab.binding.kodi/src/main/java/org/openhab/binding/kodi/internal/KodiHandlerFactory.java b/bundles/org.openhab.binding.kodi/src/main/java/org/openhab/binding/kodi/internal/KodiHandlerFactory.java index 141b7fb5744d9..dbdca6144d20a 100644 --- a/bundles/org.openhab.binding.kodi/src/main/java/org/openhab/binding/kodi/internal/KodiHandlerFactory.java +++ b/bundles/org.openhab.binding.kodi/src/main/java/org/openhab/binding/kodi/internal/KodiHandlerFactory.java @@ -19,6 +19,8 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import org.eclipse.jdt.annotation.NonNullByDefault; +import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jetty.websocket.client.WebSocketClient; import org.eclipse.smarthome.core.audio.AudioHTTPServer; import org.eclipse.smarthome.core.audio.AudioSink; @@ -33,6 +35,7 @@ import org.openhab.binding.kodi.internal.handler.KodiHandler; import org.osgi.framework.ServiceRegistration; import org.osgi.service.component.ComponentContext; +import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Reference; import org.slf4j.Logger; @@ -45,21 +48,32 @@ * @author Paul Frank - Initial contribution * @author Christoph Weitkamp - Improvements on channels for opening PVR TV or Radio streams */ +@NonNullByDefault @Component(service = ThingHandlerFactory.class, configurationPid = "binding.kodi") public class KodiHandlerFactory extends BaseThingHandlerFactory { private Logger logger = LoggerFactory.getLogger(KodiHandlerFactory.class); - private AudioHTTPServer audioHTTPServer; - private NetworkAddressService networkAddressService; + private final AudioHTTPServer audioHTTPServer; + private final NetworkAddressService networkAddressService; + private final KodiDynamicStateDescriptionProvider stateDescriptionProvider; private WebSocketClient webSocketClient; // url (scheme+server+port) to use for playing notification sounds - private String callbackUrl = null; + private @Nullable String callbackUrl; - private Map> audioSinkRegistrations = new ConcurrentHashMap<>(); + private Map> audioSinkRegistrations = new ConcurrentHashMap<>(); - private KodiDynamicStateDescriptionProvider stateDescriptionProvider; + @Activate + public KodiHandlerFactory(final @Reference AudioHTTPServer audioHTTPServer, + final @Reference NetworkAddressService networkAddressService, + final @Reference KodiDynamicStateDescriptionProvider stateDescriptionProvider, + final @Reference WebSocketFactory webSocketFactory) { + this.audioHTTPServer = audioHTTPServer; + this.networkAddressService = networkAddressService; + this.stateDescriptionProvider = stateDescriptionProvider; + this.webSocketClient = webSocketFactory.getCommonWebSocketClient(); + } @Override protected void activate(ComponentContext componentContext) { @@ -74,7 +88,7 @@ public boolean supportsThingType(ThingTypeUID thingTypeUID) { } @Override - protected ThingHandler createHandler(Thing thing) { + protected @Nullable ThingHandler createHandler(Thing thing) { ThingTypeUID thingTypeUID = thing.getThingTypeUID(); if (thingTypeUID.equals(THING_TYPE_KODI)) { @@ -94,7 +108,7 @@ protected ThingHandler createHandler(Thing thing) { return null; } - private String createCallbackUrl() { + private @Nullable String createCallbackUrl() { if (callbackUrl != null) { return callbackUrl; } else { @@ -123,40 +137,4 @@ public void unregisterHandler(Thing thing) { reg.unregister(); } } - - @Reference - protected void setHttpClientFactory(WebSocketFactory webSocketFactory) { - this.webSocketClient = webSocketFactory.getCommonWebSocketClient(); - } - - protected void unsetHttpClientFactory(WebSocketFactory webSocketFactory) { - this.webSocketClient = null; - } - - @Reference - protected void setAudioHTTPServer(AudioHTTPServer audioHTTPServer) { - this.audioHTTPServer = audioHTTPServer; - } - - protected void unsetAudioHTTPServer(AudioHTTPServer audioHTTPServer) { - this.audioHTTPServer = null; - } - - @Reference - protected void setNetworkAddressService(NetworkAddressService networkAddressService) { - this.networkAddressService = networkAddressService; - } - - protected void unsetNetworkAddressService(NetworkAddressService networkAddressService) { - this.networkAddressService = null; - } - - @Reference - protected void setDynamicStateDescriptionProvider(KodiDynamicStateDescriptionProvider stateDescriptionProvider) { - this.stateDescriptionProvider = stateDescriptionProvider; - } - - protected void unsetDynamicStateDescriptionProvider(KodiDynamicStateDescriptionProvider stateDescriptionProvider) { - this.stateDescriptionProvider = null; - } } From e020ad0d4ad592256faa0d3da184263927223e51 Mon Sep 17 00:00:00 2001 From: Laurent Garnier Date: Sat, 4 Jul 2020 18:44:01 +0200 Subject: [PATCH 2/2] final added to few class fields Signed-off-by: Laurent Garnier --- .../openhab/binding/kodi/internal/KodiHandlerFactory.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.binding.kodi/src/main/java/org/openhab/binding/kodi/internal/KodiHandlerFactory.java b/bundles/org.openhab.binding.kodi/src/main/java/org/openhab/binding/kodi/internal/KodiHandlerFactory.java index dbdca6144d20a..0f7e209baf6bc 100644 --- a/bundles/org.openhab.binding.kodi/src/main/java/org/openhab/binding/kodi/internal/KodiHandlerFactory.java +++ b/bundles/org.openhab.binding.kodi/src/main/java/org/openhab/binding/kodi/internal/KodiHandlerFactory.java @@ -52,18 +52,18 @@ @Component(service = ThingHandlerFactory.class, configurationPid = "binding.kodi") public class KodiHandlerFactory extends BaseThingHandlerFactory { - private Logger logger = LoggerFactory.getLogger(KodiHandlerFactory.class); + private final Logger logger = LoggerFactory.getLogger(KodiHandlerFactory.class); private final AudioHTTPServer audioHTTPServer; private final NetworkAddressService networkAddressService; private final KodiDynamicStateDescriptionProvider stateDescriptionProvider; - private WebSocketClient webSocketClient; + private final WebSocketClient webSocketClient; + + private final Map> audioSinkRegistrations = new ConcurrentHashMap<>(); // url (scheme+server+port) to use for playing notification sounds private @Nullable String callbackUrl; - private Map> audioSinkRegistrations = new ConcurrentHashMap<>(); - @Activate public KodiHandlerFactory(final @Reference AudioHTTPServer audioHTTPServer, final @Reference NetworkAddressService networkAddressService,