2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\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
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
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
19 package jalview.util;
\r
22 import java.lang.reflect.*;
\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
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
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
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
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
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
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
68 public class BrowserLauncher
\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
75 /** The browser for the system */
\r
76 private static Object browser;
\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
82 * Note that if this is <code>false</code>, <code>openURL()</code> will always return an
\r
85 private static boolean loadedWithoutErrors;
\r
87 /** The com.apple.mrj.MRJFileUtils class */
\r
88 private static Class mrjFileUtilsClass;
\r
90 /** The com.apple.mrj.MRJOSType class */
\r
91 private static Class mrjOSTypeClass;
\r
93 /** The com.apple.MacOS.AEDesc class */
\r
94 private static Class aeDescClass;
\r
96 /** The <init>(int) method of com.apple.MacOS.AETarget */
\r
97 private static Constructor aeTargetConstructor;
\r
99 /** The <init>(int, int, int) method of com.apple.MacOS.AppleEvent */
\r
100 private static Constructor appleEventConstructor;
\r
102 /** The <init>(String) method of com.apple.MacOS.AEDesc */
\r
103 private static Constructor aeDescConstructor;
\r
105 /** The findFolder method of com.apple.mrj.MRJFileUtils */
\r
106 private static Method findFolder;
\r
108 /** The getFileCreator method of com.apple.mrj.MRJFileUtils */
\r
109 private static Method getFileCreator;
\r
111 /** The getFileType method of com.apple.mrj.MRJFileUtils */
\r
112 private static Method getFileType;
\r
114 /** The openURL method of com.apple.mrj.MRJFileUtils */
\r
115 private static Method openURL;
\r
117 /** The makeOSType method of com.apple.MacOS.OSUtils */
\r
118 private static Method makeOSType;
\r
120 /** The putParameter method of com.apple.MacOS.AppleEvent */
\r
121 private static Method putParameter;
\r
123 /** The sendNoReply method of com.apple.MacOS.AppleEvent */
\r
124 private static Method sendNoReply;
\r
126 /** Actually an MRJOSType pointing to the System Folder on a Macintosh */
\r
127 private static Object kSystemFolderType;
\r
129 /** The keyDirectObject AppleEvent parameter type */
\r
130 private static Integer keyDirectObject;
\r
132 /** The kAutoGenerateReturnID AppleEvent code */
\r
133 private static Integer kAutoGenerateReturnID;
\r
135 /** The kAnyTransactionID AppleEvent code */
\r
136 private static Integer kAnyTransactionID;
\r
138 /** The linkage object required for JDirect 3 on Mac OS X. */
\r
139 private static Object linkage;
\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
144 /** JVM constant for MRJ 2.0 */
\r
145 private static final int MRJ_2_0 = 0;
\r
147 /** JVM constant for MRJ 2.1 or later */
\r
148 private static final int MRJ_2_1 = 1;
\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
153 /** JVM constant for MRJ 3.1 */
\r
154 private static final int MRJ_3_1 = 4;
\r
156 /** JVM constant for any Windows NT JVM */
\r
157 private static final int WINDOWS_NT = 5;
\r
159 /** JVM constant for any Windows 9x JVM */
\r
160 private static final int WINDOWS_9x = 6;
\r
162 /** JVM constant for any other platform */
\r
163 private static final int OTHER = -1;
\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
169 private static final String FINDER_TYPE = "FNDR";
\r
172 * The creator code of the Finder on a Macintosh, which is needed to send AppleEvents to the
\r
175 private static final String FINDER_CREATOR = "MACS";
\r
177 /** The name for the AppleEvent type corresponding to a GetURL event. */
\r
178 private static final String GURL_EVENT = "GURL";
\r
181 * The first parameter that needs to be passed into Runtime.exec() to open the default web
\r
182 * browser on Windows.
\r
184 private static final String FIRST_WINDOWS_PARAMETER = "/c";
\r
186 /** The second parameter for Runtime.exec() on Windows. */
\r
187 private static final String SECOND_WINDOWS_PARAMETER = "start";
\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
194 private static final String THIRD_WINDOWS_PARAMETER = "\"\"";
\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
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
206 * The message from any exception thrown throughout the initialization process.
\r
208 private static String errorMessage;
\r
211 * An initialization block that determines the operating system and loads the necessary
\r
216 loadedWithoutErrors = true;
\r
218 String osName = System.getProperty("os.name");
\r
220 if (osName.startsWith("Mac OS"))
\r
222 String mrjVersion = System.getProperty("mrj.version");
\r
223 String majorMRJVersion = mrjVersion.substring(0, 3);
\r
227 double version = Double.valueOf(majorMRJVersion).doubleValue();
\r
233 else if ( (version >= 2.1) && (version < 3))
\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
240 else if (version == 3.0)
\r
244 else if (version >= 3.1)
\r
246 // Assume that all 3.1 and later versions of MRJ work the same.
\r
251 loadedWithoutErrors = false;
\r
252 errorMessage = "Unsupported MRJ version: " + version;
\r
255 catch (NumberFormatException nfe)
\r
257 loadedWithoutErrors = false;
\r
258 errorMessage = "Invalid MRJ version: " + mrjVersion;
\r
261 else if (osName.startsWith("Windows"))
\r
263 if (osName.indexOf("9") != -1)
\r
277 if (loadedWithoutErrors)
\r
278 { // if we haven't hit any errors yet
\r
279 loadedWithoutErrors = loadClasses();
\r
284 * This class should be never be instantiated; this just ensures so.
\r
286 private BrowserLauncher()
\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
296 private static boolean loadClasses()
\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
311 aeTargetConstructor = aeTargetClass.getDeclaredConstructor(new Class[]
\r
315 appleEventConstructor = appleEventClass.getDeclaredConstructor(new
\r
318 int.class, int.class, aeTargetClass, int.class,
\r
321 aeDescConstructor = aeDescClass.getDeclaredConstructor(new Class[]
\r
326 makeOSType = osUtilsClass.getDeclaredMethod("makeOSType",
\r
329 putParameter = appleEventClass.getDeclaredMethod("putParameter",
\r
331 {int.class, aeDescClass});
\r
332 sendNoReply = appleEventClass.getDeclaredMethod("sendNoReply",
\r
336 Field keyDirectObjectField = aeClass.getDeclaredField(
\r
337 "keyDirectObject");
\r
338 keyDirectObject = (Integer) keyDirectObjectField.get(null);
\r
340 Field autoGenerateReturnIDField = appleEventClass.getDeclaredField(
\r
341 "kAutoGenerateReturnID");
\r
342 kAutoGenerateReturnID = (Integer) autoGenerateReturnIDField.get(null);
\r
344 Field anyTransactionIDField = appleEventClass.getDeclaredField(
\r
345 "kAnyTransactionID");
\r
346 kAnyTransactionID = (Integer) anyTransactionIDField.get(null);
\r
348 catch (ClassNotFoundException cnfe)
\r
350 errorMessage = cnfe.getMessage();
\r
354 catch (NoSuchMethodException nsme)
\r
356 errorMessage = nsme.getMessage();
\r
360 catch (NoSuchFieldException nsfe)
\r
362 errorMessage = nsfe.getMessage();
\r
366 catch (IllegalAccessException iae)
\r
368 errorMessage = iae.getMessage();
\r
380 mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils");
\r
381 mrjOSTypeClass = Class.forName("com.apple.mrj.MRJOSType");
\r
383 Field systemFolderField = mrjFileUtilsClass.getDeclaredField(
\r
384 "kSystemFolderType");
\r
385 kSystemFolderType = systemFolderField.get(null);
\r
386 findFolder = mrjFileUtilsClass.getDeclaredMethod("findFolder",
\r
389 getFileCreator = mrjFileUtilsClass.getDeclaredMethod("getFileCreator",
\r
392 getFileType = mrjFileUtilsClass.getDeclaredMethod("getFileType",
\r
396 catch (ClassNotFoundException cnfe)
\r
398 errorMessage = cnfe.getMessage();
\r
402 catch (NoSuchFieldException nsfe)
\r
404 errorMessage = nsfe.getMessage();
\r
408 catch (NoSuchMethodException nsme)
\r
410 errorMessage = nsme.getMessage();
\r
414 catch (SecurityException se)
\r
416 errorMessage = se.getMessage();
\r
420 catch (IllegalAccessException iae)
\r
422 errorMessage = iae.getMessage();
\r
434 Class linker = Class.forName("com.apple.mrj.jdirect.Linker");
\r
435 Constructor constructor = linker.getConstructor(new Class[]
\r
439 linkage = constructor.newInstance(new Object[]
\r
441 BrowserLauncher.class
\r
444 catch (ClassNotFoundException cnfe)
\r
446 errorMessage = cnfe.getMessage();
\r
450 catch (NoSuchMethodException nsme)
\r
452 errorMessage = nsme.getMessage();
\r
456 catch (InvocationTargetException ite)
\r
458 errorMessage = ite.getMessage();
\r
462 catch (InstantiationException ie)
\r
464 errorMessage = ie.getMessage();
\r
468 catch (IllegalAccessException iae)
\r
470 errorMessage = iae.getMessage();
\r
482 mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils");
\r
483 openURL = mrjFileUtilsClass.getDeclaredMethod("openURL",
\r
487 catch (ClassNotFoundException cnfe)
\r
489 errorMessage = cnfe.getMessage();
\r
493 catch (NoSuchMethodException nsme)
\r
495 errorMessage = nsme.getMessage();
\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
518 private static Object locateBrowser()
\r
520 if (browser != null)
\r
531 Integer finderCreatorCode = (Integer) makeOSType.invoke(null,
\r
534 Object aeTarget = aeTargetConstructor.newInstance(new Object[]
\r
538 Integer gurlType = (Integer) makeOSType.invoke(null,
\r
541 Object appleEvent = appleEventConstructor.newInstance(new Object[]
\r
543 gurlType, gurlType, aeTarget, kAutoGenerateReturnID,
\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
554 catch (IllegalAccessException iae)
\r
557 errorMessage = iae.getMessage();
\r
561 catch (InstantiationException ie)
\r
564 errorMessage = ie.getMessage();
\r
568 catch (InvocationTargetException ite)
\r
571 errorMessage = ite.getMessage();
\r
582 systemFolder = (File) findFolder.invoke(null,
\r
584 {kSystemFolderType});
\r
586 catch (IllegalArgumentException iare)
\r
589 errorMessage = iare.getMessage();
\r
593 catch (IllegalAccessException iae)
\r
596 errorMessage = iae.getMessage();
\r
600 catch (InvocationTargetException ite)
\r
603 errorMessage = ite.getTargetException().getClass() + ": " +
\r
604 ite.getTargetException().getMessage();
\r
609 String[] systemFolderFiles = systemFolder.list();
\r
611 // Avoid a FilenameFilter because that can't be stopped mid-list
\r
612 for (int i = 0; i < systemFolderFiles.length; i++)
\r
616 File file = new File(systemFolder, systemFolderFiles[i]);
\r
618 if (!file.isFile())
\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
632 if (FINDER_TYPE.equals(fileType.toString()))
\r
634 Object fileCreator = getFileCreator.invoke(null,
\r
638 if (FINDER_CREATOR.equals(fileCreator.toString()))
\r
640 browser = file.toString(); // Actually the Finder, but that's OK
\r
646 catch (IllegalArgumentException iare)
\r
648 errorMessage = iare.getMessage();
\r
652 catch (IllegalAccessException iae)
\r
655 errorMessage = iae.getMessage();
\r
659 catch (InvocationTargetException ite)
\r
662 errorMessage = ite.getTargetException().getClass() + ": " +
\r
663 ite.getTargetException().getMessage();
\r
675 browser = ""; // Return something non-null
\r
680 browser = "cmd.exe";
\r
685 browser = "command.com";
\r
691 browser = jalview.bin.Cache.getDefault("DEFAULT_BROWSER", "firefox");
\r
699 * used to ensure that browser is up-to-date after a configuration
\r
700 * change (Unix DEFAULT_BROWSER property change).
\r
702 public static void resetBrowser() {
\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
710 public static void openURL(String url)
\r
713 if (!loadedWithoutErrors)
\r
715 throw new IOException("Exception in finding browser: " +
\r
719 Object browser = locateBrowser();
\r
721 if (browser == null)
\r
723 throw new IOException("Unable to locate browser: " + errorMessage);
\r
730 Object aeDesc = null;
\r
734 aeDesc = aeDescConstructor.newInstance(new Object[]
\r
736 putParameter.invoke(browser,
\r
738 {keyDirectObject, aeDesc});
\r
739 sendNoReply.invoke(browser, new Object[]
\r
742 catch (InvocationTargetException ite)
\r
744 throw new IOException(
\r
745 "InvocationTargetException while creating AEDesc: " +
\r
748 catch (IllegalAccessException iae)
\r
750 throw new IOException(
\r
751 "IllegalAccessException while building AppleEvent: " +
\r
754 catch (InstantiationException ie)
\r
756 throw new IOException(
\r
757 "InstantiationException while creating AEDesc: " +
\r
762 aeDesc = null; // Encourage it to get disposed if it was created
\r
763 browser = null; // Ditto
\r
769 Runtime.getRuntime().exec(new String[]
\r
770 { (String) browser, url});
\r
776 int[] instance = new int[1];
\r
777 int result = ICStart(instance, 0);
\r
781 int[] selectionStart = new int[]
\r
784 byte[] urlBytes = url.getBytes();
\r
785 int[] selectionEnd = new int[]
\r
788 result = ICLaunchURL(instance[0], new byte[]
\r
790 urlBytes.length, selectionStart, selectionEnd);
\r
794 // Ignore the return value; the URL was launched successfully
\r
795 // regardless of what happens here.
\r
800 throw new IOException("Unable to launch URL: " + result);
\r
805 throw new IOException(
\r
806 "Unable to create an Internet Config instance: " + result);
\r
815 openURL.invoke(null, new Object[]
\r
818 catch (InvocationTargetException ite)
\r
820 throw new IOException(
\r
821 "InvocationTargetException while calling openURL: " +
\r
824 catch (IllegalAccessException iae)
\r
826 throw new IOException(
\r
827 "IllegalAccessException while calling openURL: " +
\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
841 (String) browser, FIRST_WINDOWS_PARAMETER,
\r
842 SECOND_WINDOWS_PARAMETER, THIRD_WINDOWS_PARAMETER,
\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
851 process.exitValue();
\r
853 catch (InterruptedException ie)
\r
855 throw new IOException(
\r
856 "InterruptedException while launching browser: " +
\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
869 /* System.out.println("Executing : "+browser+" "+
\r
870 NETSCAPE_REMOTE_PARAMETER+" "+
\r
871 NETSCAPE_OPEN_PARAMETER_START +
\r
873 NETSCAPE_OPEN_NEW_WINDOW +
\r
874 NETSCAPE_OPEN_PARAMETER_END);
\r
876 process = Runtime.getRuntime().exec(new String[]
\r
879 NETSCAPE_REMOTE_PARAMETER,
\r
881 NETSCAPE_OPEN_PARAMETER_START + url +
\r
882 NETSCAPE_OPEN_NEW_WINDOW +
\r
883 NETSCAPE_OPEN_PARAMETER_END
\r
888 int exitCode = process.waitFor();
\r
891 { // if Netscape was not open
\r
892 Runtime.getRuntime().exec(new String[]
\r
893 { (String) browser, url});
\r
896 catch (InterruptedException ie)
\r
898 throw new IOException(
\r
899 "InterruptedException while launching browser: " +
\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
917 * Methods required for Mac OS X. The presence of native methods does not cause
\r
918 * any problems on other platforms.
\r
920 private native static int ICStart(int[] instance, int signature);
\r
922 private native static int ICStop(int[] instance);
\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