From 2db03628fd23c10be7ea60af50f8bea38ceba346 Mon Sep 17 00:00:00 2001 From: vananiev Date: Sun, 3 Nov 2024 23:29:20 +0300 Subject: [PATCH] fix checker framework warn for LoadingPageServer --- .../loadingpage/LoadingPageServer.java | 31 ++++++++----------- .../loadingpage/LoadingPageServerUtils.java | 6 ++-- ...rTests.java => LoadingPageServerTest.java} | 4 +-- 3 files changed, 19 insertions(+), 22 deletions(-) rename src/test/java/ru/investbook/{LoadingPageServerTests.java => LoadingPageServerTest.java} (95%) diff --git a/src/main/java/ru/investbook/loadingpage/LoadingPageServer.java b/src/main/java/ru/investbook/loadingpage/LoadingPageServer.java index 9bd3c20b..d4d934c2 100644 --- a/src/main/java/ru/investbook/loadingpage/LoadingPageServer.java +++ b/src/main/java/ru/investbook/loadingpage/LoadingPageServer.java @@ -22,6 +22,7 @@ import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; import lombok.extern.slf4j.Slf4j; +import org.checkerframework.checker.nullness.qual.Nullable; import ru.investbook.BrowserHomePageOpener; import java.io.ByteArrayOutputStream; @@ -30,23 +31,24 @@ import java.io.OutputStream; import java.net.InetSocketAddress; -import static java.nio.charset.StandardCharsets.*; +import static java.nio.charset.StandardCharsets.UTF_8; +import static java.util.Objects.nonNull; +import static java.util.Objects.requireNonNull; @Slf4j public class LoadingPageServer implements AutoCloseable{ public static final int SERVER_PORT = 2031; public static final int DEFAULT_CLOSE_DELAY_SEC = 120; - private HttpServer server; + private volatile @Nullable HttpServer server = null; public void start() { try { - server = HttpServer.create(new InetSocketAddress(SERVER_PORT), 0); + HttpServer server = HttpServer.create(new InetSocketAddress(SERVER_PORT), 0); server.createContext("/", new LoadingPageHandler()); server.createContext("/main-app-port", new PortHandler()); - server.start(); - + this.server = server; String loadingPageUrl = "http://localhost:" + SERVER_PORT + "/loading"; BrowserHomePageOpener.open(loadingPageUrl); } catch (IOException e) { @@ -54,27 +56,20 @@ public void start() { } } - public void stopAfter(int delayInSec) { - if (isRunning()) { - server.stop(delayInSec); - server = null; - } - } - - public boolean isRunning() { - return server != null; - } - @Override public void close() { - stopAfter(DEFAULT_CLOSE_DELAY_SEC); + if (nonNull(server)) { + //noinspection DataFlowIssue + server.stop(DEFAULT_CLOSE_DELAY_SEC); + server = null; + } } static class LoadingPageHandler implements HttpHandler { @Override public void handle(HttpExchange exchange) throws IOException { byte[] data; - try (InputStream in = getClass().getResourceAsStream("/templates/loading.html")) { + try (InputStream in = requireNonNull(getClass().getResourceAsStream("/templates/loading.html"))) { ByteArrayOutputStream out = new ByteArrayOutputStream(); in.transferTo(out); data = out.toByteArray(); diff --git a/src/main/java/ru/investbook/loadingpage/LoadingPageServerUtils.java b/src/main/java/ru/investbook/loadingpage/LoadingPageServerUtils.java index f787f684..8e7add7c 100644 --- a/src/main/java/ru/investbook/loadingpage/LoadingPageServerUtils.java +++ b/src/main/java/ru/investbook/loadingpage/LoadingPageServerUtils.java @@ -20,6 +20,7 @@ import lombok.experimental.UtilityClass; import lombok.extern.slf4j.Slf4j; +import org.checkerframework.checker.nullness.qual.Nullable; import java.io.IOException; import java.io.InputStream; @@ -30,6 +31,7 @@ import java.util.Properties; import static java.nio.charset.StandardCharsets.UTF_8; +import static java.util.Objects.requireNonNull; @Slf4j @UtilityClass @@ -66,8 +68,8 @@ private static Properties loadProperties() throws IOException { properties.load(reader); } catch (Exception e) { // Properties file is not found in app installation path, read default file from class path - try (InputStream in = LoadingPageServerUtils.class.getClassLoader().getResourceAsStream(CONF_PROPERTIES); - Reader reader = new InputStreamReader(in, UTF_8)) { + try (@Nullable InputStream in = LoadingPageServerUtils.class.getResourceAsStream(CONF_PROPERTIES); + Reader reader = new InputStreamReader(requireNonNull(in), UTF_8)) { properties.load(reader); } } diff --git a/src/test/java/ru/investbook/LoadingPageServerTests.java b/src/test/java/ru/investbook/LoadingPageServerTest.java similarity index 95% rename from src/test/java/ru/investbook/LoadingPageServerTests.java rename to src/test/java/ru/investbook/LoadingPageServerTest.java index 918f81cc..248b7e3b 100644 --- a/src/test/java/ru/investbook/LoadingPageServerTests.java +++ b/src/test/java/ru/investbook/LoadingPageServerTest.java @@ -26,10 +26,10 @@ import org.mockito.junit.jupiter.MockitoExtension; import ru.investbook.loadingpage.LoadingPageServer; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.times; @ExtendWith(MockitoExtension.class) -class LoadingPageServerTests { +class LoadingPageServerTest { private LoadingPageServer loadingPageServer;