Use constructor injection (#1210)
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>pull/1219/head
parent
f682cb56e6
commit
b34ef3cf13
|
@ -1,5 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
|
|
@ -56,7 +56,12 @@ public class MDNSClientImpl implements MDNSClient, NetworkAddressChangeListener
|
|||
|
||||
private final Set<ServiceDescription> activeServices = ConcurrentHashMap.newKeySet();
|
||||
|
||||
private NetworkAddressService networkAddressService;
|
||||
private final NetworkAddressService networkAddressService;
|
||||
|
||||
@Activate
|
||||
public MDNSClientImpl(final @Reference NetworkAddressService networkAddressService) {
|
||||
this.networkAddressService = networkAddressService;
|
||||
}
|
||||
|
||||
private Set<InetAddress> getAllInetAddresses() {
|
||||
final Set<InetAddress> addresses = new HashSet<>();
|
||||
|
@ -132,6 +137,7 @@ public class MDNSClientImpl implements MDNSClient, NetworkAddressChangeListener
|
|||
|
||||
@Activate
|
||||
protected void activate() {
|
||||
networkAddressService.addNetworkAddressChangeListener(this);
|
||||
start();
|
||||
}
|
||||
|
||||
|
@ -152,6 +158,7 @@ public class MDNSClientImpl implements MDNSClient, NetworkAddressChangeListener
|
|||
public void deactivate() {
|
||||
close();
|
||||
activeServices.clear();
|
||||
networkAddressService.removeNetworkAddressChangeListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -273,15 +280,4 @@ public class MDNSClientImpl implements MDNSClient, NetworkAddressChangeListener
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,10 +17,10 @@ import java.util.Set;
|
|||
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 @@ import org.slf4j.LoggerFactory;
|
|||
*/
|
||||
@Component(immediate = true)
|
||||
public class MDNSServiceImpl implements MDNSService {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(MDNSServiceImpl.class);
|
||||
private MDNSClient mdnsClient;
|
||||
|
||||
private @Nullable MDNSClient mdnsClient;
|
||||
|
||||
private final Set<ServiceDescription> servicesToRegisterQueue = new CopyOnWriteArraySet<>();
|
||||
|
||||
public MDNSServiceImpl() {
|
||||
}
|
||||
|
||||
@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 @@ public class MDNSServiceImpl implements MDNSService {
|
|||
|
||||
@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 @@ public class MDNSServiceImpl implements MDNSService {
|
|||
}
|
||||
}
|
||||
|
||||
@Activate
|
||||
public void activate() {
|
||||
}
|
||||
|
||||
@Deactivate
|
||||
public void deactivate() {
|
||||
unregisterAllServices();
|
||||
|
@ -134,5 +119,4 @@ public class MDNSServiceImpl implements MDNSService {
|
|||
logger.debug("mDNS service has been stopped");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue