From b34ef3cf13157fd9487c7ce04ce753621de621e2 Mon Sep 17 00:00:00 2001 From: Christoph Weitkamp Date: Thu, 14 Nov 2019 19:31:14 +0100 Subject: [PATCH] Use constructor injection (#1210) Signed-off-by: Christoph Weitkamp --- .../pom.xml | 3 +- .../mdns/internal/MDNSClientImpl.java | 20 ++--- .../mdns/internal/MDNSServiceImpl.java | 78 ++++++++----------- 3 files changed, 41 insertions(+), 60 deletions(-) diff --git a/bundles/org.openhab.core.io.transport.mdns/pom.xml b/bundles/org.openhab.core.io.transport.mdns/pom.xml index fd3130c997de9..1ceec4d174fb1 100644 --- a/bundles/org.openhab.core.io.transport.mdns/pom.xml +++ b/bundles/org.openhab.core.io.transport.mdns/pom.xml @@ -1,5 +1,6 @@ - + 4.0.0 diff --git a/bundles/org.openhab.core.io.transport.mdns/src/main/java/org/eclipse/smarthome/io/transport/mdns/internal/MDNSClientImpl.java b/bundles/org.openhab.core.io.transport.mdns/src/main/java/org/eclipse/smarthome/io/transport/mdns/internal/MDNSClientImpl.java index fb4f2099f21f5..2e9f162eff42e 100644 --- a/bundles/org.openhab.core.io.transport.mdns/src/main/java/org/eclipse/smarthome/io/transport/mdns/internal/MDNSClientImpl.java +++ b/bundles/org.openhab.core.io.transport.mdns/src/main/java/org/eclipse/smarthome/io/transport/mdns/internal/MDNSClientImpl.java @@ -56,7 +56,12 @@ public class MDNSClientImpl implements MDNSClient, NetworkAddressChangeListener private final Set activeServices = ConcurrentHashMap.newKeySet(); - private NetworkAddressService networkAddressService; + private final NetworkAddressService networkAddressService; + + @Activate + public MDNSClientImpl(final @Reference NetworkAddressService networkAddressService) { + this.networkAddressService = networkAddressService; + } private Set getAllInetAddresses() { final Set addresses = new HashSet<>(); @@ -132,6 +137,7 @@ public Set getClientInstances() { @Activate protected void activate() { + networkAddressService.addNetworkAddressChangeListener(this); start(); } @@ -152,6 +158,7 @@ private void start() { public void deactivate() { close(); activeServices.clear(); + networkAddressService.removeNetworkAddressChangeListener(this); } @Override @@ -273,15 +280,4 @@ public void onChanged(List added, List removed) { close(); start(); } - - @Reference - protected void setNetworkAddressService(NetworkAddressService networkAddressService) { - this.networkAddressService = networkAddressService; - networkAddressService.addNetworkAddressChangeListener(this); - } - - protected void unsetNetworkAddressService(NetworkAddressService networkAddressService) { - networkAddressService.removeNetworkAddressChangeListener(this); - this.networkAddressService = null; - } } diff --git a/bundles/org.openhab.core.io.transport.mdns/src/main/java/org/eclipse/smarthome/io/transport/mdns/internal/MDNSServiceImpl.java b/bundles/org.openhab.core.io.transport.mdns/src/main/java/org/eclipse/smarthome/io/transport/mdns/internal/MDNSServiceImpl.java index 559e9488732ed..3c5ebb72fcc1a 100644 --- a/bundles/org.openhab.core.io.transport.mdns/src/main/java/org/eclipse/smarthome/io/transport/mdns/internal/MDNSServiceImpl.java +++ b/bundles/org.openhab.core.io.transport.mdns/src/main/java/org/eclipse/smarthome/io/transport/mdns/internal/MDNSServiceImpl.java @@ -17,10 +17,10 @@ import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.Executors; +import org.eclipse.jdt.annotation.Nullable; import org.eclipse.smarthome.io.transport.mdns.MDNSClient; import org.eclipse.smarthome.io.transport.mdns.MDNSService; import org.eclipse.smarthome.io.transport.mdns.ServiceDescription; -import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Deactivate; import org.osgi.service.component.annotations.Reference; @@ -37,43 +37,36 @@ */ @Component(immediate = true) public class MDNSServiceImpl implements MDNSService { - private final Logger logger = LoggerFactory.getLogger(MDNSServiceImpl.class); - private MDNSClient mdnsClient; - private final Set servicesToRegisterQueue = new CopyOnWriteArraySet<>(); + private @Nullable MDNSClient mdnsClient; - public MDNSServiceImpl() { - } + private final Set servicesToRegisterQueue = new CopyOnWriteArraySet<>(); @Reference(cardinality = ReferenceCardinality.OPTIONAL, policy = ReferencePolicy.DYNAMIC) protected void setMDNSClient(MDNSClient client) { this.mdnsClient = client; // register queued services if (!servicesToRegisterQueue.isEmpty()) { - Runnable runnable = new Runnable() { - @Override - public void run() { - logger.debug("Registering {} queued services", servicesToRegisterQueue.size()); - for (ServiceDescription description : servicesToRegisterQueue) { - try { - MDNSClient localClient = mdnsClient; - if (localClient != null) { - localClient.registerService(description); - } else { - break; - } - } catch (IOException e) { - logger.error("{}", e.getMessage()); - } catch (IllegalStateException e) { - logger.debug("Not registering service {}, because service is already deactivated!", - description.serviceType); + Executors.newSingleThreadExecutor().execute(() -> { + logger.debug("Registering {} queued services", servicesToRegisterQueue.size()); + for (ServiceDescription description : servicesToRegisterQueue) { + try { + MDNSClient localClient = mdnsClient; + if (localClient != null) { + localClient.registerService(description); + } else { + break; } + } catch (IOException e) { + logger.error("{}", e.getMessage()); + } catch (IllegalStateException e) { + logger.debug("Not registering service {}, because service is already deactivated!", + description.serviceType); } - servicesToRegisterQueue.clear(); } - }; - Executors.newSingleThreadExecutor().execute(runnable); + servicesToRegisterQueue.clear(); + }); } } @@ -84,25 +77,21 @@ protected void unsetMDNSClient(MDNSClient mdnsClient) { @Override public void registerService(final ServiceDescription description) { - if (mdnsClient == null) { - // queue the service to register it as soon as the mDNS client is - // available + MDNSClient localClient = mdnsClient; + if (localClient == null) { + // queue the service to register it as soon as the mDNS client is available servicesToRegisterQueue.add(description); } else { - Runnable runnable = new Runnable() { - @Override - public void run() { - try { - mdnsClient.registerService(description); - } catch (IOException e) { - logger.error("{}", e.getMessage()); - } catch (IllegalStateException e) { - logger.debug("Not registering service {}, because service is already deactivated!", - description.serviceType); - } + Executors.newSingleThreadExecutor().execute(() -> { + try { + localClient.registerService(description); + } catch (IOException e) { + logger.error("{}", e.getMessage()); + } catch (IllegalStateException e) { + logger.debug("Not registering service {}, because service is already deactivated!", + description.serviceType); } - }; - Executors.newSingleThreadExecutor().execute(runnable); + }); } } @@ -122,10 +111,6 @@ protected void unregisterAllServices() { } } - @Activate - public void activate() { - } - @Deactivate public void deactivate() { unregisterAllServices(); @@ -134,5 +119,4 @@ public void deactivate() { logger.debug("mDNS service has been stopped"); } } - }