From d9c5fa5907a80c5dacb401f1fef27f47eb199c6f Mon Sep 17 00:00:00 2001 From: Alessandro Schwaiger Date: Fri, 6 Dec 2024 21:15:45 +0100 Subject: [PATCH 1/4] Open Browser if an instance is already running --- .../TachideskVaadinUiApplication.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/main/java/online/hatsunemiku/tachideskvaadinui/TachideskVaadinUiApplication.java b/src/main/java/online/hatsunemiku/tachideskvaadinui/TachideskVaadinUiApplication.java index d1c2fb7..05f65e1 100644 --- a/src/main/java/online/hatsunemiku/tachideskvaadinui/TachideskVaadinUiApplication.java +++ b/src/main/java/online/hatsunemiku/tachideskvaadinui/TachideskVaadinUiApplication.java @@ -6,7 +6,10 @@ package online.hatsunemiku.tachideskvaadinui; +import java.io.IOException; +import java.net.ServerSocket; import lombok.extern.slf4j.Slf4j; +import online.hatsunemiku.tachideskvaadinui.utils.BrowserUtils; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; @@ -34,6 +37,12 @@ public class TachideskVaadinUiApplication { public static void main(String[] args) { boolean headless = Boolean.parseBoolean(System.getProperty("vaaui.headless")); + if (isRunningAlready()) { + log.warn("Application is already running."); + runAlreadyRunningTasks(headless); + System.exit(0); + } + log.info("Is headless: {}", headless); SpringApplication app = @@ -41,4 +50,25 @@ public static void main(String[] args) { app.run(args); } + + private static void runAlreadyRunningTasks(boolean headless) { + if (!headless) { + log.info("Opening browser..."); + + try { + BrowserUtils.openBrowser("http://localhost:3901"); + } catch (IOException e) { + log.error("Failed to open browser.", e); + } + } + } + + private static boolean isRunningAlready() { + int port = 3901; + try (ServerSocket ignored = new ServerSocket(port)) { + return false; + } catch (IOException e) { + return true; + } + } } From c07b5db10a69e878edd6a0db51b3a673581b3b4a Mon Sep 17 00:00:00 2001 From: Alessandro Schwaiger Date: Fri, 6 Dec 2024 21:15:52 +0100 Subject: [PATCH 2/4] bump version --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index ab5a708..798d622 100644 --- a/build.gradle +++ b/build.gradle @@ -23,7 +23,7 @@ java { } group = 'online.hatsunemiku' -version = '1.13.0' +version = '1.13.1' sourceCompatibility = JavaVersion.VERSION_21 targetCompatibility = JavaVersion.VERSION_21 From a64318a1a931cd379ac9e927e34708efee5cb6ce Mon Sep 17 00:00:00 2001 From: Alessandro Schwaiger Date: Fri, 6 Dec 2024 21:27:06 +0100 Subject: [PATCH 3/4] Add docs --- .../TachideskVaadinUiApplication.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/online/hatsunemiku/tachideskvaadinui/TachideskVaadinUiApplication.java b/src/main/java/online/hatsunemiku/tachideskvaadinui/TachideskVaadinUiApplication.java index 05f65e1..b43ef77 100644 --- a/src/main/java/online/hatsunemiku/tachideskvaadinui/TachideskVaadinUiApplication.java +++ b/src/main/java/online/hatsunemiku/tachideskvaadinui/TachideskVaadinUiApplication.java @@ -51,6 +51,10 @@ public static void main(String[] args) { app.run(args); } + /** + * Runs the tasks that should be run when the application is already running. + * @param headless Whether the application is running in headless mode + */ private static void runAlreadyRunningTasks(boolean headless) { if (!headless) { log.info("Opening browser..."); @@ -63,9 +67,13 @@ private static void runAlreadyRunningTasks(boolean headless) { } } + /** + * Checks if the application is already running. + * @return {@code true} if the application is already running, {@code false} otherwise + */ private static boolean isRunningAlready() { int port = 3901; - try (ServerSocket ignored = new ServerSocket(port)) { + try (ServerSocket ignored = new ServerSocket(port)) { // skipcq: JAVA-S1011 - Not a security risk, because the socket is closed immediately. return false; } catch (IOException e) { return true; From ab6464b9ad6bea065f30483bc4c896e425b21760 Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Fri, 6 Dec 2024 20:27:28 +0000 Subject: [PATCH 4/4] style: format code with Google Java Format This commit fixes the style issues introduced in a64318a according to the output from Google Java Format. Details: https://github.com/Suwayomi/Suwayomi-VaadinUI/pull/155 --- .../tachideskvaadinui/TachideskVaadinUiApplication.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/online/hatsunemiku/tachideskvaadinui/TachideskVaadinUiApplication.java b/src/main/java/online/hatsunemiku/tachideskvaadinui/TachideskVaadinUiApplication.java index b43ef77..9b8358d 100644 --- a/src/main/java/online/hatsunemiku/tachideskvaadinui/TachideskVaadinUiApplication.java +++ b/src/main/java/online/hatsunemiku/tachideskvaadinui/TachideskVaadinUiApplication.java @@ -53,6 +53,7 @@ public static void main(String[] args) { /** * Runs the tasks that should be run when the application is already running. + * * @param headless Whether the application is running in headless mode */ private static void runAlreadyRunningTasks(boolean headless) { @@ -69,11 +70,15 @@ private static void runAlreadyRunningTasks(boolean headless) { /** * Checks if the application is already running. + * * @return {@code true} if the application is already running, {@code false} otherwise */ private static boolean isRunningAlready() { int port = 3901; - try (ServerSocket ignored = new ServerSocket(port)) { // skipcq: JAVA-S1011 - Not a security risk, because the socket is closed immediately. + try (ServerSocket ignored = + new ServerSocket( + port)) { // skipcq: JAVA-S1011 - Not a security risk, because the socket is closed + // immediately. return false; } catch (IOException e) { return true;