GPL license added
[jalview.git] / src / jalview / util / BrowserLauncher.java
1 /*
2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2005 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
20 package jalview.util;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.lang.reflect.Constructor;
25 import java.lang.reflect.Field;
26 import java.lang.reflect.InvocationTargetException;
27 import java.lang.reflect.Method;
28
29 /**
30  * BrowserLauncher is a class that provides one static method, openURL, which opens the default
31  * web browser for the current user of the system to the given URL.  It may support other
32  * protocols depending on the system -- mailto, ftp, etc. -- but that has not been rigorously
33  * tested and is not guaranteed to work.
34  * <p>
35  * Yes, this is platform-specific code, and yes, it may rely on classes on certain platforms
36  * that are not part of the standard JDK.  What we're trying to do, though, is to take something
37  * that's frequently desirable but inherently platform-specific -- opening a default browser --
38  * and allow programmers (you, for example) to do so without worrying about dropping into native
39  * code or doing anything else similarly evil.
40  * <p>
41  * Anyway, this code is completely in Java and will run on all JDK 1.1-compliant systems without
42  * modification or a need for additional libraries.  All classes that are required on certain
43  * platforms to allow this to run are dynamically loaded at runtime via reflection and, if not
44  * found, will not cause this to do anything other than returning an error when opening the
45  * browser.
46  * <p>
47  * There are certain system requirements for this class, as it's running through Runtime.exec(),
48  * which is Java's way of making a native system call.  Currently, this requires that a Macintosh
49  * have a Finder which supports the GURL event, which is true for Mac OS 8.0 and 8.1 systems that
50  * have the Internet Scripting AppleScript dictionary installed in the Scripting Additions folder
51  * in the Extensions folder (which is installed by default as far as I know under Mac OS 8.0 and
52  * 8.1), and for all Mac OS 8.5 and later systems.  On Windows, it only runs under Win32 systems
53  * (Windows 95, 98, and NT 4.0, as well as later versions of all).  On other systems, this drops
54  * back from the inherently platform-sensitive concept of a default browser and simply attempts
55  * to launch Netscape via a shell command.
56  * <p>
57  * This code is Copyright 1999-2001 by Eric Albert (ejalbert\@cs.stanford.edu) and may be
58  * redistributed or modified in any form without restrictions as long as the portion of this
59  * comment from this paragraph through the end of the comment is not removed.  The author
60  * requests that he be notified of any application, applet, or other binary that makes use of
61  * this code, but that's more out of curiosity than anything and is not required.  This software
62  * includes no warranty.  The author is not repsonsible for any loss of data or functionality
63  * or any adverse or unexpected effects of using this software.
64  * <p>
65  * Credits:
66  * <br>Steven Spencer, JavaWorld magazine (<a href="http://www.javaworld.com/javaworld/javatips/jw-javatip66.html">Java Tip 66</a>)
67  * <br>Thanks also to Ron B. Yeh, Eric Shapiro, Ben Engber, Paul Teitlebaum, Andrea Cantatore,
68  * Larry Barowski, Trevor Bedzek, Frank Miedrich, and Ron Rabakukk
69  *
70  * @author Eric Albert (<a href="mailto:ejalbert@cs.stanford.edu">ejalbert@cs.stanford.edu</a>)
71  * @version 1.4b1 (Released June 20, 2001)
72  */
73 public class BrowserLauncher {
74
75         /**
76          * The Java virtual machine that we are running on.  Actually, in most cases we only care
77          * about the operating system, but some operating systems require us to switch on the VM. */
78         private static int jvm;
79
80         /** The browser for the system */
81         private static Object browser;
82
83         /**
84          * Caches whether any classes, methods, and fields that are not part of the JDK and need to
85          * be dynamically loaded at runtime loaded successfully.
86          * <p>
87          * Note that if this is <code>false</code>, <code>openURL()</code> will always return an
88          * IOException.
89          */
90         private static boolean loadedWithoutErrors;
91
92         /** The com.apple.mrj.MRJFileUtils class */
93         private static Class mrjFileUtilsClass;
94
95         /** The com.apple.mrj.MRJOSType class */
96         private static Class mrjOSTypeClass;
97
98         /** The com.apple.MacOS.AEDesc class */
99         private static Class aeDescClass;
100
101         /** The &lt;init&gt;(int) method of com.apple.MacOS.AETarget */
102         private static Constructor aeTargetConstructor;
103
104         /** The &lt;init&gt;(int, int, int) method of com.apple.MacOS.AppleEvent */
105         private static Constructor appleEventConstructor;
106
107         /** The &lt;init&gt;(String) method of com.apple.MacOS.AEDesc */
108         private static Constructor aeDescConstructor;
109
110         /** The findFolder method of com.apple.mrj.MRJFileUtils */
111         private static Method findFolder;
112
113         /** The getFileCreator method of com.apple.mrj.MRJFileUtils */
114         private static Method getFileCreator;
115
116         /** The getFileType method of com.apple.mrj.MRJFileUtils */
117         private static Method getFileType;
118
119         /** The openURL method of com.apple.mrj.MRJFileUtils */
120         private static Method openURL;
121
122         /** The makeOSType method of com.apple.MacOS.OSUtils */
123         private static Method makeOSType;
124
125         /** The putParameter method of com.apple.MacOS.AppleEvent */
126         private static Method putParameter;
127
128         /** The sendNoReply method of com.apple.MacOS.AppleEvent */
129         private static Method sendNoReply;
130
131         /** Actually an MRJOSType pointing to the System Folder on a Macintosh */
132         private static Object kSystemFolderType;
133
134         /** The keyDirectObject AppleEvent parameter type */
135         private static Integer keyDirectObject;
136
137         /** The kAutoGenerateReturnID AppleEvent code */
138         private static Integer kAutoGenerateReturnID;
139
140         /** The kAnyTransactionID AppleEvent code */
141         private static Integer kAnyTransactionID;
142
143         /** The linkage object required for JDirect 3 on Mac OS X. */
144         private static Object linkage;
145
146         /** The framework to reference on Mac OS X */
147         private static final String JDirect_MacOSX = "/System/Library/Frameworks/Carbon.framework/Frameworks/HIToolbox.framework/HIToolbox";
148
149         /** JVM constant for MRJ 2.0 */
150         private static final int MRJ_2_0 = 0;
151
152         /** JVM constant for MRJ 2.1 or later */
153         private static final int MRJ_2_1 = 1;
154
155         /** JVM constant for Java on Mac OS X 10.0 (MRJ 3.0) */
156         private static final int MRJ_3_0 = 3;
157
158         /** JVM constant for MRJ 3.1 */
159         private static final int MRJ_3_1 = 4;
160
161         /** JVM constant for any Windows NT JVM */
162         private static final int WINDOWS_NT = 5;
163
164         /** JVM constant for any Windows 9x JVM */
165         private static final int WINDOWS_9x = 6;
166
167         /** JVM constant for any other platform */
168         private static final int OTHER = -1;
169
170         /**
171          * The file type of the Finder on a Macintosh.  Hardcoding "Finder" would keep non-U.S. English
172          * systems from working properly.
173          */
174         private static final String FINDER_TYPE = "FNDR";
175
176         /**
177          * The creator code of the Finder on a Macintosh, which is needed to send AppleEvents to the
178          * application.
179          */
180         private static final String FINDER_CREATOR = "MACS";
181
182         /** The name for the AppleEvent type corresponding to a GetURL event. */
183         private static final String GURL_EVENT = "GURL";
184
185         /**
186          * The first parameter that needs to be passed into Runtime.exec() to open the default web
187          * browser on Windows.
188          */
189     private static final String FIRST_WINDOWS_PARAMETER = "/c";
190
191     /** The second parameter for Runtime.exec() on Windows. */
192     private static final String SECOND_WINDOWS_PARAMETER = "start";
193
194     /**
195      * The third parameter for Runtime.exec() on Windows.  This is a "title"
196      * parameter that the command line expects.  Setting this parameter allows
197      * URLs containing spaces to work.
198      */
199     private static final String THIRD_WINDOWS_PARAMETER = "\"\"";
200
201         /**
202          * The shell parameters for Netscape that opens a given URL in an already-open copy of Netscape
203          * on many command-line systems.
204          */
205         private static final String NETSCAPE_REMOTE_PARAMETER = "-remote";
206         private static final String NETSCAPE_OPEN_PARAMETER_START = "openURL(";
207         private static final String NETSCAPE_OPEN_NEW_WINDOW = ", new-window";
208         private static final String NETSCAPE_OPEN_PARAMETER_END = ")";
209
210         /**
211          * The message from any exception thrown throughout the initialization process.
212          */
213         private static String errorMessage;
214
215         /**
216          * An initialization block that determines the operating system and loads the necessary
217          * runtime data.
218          */
219         static {
220                 loadedWithoutErrors = true;
221                 String osName = System.getProperty("os.name");
222                 if (osName.startsWith("Mac OS")) {
223                         String mrjVersion = System.getProperty("mrj.version");
224                         String majorMRJVersion = mrjVersion.substring(0, 3);
225                         try {
226                                 double version = Double.valueOf(majorMRJVersion).doubleValue();
227                                 if (version == 2) {
228                                         jvm = MRJ_2_0;
229                                 } else if (version >= 2.1 && version < 3) {
230                                         // Assume that all 2.x versions of MRJ work the same.  MRJ 2.1 actually
231                                         // works via Runtime.exec() and 2.2 supports that but has an openURL() method
232                                         // as well that we currently ignore.
233                                         jvm = MRJ_2_1;
234                                 } else if (version == 3.0) {
235                                         jvm = MRJ_3_0;
236                                 } else if (version >= 3.1) {
237                                         // Assume that all 3.1 and later versions of MRJ work the same.
238                                         jvm = MRJ_3_1;
239                                 } else {
240                                         loadedWithoutErrors = false;
241                                         errorMessage = "Unsupported MRJ version: " + version;
242                                 }
243                         } catch (NumberFormatException nfe) {
244                                 loadedWithoutErrors = false;
245                                 errorMessage = "Invalid MRJ version: " + mrjVersion;
246                         }
247                 } else if (osName.startsWith("Windows")) {
248                         if (osName.indexOf("9") != -1) {
249                                 jvm = WINDOWS_9x;
250                         } else {
251                                 jvm = WINDOWS_NT;
252                         }
253                 } else {
254                         jvm = OTHER;
255                 }
256
257                 if (loadedWithoutErrors) {      // if we haven't hit any errors yet
258                         loadedWithoutErrors = loadClasses();
259                 }
260         }
261
262         /**
263          * This class should be never be instantiated; this just ensures so.
264          */
265         private BrowserLauncher() { }
266
267         /**
268          * Called by a static initializer to load any classes, fields, and methods required at runtime
269          * to locate the user's web browser.
270          * @return <code>true</code> if all intialization succeeded
271          *                      <code>false</code> if any portion of the initialization failed
272          */
273         private static boolean loadClasses() {
274                 switch (jvm) {
275                         case MRJ_2_0:
276                                 try {
277                                         Class aeTargetClass = Class.forName("com.apple.MacOS.AETarget");
278                                         Class osUtilsClass = Class.forName("com.apple.MacOS.OSUtils");
279                                         Class appleEventClass = Class.forName("com.apple.MacOS.AppleEvent");
280                                         Class aeClass = Class.forName("com.apple.MacOS.ae");
281                                         aeDescClass = Class.forName("com.apple.MacOS.AEDesc");
282
283                                         aeTargetConstructor = aeTargetClass.getDeclaredConstructor(new Class [] { int.class });
284                                         appleEventConstructor = appleEventClass.getDeclaredConstructor(new Class[] { int.class, int.class, aeTargetClass, int.class, int.class });
285                                         aeDescConstructor = aeDescClass.getDeclaredConstructor(new Class[] { String.class });
286
287                                         makeOSType = osUtilsClass.getDeclaredMethod("makeOSType", new Class [] { String.class });
288                                         putParameter = appleEventClass.getDeclaredMethod("putParameter", new Class[] { int.class, aeDescClass });
289                                         sendNoReply = appleEventClass.getDeclaredMethod("sendNoReply", new Class[] { });
290
291                                         Field keyDirectObjectField = aeClass.getDeclaredField("keyDirectObject");
292                                         keyDirectObject = (Integer) keyDirectObjectField.get(null);
293                                         Field autoGenerateReturnIDField = appleEventClass.getDeclaredField("kAutoGenerateReturnID");
294                                         kAutoGenerateReturnID = (Integer) autoGenerateReturnIDField.get(null);
295                                         Field anyTransactionIDField = appleEventClass.getDeclaredField("kAnyTransactionID");
296                                         kAnyTransactionID = (Integer) anyTransactionIDField.get(null);
297                                 } catch (ClassNotFoundException cnfe) {
298                                         errorMessage = cnfe.getMessage();
299                                         return false;
300                                 } catch (NoSuchMethodException nsme) {
301                                         errorMessage = nsme.getMessage();
302                                         return false;
303                                 } catch (NoSuchFieldException nsfe) {
304                                         errorMessage = nsfe.getMessage();
305                                         return false;
306                                 } catch (IllegalAccessException iae) {
307                                         errorMessage = iae.getMessage();
308                                         return false;
309                                 }
310                                 break;
311                         case MRJ_2_1:
312                                 try {
313                                         mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils");
314                                         mrjOSTypeClass = Class.forName("com.apple.mrj.MRJOSType");
315                                         Field systemFolderField = mrjFileUtilsClass.getDeclaredField("kSystemFolderType");
316                                         kSystemFolderType = systemFolderField.get(null);
317                                         findFolder = mrjFileUtilsClass.getDeclaredMethod("findFolder", new Class[] { mrjOSTypeClass });
318                                         getFileCreator = mrjFileUtilsClass.getDeclaredMethod("getFileCreator", new Class[] { File.class });
319                                         getFileType = mrjFileUtilsClass.getDeclaredMethod("getFileType", new Class[] { File.class });
320                                 } catch (ClassNotFoundException cnfe) {
321                                         errorMessage = cnfe.getMessage();
322                                         return false;
323                                 } catch (NoSuchFieldException nsfe) {
324                                         errorMessage = nsfe.getMessage();
325                                         return false;
326                                 } catch (NoSuchMethodException nsme) {
327                                         errorMessage = nsme.getMessage();
328                                         return false;
329                                 } catch (SecurityException se) {
330                                         errorMessage = se.getMessage();
331                                         return false;
332                                 } catch (IllegalAccessException iae) {
333                                         errorMessage = iae.getMessage();
334                                         return false;
335                                 }
336                                 break;
337                         case MRJ_3_0:
338                             try {
339                                         Class linker = Class.forName("com.apple.mrj.jdirect.Linker");
340                                         Constructor constructor = linker.getConstructor(new Class[]{ Class.class });
341                                         linkage = constructor.newInstance(new Object[] { BrowserLauncher.class });
342                                 } catch (ClassNotFoundException cnfe) {
343                                         errorMessage = cnfe.getMessage();
344                                         return false;
345                                 } catch (NoSuchMethodException nsme) {
346                                         errorMessage = nsme.getMessage();
347                                         return false;
348                                 } catch (InvocationTargetException ite) {
349                                         errorMessage = ite.getMessage();
350                                         return false;
351                                 } catch (InstantiationException ie) {
352                                         errorMessage = ie.getMessage();
353                                         return false;
354                                 } catch (IllegalAccessException iae) {
355                                         errorMessage = iae.getMessage();
356                                         return false;
357                                 }
358                                 break;
359                         case MRJ_3_1:
360                                 try {
361                                         mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils");
362                                         openURL = mrjFileUtilsClass.getDeclaredMethod("openURL", new Class[] { String.class });
363                                 } catch (ClassNotFoundException cnfe) {
364                                         errorMessage = cnfe.getMessage();
365                                         return false;
366                                 } catch (NoSuchMethodException nsme) {
367                                         errorMessage = nsme.getMessage();
368                                         return false;
369                                 }
370                                 break;
371                         default:
372                             break;
373                 }
374                 return true;
375         }
376
377         /**
378          * Attempts to locate the default web browser on the local system.  Caches results so it
379          * only locates the browser once for each use of this class per JVM instance.
380          * @return The browser for the system.  Note that this may not be what you would consider
381          *                      to be a standard web browser; instead, it's the application that gets called to
382          *                      open the default web browser.  In some cases, this will be a non-String object
383          *                      that provides the means of calling the default browser.
384          */
385         private static Object locateBrowser() {
386                 if (browser != null) {
387                         return browser;
388                 }
389                 switch (jvm) {
390                         case MRJ_2_0:
391                                 try {
392                                         Integer finderCreatorCode = (Integer) makeOSType.invoke(null, new Object[] { FINDER_CREATOR });
393                                         Object aeTarget = aeTargetConstructor.newInstance(new Object[] { finderCreatorCode });
394                                         Integer gurlType = (Integer) makeOSType.invoke(null, new Object[] { GURL_EVENT });
395                                         Object appleEvent = appleEventConstructor.newInstance(new Object[] { gurlType, gurlType, aeTarget, kAutoGenerateReturnID, kAnyTransactionID });
396                                         // Don't set browser = appleEvent because then the next time we call
397                                         // locateBrowser(), we'll get the same AppleEvent, to which we'll already have
398                                         // added the relevant parameter. Instead, regenerate the AppleEvent every time.
399                                         // There's probably a way to do this better; if any has any ideas, please let
400                                         // me know.
401                                         return appleEvent;
402                                 } catch (IllegalAccessException iae) {
403                                         browser = null;
404                                         errorMessage = iae.getMessage();
405                                         return browser;
406                                 } catch (InstantiationException ie) {
407                                         browser = null;
408                                         errorMessage = ie.getMessage();
409                                         return browser;
410                                 } catch (InvocationTargetException ite) {
411                                         browser = null;
412                                         errorMessage = ite.getMessage();
413                                         return browser;
414                                 }
415                         case MRJ_2_1:
416                                 File systemFolder;
417                                 try {
418                                         systemFolder = (File) findFolder.invoke(null, new Object[] { kSystemFolderType });
419                                 } catch (IllegalArgumentException iare) {
420                                         browser = null;
421                                         errorMessage = iare.getMessage();
422                                         return browser;
423                                 } catch (IllegalAccessException iae) {
424                                         browser = null;
425                                         errorMessage = iae.getMessage();
426                                         return browser;
427                                 } catch (InvocationTargetException ite) {
428                                         browser = null;
429                                         errorMessage = ite.getTargetException().getClass() + ": " + ite.getTargetException().getMessage();
430                                         return browser;
431                                 }
432                                 String[] systemFolderFiles = systemFolder.list();
433                                 // Avoid a FilenameFilter because that can't be stopped mid-list
434                                 for(int i = 0; i < systemFolderFiles.length; i++) {
435                                         try {
436                                                 File file = new File(systemFolder, systemFolderFiles[i]);
437                                                 if (!file.isFile()) {
438                                                         continue;
439                                                 }
440                                                 // We're looking for a file with a creator code of 'MACS' and
441                                                 // a type of 'FNDR'.  Only requiring the type results in non-Finder
442                                                 // applications being picked up on certain Mac OS 9 systems,
443                                                 // especially German ones, and sending a GURL event to those
444                                                 // applications results in a logout under Multiple Users.
445                                                 Object fileType = getFileType.invoke(null, new Object[] { file });
446                                                 if (FINDER_TYPE.equals(fileType.toString())) {
447                                                         Object fileCreator = getFileCreator.invoke(null, new Object[] { file });
448                                                         if (FINDER_CREATOR.equals(fileCreator.toString())) {
449                                                                 browser = file.toString();      // Actually the Finder, but that's OK
450                                                                 return browser;
451                                                         }
452                                                 }
453                                         } catch (IllegalArgumentException iare) {
454                                                 browser = browser;
455                                                 errorMessage = iare.getMessage();
456                                                 return null;
457                                         } catch (IllegalAccessException iae) {
458                                                 browser = null;
459                                                 errorMessage = iae.getMessage();
460                                                 return browser;
461                                         } catch (InvocationTargetException ite) {
462                                                 browser = null;
463                                                 errorMessage = ite.getTargetException().getClass() + ": " + ite.getTargetException().getMessage();
464                                                 return browser;
465                                         }
466                                 }
467                                 browser = null;
468                                 break;
469                         case MRJ_3_0:
470                         case MRJ_3_1:
471                                 browser = "";   // Return something non-null
472                                 break;
473                         case WINDOWS_NT:
474                                 browser = "cmd.exe";
475                                 break;
476                         case WINDOWS_9x:
477                                 browser = "command.com";
478                                 break;
479                         case OTHER:
480                         default:
481                                 browser = jalview.bin.Cache.applicationProperties.getProperty("jalview.browser");
482                                 if (browser==null) {
483                                   // hope firefox exists :-/
484                                   browser = "firefox";
485                                 }
486                                 break;
487                 }
488                 return browser;
489         }
490
491         /**
492          * Attempts to open the default web browser to the given URL.
493          * @param url The URL to open
494          * @throws IOException If the web browser could not be located or does not run
495          */
496         public static void openURL(String url) throws IOException {
497                 if (!loadedWithoutErrors) {
498                         throw new IOException("Exception in finding browser: " + errorMessage);
499                 }
500                 Object browser = locateBrowser();
501                 if (browser == null) {
502                         throw new IOException("Unable to locate browser: " + errorMessage);
503                 }
504
505                 switch (jvm) {
506                         case MRJ_2_0:
507                                 Object aeDesc = null;
508                                 try {
509                                         aeDesc = aeDescConstructor.newInstance(new Object[] { url });
510                                         putParameter.invoke(browser, new Object[] { keyDirectObject, aeDesc });
511                                         sendNoReply.invoke(browser, new Object[] { });
512                                 } catch (InvocationTargetException ite) {
513                                         throw new IOException("InvocationTargetException while creating AEDesc: " + ite.getMessage());
514                                 } catch (IllegalAccessException iae) {
515                                         throw new IOException("IllegalAccessException while building AppleEvent: " + iae.getMessage());
516                                 } catch (InstantiationException ie) {
517                                         throw new IOException("InstantiationException while creating AEDesc: " + ie.getMessage());
518                                 } finally {
519                                         aeDesc = null;  // Encourage it to get disposed if it was created
520                                         browser = null; // Ditto
521                                 }
522                                 break;
523                         case MRJ_2_1:
524                                 Runtime.getRuntime().exec(new String[] { (String) browser, url } );
525                                 break;
526                         case MRJ_3_0:
527                                 int[] instance = new int[1];
528                                 int result = ICStart(instance, 0);
529                                 if (result == 0) {
530                                         int[] selectionStart = new int[] { 0 };
531                                         byte[] urlBytes = url.getBytes();
532                                         int[] selectionEnd = new int[] { urlBytes.length };
533                                         result = ICLaunchURL(instance[0], new byte[] { 0 }, urlBytes,
534                                                                                         urlBytes.length, selectionStart,
535                                                                                         selectionEnd);
536                                         if (result == 0) {
537                                                 // Ignore the return value; the URL was launched successfully
538                                                 // regardless of what happens here.
539                                                 ICStop(instance);
540                                         } else {
541                                                 throw new IOException("Unable to launch URL: " + result);
542                                         }
543                                 } else {
544                                         throw new IOException("Unable to create an Internet Config instance: " + result);
545                                 }
546                                 break;
547                         case MRJ_3_1:
548                                 try {
549                                         openURL.invoke(null, new Object[] { url });
550                                 } catch (InvocationTargetException ite) {
551                                         throw new IOException("InvocationTargetException while calling openURL: " + ite.getMessage());
552                                 } catch (IllegalAccessException iae) {
553                                         throw new IOException("IllegalAccessException while calling openURL: " + iae.getMessage());
554                                 }
555                                 break;
556                     case WINDOWS_NT:
557                     case WINDOWS_9x:
558                             // Add quotes around the URL to allow ampersands and other special
559                             // characters to work.
560                                 Process process = Runtime.getRuntime().exec(new String[] { (String) browser,
561                                                                                                                                 FIRST_WINDOWS_PARAMETER,
562                                                                                                                                 SECOND_WINDOWS_PARAMETER,
563                                                                                                                                 THIRD_WINDOWS_PARAMETER,
564                                                                                                                                 '"' + url + '"' });
565                                 // This avoids a memory leak on some versions of Java on Windows.
566                                 // That's hinted at in <http://developer.java.sun.com/developer/qow/archive/68/>.
567                                 try {
568                                         process.waitFor();
569                                         process.exitValue();
570                                 } catch (InterruptedException ie) {
571                                         throw new IOException("InterruptedException while launching browser: " + ie.getMessage());
572                                 }
573                                 break;
574                         case OTHER:
575                                 // Assume that we're on Unix and that Netscape (actually Firefox) is installed
576
577                                 // First, attempt to open the URL in a currently running session of Netscape
578
579                         // JBPNote log debug
580     /* System.out.println("Executing : "+browser+" "+
581                                                                                                         NETSCAPE_REMOTE_PARAMETER+" "+
582                                                                                                         NETSCAPE_OPEN_PARAMETER_START +
583                                                                                                         url +
584                                                                                                         NETSCAPE_OPEN_NEW_WINDOW +
585                                                                                                         NETSCAPE_OPEN_PARAMETER_END);
586      */
587                             process = Runtime.getRuntime().exec(new String[] { (String) browser,
588                                                                                                         NETSCAPE_REMOTE_PARAMETER,
589                                                                                                         NETSCAPE_OPEN_PARAMETER_START +
590                                                                                                         url +
591                                                                                                         NETSCAPE_OPEN_NEW_WINDOW +
592                                                                                                         NETSCAPE_OPEN_PARAMETER_END });
593                                 try {
594                                         int exitCode = process.waitFor();
595                                         if (exitCode != 0) {    // if Netscape was not open
596        // JBPNote log debug
597        //       System.out.println("Executing : "+browser+" "+url+"\n");
598        Runtime.getRuntime().exec(new String[] { (String) browser, url });
599                                         }
600                                 } catch (InterruptedException ie) {
601                                         throw new IOException("InterruptedException while launching browser: " + ie.getMessage());
602                                 }
603                                 break;
604                         default:
605                                 // This should never occur, but if it does, we'll try the simplest thing possible
606                                 Runtime.getRuntime().exec(new String[] { (String) browser, url });
607                                 break;
608                 }
609         }
610
611         /**
612          * Methods required for Mac OS X.  The presence of native methods does not cause
613          * any problems on other platforms.
614          */
615         private native static int ICStart(int[] instance, int signature);
616         private native static int ICStop(int[] instance);
617         private native static int ICLaunchURL(int instance, byte[] hint, byte[] data, int len,
618                                                                                         int[] selectionStart, int[] selectionEnd);
619 }