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

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@7480 6c8d7289-2bf4-0310-a012-ef5d649a1542
pull/1/head
orbiter 14 years ago
parent 5892fff51f
commit 6c52e31993

@ -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

@ -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";
/**

@ -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 = "";

@ -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

Loading…
Cancel
Save