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