Skip to content

Distributed Peer-to-Peer Web Search Engine and Intranet Search Appliance

Prevent yacy main thread from hanging on browser opening process.

lucciomanb8f645815253
diff --git a/source/net/yacy/gui/framework/Browser.java b/source/net/yacy/gui/framework/Browser.java
index 62e7d4ac7..c49235dfa 100644
--- a/source/net/yacy/gui/framework/Browser.java
+++ b/source/net/yacy/gui/framework/Browser.java
@@ -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!");
+ }
}
diff --git a/source/net/yacy/yacy.java b/source/net/yacy/yacy.java
index 0176185b4..4813f00f6 100644
--- a/source/net/yacy/yacy.java
+++ b/source/net/yacy/yacy.java
@@ -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