Просмотр исходного кода

better default for publicUriBase, add BrowserLauncherListener

tags/2.0.1
Jonathan Cobb 4 лет назад
Родитель
Сommit
08ae0d6c5d
2 измененных файлов: 47 добавлений и 1 удалений
  1. +5
    -1
      wizard-server/src/main/java/org/cobbzilla/wizard/server/config/RestServerConfiguration.java
  2. +42
    -0
      wizard-server/src/main/java/org/cobbzilla/wizard/server/listener/BrowserLauncherListener.java

+ 5
- 1
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";


+ 42
- 0
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);
}
}

Загрузка…
Отмена
Сохранить