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