PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/source/net/yacy/gui/framework/Browser.java

https://gitorious.org/yacy
Java | 192 lines | 129 code | 18 blank | 45 comment | 45 complexity | ab1cfe365368df9df1cb7393cde8868f MD5 | raw file
Possible License(s): Apache-2.0, JSON, BSD-3-Clause, LGPL-3.0, GPL-2.0, LGPL-2.1
  1. /**
  2. * Browser
  3. * Copyright 2010 by Michael Peter Christen; mc@yacy.net, Frankfurt a. M., Germany
  4. * First released 05.08.2010 at http://yacy.net
  5. *
  6. * $LastChangedDate$
  7. * $LastChangedRevision$
  8. * $LastChangedBy$
  9. *
  10. * This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public License
  21. * along with this program in the file lgpl21.txt
  22. * If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. package net.yacy.gui.framework;
  25. import java.awt.Desktop;
  26. import java.io.BufferedReader;
  27. import java.io.IOException;
  28. import java.io.InputStreamReader;
  29. import java.net.URI;
  30. import java.util.Properties;
  31. import net.yacy.cora.util.ConcurrentLog;
  32. public class Browser {
  33. // constants for system identification
  34. public static final int systemMacOSC = 0; // 'classic' Mac OS 7.6.1/8.*/9.*
  35. public static final int systemMacOSX = 1; // all Mac OS X
  36. public static final int systemUnix = 2; // all Unix/Linux type systems
  37. public static final int systemWindows = 3; // all Windows 95/98/NT/2K/XP
  38. public static final int systemUnknown = -1; // any other system
  39. // constants for file type identification (Mac only)
  40. public static final String blankTypeString = "____";
  41. // system-identification statics
  42. public static final int systemOS;
  43. public static final boolean isMacArchitecture;
  44. public static final boolean isUnixFS;
  45. public static final boolean canExecUnix;
  46. public static final boolean isWindows;
  47. public static final boolean isWin32;
  48. // calculated system constants
  49. public static int maxPathLength = 65535;
  50. // static initialization
  51. static {
  52. // check operation system type
  53. final Properties sysprop = System.getProperties();
  54. final String sysname = sysprop.getProperty("os.name","").toLowerCase();
  55. if (sysname.startsWith("mac os x")) {
  56. systemOS = systemMacOSX;
  57. } else if (sysname.startsWith("mac os")) {
  58. systemOS = systemMacOSC;
  59. } else if (sysname.startsWith("windows")) {
  60. systemOS = systemWindows;
  61. } else if ((sysname.startsWith("linux")) || (sysname.startsWith("unix"))) {
  62. systemOS = systemUnix;
  63. } else {
  64. systemOS = systemUnknown;
  65. }
  66. isMacArchitecture = ((systemOS == systemMacOSC) || (systemOS == systemMacOSX));
  67. isUnixFS = ((systemOS == systemMacOSX) || (systemOS == systemUnix));
  68. canExecUnix = ((isUnixFS) || (!((systemOS == systemMacOSC) || (systemOS == systemWindows))));
  69. isWindows = (systemOS == systemWindows);
  70. isWin32 = (isWindows && System.getProperty("os.arch", "").contains("x86"));
  71. // set up maximum path length according to system
  72. if (isWindows) {
  73. maxPathLength = 255;
  74. } else {
  75. maxPathLength = 65535;
  76. }
  77. }
  78. public static void openBrowser(final String url) {
  79. boolean head = System.getProperty("java.awt.headless", "").equals("false");
  80. try {
  81. if (systemOS == systemMacOSX) {
  82. openBrowserMac(url);
  83. } else if (systemOS == systemUnix) {
  84. //try {
  85. // openBrowserUnixGeneric(url);
  86. //} catch (final Exception e) {
  87. openBrowserUnixFirefox(url);
  88. //}
  89. } else if (systemOS == systemWindows) {
  90. openBrowserWin(url);
  91. } else {
  92. throw new RuntimeException("System unknown");
  93. }
  94. } catch (final Throwable e) {
  95. if (head) {
  96. try {
  97. openBrowserJava(url);
  98. } catch (final Exception ee) {
  99. logBrowserFail(url);
  100. }
  101. } else {
  102. logBrowserFail(url);
  103. }
  104. }
  105. }
  106. private static void openBrowserJava(final String url) throws Exception {
  107. Desktop.getDesktop().browse(new URI(url));
  108. }
  109. private static void openBrowserMac(final String url) throws Exception {
  110. Process p = Runtime.getRuntime().exec(new String[] {"/usr/bin/osascript", "-e", "open location \"" + url + "\""});
  111. p.waitFor();
  112. if (p.exitValue() != 0) {
  113. throw new RuntimeException("Mac Exec Error: " + errorResponse(p));
  114. }
  115. }
  116. private static void openBrowserUnixFirefox(final String url) throws Exception {
  117. String cmd = "firefox -remote openURL(" + url + ")";
  118. Process p = Runtime.getRuntime().exec(cmd);
  119. p.waitFor();
  120. if (p.exitValue() != 0) {
  121. throw new RuntimeException("Unix Exec Error/Firefox: " + errorResponse(p));
  122. }
  123. }
  124. /*
  125. private static void openBrowserUnixGeneric(final String url) throws Exception {
  126. String cmd = "/etc/alternatives/www-browser " + url;
  127. Process p = Runtime.getRuntime().exec(cmd);
  128. p.waitFor();
  129. if (p.exitValue() != 0) throw new RuntimeException("Unix Exec Error/generic: " + errorResponse(p));
  130. }
  131. */
  132. private static void openBrowserWin(final String url) throws Exception {
  133. // see forum at http://forum.java.sun.com/thread.jsp?forum=57&thread=233364&message=838441
  134. String cmd;
  135. if (System.getProperty("os.name").contains("2000")) {
  136. cmd = "rundll32 url.dll,FileProtocolHandler " + url;
  137. } else {
  138. cmd = "rundll32 url.dll,FileProtocolHandler \"" + url + "\"";
  139. }
  140. //cmd = "cmd.exe /c start javascript:document.location='" + url + "'";
  141. Process p = Runtime.getRuntime().exec(cmd);
  142. p.waitFor();
  143. if (p.exitValue() != 0) {
  144. throw new RuntimeException("EXEC ERROR: " + errorResponse(p));
  145. }
  146. }
  147. private static void logBrowserFail(final String url) {
  148. //if (e != null) Log.logException(e);
  149. ConcurrentLog.info("Browser", "please start your browser and open the following location: " + url);
  150. }
  151. private static String errorResponse(final Process p) {
  152. final BufferedReader err = new BufferedReader(new InputStreamReader(p.getErrorStream()));
  153. String line, error = "";
  154. try {
  155. while ((line = err.readLine()) != null) {
  156. error = line + "\n";
  157. }
  158. return error;
  159. } catch (final IOException e) {
  160. return null;
  161. } finally {
  162. try {
  163. err.close();
  164. } catch (final IOException e) {
  165. ConcurrentLog.logException(e);
  166. }
  167. }
  168. }
  169. public static void main(final String[] args) {
  170. if ("-u".equals(args[0])) {
  171. openBrowser(args[1]);
  172. }
  173. }
  174. }