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