diff --git a/src/main/java/de/doubleslash/usb_led_matrix/graph/Graph.java b/src/main/java/de/doubleslash/usb_led_matrix/graph/Graph.java index c28eec0..7fc3380 100644 --- a/src/main/java/de/doubleslash/usb_led_matrix/graph/Graph.java +++ b/src/main/java/de/doubleslash/usb_led_matrix/graph/Graph.java @@ -12,6 +12,7 @@ import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; @@ -44,15 +45,14 @@ public class Graph { private HttpClient client = HttpClient.newHttpClient(); public Graph() { - try { - final InputStream input = new FileInputStream(refreshFile); + try (FileInputStream input = new FileInputStream(refreshFile)) { properties.load(input); final String tenantID = properties.getProperty(TENANT_ID_PROPERTY); if (tenantID != null) { tokenURI = String.format("https://login.microsoftonline.com/%s/oauth2/v2.0/token", tenantID); } } catch (IOException e) { - LOG.error("Could not load properties from file '{}'", refreshFile.getAbsolutePath(), e); + LOG.error("Error loading properties from file '{}'", refreshFile.getAbsolutePath(), e); } } @@ -143,8 +143,9 @@ private HttpRequest.BodyPublisher ofFormData(final Map data) { } private void getNewAccessToken() throws IOException, InterruptedException { - final InputStream input = new FileInputStream(refreshFile); - properties.load(input); + try (InputStream input = new FileInputStream(refreshFile)) { + properties.load(input); + } final Map values = new HashMap<>(); @@ -213,6 +214,10 @@ public Color getStatusColorFromTeams() throws IOException, InterruptedException } catch (final IllegalArgumentException e) { LOG.warn("Unknown availability '{}'.", teamsStatus, e); } - return availabilityStatus.getColor(); + if (availabilityStatus != null) { + return availabilityStatus.getColor(); + } else { + return null; + } } } diff --git a/src/main/java/de/doubleslash/usb_led_matrix/view/AuthenticationView.java b/src/main/java/de/doubleslash/usb_led_matrix/view/AuthenticationView.java index 560e7a5..910d530 100644 --- a/src/main/java/de/doubleslash/usb_led_matrix/view/AuthenticationView.java +++ b/src/main/java/de/doubleslash/usb_led_matrix/view/AuthenticationView.java @@ -80,8 +80,7 @@ public void customInitialize() { CommandLineOptions.Dark(scene); } final String fileName = "refreshToken.txt"; - try { - final InputStream input = new FileInputStream(new File(fileName)); + try (InputStream input = new FileInputStream(new File(fileName))) { final Properties properties = new Properties(); properties.load(input); final String tenantID = properties.getProperty("tenant_id"); @@ -136,9 +135,7 @@ public void setScene(final Scene scene) { @FXML void setTeamsID(ActionEvent event) { - final FileWriter fw; - try { - fw = new FileWriter("refreshToken.txt"); + try (FileWriter fw = new FileWriter("refreshToken.txt")) { final Properties p = new Properties(); p.setProperty("tenant_id", tenantIDTextField.getText()); p.setProperty("client_id", clientIDTextField.getText()); @@ -183,8 +180,11 @@ public void setWebsiteCode() { initializeCopyButton(); this.deviceCode.set(deviceCode); - } catch (IOException | InterruptedException e) { + } catch (IOException e) { LOG.error("Could not retrieve device code: ", e); + } catch (InterruptedException e) { + LOG.error("CurrentThread was interrupt", e); + Thread.currentThread().interrupt(); } } } diff --git a/src/main/java/de/doubleslash/usb_led_matrix/view/ConfigurationView.java b/src/main/java/de/doubleslash/usb_led_matrix/view/ConfigurationView.java index 82b5267..562f962 100644 --- a/src/main/java/de/doubleslash/usb_led_matrix/view/ConfigurationView.java +++ b/src/main/java/de/doubleslash/usb_led_matrix/view/ConfigurationView.java @@ -39,7 +39,7 @@ import java.util.Optional; public class ConfigurationView { - private static final int POLLING_INTERVALL_SECONDS = 10; + private static final long POLLING_INTERVALL_SECONDS = 10; private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); @@ -94,6 +94,7 @@ public class ConfigurationView { Thread.sleep(POLLING_INTERVALL_SECONDS * 1000); } catch (final InterruptedException e) { LOG.debug("Manual mode was interrupted.", e); + Thread.currentThread().interrupt(); return; } } @@ -111,6 +112,7 @@ public class ConfigurationView { Thread.sleep(POLLING_INTERVALL_SECONDS * 1000); } catch (final InterruptedException ie) { LOG.debug("MS Teams status polling was interrupted.", ie); + Thread.currentThread().interrupt(); return; } } @@ -124,6 +126,7 @@ public class ConfigurationView { Thread.sleep(POLLING_INTERVALL_SECONDS * 1000); } catch (final InterruptedException e) { LOG.debug("Connection check was interrupted.", e); + Thread.currentThread().interrupt(); return; } turnOffAutomaticallyIfNeeded(LocalTime.now()); @@ -137,6 +140,8 @@ public class ConfigurationView { Thread.sleep(POLLING_INTERVALL_SECONDS * 1000); } catch (final InterruptedException e) { LOG.debug("Reconnection check was interrupted.", e); + Thread.currentThread().interrupt(); + return; } turnOffAutomaticallyIfNeeded(LocalTime.now()); @@ -154,8 +159,11 @@ protected Void call() { answerBody = graph.pollingForToken(model.getDeviceCode()); LOG.debug("Polling for token. Answer was: '{}'.", answerBody); Thread.sleep(POLLING_INTERVALL_SECONDS * 500); - } catch (IOException | InterruptedException e) { + } catch (IOException e) { + LOG.error("Could not get answer from server.", e); + } catch (InterruptedException e) { LOG.error("Could not get answer from server.", e); + Thread.currentThread().interrupt(); } } if (answerBody.contains("access_token")) {