PageRenderTime 53ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/release/installer/mac/console/src/main/java/org/geoserver/console/Browser.java

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