PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/src/com/jbidwatcher/util/browser/BrowserLauncher.java

https://github.com/cyberfox/jbidwatcher
Java | 613 lines | 397 code | 51 blank | 165 comment | 58 complexity | 6a2c0c9e27fbd77d757fb8a5af28ed33 MD5 | raw file
  1. package com.jbidwatcher.util.browser;
  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. * <br>Minor changes by Morgan Schweers to better support Windows and post-3.x (mrj.version > 100) MRJ versions.
  49. *
  50. * @author Eric Albert (<a href="mailto:ejalbert@cs.stanford.edu">ejalbert@cs.stanford.edu</a>)
  51. * @version 1.4b1 (Released June 20, 2001)
  52. */
  53. @SuppressWarnings({"JavaDoc", "FieldCanBeLocal"})
  54. public class BrowserLauncher {
  55. /**
  56. * The Java virtual machine that we are running on. Actually, in most cases we only care
  57. * about the operating system, but some operating systems require us to switch on the VM. */
  58. private static int jvm;
  59. /** The browser for the system */
  60. private static String browser;
  61. /**
  62. * Caches whether any classes, methods, and fields that are not part of the JDK and need to
  63. * be dynamically loaded at runtime loaded successfully.
  64. * <p>
  65. * Note that if this is <code>false</code>, <code>openURL()</code> will always return an
  66. * IOException.
  67. */
  68. private static boolean loadedWithoutErrors;
  69. /** The com.apple.mrj.MRJFileUtils class */
  70. private static Class mrjFileUtilsClass;
  71. /** The com.apple.mrj.MRJOSType class */
  72. private static Class mrjOSTypeClass;
  73. /** The com.apple.MacOS.AEDesc class */
  74. private static Class<?> aeDescClass;
  75. /** The <init>(int) method of com.apple.MacOS.AETarget */
  76. private static Constructor<?> aeTargetConstructor;
  77. /** The <init>(int, int, int) method of com.apple.MacOS.AppleEvent */
  78. private static Constructor<?> appleEventConstructor;
  79. /** The <init>(String) method of com.apple.MacOS.AEDesc */
  80. private static Constructor<?> aeDescConstructor;
  81. /** The findFolder method of com.apple.mrj.MRJFileUtils */
  82. private static Method findFolder;
  83. /** The getFileCreator method of com.apple.mrj.MRJFileUtils */
  84. private static Method getFileCreator;
  85. /** The getFileType method of com.apple.mrj.MRJFileUtils */
  86. private static Method getFileType;
  87. /** The openURL method of com.apple.mrj.MRJFileUtils */
  88. private static Method openURL;
  89. /** The makeOSType method of com.apple.MacOS.OSUtils */
  90. private static Method makeOSType;
  91. /** The putParameter method of com.apple.MacOS.AppleEvent */
  92. private static Method putParameter;
  93. /** The sendNoReply method of com.apple.MacOS.AppleEvent */
  94. private static Method sendNoReply;
  95. /** Actually an MRJOSType pointing to the System Folder on a Macintosh */
  96. private static Object kSystemFolderType;
  97. /** The keyDirectObject AppleEvent parameter type */
  98. private static Integer keyDirectObject;
  99. /** The kAutoGenerateReturnID AppleEvent code */
  100. private static Integer kAutoGenerateReturnID;
  101. /** The kAnyTransactionID AppleEvent code */
  102. private static Integer kAnyTransactionID;
  103. /** JVM constant for MRJ 2.0 */
  104. private static final int MRJ_2_0 = 0;
  105. /** JVM constant for MRJ 2.1 or later */
  106. private static final int MRJ_2_1 = 1;
  107. /** JVM constant for Java on Mac OS X 10.0 (MRJ 3.0) */
  108. private static final int MRJ_3_0 = 3;
  109. /** JVM constant for MRJ 3.1 */
  110. private static final int MRJ_3_1 = 4;
  111. /** JVM constant for any Windows NT JVM */
  112. private static final int WINDOWS_NT = 5;
  113. /** JVM constant for any Windows 9x JVM */
  114. private static final int WINDOWS_9x = 6;
  115. /** JVM constant for any other platform */
  116. private static final int OTHER = -1;
  117. /**
  118. * The file type of the Finder on a Macintosh. Hardcoding "Finder" would keep non-U.S. English
  119. * systems from working properly.
  120. */
  121. private static final String FINDER_TYPE = "FNDR";
  122. /**
  123. * The creator code of the Finder on a Macintosh, which is needed to send AppleEvents to the
  124. * application.
  125. */
  126. private static final String FINDER_CREATOR = "MACS";
  127. /** The name for the AppleEvent type corresponding to a GetURL event. */
  128. private static final String GURL_EVENT = "GURL";
  129. /**
  130. * The first parameter that needs to be passed into Runtime.exec() to open the default web
  131. * browser on Windows.
  132. */
  133. private static final String FIRST_WINDOWS_PARAMETER = "/c";
  134. /** The second parameter for Runtime.exec() on Windows. */
  135. private static final String SECOND_WINDOWS_PARAMETER = "start";
  136. /**
  137. * The third parameter for Runtime.exec() on Windows. This is a "title"
  138. * parameter that the command line expects. Setting this parameter allows
  139. * URLs containing spaces to work.
  140. */
  141. private static final String THIRD_WINDOWS_PARAMETER = "\"\"";
  142. /**
  143. * The shell parameters for Netscape that opens a given URL in an already-open copy of Netscape
  144. * on many command-line systems.
  145. */
  146. private static final String NETSCAPE_REMOTE_PARAMETER = "-remote";
  147. private static final String NETSCAPE_OPEN_PARAMETER_START = "'openURL(";
  148. private static final String NETSCAPE_OPEN_PARAMETER_END = ")'";
  149. /**
  150. * The message from any exception thrown throughout the initialization process.
  151. */
  152. private static String errorMessage;
  153. /**
  154. * An initialization block that determines the operating system and loads the necessary
  155. * runtime data.
  156. */
  157. static {
  158. loadedWithoutErrors = true;
  159. String osName = System.getProperty("os.name");
  160. if (osName.startsWith("Mac OS")) {
  161. String mrjVersion = System.getProperty("mrj.version");
  162. String majorMRJVersion;
  163. // If it's in the form of 'm.n', or a 3-digit number (big version number!), this'll work.
  164. if((mrjVersion.length() > 3) && (mrjVersion.indexOf('.') == 1)) {
  165. majorMRJVersion = mrjVersion.substring(0, 3);
  166. } else {
  167. // Treat the post-Panther JRE version numbers (99 and above) as 3.1 or later.
  168. majorMRJVersion = "3.1";
  169. }
  170. try {
  171. double version = Double.valueOf(majorMRJVersion);
  172. if (version == 2) {
  173. jvm = MRJ_2_0;
  174. } else if (version >= 2.1 && version < 3) {
  175. // Assume that all 2.x versions of MRJ work the same. MRJ 2.1 actually
  176. // works via Runtime.exec() and 2.2 supports that but has an openURL() method
  177. // as well that we currently ignore.
  178. jvm = MRJ_2_1;
  179. } else if (version == 3.0) {
  180. jvm = MRJ_3_0;
  181. } else if (version >= 3.1) {
  182. // Assume that all 3.1 and later versions of MRJ work the same.
  183. jvm = MRJ_3_1;
  184. } else {
  185. loadedWithoutErrors = false;
  186. errorMessage = "Unsupported MRJ version: " + version;
  187. }
  188. } catch (NumberFormatException nfe) {
  189. loadedWithoutErrors = false;
  190. errorMessage = "Invalid MRJ version: " + mrjVersion;
  191. }
  192. } else if (osName.startsWith("Windows")) {
  193. if (osName.indexOf("9") != -1) {
  194. jvm = WINDOWS_9x;
  195. } else {
  196. jvm = WINDOWS_NT;
  197. }
  198. } else {
  199. jvm = OTHER;
  200. }
  201. if (loadedWithoutErrors) { // if we haven't hit any errors yet
  202. loadedWithoutErrors = loadClasses();
  203. }
  204. }
  205. /**
  206. * This class should be never be instantiated; this just ensures so.
  207. */
  208. private BrowserLauncher() { }
  209. /**
  210. * Called by a static initializer to load any classes, fields, and methods required at runtime
  211. * to locate the user's web browser.
  212. * @return <code>true</code> if all intialization succeeded
  213. * <code>false</code> if any portion of the initialization failed
  214. */
  215. private static boolean loadClasses() {
  216. switch (jvm) {
  217. case MRJ_2_0:
  218. try {
  219. Class<?> aeTargetClass = Class.forName("com.apple.MacOS.AETarget");
  220. Class osUtilsClass = Class.forName("com.apple.MacOS.OSUtils");
  221. Class<?> appleEventClass = Class.forName("com.apple.MacOS.AppleEvent");
  222. Class aeClass = Class.forName("com.apple.MacOS.ae");
  223. aeDescClass = Class.forName("com.apple.MacOS.AEDesc");
  224. aeTargetConstructor = aeTargetClass.getDeclaredConstructor(int.class);
  225. appleEventConstructor = appleEventClass.getDeclaredConstructor(int.class, int.class, aeTargetClass, int.class, int.class);
  226. aeDescConstructor = aeDescClass.getDeclaredConstructor(String.class);
  227. makeOSType = osUtilsClass.getDeclaredMethod("makeOSType", String.class);
  228. putParameter = appleEventClass.getDeclaredMethod("putParameter", int.class, aeDescClass);
  229. sendNoReply = appleEventClass.getDeclaredMethod("sendNoReply");
  230. Field keyDirectObjectField = aeClass.getDeclaredField("keyDirectObject");
  231. keyDirectObject = (Integer) keyDirectObjectField.get(null);
  232. Field autoGenerateReturnIDField = appleEventClass.getDeclaredField("kAutoGenerateReturnID");
  233. kAutoGenerateReturnID = (Integer) autoGenerateReturnIDField.get(null);
  234. Field anyTransactionIDField = appleEventClass.getDeclaredField("kAnyTransactionID");
  235. kAnyTransactionID = (Integer) anyTransactionIDField.get(null);
  236. } catch (ClassNotFoundException cnfe) {
  237. errorMessage = cnfe.getMessage();
  238. return false;
  239. } catch (NoSuchMethodException nsme) {
  240. errorMessage = nsme.getMessage();
  241. return false;
  242. } catch (NoSuchFieldException nsfe) {
  243. errorMessage = nsfe.getMessage();
  244. return false;
  245. } catch (IllegalAccessException iae) {
  246. errorMessage = iae.getMessage();
  247. return false;
  248. }
  249. break;
  250. case MRJ_2_1:
  251. try {
  252. mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils");
  253. mrjOSTypeClass = Class.forName("com.apple.mrj.MRJOSType");
  254. Field systemFolderField = mrjFileUtilsClass.getDeclaredField("kSystemFolderType");
  255. kSystemFolderType = systemFolderField.get(null);
  256. findFolder = mrjFileUtilsClass.getDeclaredMethod("findFolder", mrjOSTypeClass);
  257. getFileCreator = mrjFileUtilsClass.getDeclaredMethod("getFileCreator", File.class);
  258. getFileType = mrjFileUtilsClass.getDeclaredMethod("getFileType", File.class);
  259. } catch (ClassNotFoundException cnfe) {
  260. errorMessage = cnfe.getMessage();
  261. return false;
  262. } catch (NoSuchFieldException nsfe) {
  263. errorMessage = nsfe.getMessage();
  264. return false;
  265. } catch (NoSuchMethodException nsme) {
  266. errorMessage = nsme.getMessage();
  267. return false;
  268. } catch (SecurityException se) {
  269. errorMessage = se.getMessage();
  270. return false;
  271. } catch (IllegalAccessException iae) {
  272. errorMessage = iae.getMessage();
  273. return false;
  274. }
  275. break;
  276. case MRJ_3_0:
  277. try {
  278. Class linker = Class.forName("com.apple.mrj.jdirect.Linker");
  279. Constructor constructor = linker.getConstructor(Class.class);
  280. // linkage =
  281. constructor.newInstance(BrowserLauncher.class);
  282. } catch (ClassNotFoundException cnfe) {
  283. errorMessage = cnfe.getMessage();
  284. return false;
  285. } catch (NoSuchMethodException nsme) {
  286. errorMessage = nsme.getMessage();
  287. return false;
  288. } catch (InvocationTargetException ite) {
  289. errorMessage = ite.getMessage();
  290. return false;
  291. } catch (InstantiationException ie) {
  292. errorMessage = ie.getMessage();
  293. return false;
  294. } catch (IllegalAccessException iae) {
  295. errorMessage = iae.getMessage();
  296. return false;
  297. }
  298. break;
  299. case MRJ_3_1:
  300. try {
  301. mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils");
  302. openURL = mrjFileUtilsClass.getDeclaredMethod("openURL", String.class);
  303. } catch (ClassNotFoundException cnfe) {
  304. errorMessage = cnfe.getMessage();
  305. return false;
  306. } catch (NoSuchMethodException nsme) {
  307. errorMessage = nsme.getMessage();
  308. return false;
  309. }
  310. break;
  311. default:
  312. break;
  313. }
  314. return true;
  315. }
  316. /**
  317. * Attempts to locate the default web browser on the local system. Caches results so it
  318. * only locates the browser once for each use of this class per JVM instance.
  319. * @return The browser for the system. Note that this may not be what you would consider
  320. * to be a standard web browser; instead, it's the application that gets called to
  321. * open the default web browser. In some cases, this will be a non-String object
  322. * that provides the means of calling the default browser.
  323. */
  324. private static String locateBrowser() {
  325. if (browser != null) {
  326. return browser;
  327. }
  328. switch (jvm) {
  329. case MRJ_2_0:
  330. try {
  331. Integer finderCreatorCode = (Integer) makeOSType.invoke(null, FINDER_CREATOR);
  332. Object aeTarget = aeTargetConstructor.newInstance(finderCreatorCode);
  333. Integer gurlType = (Integer) makeOSType.invoke(null, GURL_EVENT);
  334. // Don't set browser = appleEvent because then the next time we call
  335. // locateBrowser(), we'll get the same AppleEvent, to which we'll already have
  336. // added the relevant parameter. Instead, regenerate the AppleEvent every time.
  337. // There's probably a way to do this better; if any has any ideas, please let
  338. // me know.
  339. return (String)appleEventConstructor.newInstance(gurlType, gurlType, aeTarget, kAutoGenerateReturnID, kAnyTransactionID);
  340. } catch (IllegalAccessException iae) {
  341. browser = null;
  342. errorMessage = iae.getMessage();
  343. return browser;
  344. } catch (InstantiationException ie) {
  345. browser = null;
  346. errorMessage = ie.getMessage();
  347. return browser;
  348. } catch (InvocationTargetException ite) {
  349. browser = null;
  350. errorMessage = ite.getMessage();
  351. return browser;
  352. }
  353. case MRJ_2_1:
  354. File systemFolder;
  355. try {
  356. systemFolder = (File) findFolder.invoke(null, kSystemFolderType);
  357. } catch (IllegalArgumentException iare) {
  358. browser = null;
  359. errorMessage = iare.getMessage();
  360. return browser;
  361. } catch (IllegalAccessException iae) {
  362. browser = null;
  363. errorMessage = iae.getMessage();
  364. return browser;
  365. } catch (InvocationTargetException ite) {
  366. browser = null;
  367. errorMessage = ite.getTargetException().getClass() + ": " + ite.getTargetException().getMessage();
  368. return browser;
  369. }
  370. String[] systemFolderFiles = systemFolder.list();
  371. // Avoid a FilenameFilter because that can't be stopped mid-list
  372. for (String systemFolderFile : systemFolderFiles) {
  373. try {
  374. File file = new File(systemFolder, systemFolderFile);
  375. if (!file.isFile()) {
  376. continue;
  377. }
  378. // We're looking for a file with a creator code of 'MACS' and
  379. // a type of 'FNDR'. Only requiring the type results in non-Finder
  380. // applications being picked up on certain Mac OS 9 systems,
  381. // especially German ones, and sending a GURL event to those
  382. // applications results in a logout under Multiple Users.
  383. Object fileType = getFileType.invoke(null, file);
  384. if (FINDER_TYPE.equals(fileType.toString())) {
  385. Object fileCreator = getFileCreator.invoke(null, file);
  386. if (FINDER_CREATOR.equals(fileCreator.toString())) {
  387. browser = file.toString(); // Actually the Finder, but that's OK
  388. return browser;
  389. }
  390. }
  391. } catch (IllegalArgumentException iare) {
  392. errorMessage = iare.getMessage();
  393. return null;
  394. } catch (IllegalAccessException iae) {
  395. browser = null;
  396. errorMessage = iae.getMessage();
  397. return browser;
  398. } catch (InvocationTargetException ite) {
  399. browser = null;
  400. errorMessage = ite.getTargetException().getClass() + ": " + ite.getTargetException().getMessage();
  401. return browser;
  402. }
  403. }
  404. browser = null;
  405. break;
  406. case MRJ_3_0:
  407. case MRJ_3_1:
  408. browser = ""; // Return something non-null
  409. break;
  410. case WINDOWS_NT:
  411. browser = "cmd.exe";
  412. break;
  413. case WINDOWS_9x:
  414. browser = "command.com";
  415. break;
  416. case OTHER:
  417. default:
  418. browser = "firefox";
  419. break;
  420. }
  421. return browser;
  422. }
  423. public static void openURL(String url) throws IOException { openURL(url, null, false); }
  424. public static void openURL(String url, String optlaunch) throws IOException { openURL(url, optlaunch, false); }
  425. /**
  426. * Attempts to open the default web browser to the given URL.
  427. * @param url The URL to open
  428. * @param optlaunch An optional launcher program, used if we can't get the real launcher.
  429. * @param forceOverride - Override the automatically detected browser and run optlaunch.
  430. * @throws IOException If the web browser could not be located or does not run
  431. */
  432. public static void openURL(String url, String optlaunch, boolean forceOverride) throws IOException {
  433. if (!loadedWithoutErrors) {
  434. throw new IOException("Exception in finding browser: " + errorMessage);
  435. }
  436. browser = locateBrowser();
  437. if (browser == null) {
  438. throw new IOException("Unable to locate browser: " + errorMessage);
  439. }
  440. switch (jvm) {
  441. case MRJ_2_0:
  442. Object aeDesc;
  443. try {
  444. aeDesc = aeDescConstructor.newInstance(url);
  445. putParameter.invoke(browser, keyDirectObject, aeDesc);
  446. sendNoReply.invoke(browser);
  447. } catch (InvocationTargetException ite) {
  448. throw new IOException("InvocationTargetException while creating AEDesc: " + ite.getMessage());
  449. } catch (IllegalAccessException iae) {
  450. throw new IOException("IllegalAccessException while building AppleEvent: " + iae.getMessage());
  451. } catch (InstantiationException ie) {
  452. throw new IOException("InstantiationException while creating AEDesc: " + ie.getMessage());
  453. } finally {
  454. browser = null; // Ditto
  455. }
  456. break;
  457. case MRJ_2_1:
  458. Runtime.getRuntime().exec(new String[] {browser, url } );
  459. break;
  460. case MRJ_3_0:
  461. int[] instance = new int[1];
  462. int result = ICStart(instance, 0);
  463. if (result == 0) {
  464. int[] selectionStart = new int[] { 0 };
  465. byte[] urlBytes = url.getBytes();
  466. int[] selectionEnd = new int[] { urlBytes.length };
  467. result = ICLaunchURL(instance[0], new byte[] { 0 }, urlBytes,
  468. urlBytes.length, selectionStart,
  469. selectionEnd);
  470. if (result == 0) {
  471. // Ignore the return value; the URL was launched successfully
  472. // regardless of what happens here.
  473. ICStop(instance);
  474. } else {
  475. throw new IOException("Unable to launch URL: " + result);
  476. }
  477. } else {
  478. throw new IOException("Unable to create an Internet Config instance: " + result);
  479. }
  480. break;
  481. case MRJ_3_1:
  482. try {
  483. openURL.invoke(null, url);
  484. } catch (InvocationTargetException ite) {
  485. throw new IOException("InvocationTargetException while calling openURL: " + ite.getMessage());
  486. } catch (IllegalAccessException iae) {
  487. throw new IOException("IllegalAccessException while calling openURL: " + iae.getMessage());
  488. }
  489. break;
  490. case WINDOWS_NT:
  491. case WINDOWS_9x:
  492. Process process = null;
  493. String cmdLine[];
  494. if(!forceOverride) {
  495. process = WindowsBrowserLauncher.Launch(url);
  496. }
  497. if(process == null) {
  498. if(optlaunch != null) {
  499. if(optlaunch.indexOf("%1") != -1) {
  500. cmdLine = WindowsBrowserLauncher.splitCommandLine(optlaunch, url);
  501. } else {
  502. String[] cmdLine2;
  503. cmdLine2 = WindowsBrowserLauncher.splitCommandLine(optlaunch);
  504. cmdLine = new String[cmdLine2.length + 1];
  505. System.arraycopy(cmdLine2, 0, cmdLine, 0, cmdLine2.length);
  506. cmdLine[cmdLine2.length] = '"' + url + '"';
  507. }
  508. Runtime.getRuntime().exec(cmdLine);
  509. } else {
  510. // Add quotes around the URL to allow ampersands and other special
  511. // characters to work.
  512. Runtime.getRuntime().exec(new String[] {browser, FIRST_WINDOWS_PARAMETER,
  513. SECOND_WINDOWS_PARAMETER,
  514. THIRD_WINDOWS_PARAMETER,
  515. '"' + url + '"' });
  516. }
  517. }
  518. break;
  519. case OTHER:
  520. // IF the user hasn't provided an 'optional launcher',
  521. // presume that we're on Unix and that Netscape is
  522. // installed
  523. if(optlaunch != null) {
  524. if(!optlaunch.equals(browser)) {
  525. Runtime.getRuntime().exec(new String[] { optlaunch, url } );
  526. break;
  527. }
  528. }
  529. // First, attempt to open the URL in a currently running session of Netscape
  530. process = Runtime.getRuntime().exec(new String[] {browser,
  531. NETSCAPE_REMOTE_PARAMETER,
  532. NETSCAPE_OPEN_PARAMETER_START +
  533. url +
  534. NETSCAPE_OPEN_PARAMETER_END });
  535. try {
  536. int exitCode = process.waitFor();
  537. if (exitCode != 0) { // if Netscape was not open
  538. Runtime.getRuntime().exec(new String[] {browser, url });
  539. }
  540. } catch (InterruptedException ie) {
  541. throw new IOException("InterruptedException while launching browser: " + ie.getMessage());
  542. }
  543. break;
  544. default:
  545. // This should never occur, but if it does, we'll try the simplest thing possible
  546. if(optlaunch == null) {
  547. Runtime.getRuntime().exec(new String[] {browser, url });
  548. } else {
  549. Runtime.getRuntime().exec(new String[] {optlaunch, url });
  550. }
  551. break;
  552. }
  553. }
  554. /**
  555. * Methods required for Mac OS X. The presence of native methods does not cause
  556. * any problems on other platforms.
  557. */
  558. private native static int ICStart(int[] instance, int signature);
  559. private native static int ICStop(int[] instance);
  560. private native static int ICLaunchURL(int instance, byte[] hint, byte[] data, int len,
  561. int[] selectionStart, int[] selectionEnd);
  562. }