From 08ae0d6c5d07a5e37a071a9c47afe7c045b268ac Mon Sep 17 00:00:00 2001 From: Jonathan Cobb Date: Mon, 6 Jan 2020 12:34:07 -0500 Subject: [PATCH] better default for publicUriBase, add BrowserLauncherListener --- .../config/RestServerConfiguration.java | 6 ++- .../listener/BrowserLauncherListener.java | 42 +++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 wizard-server/src/main/java/org/cobbzilla/wizard/server/listener/BrowserLauncherListener.java diff --git a/wizard-server/src/main/java/org/cobbzilla/wizard/server/config/RestServerConfiguration.java b/wizard-server/src/main/java/org/cobbzilla/wizard/server/config/RestServerConfiguration.java index 27fa3ab..862ec4a 100644 --- a/wizard-server/src/main/java/org/cobbzilla/wizard/server/config/RestServerConfiguration.java +++ b/wizard-server/src/main/java/org/cobbzilla/wizard/server/config/RestServerConfiguration.java @@ -31,6 +31,7 @@ import java.util.concurrent.TimeUnit; import static org.cobbzilla.util.daemon.ZillaRuntime.die; import static org.cobbzilla.util.daemon.ZillaRuntime.empty; +import static org.cobbzilla.util.network.NetworkUtil.getLocalhostIpv4; import static org.cobbzilla.util.reflect.ReflectionUtil.forName; import static org.cobbzilla.util.reflect.ReflectionUtil.instantiate; @@ -53,7 +54,10 @@ public class RestServerConfiguration { @Getter @Setter private String serverName; @Setter private String publicUriBase; - public String getPublicUriBase () { return !empty(publicUriBase) && publicUriBase.endsWith("/") ? publicUriBase.substring(0, publicUriBase.length()-1) : publicUriBase; } + public String getPublicUriBase () { + if (empty(publicUriBase)) return "http://"+getLocalhostIpv4()+":"+getHttp().getPort(); + return !empty(publicUriBase) && publicUriBase.endsWith("/") ? publicUriBase.substring(0, publicUriBase.length()-1) : publicUriBase; + } @Getter @Setter private String springContextPath = "classpath:/spring.xml"; @Getter @Setter private String springShardContextPath = "classpath:/spring-shard.xml"; diff --git a/wizard-server/src/main/java/org/cobbzilla/wizard/server/listener/BrowserLauncherListener.java b/wizard-server/src/main/java/org/cobbzilla/wizard/server/listener/BrowserLauncherListener.java new file mode 100644 index 0000000..fae9198 --- /dev/null +++ b/wizard-server/src/main/java/org/cobbzilla/wizard/server/listener/BrowserLauncherListener.java @@ -0,0 +1,42 @@ +package org.cobbzilla.wizard.server.listener; + +import lombok.extern.slf4j.Slf4j; +import org.cobbzilla.util.http.URIUtil; +import org.cobbzilla.wizard.server.RestServer; +import org.cobbzilla.wizard.server.RestServerLifecycleListenerBase; +import org.cobbzilla.wizard.server.config.RestServerConfiguration; + +import java.awt.*; + +import static java.awt.Desktop.isDesktopSupported; +import static org.cobbzilla.util.daemon.ZillaRuntime.die; + +@Slf4j +public class BrowserLauncherListener extends RestServerLifecycleListenerBase { + + @Override public void onStart(RestServer server) { + final RestServerConfiguration config = server.getConfiguration(); + final String baseUri = config.getPublicUriBase(); + final Thread appThread = new Thread(new Runnable() { + @Override public void run() { + final Desktop desktop = isDesktopSupported() ? Desktop.getDesktop() : null; + if (desktop != null && desktop.isSupported(Desktop.Action.BROWSE)) { + try { + desktop.browse(URIUtil.toUri(baseUri)); + } catch (Exception e) { + final String msg = "onStart: error launching default browser with url '" + baseUri + "': " + e; + log.error(msg, e); + die(msg, e); + } + } else { + // no browser. tell the user where the server is listening via log statement + log.info("\n\n"+server.getConfiguration().getServerName()+" Successfully Started\n\nNot launching browser: System lacks a browser and/or desktop window manager.\n\nWeb UI is: "+baseUri+"\nAPI is: "+baseUri+"/api\nHit Control-C to stop the server\n"); + } + } + }); + appThread.setDaemon(true); + appThread.start(); + + super.onStart(server); + } +}