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