diff --git a/coap-core/src/main/java/com/mbed/coap/server/CoapServerBuilder.java b/coap-core/src/main/java/com/mbed/coap/server/CoapServerBuilder.java index 38134387..c287bafd 100644 --- a/coap-core/src/main/java/com/mbed/coap/server/CoapServerBuilder.java +++ b/coap-core/src/main/java/com/mbed/coap/server/CoapServerBuilder.java @@ -230,7 +230,7 @@ public CoapServerBuilder transportLogging(boolean isTransportLoggingEnabled) { public CoapServer build() { CoapTransport realTransport = requireNonNull(this.coapTransport.get(), "Missing transport"); - CoapTransport coapTransport = isTransportLoggingEnabled ? new LoggingCoapTransport(realTransport) : realTransport; + CoapTransport coapTransport = isTransportLoggingEnabled ? LoggingCoapTransport.wrap(realTransport) : realTransport; final boolean stopExecutor = scheduledExecutorService == null; final ScheduledExecutorService effectiveExecutorService = scheduledExecutorService != null ? scheduledExecutorService : Executors.newSingleThreadScheduledExecutor(); Timer timer = toTimer(effectiveExecutorService); diff --git a/coap-core/src/main/java/com/mbed/coap/transport/LoggingCoapTransport.java b/coap-core/src/main/java/com/mbed/coap/transport/LoggingCoapTransport.java index d844d45b..dfa8124b 100644 --- a/coap-core/src/main/java/com/mbed/coap/transport/LoggingCoapTransport.java +++ b/coap-core/src/main/java/com/mbed/coap/transport/LoggingCoapTransport.java @@ -27,8 +27,11 @@ public class LoggingCoapTransport implements CoapTransport { private final CoapTransport transport; - public LoggingCoapTransport(CoapTransport transport) { - this.transport = transport; + public static LoggingCoapTransport wrap(CoapTransport transport) { + if (transport instanceof LoggingCoapTransport) { + return ((LoggingCoapTransport) transport); + } + return new LoggingCoapTransport(transport); } public static CoapTransport unwrap(CoapTransport transport) { @@ -38,6 +41,10 @@ public static CoapTransport unwrap(CoapTransport transport) { return transport; } + private LoggingCoapTransport(CoapTransport transport) { + this.transport = transport; + } + public CoapTransport getTransport() { return transport; } diff --git a/coap-core/src/test/java/com/mbed/coap/transport/LoggingCoapTransportTest.java b/coap-core/src/test/java/com/mbed/coap/transport/LoggingCoapTransportTest.java index ade94235..a0da3de9 100644 --- a/coap-core/src/test/java/com/mbed/coap/transport/LoggingCoapTransportTest.java +++ b/coap-core/src/test/java/com/mbed/coap/transport/LoggingCoapTransportTest.java @@ -24,10 +24,23 @@ class LoggingCoapTransportTest { @Test void shouldUnwrapTransport() { CoapTransport transport = Mockito.mock(CoapTransport.class); - LoggingCoapTransport loggingCoapTransport = new LoggingCoapTransport(transport); + LoggingCoapTransport loggingCoapTransport = LoggingCoapTransport.wrap(transport); assertEquals(transport, LoggingCoapTransport.unwrap(loggingCoapTransport)); assertEquals(transport, LoggingCoapTransport.unwrap(transport)); } + @Test + void shouldNotWrapTransportWhenAlreadyLoggingCoapTransport() { + CoapTransport transport = Mockito.mock(CoapTransport.class); + LoggingCoapTransport loggingCoapTransport = LoggingCoapTransport.wrap(transport); + + // when + LoggingCoapTransport wrapped = LoggingCoapTransport.wrap(loggingCoapTransport); + + // then + assertEquals(loggingCoapTransport, wrapped); + assertEquals(transport, loggingCoapTransport.getTransport()); + } + } diff --git a/coap-tcp/src/main/java/com/mbed/coap/server/CoapServerBuilderForTcp.java b/coap-tcp/src/main/java/com/mbed/coap/server/CoapServerBuilderForTcp.java index ba5cf921..5648a014 100644 --- a/coap-tcp/src/main/java/com/mbed/coap/server/CoapServerBuilderForTcp.java +++ b/coap-tcp/src/main/java/com/mbed/coap/server/CoapServerBuilderForTcp.java @@ -140,7 +140,7 @@ public CoapClient buildClient(InetSocketAddress target) throws IOException { } public CoapServer build() { - Service sender = (isTransportLoggingEnabled ? new LoggingCoapTransport(coapTransport) : coapTransport)::sendPacket; + Service sender = (isTransportLoggingEnabled ? LoggingCoapTransport.wrap(coapTransport) : coapTransport)::sendPacket; // NOTIFICATION Service sendNotification = new NotificationValidator()