PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/src/edu/usfca/xj/appkit/utils/BrowserLauncher.java

https://bitbucket.org/seydar/eecs376
Java | 606 lines | 389 code | 54 blank | 163 comment | 44 complexity | 72b3c75296788ba14fe6127b21482507 MD5 | raw file
  1. package edu.usfca.xj.appkit.utils;
  2. import java.io.File;
  3. import java.io.IOException;
  4. import java.lang.reflect.Constructor;
  5. import java.lang.reflect.Field;
  6. import java.lang.reflect.InvocationTargetException;
  7. import java.lang.reflect.Method;
  8. /**
  9. * BrowserLauncher is a class that provides one static method, openURL, which opens the default
  10. * web browser for the current user of the system to the given URL. It may support other
  11. * protocols depending on the system -- mailto, ftp, etc. -- but that has not been rigorously
  12. * tested and is not guaranteed to work.
  13. * <p>
  14. * Yes, this is platform-specific code, and yes, it may rely on classes on certain platforms
  15. * that are not part of the standard JDK. What we're trying to do, though, is to take something
  16. * that's frequently desirable but inherently platform-specific -- opening a default browser --
  17. * and allow programmers (you, for example) to do so without worrying about dropping into native
  18. * code or doing anything else similarly evil.
  19. * <p>
  20. * Anyway, this code is completely in Java and will run on all JDK 1.1-compliant systems without
  21. * modification or a need for additional libraries. All classes that are required on certain
  22. * platforms to allow this to run are dynamically loaded at runtime via reflection and, if not
  23. * found, will not cause this to do anything other than returning an error when opening the
  24. * browser.
  25. * <p>
  26. * There are certain system requirements for this class, as it's running through Runtime.exec(),
  27. * which is Java's way of making a native system call. Currently, this requires that a Macintosh
  28. * have a Finder which supports the GURL event, which is true for Mac OS 8.0 and 8.1 systems that
  29. * have the Internet Scripting AppleScript dictionary installed in the Scripting Additions folder
  30. * in the Extensions folder (which is installed by default as far as I know under Mac OS 8.0 and
  31. * 8.1), and for all Mac OS 8.5 and later systems. On Windows, it only runs under Win32 systems
  32. * (Windows 95, 98, and NT 4.0, as well as later versions of all). On other systems, this drops
  33. * back from the inherently platform-sensitive concept of a default browser and simply attempts
  34. * to launch Netscape via a shell command.
  35. * <p>
  36. * This code is Copyright 1999-2001 by Eric Albert (ejalbert@cs.stanford.edu) and may be
  37. * redistributed or modified in any form without restrictions as long as the portion of this
  38. * comment from this paragraph through the end of the comment is not removed. The author
  39. * requests that he be notified of any application, applet, or other binary that makes use of
  40. * this code, but that's more out of curiosity than anything and is not required. This software
  41. * includes no warranty. The author is not repsonsible for any loss of data or functionality
  42. * or any adverse or unexpected effects of using this software.
  43. * <p>
  44. * Credits:
  45. * <br>Steven Spencer, JavaWorld magazine (<a href="http://www.javaworld.com/javaworld/javatips/jw-javatip66.html">Java Tip 66</a>)
  46. * <br>Thanks also to Ron B. Yeh, Eric Shapiro, Ben Engber, Paul Teitlebaum, Andrea Cantatore,
  47. * Larry Barowski, Trevor Bedzek, Frank Miedrich, and Ron Rabakukk
  48. *
  49. * @author Eric Albert (<a href="mailto:ejalbert@cs.stanford.edu">ejalbert@cs.stanford.edu</a>)
  50. * @version 1.4b1 (Released June 20, 2001)
  51. */
  52. public class BrowserLauncher {
  53. /**
  54. * The Java virtual machine that we are running on. Actually, in most cases we only care
  55. * about the operating system, but some operating systems require us to switch on the VM. */
  56. private static int jvm;
  57. /** The browser for the system */
  58. private static Object browser;
  59. /**
  60. * Caches whether any classes, methods, and fields that are not part of the JDK and need to
  61. * be dynamically loaded at runtime loaded successfully.
  62. * <p>
  63. * Note that if this is <code>false</code>, <code>openURL()</code> will always return an
  64. * IOException.
  65. */
  66. private static boolean loadedWithoutErrors;
  67. /** The com.apple.mrj.MRJFileUtils class */
  68. private static Class mrjFileUtilsClass;
  69. /** The com.apple.mrj.MRJOSType class */
  70. private static Class mrjOSTypeClass;
  71. /** The com.apple.MacOS.AEDesc class */
  72. private static Class aeDescClass;
  73. /** The <init>(int) method of com.apple.MacOS.AETarget */
  74. private static Constructor aeTargetConstructor;
  75. /** The <init>(int, int, int) method of com.apple.MacOS.AppleEvent */
  76. private static Constructor appleEventConstructor;
  77. /** The <init>(String) method of com.apple.MacOS.AEDesc */
  78. private static Constructor aeDescConstructor;
  79. /** The findFolder method of com.apple.mrj.MRJFileUtils */
  80. private static Method findFolder;
  81. /** The getFileCreator method of com.apple.mrj.MRJFileUtils */
  82. private static Method getFileCreator;
  83. /** The getFileType method of com.apple.mrj.MRJFileUtils */
  84. private static Method getFileType;
  85. /** The openURL method of com.apple.mrj.MRJFileUtils */
  86. private static Method openURL;
  87. /** The makeOSType method of com.apple.MacOS.OSUtils */
  88. private static Method makeOSType;
  89. /** The putParameter method of com.apple.MacOS.AppleEvent */
  90. private static Method putParameter;
  91. /** The sendNoReply method of com.apple.MacOS.AppleEvent */
  92. private static Method sendNoReply;
  93. /** Actually an MRJOSType pointing to the System Folder on a Macintosh */
  94. private static Object kSystemFolderType;
  95. /** The keyDirectObject AppleEvent parameter type */
  96. private static Integer keyDirectObject;
  97. /** The kAutoGenerateReturnID AppleEvent code */
  98. private static Integer kAutoGenerateReturnID;
  99. /** The kAnyTransactionID AppleEvent code */
  100. private static Integer kAnyTransactionID;
  101. /** The linkage object required for JDirect 3 on Mac OS X. */
  102. private static Object linkage;
  103. /** The framework to reference on Mac OS X */
  104. private static final String JDirect_MacOSX = "/System/Library/Frameworks/Carbon.framework/Frameworks/HIToolbox.framework/HIToolbox";
  105. /** JVM constant for MRJ 2.0 */
  106. private static final int MRJ_2_0 = 0;
  107. /** JVM constant for MRJ 2.1 or later */
  108. private static final int MRJ_2_1 = 1;
  109. /** JVM constant for Java on Mac OS X 10.0 (MRJ 3.0) */
  110. private static final int MRJ_3_0 = 3;
  111. /** JVM constant for MRJ 3.1 */
  112. private static final int MRJ_3_1 = 4;
  113. /** JVM constant for any Windows NT JVM */
  114. private static final int WINDOWS_NT = 5;
  115. /** JVM constant for any Windows 9x JVM */
  116. private static final int WINDOWS_9x = 6;
  117. private static final int LINUX = 7;
  118. /** JVM constant for any other platform */
  119. private static final int OTHER = -1;
  120. /**
  121. * The file type of the Finder on a Macintosh. Hardcoding "Finder" would keep non-U.S. English
  122. * systems from working properly.
  123. */
  124. private static final String FINDER_TYPE = "FNDR";
  125. /**
  126. * The creator code of the Finder on a Macintosh, which is needed to send AppleEvents to the
  127. * application.
  128. */
  129. private static final String FINDER_CREATOR = "MACS";
  130. /** The name for the AppleEvent type corresponding to a GetURL event. */
  131. private static final String GURL_EVENT = "GURL";
  132. /**
  133. * The first parameter that needs to be passed into Runtime.exec() to open the default web
  134. * browser on Windows.
  135. */
  136. private static final String FIRST_WINDOWS_PARAMETER = "/c";
  137. /** The second parameter for Runtime.exec() on Windows. */
  138. private static final String SECOND_WINDOWS_PARAMETER = "start";
  139. /**
  140. * The third parameter for Runtime.exec() on Windows. This is a "title"
  141. * parameter that the command line expects. Setting this parameter allows
  142. * URLs containing spaces to work.
  143. */
  144. private static final String THIRD_WINDOWS_PARAMETER = "\"\"";
  145. /**
  146. * The shell parameters for Netscape that opens a given URL in an already-open copy of Netscape
  147. * on many command-line systems.
  148. */
  149. private static final String NETSCAPE_REMOTE_PARAMETER = "-remote";
  150. private static final String NETSCAPE_OPEN_PARAMETER_START = "'openURL(";
  151. private static final String NETSCAPE_OPEN_PARAMETER_END = ")'";
  152. /**
  153. * The message from any exception thrown throughout the initialization process.
  154. */
  155. private static String errorMessage;
  156. /**
  157. * An initialization block that determines the operating system and loads the necessary
  158. * runtime data.
  159. */
  160. static {
  161. loadedWithoutErrors = true;
  162. String osName = System.getProperty("os.name");
  163. if (osName.startsWith("Mac OS")) {
  164. String mrjVersion = System.getProperty("mrj.version");
  165. String majorMRJVersion = mrjVersion.substring(0, 3);
  166. try {
  167. double version = Double.valueOf(majorMRJVersion).doubleValue();
  168. if (version == 2) {
  169. jvm = MRJ_2_0;
  170. } else if (version >= 2.1 && version < 3) {
  171. // Assume that all 2.x versions of MRJ work the same. MRJ 2.1 actually
  172. // works via Runtime.exec() and 2.2 supports that but has an openURL() method
  173. // as well that we currently ignore.
  174. jvm = MRJ_2_1;
  175. } else if (version == 3.0) {
  176. jvm = MRJ_3_0;
  177. } else if (version >= 3.1) {
  178. // Assume that all 3.1 and later versions of MRJ work the same.
  179. jvm = MRJ_3_1;
  180. } else {
  181. loadedWithoutErrors = false;
  182. errorMessage = "Unsupported MRJ version: " + version;
  183. }
  184. } catch (NumberFormatException nfe) {
  185. loadedWithoutErrors = false;
  186. errorMessage = "Invalid MRJ version: " + mrjVersion;
  187. }
  188. } else if (osName.startsWith("Windows")) {
  189. if (osName.indexOf("9") != -1) {
  190. jvm = WINDOWS_9x;
  191. } else {
  192. jvm = WINDOWS_NT;
  193. }
  194. } else if (osName.startsWith("Linux")) {
  195. jvm = LINUX;
  196. } else {
  197. jvm = OTHER;
  198. }
  199. if (loadedWithoutErrors) { // if we haven't hit any errors yet
  200. loadedWithoutErrors = loadClasses();
  201. }
  202. }
  203. /**
  204. * This class should be never be instantiated; this just ensures so.
  205. */
  206. private BrowserLauncher() { }
  207. /**
  208. * Called by a static initializer to load any classes, fields, and methods required at runtime
  209. * to locate the user's web browser.
  210. * @return <code>true</code> if all intialization succeeded
  211. * <code>false</code> if any portion of the initialization failed
  212. */
  213. private static boolean loadClasses() {
  214. switch (jvm) {
  215. case MRJ_2_0:
  216. try {
  217. Class aeTargetClass = Class.forName("com.apple.MacOS.AETarget");
  218. Class osUtilsClass = Class.forName("com.apple.MacOS.OSUtils");
  219. Class appleEventClass = Class.forName("com.apple.MacOS.AppleEvent");
  220. Class aeClass = Class.forName("com.apple.MacOS.ae");
  221. aeDescClass = Class.forName("com.apple.MacOS.AEDesc");
  222. aeTargetConstructor = aeTargetClass.getDeclaredConstructor(new Class [] { int.class });
  223. appleEventConstructor = appleEventClass.getDeclaredConstructor(new Class[] { int.class, int.class, aeTargetClass, int.class, int.class });
  224. aeDescConstructor = aeDescClass.getDeclaredConstructor(new Class[] { String.class });
  225. makeOSType = osUtilsClass.getDeclaredMethod("makeOSType", new Class [] { String.class });
  226. putParameter = appleEventClass.getDeclaredMethod("putParameter", new Class[] { int.class, aeDescClass });
  227. sendNoReply = appleEventClass.getDeclaredMethod("sendNoReply", new Class[] { });
  228. Field keyDirectObjectField = aeClass.getDeclaredField("keyDirectObject");
  229. keyDirectObject = (Integer) keyDirectObjectField.get(null);
  230. Field autoGenerateReturnIDField = appleEventClass.getDeclaredField("kAutoGenerateReturnID");
  231. kAutoGenerateReturnID = (Integer) autoGenerateReturnIDField.get(null);
  232. Field anyTransactionIDField = appleEventClass.getDeclaredField("kAnyTransactionID");
  233. kAnyTransactionID = (Integer) anyTransactionIDField.get(null);
  234. } catch (ClassNotFoundException cnfe) {
  235. errorMessage = cnfe.getMessage();
  236. return false;
  237. } catch (NoSuchMethodException nsme) {
  238. errorMessage = nsme.getMessage();
  239. return false;
  240. } catch (NoSuchFieldException nsfe) {
  241. errorMessage = nsfe.getMessage();
  242. return false;
  243. } catch (IllegalAccessException iae) {
  244. errorMessage = iae.getMessage();
  245. return false;
  246. }
  247. break;
  248. case MRJ_2_1:
  249. try {
  250. mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils");
  251. mrjOSTypeClass = Class.forName("com.apple.mrj.MRJOSType");
  252. Field systemFolderField = mrjFileUtilsClass.getDeclaredField("kSystemFolderType");
  253. kSystemFolderType = systemFolderField.get(null);
  254. findFolder = mrjFileUtilsClass.getDeclaredMethod("findFolder", new Class[] { mrjOSTypeClass });
  255. getFileCreator = mrjFileUtilsClass.getDeclaredMethod("getFileCreator", new Class[] { File.class });
  256. getFileType = mrjFileUtilsClass.getDeclaredMethod("getFileType", new Class[] { File.class });
  257. } catch (ClassNotFoundException cnfe) {
  258. errorMessage = cnfe.getMessage();
  259. return false;
  260. } catch (NoSuchFieldException nsfe) {
  261. errorMessage = nsfe.getMessage();
  262. return false;
  263. } catch (NoSuchMethodException nsme) {
  264. errorMessage = nsme.getMessage();
  265. return false;
  266. } catch (SecurityException se) {
  267. errorMessage = se.getMessage();
  268. return false;
  269. } catch (IllegalAccessException iae) {
  270. errorMessage = iae.getMessage();
  271. return false;
  272. }
  273. break;
  274. case MRJ_3_0:
  275. try {
  276. Class linker = Class.forName("com.apple.mrj.jdirect.Linker");
  277. Constructor constructor = linker.getConstructor(new Class[]{ Class.class });
  278. linkage = constructor.newInstance(new Object[] { BrowserLauncher.class });
  279. } catch (ClassNotFoundException cnfe) {
  280. errorMessage = cnfe.getMessage();
  281. return false;
  282. } catch (NoSuchMethodException nsme) {
  283. errorMessage = nsme.getMessage();
  284. return false;
  285. } catch (InvocationTargetException ite) {
  286. errorMessage = ite.getMessage();
  287. return false;
  288. } catch (InstantiationException ie) {
  289. errorMessage = ie.getMessage();
  290. return false;
  291. } catch (IllegalAccessException iae) {
  292. errorMessage = iae.getMessage();
  293. return false;
  294. }
  295. break;
  296. case MRJ_3_1:
  297. try {
  298. mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils");
  299. openURL = mrjFileUtilsClass.getDeclaredMethod("openURL", new Class[] { String.class });
  300. } catch (ClassNotFoundException cnfe) {
  301. errorMessage = cnfe.getMessage();
  302. return false;
  303. } catch (NoSuchMethodException nsme) {
  304. errorMessage = nsme.getMessage();
  305. return false;
  306. }
  307. break;
  308. default:
  309. break;
  310. }
  311. return true;
  312. }
  313. /**
  314. * Attempts to locate the default web browser on the local system. Caches results so it
  315. * only locates the browser once for each use of this class per JVM instance.
  316. * @return The browser for the system. Note that this may not be what you would consider
  317. * to be a standard web browser; instead, it's the application that gets called to
  318. * open the default web browser. In some cases, this will be a non-String object
  319. * that provides the means of calling the default browser.
  320. */
  321. private static Object locateBrowser() {
  322. if (browser != null) {
  323. return browser;
  324. }
  325. switch (jvm) {
  326. case MRJ_2_0:
  327. try {
  328. Integer finderCreatorCode = (Integer) makeOSType.invoke(null, new Object[] { FINDER_CREATOR });
  329. Object aeTarget = aeTargetConstructor.newInstance(new Object[] { finderCreatorCode });
  330. Integer gurlType = (Integer) makeOSType.invoke(null, new Object[] { GURL_EVENT });
  331. Object appleEvent = appleEventConstructor.newInstance(new Object[] { gurlType, gurlType, aeTarget, kAutoGenerateReturnID, kAnyTransactionID });
  332. // Don't set browser = appleEvent because then the next time we call
  333. // locateBrowser(), we'll get the same AppleEvent, to which we'll already have
  334. // added the relevant parameter. Instead, regenerate the AppleEvent every time.
  335. // There's probably a way to do this better; if any has any ideas, please let
  336. // me know.
  337. return appleEvent;
  338. } catch (IllegalAccessException iae) {
  339. browser = null;
  340. errorMessage = iae.getMessage();
  341. return browser;
  342. } catch (InstantiationException ie) {
  343. browser = null;
  344. errorMessage = ie.getMessage();
  345. return browser;
  346. } catch (InvocationTargetException ite) {
  347. browser = null;
  348. errorMessage = ite.getMessage();
  349. return browser;
  350. }
  351. case MRJ_2_1:
  352. File systemFolder;
  353. try {
  354. systemFolder = (File) findFolder.invoke(null, new Object[] { kSystemFolderType });
  355. } catch (IllegalArgumentException iare) {
  356. browser = null;
  357. errorMessage = iare.getMessage();
  358. return browser;
  359. } catch (IllegalAccessException iae) {
  360. browser = null;
  361. errorMessage = iae.getMessage();
  362. return browser;
  363. } catch (InvocationTargetException ite) {
  364. browser = null;
  365. errorMessage = ite.getTargetException().getClass() + ": " + ite.getTargetException().getMessage();
  366. return browser;
  367. }
  368. String[] systemFolderFiles = systemFolder.list();
  369. // Avoid a FilenameFilter because that can't be stopped mid-list
  370. for(int i = 0; i < systemFolderFiles.length; i++) {
  371. try {
  372. File file = new File(systemFolder, systemFolderFiles[i]);
  373. if (!file.isFile()) {
  374. continue;
  375. }
  376. // We're looking for a file with a creator code of 'MACS' and
  377. // a type of 'FNDR'. Only requiring the type results in non-Finder
  378. // applications being picked up on certain Mac OS 9 systems,
  379. // especially German ones, and sending a GURL event to those
  380. // applications results in a logout under Multiple Users.
  381. Object fileType = getFileType.invoke(null, new Object[] { file });
  382. if (FINDER_TYPE.equals(fileType.toString())) {
  383. Object fileCreator = getFileCreator.invoke(null, new Object[] { file });
  384. if (FINDER_CREATOR.equals(fileCreator.toString())) {
  385. browser = file.toString(); // Actually the Finder, but that's OK
  386. return browser;
  387. }
  388. }
  389. } catch (IllegalArgumentException iare) {
  390. errorMessage = iare.getMessage();
  391. return null;
  392. } catch (IllegalAccessException iae) {
  393. browser = null;
  394. errorMessage = iae.getMessage();
  395. return browser;
  396. } catch (InvocationTargetException ite) {
  397. browser = null;
  398. errorMessage = ite.getTargetException().getClass() + ": " + ite.getTargetException().getMessage();
  399. return browser;
  400. }
  401. }
  402. browser = null;
  403. break;
  404. case MRJ_3_0:
  405. case MRJ_3_1:
  406. browser = ""; // Return something non-null
  407. break;
  408. case WINDOWS_NT:
  409. browser = "cmd.exe";
  410. break;
  411. case WINDOWS_9x:
  412. browser = "command.com";
  413. break;
  414. case LINUX:
  415. case OTHER:
  416. default:
  417. // Jean Bovet: look for multiple browser in case netscape is not installed
  418. browser = new String[] {"firefox", "mozilla", "netscape", "opera", "konqueror", "galeon", "firebird"};
  419. break;
  420. }
  421. return browser;
  422. }
  423. /**
  424. * Attempts to open the default web browser to the given URL.
  425. * @param url The URL to open
  426. * @throws IOException If the web browser could not be located or does not run
  427. */
  428. public static void openURL(String url) throws IOException {
  429. if (!loadedWithoutErrors) {
  430. throw new IOException("Exception in finding browser: " + errorMessage);
  431. }
  432. Object browser = locateBrowser();
  433. if (browser == null) {
  434. throw new IOException("Unable to locate browser: " + errorMessage);
  435. }
  436. switch (jvm) {
  437. case MRJ_2_0:
  438. Object aeDesc;
  439. try {
  440. aeDesc = aeDescConstructor.newInstance(new Object[] { url });
  441. putParameter.invoke(browser, new Object[] { keyDirectObject, aeDesc });
  442. sendNoReply.invoke(browser, new Object[] { });
  443. } catch (InvocationTargetException ite) {
  444. throw new IOException("InvocationTargetException while creating AEDesc: " + ite.getMessage());
  445. } catch (IllegalAccessException iae) {
  446. throw new IOException("IllegalAccessException while building AppleEvent: " + iae.getMessage());
  447. } catch (InstantiationException ie) {
  448. throw new IOException("InstantiationException while creating AEDesc: " + ie.getMessage());
  449. } finally {
  450. aeDesc = null; // Encourage it to get disposed if it was created
  451. browser = null; // Ditto
  452. }
  453. break;
  454. case MRJ_2_1:
  455. Runtime.getRuntime().exec(new String[] { (String) browser, url } );
  456. break;
  457. case MRJ_3_0:
  458. int[] instance = new int[1];
  459. int result = ICStart(instance, 0);
  460. if (result == 0) {
  461. int[] selectionStart = new int[] { 0 };
  462. byte[] urlBytes = url.getBytes();
  463. int[] selectionEnd = new int[] { urlBytes.length };
  464. result = ICLaunchURL(instance[0], new byte[] { 0 }, urlBytes,
  465. urlBytes.length, selectionStart,
  466. selectionEnd);
  467. if (result == 0) {
  468. // Ignore the return value; the URL was launched successfully
  469. // regardless of what happens here.
  470. ICStop(instance);
  471. } else {
  472. throw new IOException("Unable to launch URL: " + result);
  473. }
  474. } else {
  475. throw new IOException("Unable to create an Internet Config instance: " + result);
  476. }
  477. break;
  478. case MRJ_3_1:
  479. try {
  480. openURL.invoke(null, new Object[] { url });
  481. } catch (InvocationTargetException ite) {
  482. throw new IOException("InvocationTargetException while calling openURL: " + ite.getMessage());
  483. } catch (IllegalAccessException iae) {
  484. throw new IOException("IllegalAccessException while calling openURL: " + iae.getMessage());
  485. }
  486. break;
  487. case WINDOWS_NT:
  488. case WINDOWS_9x:
  489. // Add quotes around the URL to allow ampersands and other special
  490. // characters to work.
  491. Process process = Runtime.getRuntime().exec(new String[] { (String) browser,
  492. FIRST_WINDOWS_PARAMETER,
  493. SECOND_WINDOWS_PARAMETER,
  494. THIRD_WINDOWS_PARAMETER,
  495. '"' + url + '"' });
  496. // This avoids a memory leak on some versions of Java on Windows.
  497. // That's hinted at in <http://developer.java.sun.com/developer/qow/archive/68/>.
  498. try {
  499. process.waitFor();
  500. process.exitValue();
  501. } catch (InterruptedException ie) {
  502. throw new IOException("InterruptedException while launching browser: " + ie.getMessage());
  503. }
  504. break;
  505. case LINUX:
  506. case OTHER:
  507. // Assume that we're on Unix and that Netscape is installed
  508. String[] array = (String[])browser;
  509. for (int i = 0; i < array.length; i++) {
  510. if(openUnixBrowser(array[i], url))
  511. return;
  512. }
  513. throw new IOException("Unable to locate browser");
  514. default:
  515. // This should never occur, but if it does, we'll try the simplest thing possible
  516. Runtime.getRuntime().exec(new String[] { (String) browser, url });
  517. break;
  518. }
  519. }
  520. private static boolean openUnixBrowser(String browser, String url) {
  521. boolean success = false;
  522. Process process;
  523. try {
  524. // First, attempt to open the URL in a currently running session of Netscape
  525. process = Runtime.getRuntime().exec(new String[] { (String) browser,
  526. NETSCAPE_REMOTE_PARAMETER,
  527. NETSCAPE_OPEN_PARAMETER_START +
  528. url +
  529. NETSCAPE_OPEN_PARAMETER_END });
  530. try {
  531. int exitCode = process.waitFor();
  532. if (exitCode != 0) { // if Netscape was not open
  533. Runtime.getRuntime().exec(new String[] { (String) browser, url });
  534. exitCode = 0;
  535. }
  536. success = (exitCode == 0);
  537. } catch (InterruptedException ie) {
  538. //throw new IOException("InterruptedException while launching browser: " + ie.getMessage());
  539. }
  540. } catch(IOException e) {
  541. }
  542. return success;
  543. }
  544. /**
  545. * Methods required for Mac OS X. The presence of native methods does not cause
  546. * any problems on other platforms.
  547. */
  548. private native static int ICStart(int[] instance, int signature);
  549. private native static int ICStop(int[] instance);
  550. private native static int ICLaunchURL(int instance, byte[] hint, byte[] data, int len,
  551. int[] selectionStart, int[] selectionEnd);
  552. }