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