Skip to content

Commit

Permalink
fix checker framework warn for LoadingPageServer
Browse files Browse the repository at this point in the history
  • Loading branch information
vananiev committed Nov 3, 2024
1 parent 412776c commit 2db0362
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
31 changes: 13 additions & 18 deletions src/main/java/ru/investbook/loadingpage/LoadingPageServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,51 +31,45 @@
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) {
log.warn("Can't open /loading page", e);
}
}

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;

Check warning on line 64 in src/main/java/ru/investbook/loadingpage/LoadingPageServer.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/ru/investbook/loadingpage/LoadingPageServer.java#L63-L64

Added lines #L63 - L64 were not covered by tests
}
}

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"))) {

Check warning on line 72 in src/main/java/ru/investbook/loadingpage/LoadingPageServer.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/ru/investbook/loadingpage/LoadingPageServer.java#L72

Added line #L72 was not covered by tests
ByteArrayOutputStream out = new ByteArrayOutputStream();
in.transferTo(out);
data = out.toByteArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -30,6 +31,7 @@
import java.util.Properties;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Objects.requireNonNull;

@Slf4j
@UtilityClass
Expand Down Expand Up @@ -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)) {

Check warning on line 72 in src/main/java/ru/investbook/loadingpage/LoadingPageServerUtils.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/ru/investbook/loadingpage/LoadingPageServerUtils.java#L71-L72

Added lines #L71 - L72 were not covered by tests
properties.load(reader);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 2db0362

Please sign in to comment.