Prevent yacy main thread from hanging on browser opening process.

First fix for mantis 689 (http://mantis.tokeek.de/view.php?id=689).

On Debian Linux, with a headless jre and no open browser,
browser.openBrowserClassic() was called and waited forever the browser
process end (p.waitFor()). YaCy shutdown was therefore not working until
the browser was closed.

Also modified browser opening command for Unix platform to open the
default the browser (with xdg-open util) instead of Firefox.

xdg-open also has the advantage to be asynchronous (not blocking).
pull/77/head
luccioman 8 years ago
parent 4aba491156
commit b8f6458152

@ -105,13 +105,14 @@ public class Browser {
if (systemOS == systemMacOSX) {
openBrowserMac(url);
} else if (systemOS == systemUnix) {
openBrowserUnixFirefox(url);
openDefaultUnixBrowser(url);
} else if (systemOS == systemWindows) {
openBrowserWin(url);
} else {
throw new RuntimeException("System unknown");
}
} catch (final Throwable e) {
ConcurrentLog.warn("BROWSER", "Could not open browser : " + e.getMessage() != null ? e.getMessage() : e.toString());
}
}
@ -123,12 +124,21 @@ public class Browser {
}
}
private static void openBrowserUnixFirefox(final String url) throws Exception {
String cmd = "firefox " + url;
/**
* Tries to open the default browser on Unix platform.
* @param url the url to open. Must not be null.
* @throws Exception when an error occured
*/
private static void openDefaultUnixBrowser(final String url) throws Exception {
/* Use the freedesktop xdg-open to open url with the default browser.
* xdg-open is included in xdg-utils tools set (https://www.freedesktop.org/wiki/Software/xdg-utils/)
* It is part of the LSB (Linux Standard Base) and therefore included in all recent Linux Distributions supporting it
* (see https://www.linuxbase.org/navigator/browse/cmd_single.php?cmd=list-by-name&Section=ABI&Cname=xdg-open) */
String cmd = "xdg-open " + url;
Process p = Runtime.getRuntime().exec(cmd);
p.waitFor();
if (p.exitValue() != 0) {
throw new RuntimeException("Unix Exec Error/Firefox: " + errorResponse(p));
throw new RuntimeException("Unix Exec Error/xdg-open: " + errorResponse(p));
}
}
@ -167,9 +177,20 @@ public class Browser {
}
}
/**
* Test platform specific browser opening.
* @param args
*/
public static void main(final String[] args) {
if ("-u".equals(args[0])) {
openBrowser(args[1]);
}
}
try {
if (args.length > 0 && "-u".equals(args[0])) {
openBrowser(args[1]);
} else {
System.out.println("Usage java " + Browser.class.getCanonicalName() + " -u [URL]");
}
} finally {
ConcurrentLog.shutdown();
}
System.out.println("The End!");
}
}

@ -304,7 +304,14 @@ public final class yacy {
final String browserPopUpPage = sb.getConfig(SwitchboardConstants.BROWSER_POP_UP_PAGE, "ConfigBasic.html");
//boolean properPW = (sb.getConfig(SwitchboardConstants.ADMIN_ACCOUNT, "").isEmpty()) && (sb.getConfig(httpd.ADMIN_ACCOUNT_B64MD5, "").length() > 0);
//if (!properPW) browserPopUpPage = "ConfigBasic.html";
Browser.openBrowser(("http://localhost:"+port) + "/" + browserPopUpPage);
/* YaCy main startup process must not hang because browser opening is long or fails.
* Let's open try opening the browser in a separate thread */
new Thread("Browser opening") {
@Override
public void run() {
Browser.openBrowser(("http://localhost:"+port) + "/" + browserPopUpPage);
}
}.start();
// Browser.openBrowser((server.withSSL()?"https":"http") + "://localhost:" + serverCore.getPortNr(port) + "/" + browserPopUpPage);
} catch (final Throwable e) {
// cannot open browser. This may be normal in headless environments

Loading…
Cancel
Save