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 de61124
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 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;
}
}

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();
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)) {
properties.load(reader);
}
}
Expand Down

0 comments on commit de61124

Please sign in to comment.