Simplify lifecycle by using constructor injection (#1431)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
pull/1436/head
Christoph Weitkamp 2020-04-19 14:33:08 +02:00 committed by GitHub
parent 92027ca922
commit 7940141421
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 19 deletions

View File

@ -38,7 +38,7 @@ import gnu.io.NoSuchPortException;
* @author Wouter Born - Fix serial ports missing when ports are added to system property * @author Wouter Born - Fix serial ports missing when ports are added to system property
*/ */
@NonNullByDefault @NonNullByDefault
@Component @Component(service = SerialPortProvider.class)
public class RxTxPortProvider implements SerialPortProvider { public class RxTxPortProvider implements SerialPortProvider {
private final Logger logger = LoggerFactory.getLogger(RxTxPortProvider.class); private final Logger logger = LoggerFactory.getLogger(RxTxPortProvider.class);

View File

@ -20,6 +20,7 @@ import org.eclipse.jdt.annotation.Nullable;
import org.openhab.core.io.transport.serial.SerialPortIdentifier; import org.openhab.core.io.transport.serial.SerialPortIdentifier;
import org.openhab.core.io.transport.serial.SerialPortManager; import org.openhab.core.io.transport.serial.SerialPortManager;
import org.openhab.core.io.transport.serial.SerialPortProvider; import org.openhab.core.io.transport.serial.SerialPortProvider;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference; import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -37,24 +38,15 @@ public class SerialPortManagerImpl implements SerialPortManager {
private final Logger logger = LoggerFactory.getLogger(SerialPortManagerImpl.class); private final Logger logger = LoggerFactory.getLogger(SerialPortManagerImpl.class);
private @NonNullByDefault({}) SerialPortRegistry registry; private final SerialPortRegistry registry;
@Reference @Activate
protected void setSerialportRegistry(SerialPortRegistry registry) { public SerialPortManagerImpl(final @Reference SerialPortRegistry registry) {
this.registry = registry;
}
protected void unsetSerialportRegistry(SerialPortRegistry registry) {
this.registry = registry; this.registry = registry;
} }
@Override @Override
public Stream<SerialPortIdentifier> getIdentifiers() { public Stream<SerialPortIdentifier> getIdentifiers() {
if (registry == null) {
logger.warn("SerialPortRegistry is not set; no SerialPortIdentifier found");
return Stream.empty();
}
return registry.getPortCreators().stream().flatMap(provider -> { return registry.getPortCreators().stream().flatMap(provider -> {
try { try {
return provider.getSerialPortIdentifiers(); return provider.getSerialPortIdentifiers();
@ -77,10 +69,6 @@ public class SerialPortManagerImpl implements SerialPortManager {
@Override @Override
public @Nullable SerialPortIdentifier getIdentifier(String name) { public @Nullable SerialPortIdentifier getIdentifier(String name) {
if (registry == null) {
logger.warn("SerialPortRegistry is not set; no SerialPortProvider found for: {}", name);
return null;
}
final URI portUri = URI.create(name); final URI portUri = URI.create(name);
for (final SerialPortProvider provider : registry.getPortProvidersForPortName(portUri)) { for (final SerialPortProvider provider : registry.getPortProvidersForPortName(portUri)) {
try { try {

View File

@ -42,8 +42,8 @@ public class SerialCommandExtension extends AbstractConsoleCommandExtension {
private static final String SUBCMD_IDENTIFIER_NAME = "identifier"; private static final String SUBCMD_IDENTIFIER_NAME = "identifier";
private static final String SUBCMD_PORT_CREATORS = "creators"; private static final String SUBCMD_PORT_CREATORS = "creators";
private SerialPortManager serialPortManager; private final SerialPortManager serialPortManager;
private SerialPortRegistry serialPortRegistry; private final SerialPortRegistry serialPortRegistry;
@Activate @Activate
public SerialCommandExtension(final @Reference SerialPortManager serialPortManager, public SerialCommandExtension(final @Reference SerialPortManager serialPortManager,