Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open on running feature #155

Merged
merged 4 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ java {
}

group = 'online.hatsunemiku'
version = '1.13.0'
version = '1.13.1'
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -34,11 +37,51 @@ 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 =
new SpringApplicationBuilder(TachideskVaadinUiApplication.class).headless(headless).build();

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...");

try {
BrowserUtils.openBrowser("http://localhost:3901");
} catch (IOException e) {
log.error("Failed to open browser.", e);
}
}
}

/**
* 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.
return false;
} catch (IOException e) {
return true;
}
}
}