66a9c95dcecae907d323d663d18d656eb0c1087f
[jalview.git] / src / jalview / util / BrowserLauncher.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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       {
239         // must be on some later build with mrj support
240         majorMRJVersion = "3.1";
241       }
242       else
243       {
244         majorMRJVersion = mrjVersion.substring(0, 3);
245       }
246
247       try
248       {
249         double version = Double.valueOf(majorMRJVersion).doubleValue();
250
251         if (version == 2)
252         {
253           jvm = MRJ_2_0;
254         }
255         else if ((version >= 2.1) && (version < 3))
256         {
257           // Assume that all 2.x versions of MRJ work the same. MRJ 2.1 actually
258           // works via Runtime.exec() and 2.2 supports that but has an openURL()
259           // method
260           // as well that we currently ignore.
261           jvm = MRJ_2_1;
262         }
263         else if (version == 3.0)
264         {
265           jvm = MRJ_3_0;
266         }
267         else if (version >= 3.1)
268         {
269           // Assume that all 3.1 and later versions of MRJ work the same.
270           jvm = MRJ_3_1;
271         }
272         else
273         {
274           loadedWithoutErrors = false;
275           errorMessage = "Unsupported MRJ version: " + version;
276         }
277       } catch (NumberFormatException nfe)
278       {
279         loadedWithoutErrors = false;
280         errorMessage = "Invalid MRJ version: " + mrjVersion;
281       }
282     }
283     else if (osName.startsWith("Windows"))
284     {
285       if (osName.indexOf("9") != -1)
286       {
287         jvm = WINDOWS_9x;
288       }
289       else
290       {
291         jvm = WINDOWS_NT;
292       }
293     }
294     else
295     {
296       jvm = OTHER;
297     }
298
299     if (loadedWithoutErrors)
300     { // if we haven't hit any errors yet
301       loadedWithoutErrors = loadClasses();
302     }
303   }
304
305   /**
306    * This class should be never be instantiated; this just ensures so.
307    */
308   private BrowserLauncher()
309   {
310   }
311
312   /**
313    * Called by a static initializer to load any classes, fields, and methods
314    * required at runtime to locate the user's web browser.
315    * 
316    * @return <code>true</code> if all intialization succeeded <code>false</code>
317    *         if any portion of the initialization failed
318    */
319   private static boolean loadClasses()
320   {
321     switch (jvm)
322     {
323     case MRJ_2_0:
324
325       try
326       {
327         Class aeTargetClass = Class.forName("com.apple.MacOS.AETarget");
328         Class osUtilsClass = Class.forName("com.apple.MacOS.OSUtils");
329         Class appleEventClass = Class.forName("com.apple.MacOS.AppleEvent");
330         Class aeClass = Class.forName("com.apple.MacOS.ae");
331         aeDescClass = Class.forName("com.apple.MacOS.AEDesc");
332
333         aeTargetConstructor = aeTargetClass
334                 .getDeclaredConstructor(new Class[]
335                 { int.class });
336         appleEventConstructor = appleEventClass
337                 .getDeclaredConstructor(new Class[]
338                 { int.class, int.class, aeTargetClass, int.class, int.class });
339         aeDescConstructor = aeDescClass.getDeclaredConstructor(new Class[]
340         { String.class });
341
342         makeOSType = osUtilsClass.getDeclaredMethod("makeOSType",
343                 new Class[]
344                 { String.class });
345         putParameter = appleEventClass.getDeclaredMethod("putParameter",
346                 new Class[]
347                 { int.class, aeDescClass });
348         sendNoReply = appleEventClass.getDeclaredMethod("sendNoReply",
349                 new Class[]
350                 {});
351
352         Field keyDirectObjectField = aeClass
353                 .getDeclaredField("keyDirectObject");
354         keyDirectObject = (Integer) keyDirectObjectField.get(null);
355
356         Field autoGenerateReturnIDField = appleEventClass
357                 .getDeclaredField("kAutoGenerateReturnID");
358         kAutoGenerateReturnID = (Integer) autoGenerateReturnIDField
359                 .get(null);
360
361         Field anyTransactionIDField = appleEventClass
362                 .getDeclaredField("kAnyTransactionID");
363         kAnyTransactionID = (Integer) anyTransactionIDField.get(null);
364       } catch (ClassNotFoundException cnfe)
365       {
366         errorMessage = cnfe.getMessage();
367
368         return false;
369       } catch (NoSuchMethodException nsme)
370       {
371         errorMessage = nsme.getMessage();
372
373         return false;
374       } catch (NoSuchFieldException nsfe)
375       {
376         errorMessage = nsfe.getMessage();
377
378         return false;
379       } catch (IllegalAccessException iae)
380       {
381         errorMessage = iae.getMessage();
382
383         return false;
384       }
385
386       break;
387
388     case MRJ_2_1:
389
390       try
391       {
392         mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils");
393         mrjOSTypeClass = Class.forName("com.apple.mrj.MRJOSType");
394
395         Field systemFolderField = mrjFileUtilsClass
396                 .getDeclaredField("kSystemFolderType");
397         kSystemFolderType = systemFolderField.get(null);
398         findFolder = mrjFileUtilsClass.getDeclaredMethod("findFolder",
399                 new Class[]
400                 { mrjOSTypeClass });
401         getFileCreator = mrjFileUtilsClass.getDeclaredMethod(
402                 "getFileCreator", new Class[]
403                 { File.class });
404         getFileType = mrjFileUtilsClass.getDeclaredMethod("getFileType",
405                 new Class[]
406                 { File.class });
407       } catch (ClassNotFoundException cnfe)
408       {
409         errorMessage = cnfe.getMessage();
410
411         return false;
412       } catch (NoSuchFieldException nsfe)
413       {
414         errorMessage = nsfe.getMessage();
415
416         return false;
417       } catch (NoSuchMethodException nsme)
418       {
419         errorMessage = nsme.getMessage();
420
421         return false;
422       } catch (SecurityException se)
423       {
424         errorMessage = se.getMessage();
425
426         return false;
427       } catch (IllegalAccessException iae)
428       {
429         errorMessage = iae.getMessage();
430
431         return false;
432       }
433
434       break;
435
436     case MRJ_3_0:
437
438       try
439       {
440         Class linker = Class.forName("com.apple.mrj.jdirect.Linker");
441         Constructor constructor = linker.getConstructor(new Class[]
442         { Class.class });
443         linkage = constructor.newInstance(new Object[]
444         { BrowserLauncher.class });
445       } catch (ClassNotFoundException cnfe)
446       {
447         errorMessage = cnfe.getMessage();
448
449         return false;
450       } catch (NoSuchMethodException nsme)
451       {
452         errorMessage = nsme.getMessage();
453
454         return false;
455       } catch (InvocationTargetException ite)
456       {
457         errorMessage = ite.getMessage();
458
459         return false;
460       } catch (InstantiationException ie)
461       {
462         errorMessage = ie.getMessage();
463
464         return false;
465       } catch (IllegalAccessException iae)
466       {
467         errorMessage = iae.getMessage();
468
469         return false;
470       }
471
472       break;
473
474     case MRJ_3_1:
475
476       try
477       {
478         mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils");
479         openURL = mrjFileUtilsClass.getDeclaredMethod("openURL",
480                 new Class[]
481                 { String.class });
482       } catch (ClassNotFoundException cnfe)
483       {
484         errorMessage = cnfe.getMessage();
485
486         return false;
487       } catch (NoSuchMethodException nsme)
488       {
489         errorMessage = nsme.getMessage();
490
491         return false;
492       }
493
494       break;
495
496     default:
497       break;
498     }
499
500     return true;
501   }
502
503   /**
504    * Attempts to locate the default web browser on the local system. s results
505    * so it only locates the browser once for each use of this class per JVM
506    * instance.
507    * 
508    * @return The browser for the system. Note that this may not be what you
509    *         would consider to be a standard web browser; instead, it's the
510    *         application that gets called to open the default web browser. In
511    *         some cases, this will be a non-String object that provides the
512    *         means of calling the default browser.
513    */
514   private static Object locateBrowser()
515   {
516     if (browser != null)
517     {
518       return browser;
519     }
520
521     switch (jvm)
522     {
523     case MRJ_2_0:
524
525       try
526       {
527         Integer finderCreatorCode = (Integer) makeOSType.invoke(null,
528                 new Object[]
529                 { FINDER_CREATOR });
530         Object aeTarget = aeTargetConstructor.newInstance(new Object[]
531         { finderCreatorCode });
532         Integer gurlType = (Integer) makeOSType.invoke(null, new Object[]
533         { GURL_EVENT });
534         Object appleEvent = appleEventConstructor.newInstance(new Object[]
535         { gurlType, gurlType, aeTarget, kAutoGenerateReturnID,
536             kAnyTransactionID });
537
538         // Don't set browser = appleEvent because then the next time we call
539         // locateBrowser(), we'll get the same AppleEvent, to which we'll
540         // already have
541         // added the relevant parameter. Instead, regenerate the AppleEvent
542         // every time.
543         // There's probably a way to do this better; if any has any ideas,
544         // please let
545         // me know.
546         return appleEvent;
547       } catch (IllegalAccessException iae)
548       {
549         browser = null;
550         errorMessage = iae.getMessage();
551
552         return browser;
553       } catch (InstantiationException ie)
554       {
555         browser = null;
556         errorMessage = ie.getMessage();
557
558         return browser;
559       } catch (InvocationTargetException ite)
560       {
561         browser = null;
562         errorMessage = ite.getMessage();
563
564         return browser;
565       }
566
567     case MRJ_2_1:
568
569       File systemFolder;
570
571       try
572       {
573         systemFolder = (File) findFolder.invoke(null, new Object[]
574         { kSystemFolderType });
575       } catch (IllegalArgumentException iare)
576       {
577         browser = null;
578         errorMessage = iare.getMessage();
579
580         return browser;
581       } catch (IllegalAccessException iae)
582       {
583         browser = null;
584         errorMessage = iae.getMessage();
585
586         return browser;
587       } catch (InvocationTargetException ite)
588       {
589         browser = null;
590         errorMessage = ite.getTargetException().getClass() + ": "
591                 + ite.getTargetException().getMessage();
592
593         return browser;
594       }
595
596       String[] systemFolderFiles = systemFolder.list();
597
598       // Avoid a FilenameFilter because that can't be stopped mid-list
599       for (int i = 0; i < systemFolderFiles.length; i++)
600       {
601         try
602         {
603           File file = new File(systemFolder, systemFolderFiles[i]);
604
605           if (!file.isFile())
606           {
607             continue;
608           }
609
610           // We're looking for a file with a creator code of 'MACS' and
611           // a type of 'FNDR'. Only requiring the type results in non-Finder
612           // applications being picked up on certain Mac OS 9 systems,
613           // especially German ones, and sending a GURL event to those
614           // applications results in a logout under Multiple Users.
615           Object fileType = getFileType.invoke(null, new Object[]
616           { file });
617
618           if (FINDER_TYPE.equals(fileType.toString()))
619           {
620             Object fileCreator = getFileCreator.invoke(null, new Object[]
621             { file });
622
623             if (FINDER_CREATOR.equals(fileCreator.toString()))
624             {
625               browser = file.toString(); // Actually the Finder, but that's OK
626
627               return browser;
628             }
629           }
630         } catch (IllegalArgumentException iare)
631         {
632           errorMessage = iare.getMessage();
633
634           return null;
635         } catch (IllegalAccessException iae)
636         {
637           browser = null;
638           errorMessage = iae.getMessage();
639
640           return browser;
641         } catch (InvocationTargetException ite)
642         {
643           browser = null;
644           errorMessage = ite.getTargetException().getClass() + ": "
645                   + ite.getTargetException().getMessage();
646
647           return browser;
648         }
649       }
650
651       browser = null;
652
653       break;
654
655     case MRJ_3_0:
656     case MRJ_3_1:
657       browser = ""; // Return something non-null
658
659       break;
660
661     case WINDOWS_NT:
662       browser = "cmd.exe";
663
664       break;
665
666     case WINDOWS_9x:
667       browser = "command.com";
668
669       break;
670
671     case OTHER:
672     default:
673       browser = jalview.bin.Cache.getDefault("DEFAULT_BROWSER", "firefox");
674
675       break;
676     }
677
678     return browser;
679   }
680
681   /**
682    * used to ensure that browser is up-to-date after a configuration change
683    * (Unix DEFAULT_BROWSER property change).
684    */
685   public static void resetBrowser()
686   {
687     browser = null;
688   }
689
690   /**
691    * Attempts to open the default web browser to the given URL.
692    * 
693    * @param url
694    *          The URL to open
695    * @throws IOException
696    *           If the web browser could not be located or does not run
697    */
698   public static void openURL(String url) throws IOException
699   {
700     if (!loadedWithoutErrors)
701     {
702       throw new IOException("Exception in finding browser: " + errorMessage);
703     }
704
705     Object browser = locateBrowser();
706
707     if (browser == null)
708     {
709       throw new IOException("Unable to locate browser: " + errorMessage);
710     }
711
712     switch (jvm)
713     {
714     case MRJ_2_0:
715
716       Object aeDesc = null;
717
718       try
719       {
720         aeDesc = aeDescConstructor.newInstance(new Object[]
721         { url });
722         putParameter.invoke(browser, new Object[]
723         { keyDirectObject, aeDesc });
724         sendNoReply.invoke(browser, new Object[]
725         {});
726       } catch (InvocationTargetException ite)
727       {
728         throw new IOException(
729                 "InvocationTargetException while creating AEDesc: "
730                         + ite.getMessage());
731       } catch (IllegalAccessException iae)
732       {
733         throw new IOException(
734                 "IllegalAccessException while building AppleEvent: "
735                         + iae.getMessage());
736       } catch (InstantiationException ie)
737       {
738         throw new IOException(
739                 "InstantiationException while creating AEDesc: "
740                         + ie.getMessage());
741       } finally
742       {
743         aeDesc = null; // Encourage it to get disposed if it was created
744         browser = null; // Ditto
745       }
746
747       break;
748
749     case MRJ_2_1:
750       Runtime.getRuntime().exec(new String[]
751       { (String) browser, url });
752
753       break;
754
755     case MRJ_3_0:
756
757       int[] instance = new int[1];
758       int result = ICStart(instance, 0);
759
760       if (result == 0)
761       {
762         int[] selectionStart = new int[]
763         { 0 };
764         byte[] urlBytes = url.getBytes();
765         int[] selectionEnd = new int[]
766         { urlBytes.length };
767         result = ICLaunchURL(instance[0], new byte[]
768         { 0 }, urlBytes, urlBytes.length, selectionStart, selectionEnd);
769
770         if (result == 0)
771         {
772           // Ignore the return value; the URL was launched successfully
773           // regardless of what happens here.
774           ICStop(instance);
775         }
776         else
777         {
778           throw new IOException("Unable to launch URL: " + result);
779         }
780       }
781       else
782       {
783         throw new IOException(
784                 "Unable to create an Internet Config instance: " + result);
785       }
786
787       break;
788
789     case MRJ_3_1:
790
791       try
792       {
793         openURL.invoke(null, new Object[]
794         { url });
795       } catch (InvocationTargetException ite)
796       {
797         throw new IOException(
798                 "InvocationTargetException while calling openURL: "
799                         + ite.getMessage());
800       } catch (IllegalAccessException iae)
801       {
802         throw new IOException(
803                 "IllegalAccessException while calling openURL: "
804                         + iae.getMessage());
805       }
806
807       break;
808
809     case WINDOWS_NT:
810     case WINDOWS_9x:
811
812       // Add quotes around the URL to allow ampersands and other special
813       // characters to work.
814       Process process = Runtime.getRuntime().exec(
815               new String[]
816               { (String) browser, FIRST_WINDOWS_PARAMETER,
817                   SECOND_WINDOWS_PARAMETER, THIRD_WINDOWS_PARAMETER,
818                   '"' + url + '"' });
819
820       // This avoids a memory leak on some versions of Java on Windows.
821       // That's hinted at in
822       // <http://developer.java.sun.com/developer/qow/archive/68/>.
823       try
824       {
825         process.waitFor();
826         process.exitValue();
827       } catch (InterruptedException ie)
828       {
829         throw new IOException(
830                 "InterruptedException while launching browser: "
831                         + ie.getMessage());
832       }
833
834       break;
835
836     case OTHER:
837
838       // Assume that we're on Unix and that Netscape (actually Firefox) is
839       // installed
840       // First, attempt to open the URL in a currently running session of
841       // Netscape
842       // JBPNote log debug
843
844       /*
845        * System.out.println("Executing : "+browser+" "+
846        * NETSCAPE_REMOTE_PARAMETER+" "+ NETSCAPE_OPEN_PARAMETER_START + url +
847        * NETSCAPE_OPEN_NEW_WINDOW + NETSCAPE_OPEN_PARAMETER_END);
848        */
849       process = Runtime.getRuntime().exec(
850               new String[]
851               {
852                   (String) browser,
853                   NETSCAPE_REMOTE_PARAMETER,
854
855                   NETSCAPE_OPEN_PARAMETER_START + url
856                           + NETSCAPE_OPEN_NEW_WINDOW
857                           + NETSCAPE_OPEN_PARAMETER_END });
858
859       try
860       {
861         int exitCode = process.waitFor();
862
863         if (exitCode != 0)
864         { // if Netscape was not open
865           Runtime.getRuntime().exec(new String[]
866           { (String) browser, url });
867         }
868       } catch (InterruptedException ie)
869       {
870         throw new IOException(
871                 "InterruptedException while launching browser: "
872                         + ie.getMessage());
873       }
874
875       break;
876
877     default:
878
879       // This should never occur, but if it does, we'll try the simplest thing
880       // possible
881       Runtime.getRuntime().exec(new String[]
882       { (String) browser, url });
883
884       break;
885     }
886   }
887
888   /**
889    * Methods required for Mac OS X. The presence of native methods does not
890    * cause any problems on other platforms.
891    */
892   private native static int ICStart(int[] instance, int signature);
893
894   private native static int ICStop(int[] instance);
895
896   private native static int ICLaunchURL(int instance, byte[] hint,
897           byte[] data, int len, int[] selectionStart, int[] selectionEnd);
898 }