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