From 57dc68d04ddfeaed9c132d4c481621489fb31a15 Mon Sep 17 00:00:00 2001 From: lolodomo Date: Sun, 5 Jul 2020 11:41:15 +0200 Subject: [PATCH] [kodi] Thing handler factory with null annotations and constructor injection (#8075) * [kodi] Thing handler factory with null annotations and constructor injection Signed-off-by: Laurent Garnier --- .../kodi/internal/KodiHandlerFactory.java | 70 +++++++------------ 1 file changed, 24 insertions(+), 46 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..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 @@ -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 final Logger logger = LoggerFactory.getLogger(KodiHandlerFactory.class); - private AudioHTTPServer audioHTTPServer; - private NetworkAddressService networkAddressService; - private WebSocketClient webSocketClient; + private final AudioHTTPServer audioHTTPServer; + private final NetworkAddressService networkAddressService; + private final KodiDynamicStateDescriptionProvider stateDescriptionProvider; + private final WebSocketClient webSocketClient; - // url (scheme+server+port) to use for playing notification sounds - private String callbackUrl = null; + private final Map> audioSinkRegistrations = new ConcurrentHashMap<>(); - private Map> audioSinkRegistrations = new ConcurrentHashMap<>(); + // url (scheme+server+port) to use for playing notification sounds + private @Nullable String callbackUrl; - 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; - } }