Skip to content

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

new methods to open a browser - if YaCy is started with the option -gui, it is not in headless mode. Then the java 1.6 browse method is used if all other methods fail - in linux, the path /etc/alternatives/www-browser is used if no firefox is installed

orbiter6c52e3199344
diff --git a/defaults/yacy.init b/defaults/yacy.init
index 2391a515d..37768a31f 100644
--- a/defaults/yacy.init
+++ b/defaults/yacy.init
@@ -446,10 +446,8 @@ staticIP=
# status page. This is active by default, to make it easier for first-time
# users to understand what this application does. You can disable browser
# pop-up here or set a different start page, like the search page
-# the browser type is optional and works only under certain conditions
browserPopUpTrigger=true
browserPopUpPage=index.html
-browserPopUpApplication=firefox
# a forward page can be given for the index.html page
# when a user accesses the index.html page, he/she is forwarded to the page
diff --git a/source/de/anomic/search/SwitchboardConstants.java b/source/de/anomic/search/SwitchboardConstants.java
index f0f006c8b..7a2f2ceef 100644
--- a/source/de/anomic/search/SwitchboardConstants.java
+++ b/source/de/anomic/search/SwitchboardConstants.java
@@ -408,7 +408,6 @@ public final class SwitchboardConstants {
* browser pop up
*/
public static final String BROWSER_POP_UP_TRIGGER = "browserPopUpTrigger";
- public static final String BROWSER_POP_UP_APPLICATION = "browserPopUpApplication";
public static final String BROWSER_POP_UP_PAGE = "browserPopUpPage";
/**
diff --git a/source/net/yacy/gui/framework/Browser.java b/source/net/yacy/gui/framework/Browser.java
index 4f3bab3a5..f4ba82243 100644
--- a/source/net/yacy/gui/framework/Browser.java
+++ b/source/net/yacy/gui/framework/Browser.java
@@ -24,10 +24,11 @@
package net.yacy.gui.framework;
+import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
-import java.lang.reflect.Method;
+import java.net.URI;
import java.util.Properties;
import net.yacy.kelondro.logging.Log;
@@ -55,10 +56,6 @@ public class Browser {
// calculated system constants
public static int maxPathLength = 65535;
- // Macintosh-specific statics
- private static Class<?> macMRJFileUtils = null;
- private static Method macOpenURL = null;
-
// static initialization
static {
// check operation system type
@@ -76,65 +73,70 @@ public class Browser {
isWindows = (systemOS == systemWindows);
isWin32 = (isWindows && System.getProperty("os.arch", "").contains("x86"));
- // set up the MRJ Methods through reflection
- if (isMacArchitecture) try {
- macMRJFileUtils = Class.forName("com.apple.mrj.MRJFileUtils");
- macOpenURL = macMRJFileUtils.getMethod("openURL", new Class[] {Class.forName("java.lang.String")});
- final byte[] nullb = new byte[4];
- for (int i = 0; i < 4; i++) nullb[i] = 0;
- } catch (final Exception e) {
- //Log.logException(e);
- macMRJFileUtils = null;
- }
-
// set up maximum path length according to system
if (isWindows) maxPathLength = 255; else maxPathLength = 65535;
}
-
-
+
public static void openBrowser(final String url) {
- openBrowser(url, "firefox");
- }
-
- public static void openBrowser(final String url, final String app) {
+ boolean head = System.getProperty("java.awt.headless", "").equals("false");
try {
- String cmd;
- Process p;
- if (systemOS != systemUnknown) {
- if (systemOS == systemMacOSC) {
- if ((isMacArchitecture) && (macMRJFileUtils != null)) {
- macOpenURL.invoke(null, new Object[] {url});
- }
- } else if (systemOS == systemMacOSX) {
- p = Runtime.getRuntime().exec(new String[] {"/usr/bin/osascript", "-e", "open location \"" + url + "\""});
- p.waitFor();
- if (p.exitValue() != 0) throw new RuntimeException("EXEC ERROR: " + errorResponse(p));
- } else if (systemOS == systemUnix) {
- cmd = app + " -remote openURL(" + url + ")";
- p = Runtime.getRuntime().exec(cmd);
- p.waitFor();
- if (p.exitValue() != 0) {
- cmd = app + " " + url;
- p = Runtime.getRuntime().exec(cmd);
- // no error checking, because it blocks until firefox is closed
- }
- } else if (systemOS == systemWindows) {
- // see forum at http://forum.java.sun.com/thread.jsp?forum=57&thread=233364&message=838441
- if (System.getProperty("os.name").contains("2000")) cmd = "rundll32 url.dll,FileProtocolHandler " + url;
- else cmd = "rundll32 url.dll,FileProtocolHandler \"" + url + "\"";
- //cmd = "cmd.exe /c start javascript:document.location='" + url + "'";
- p = Runtime.getRuntime().exec(cmd);
- p.waitFor();
- if (p.exitValue() != 0) throw new RuntimeException("EXEC ERROR: " + errorResponse(p));
- }
+ if (systemOS == systemMacOSX) {
+ openBrowserMac(url);
+ } else if (systemOS == systemUnix) {
+ openBrowserUnix(url);
+ } else if (systemOS == systemWindows) {
+ openBrowserWin(url);
+ } else {
+ throw new RuntimeException("System unknown");
}
- } catch (final Exception e) {
- System.err.println("ERROR "+ e.getClass() +" in openBrowser(): "+ e.getMessage());
- // browser could not be started automatically, tell user to do it
- System.out.println("please start your browser and open the following location: " + url);
+ } catch (Exception e) {
+ if (head) try {
+ openBrowserJava(url);
+ } catch (Exception ee) {
+ logBrowserFail(url, ee);
+ } else {
+ logBrowserFail(url, e);
+ }
+ }
+ }
+
+ private static void openBrowserJava(final String url) throws Exception {
+ Desktop.getDesktop().browse(new URI(url));
+ }
+
+ private static void openBrowserMac(final String url) throws Exception {
+ Process p = Runtime.getRuntime().exec(new String[] {"/usr/bin/osascript", "-e", "open location \"" + url + "\""});
+ p.waitFor();
+ if (p.exitValue() != 0) throw new RuntimeException("EXEC ERROR: " + errorResponse(p));
+ }
+
+ private static void openBrowserUnix(final String url) throws Exception {
+ String cmd = "firefox -remote openURL(" + url + ")";
+ Process p = Runtime.getRuntime().exec(cmd);
+ p.waitFor();
+ if (p.exitValue() != 0) {
+ cmd = "/etc/alternatives/www-browser " + url;
+ p = Runtime.getRuntime().exec(cmd);
+ // no error checking, because it blocks until firefox is closed
}
}
+ private static void openBrowserWin(final String url) throws Exception {
+ // see forum at http://forum.java.sun.com/thread.jsp?forum=57&thread=233364&message=838441
+ String cmd;
+ if (System.getProperty("os.name").contains("2000")) cmd = "rundll32 url.dll,FileProtocolHandler " + url;
+ else cmd = "rundll32 url.dll,FileProtocolHandler \"" + url + "\"";
+ //cmd = "cmd.exe /c start javascript:document.location='" + url + "'";
+ Process p = Runtime.getRuntime().exec(cmd);
+ p.waitFor();
+ if (p.exitValue() != 0) throw new RuntimeException("EXEC ERROR: " + errorResponse(p));
+ }
+
+ private static void logBrowserFail(final String url, Exception e) {
+ if (e != null) Log.logException(e);
+ Log.logInfo("Browser", "please start your browser and open the following location: " + url);
+ }
+
private static String errorResponse(final Process p) {
final BufferedReader err = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line, error = "";
diff --git a/source/net/yacy/yacy.java b/source/net/yacy/yacy.java
index 44a118122..7036446d7 100644
--- a/source/net/yacy/yacy.java
+++ b/source/net/yacy/yacy.java
@@ -312,8 +312,7 @@ public final class yacy {
final String browserPopUpPage = sb.getConfig(SwitchboardConstants.BROWSER_POP_UP_PAGE, "ConfigBasic.html");
//boolean properPW = (sb.getConfig("adminAccount", "").length() == 0) && (sb.getConfig(httpd.ADMIN_ACCOUNT_B64MD5, "").length() > 0);
//if (!properPW) browserPopUpPage = "ConfigBasic.html";
- final String browserPopUpApplication = sb.getConfig(SwitchboardConstants.BROWSER_POP_UP_APPLICATION, "firefox");
- Browser.openBrowser((server.withSSL()?"https":"http") + "://localhost:" + serverCore.getPortNr(port) + "/" + browserPopUpPage, browserPopUpApplication);
+ Browser.openBrowser((server.withSSL()?"https":"http") + "://localhost:" + serverCore.getPortNr(port) + "/" + browserPopUpPage);
}
// unlock yacyTray browser popup