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