PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/core/src/seco/notebook/html/BrowserLauncher.java

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