less noise if a browser cannot be opened

pull/1/head
Michael Peter Christen 13 years ago
parent eff966f396
commit 087f97d4c0

@ -2,7 +2,7 @@
* Browser * Browser
* Copyright 2010 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany * Copyright 2010 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
* First released 05.08.2010 at http://yacy.net * First released 05.08.2010 at http://yacy.net
* *
* $LastChangedDate$ * $LastChangedDate$
* $LastChangedRevision$ * $LastChangedRevision$
* $LastChangedBy$ * $LastChangedBy$
@ -11,12 +11,12 @@
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either * License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version. * version 2.1 of the License, or (at your option) any later version.
* *
* This library is distributed in the hope that it will be useful, * This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details. * Lesser General Public License for more details.
* *
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with this program in the file lgpl21.txt * along with this program in the file lgpl21.txt
* If not, see <http://www.gnu.org/licenses/>. * If not, see <http://www.gnu.org/licenses/>.
@ -61,11 +61,17 @@ public class Browser {
// check operation system type // check operation system type
final Properties sysprop = System.getProperties(); final Properties sysprop = System.getProperties();
final String sysname = sysprop.getProperty("os.name","").toLowerCase(); final String sysname = sysprop.getProperty("os.name","").toLowerCase();
if (sysname.startsWith("mac os x")) systemOS = systemMacOSX; if (sysname.startsWith("mac os x")) {
else if (sysname.startsWith("mac os")) systemOS = systemMacOSC; systemOS = systemMacOSX;
else if (sysname.startsWith("windows")) systemOS = systemWindows; } else if (sysname.startsWith("mac os")) {
else if ((sysname.startsWith("linux")) || (sysname.startsWith("unix"))) systemOS = systemUnix; systemOS = systemMacOSC;
else systemOS = systemUnknown; } else if (sysname.startsWith("windows")) {
systemOS = systemWindows;
} else if ((sysname.startsWith("linux")) || (sysname.startsWith("unix"))) {
systemOS = systemUnix;
} else {
systemOS = systemUnknown;
}
isMacArchitecture = ((systemOS == systemMacOSC) || (systemOS == systemMacOSX)); isMacArchitecture = ((systemOS == systemMacOSC) || (systemOS == systemMacOSX));
isUnixFS = ((systemOS == systemMacOSX) || (systemOS == systemUnix)); isUnixFS = ((systemOS == systemMacOSX) || (systemOS == systemUnix));
@ -74,9 +80,13 @@ public class Browser {
isWin32 = (isWindows && System.getProperty("os.arch", "").contains("x86")); isWin32 = (isWindows && System.getProperty("os.arch", "").contains("x86"));
// set up maximum path length according to system // set up maximum path length according to system
if (isWindows) maxPathLength = 255; else maxPathLength = 65535; if (isWindows) {
maxPathLength = 255;
} else {
maxPathLength = 65535;
}
} }
public static void openBrowser(final String url) { public static void openBrowser(final String url) {
boolean head = System.getProperty("java.awt.headless", "").equals("false"); boolean head = System.getProperty("java.awt.headless", "").equals("false");
try { try {
@ -93,11 +103,13 @@ public class Browser {
} else { } else {
throw new RuntimeException("System unknown"); throw new RuntimeException("System unknown");
} }
} catch (Exception e) { } catch (Throwable e) {
if (head) try { if (head) {
openBrowserJava(url); try {
} catch (Exception ee) { openBrowserJava(url);
logBrowserFail(url, ee); } catch (Exception ee) {
logBrowserFail(url, ee);
}
} else { } else {
logBrowserFail(url, e); logBrowserFail(url, e);
} }
@ -107,18 +119,22 @@ public class Browser {
private static void openBrowserJava(final String url) throws Exception { private static void openBrowserJava(final String url) throws Exception {
Desktop.getDesktop().browse(new URI(url)); Desktop.getDesktop().browse(new URI(url));
} }
private static void openBrowserMac(final String url) throws Exception { private static void openBrowserMac(final String url) throws Exception {
Process p = Runtime.getRuntime().exec(new String[] {"/usr/bin/osascript", "-e", "open location \"" + url + "\""}); Process p = Runtime.getRuntime().exec(new String[] {"/usr/bin/osascript", "-e", "open location \"" + url + "\""});
p.waitFor(); p.waitFor();
if (p.exitValue() != 0) throw new RuntimeException("Mac Exec Error: " + errorResponse(p)); if (p.exitValue() != 0) {
throw new RuntimeException("Mac Exec Error: " + errorResponse(p));
}
} }
private static void openBrowserUnixFirefox(final String url) throws Exception { private static void openBrowserUnixFirefox(final String url) throws Exception {
String cmd = "firefox -remote openURL(" + url + ")"; String cmd = "firefox -remote openURL(" + url + ")";
Process p = Runtime.getRuntime().exec(cmd); Process p = Runtime.getRuntime().exec(cmd);
p.waitFor(); p.waitFor();
if (p.exitValue() != 0) throw new RuntimeException("Unix Exec Error/Firefox: " + errorResponse(p)); if (p.exitValue() != 0) {
throw new RuntimeException("Unix Exec Error/Firefox: " + errorResponse(p));
}
} }
/* /*
private static void openBrowserUnixGeneric(final String url) throws Exception { private static void openBrowserUnixGeneric(final String url) throws Exception {
@ -131,19 +147,24 @@ public class Browser {
private static void openBrowserWin(final String url) throws Exception { 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 // see forum at http://forum.java.sun.com/thread.jsp?forum=57&thread=233364&message=838441
String cmd; String cmd;
if (System.getProperty("os.name").contains("2000")) cmd = "rundll32 url.dll,FileProtocolHandler " + url; if (System.getProperty("os.name").contains("2000")) {
else cmd = "rundll32 url.dll,FileProtocolHandler \"" + url + "\""; cmd = "rundll32 url.dll,FileProtocolHandler " + url;
} else {
cmd = "rundll32 url.dll,FileProtocolHandler \"" + url + "\"";
}
//cmd = "cmd.exe /c start javascript:document.location='" + url + "'"; //cmd = "cmd.exe /c start javascript:document.location='" + url + "'";
Process p = Runtime.getRuntime().exec(cmd); Process p = Runtime.getRuntime().exec(cmd);
p.waitFor(); p.waitFor();
if (p.exitValue() != 0) throw new RuntimeException("EXEC ERROR: " + errorResponse(p)); if (p.exitValue() != 0) {
throw new RuntimeException("EXEC ERROR: " + errorResponse(p));
}
} }
private static void logBrowserFail(final String url, Exception e) { private static void logBrowserFail(final String url, Throwable e) {
if (e != null) Log.logException(e); //if (e != null) Log.logException(e);
Log.logInfo("Browser", "please start your browser and open the following location: " + url); Log.logInfo("Browser", "please start your browser and open the following location: " + url);
} }
private static String errorResponse(final Process p) { private static String errorResponse(final Process p) {
final BufferedReader err = new BufferedReader(new InputStreamReader(p.getErrorStream())); final BufferedReader err = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line, error = ""; String line, error = "";
@ -162,7 +183,7 @@ public class Browser {
} }
} }
} }
public static void main(final String[] args) { public static void main(final String[] args) {
if ("-u".equals(args[0])) { if ("-u".equals(args[0])) {
openBrowser(args[1]); openBrowser(args[1]);

Loading…
Cancel
Save