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

feat: Report application URL when application is started #20027

Merged
merged 4 commits into from
Sep 24, 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
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
Loading