Skip to content

Commit

Permalink
feat: Report application URL when application is started (#20027) (#2…
Browse files Browse the repository at this point in the history
…0037)

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 <[email protected]>
  • Loading branch information
vaadin-bot and Artur- authored Sep 24, 2024
1 parent acb4a4c commit c01cdbb
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ void initDevModeHandler(Set<Class<?>> classes, VaadinContext context)
*/
void launchBrowserInDevelopmentMode(String url);

/**
* Sets the application URL for the given application.
* <p>
* This is only called if the URL is known.
*
* @param applicationUrl
* the application url
*/
void setApplicationUrl(String applicationUrl);

/**
* Gets the {@link DevModeHandler}.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ private static final class DevModeHandlerAlreadyStartedAttribute
private BrowserLauncher browserLauncher;
final private Set<Closeable> watchers = new HashSet<>();

private String applicationUrl;
private boolean fullyStarted = false;

@Override
public Class<?>[] getHandlesTypes() {
return DevModeStartupListener.class.getAnnotation(HandlesTypes.class)
Expand Down Expand Up @@ -107,6 +110,7 @@ public void initDevModeHandler(Set<Class<?>> classes, VaadinContext context)

startWatchingThemeFolder(context, config);
watchExternalDependencies(context, config);
setFullyStarted(true);
});
setDevModeStarted(context);
this.browserLauncher = new BrowserLauncher(context);
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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 "
Expand All @@ -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);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ public DevModeHandler getDevModeHandler() {
public void launchBrowserInDevelopmentMode(String url) {

}

@Override
public void setApplicationUrl(String applicationUrl) {
}
}

@TestConfiguration
Expand Down

0 comments on commit c01cdbb

Please sign in to comment.