From c01cdbb7e2168cae6df009e6cdc70785e820ab95 Mon Sep 17 00:00:00 2001 From: Vaadin Bot Date: Tue, 24 Sep 2024 09:59:33 +0200 Subject: [PATCH] feat: Report application URL when application is started (#20027) (#20037) This is useful in a couple of cases: 1. Browser launcher is not enabled 2. Browser launcher does not work 3. You closed the browser tab and a new one is not automatically reopened when you restart Co-authored-by: Artur --- .../flow/internal/DevModeHandlerManager.java | 10 +++++++++ .../devserver/DevModeHandlerManagerImpl.java | 21 +++++++++++++++++++ .../flow/spring/DevModeBrowserLauncher.java | 17 +++++++++------ .../flow/spring/DevModeHandlerStopTest.java | 4 ++++ 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/flow-server/src/main/java/com/vaadin/flow/internal/DevModeHandlerManager.java b/flow-server/src/main/java/com/vaadin/flow/internal/DevModeHandlerManager.java index 503c0d19329..2b0d71788ba 100644 --- a/flow-server/src/main/java/com/vaadin/flow/internal/DevModeHandlerManager.java +++ b/flow-server/src/main/java/com/vaadin/flow/internal/DevModeHandlerManager.java @@ -87,6 +87,16 @@ void initDevModeHandler(Set> classes, VaadinContext context) */ void launchBrowserInDevelopmentMode(String url); + /** + * Sets the application URL for the given application. + *

+ * This is only called if the URL is known. + * + * @param applicationUrl + * the application url + */ + void setApplicationUrl(String applicationUrl); + /** * Gets the {@link DevModeHandler}. * diff --git a/vaadin-dev-server/src/main/java/com/vaadin/base/devserver/DevModeHandlerManagerImpl.java b/vaadin-dev-server/src/main/java/com/vaadin/base/devserver/DevModeHandlerManagerImpl.java index c9b80950cf5..abf339f2739 100644 --- a/vaadin-dev-server/src/main/java/com/vaadin/base/devserver/DevModeHandlerManagerImpl.java +++ b/vaadin-dev-server/src/main/java/com/vaadin/base/devserver/DevModeHandlerManagerImpl.java @@ -68,6 +68,9 @@ private static final class DevModeHandlerAlreadyStartedAttribute private BrowserLauncher browserLauncher; final private Set watchers = new HashSet<>(); + private String applicationUrl; + private boolean fullyStarted = false; + @Override public Class[] getHandlesTypes() { return DevModeStartupListener.class.getAnnotation(HandlesTypes.class) @@ -107,6 +110,7 @@ public void initDevModeHandler(Set> classes, VaadinContext context) startWatchingThemeFolder(context, config); watchExternalDependencies(context, config); + setFullyStarted(true); }); setDevModeStarted(context); this.browserLauncher = new BrowserLauncher(context); @@ -170,6 +174,23 @@ public void launchBrowserInDevelopmentMode(String url) { browserLauncher.launchBrowserInDevelopmentMode(url); } + @Override + public void setApplicationUrl(String applicationUrl) { + this.applicationUrl = applicationUrl; + reportApplicationUrl(); + } + + private void setFullyStarted(boolean fullyStarted) { + this.fullyStarted = fullyStarted; + reportApplicationUrl(); + } + + private void reportApplicationUrl() { + if (fullyStarted && applicationUrl != null) { + getLogger().info("Application running at {}", applicationUrl); + } + } + private void setDevModeStarted(VaadinContext context) { context.setAttribute(DevModeHandlerAlreadyStartedAttribute.class, new DevModeHandlerAlreadyStartedAttribute()); diff --git a/vaadin-spring/src/main/java/com/vaadin/flow/spring/DevModeBrowserLauncher.java b/vaadin-spring/src/main/java/com/vaadin/flow/spring/DevModeBrowserLauncher.java index 3b6dce95a80..66ab4045576 100644 --- a/vaadin-spring/src/main/java/com/vaadin/flow/spring/DevModeBrowserLauncher.java +++ b/vaadin-spring/src/main/java/com/vaadin/flow/spring/DevModeBrowserLauncher.java @@ -50,9 +50,8 @@ public void ready(ConfigurableApplicationContext context, VaadinConfigurationProperties properties = context .getBean(VaadinConfigurationProperties.class); - if (properties.isLaunchBrowser()) { - launchBrowserInDevelopmentMode(context); - } + maybeLaunchBrowserInDevelopmentMode(context, + properties.isLaunchBrowser()); } catch (Exception e) { getLogger().debug("Failed to launch browser", e); } @@ -66,8 +65,11 @@ public void ready(ConfigurableApplicationContext context, * * @param appContext * the application context + * @param launch + * true to launch the browser, false to only report the url */ - private void launchBrowserInDevelopmentMode(ApplicationContext appContext) { + private void maybeLaunchBrowserInDevelopmentMode( + ApplicationContext appContext, boolean launch) { if (!(appContext instanceof GenericWebApplicationContext)) { getLogger().warn( "Unable to determine production mode for an Spring Boot application context of type " @@ -82,8 +84,11 @@ private void launchBrowserInDevelopmentMode(ApplicationContext appContext) { DevModeHandlerManager devModeHandlerManager = lookup .lookup(DevModeHandlerManager.class); if (devModeHandlerManager != null) { - devModeHandlerManager - .launchBrowserInDevelopmentMode(getUrl(webAppContext)); + String url = getUrl(webAppContext); + devModeHandlerManager.setApplicationUrl(url); + if (launch) { + devModeHandlerManager.launchBrowserInDevelopmentMode(url); + } } } diff --git a/vaadin-spring/src/test/java/com/vaadin/flow/spring/DevModeHandlerStopTest.java b/vaadin-spring/src/test/java/com/vaadin/flow/spring/DevModeHandlerStopTest.java index 9cd2a6fe2b9..701517c5503 100644 --- a/vaadin-spring/src/test/java/com/vaadin/flow/spring/DevModeHandlerStopTest.java +++ b/vaadin-spring/src/test/java/com/vaadin/flow/spring/DevModeHandlerStopTest.java @@ -113,6 +113,10 @@ public DevModeHandler getDevModeHandler() { public void launchBrowserInDevelopmentMode(String url) { } + + @Override + public void setApplicationUrl(String applicationUrl) { + } } @TestConfiguration