|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--org.moyoman.client.reference.util.BrowserLauncher
BrowserLauncher is a class that provides one static method, openURL, which opens the default web browser for the current user of the system to the given URL. It may support other protocols depending on the system -- mailto, ftp, etc. -- but that has not been rigorously tested and is not guaranteed to work.
Yes, this is platform-specific code, and yes, it may rely on classes on certain platforms that are not part of the standard JDK. What we're trying to do, though, is to take something that's frequently desirable but inherently platform-specific -- opening a default browser -- and allow programmers (you, for example) to do so without worrying about dropping into native code or doing anything else similarly evil.
Anyway, this code is completely in Java and will run on all JDK 1.1-compliant systems without modification or a need for additional libraries. All classes that are required on certain platforms to allow this to run are dynamically loaded at runtime via reflection and, if not found, will not cause this to do anything other than returning an error when opening the browser.
There are certain system requirements for this class, as it's running through Runtime.exec(), which is Java's way of making a native system call. Currently, this requires that a Macintosh have a Finder which supports the GURL event, which is true for Mac OS 8.0 and 8.1 systems that have the Internet Scripting AppleScript dictionary installed in the Scripting Additions folder in the Extensions folder (which is installed by default as far as I know under Mac OS 8.0 and 8.1), and for all Mac OS 8.5 and later systems. On Windows, it only runs under Win32 systems (Windows 95, 98, and NT 4.0, as well as later versions of all). On other systems, this drops back from the inherently platform-sensitive concept of a default browser and simply attempts to launch Netscape via a shell command.
This code is Copyright 1999-2002 by Eric Albert (ejalbert AT cs.stanford.edu) and may be redistributed or modified in any form without restrictions as long as the portion of this comment from this paragraph through the end of the comment is not removed. The author requests that he be notified of any application, applet, or other binary that makes use of this code, but that's more out of curiosity than anything and is not required. This software includes no warranty. The author is not repsonsible for any loss of data or functionality or any adverse or unexpected effects of using this software.
Credits:
Steven Spencer, JavaWorld magazine (Java
Tip 66)
Thanks also to Ron B. Yeh, Eric Shapiro, Ben Engber, Paul Teitlebaum,
Andrea Cantatore, Larry Barowski, Trevor Bedzek, Frank Miedrich, Ron
Rabakukk, and Glenn Vanderburg
Field Summary | |
private static List |
_linuxBrowsers
Collection of Linux browser commands, in order of preference. |
private static Class |
aeDescClass
The com.apple.MacOS.AEDesc class |
private static Constructor |
aeDescConstructor
The <init>(String) method of com.apple.MacOS.AEDesc |
private static Constructor |
aeTargetConstructor
The <init>(int) method of com.apple.MacOS.AETarget |
private static Constructor |
appleEventConstructor
The <init>(int, int, int) method of com.apple.MacOS.AppleEvent |
private static Object |
browser
The browser for the system |
private static String |
errorMessage
The message from any exception thrown throughout the initialization process. |
private static String |
FINDER_CREATOR
The creator code of the Finder on a Macintosh, which is needed to send AppleEvents to the application. |
private static String |
FINDER_TYPE
The file type of the Finder on a Macintosh. |
private static Method |
findFolder
The findFolder method of com.apple.mrj.MRJFileUtils |
private static String |
FIRST_WINDOWS_PARAMETER
The first parameter that needs to be passed into Runtime.exec() to open the default web browser on Windows. |
private static Method |
getFileCreator
The getFileCreator method of com.apple.mrj.MRJFileUtils |
private static Method |
getFileType
The getFileType method of com.apple.mrj.MRJFileUtils |
private static String |
GURL_EVENT
The name for the AppleEvent type corresponding to a GetURL event. |
private static String |
JDirect_MacOSX
The framework to reference on Mac OS X 10.0.x |
private static int |
jvm
The Java virtual machine that we are running on. |
private static Integer |
kAnyTransactionID
The kAnyTransactionID AppleEvent code |
private static Integer |
kAutoGenerateReturnID
The kAutoGenerateReturnID AppleEvent code |
private static Integer |
keyDirectObject
The keyDirectObject AppleEvent parameter type |
private static Object |
kSystemFolderType
Actually an MRJOSType pointing to the System Folder on a Macintosh |
private static Object |
linkage
The linkage object required for JDirect 3 on Mac OS X |
private static int |
LINUX
JVM constant for Linux. |
private static boolean |
loadedWithoutErrors
Caches whether any classes, methods, and fields that are not part of the JDK and need to be dynamically loaded at runtime loaded successfully. |
private static Method |
makeOSType
The makeOSType method of com.apple.MacOS.OSUtils |
private static int |
MRJ_2_0
JVM constant for MRJ 2.0 |
private static int |
MRJ_2_1
JVM constant for MRJ 2.1.x and 2.2.x |
private static int |
MRJ_3_0
JVM constant for Java on Mac OS X 10.0 (MRJ 3.0) |
private static int |
MRJ_3_1
JVM constant for Java 1.3.x on Mac OS X 10.1 and later (MRJ 3.1 and 3.2) |
private static int |
MRJ_COCOA
JVM constant for Java 1.4.x and later on Mac OS X |
private static Class |
mrjFileUtilsClass
The com.apple.mrj.MRJFileUtils class |
private static Class |
mrjOSTypeClass
The com.apple.mrj.MRJOSType class |
private static String |
NETSCAPE_OPEN_PARAMETER_END
|
private static String |
NETSCAPE_OPEN_PARAMETER_START
|
private static String |
NETSCAPE_REMOTE_PARAMETER
The shell parameters for Netscape that opens a given URL in an already-open copy of Netscape on many command-line systems. |
private static Method |
openURL
The openURL method of com.apple.mrj.MRJFileUtils |
private static int |
OTHER
JVM constant for any other platform |
private static Method |
putParameter
The putParameter method of com.apple.MacOS.AppleEvent |
private static String |
SECOND_WINDOWS_PARAMETER
The second parameter for Runtime.exec() on Windows. |
private static Method |
sendNoReply
The sendNoReply method of com.apple.MacOS.AppleEvent |
private static String |
THIRD_WINDOWS_PARAMETER
The third parameter for Runtime.exec() on Windows. |
private static int |
WINDOWS_9x
JVM constant for any Windows 9x JVM |
private static int |
WINDOWS_NT
JVM constant for any Windows NT JVM |
Constructor Summary | |
private |
BrowserLauncher()
This class should be never be instantiated; this just ensures so. |
Method Summary | |
private static String |
getEnvironmentBrowser()
Tries to read the BROWSER environment variable, which should be set to the absolute path of the user's preferred web browser as proposed at http://tuxedo.org/~esr/BROWSER. |
private static int |
ICLaunchURL(int instance,
byte[] hint,
byte[] data,
int len,
int[] selectionStart,
int[] selectionEnd)
|
private static int |
ICStart(int[] instance,
int signature)
Methods required for Mac OS X 10.0.x. |
private static int |
ICStop(int[] instance)
|
private static boolean |
loadClasses()
Called by a static initializer to load any classes, fields, and methods required at runtime to locate the user's web browser. |
private static boolean |
loadMRJ20Classes()
Loads the classes, fields, and methods needed when running under MRJ 2.0. |
private static Object |
locateBrowser()
Attempts to locate the default web browser on the local system. |
static void |
openURL(String url)
Attempts to open the default web browser to the given URL. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
private static int jvm
private static Object browser
private static boolean loadedWithoutErrors
Note that if this is false
, openURL()
will
always return an IOException.
private static Class mrjFileUtilsClass
private static Class mrjOSTypeClass
private static Class aeDescClass
private static Constructor aeTargetConstructor
private static Constructor appleEventConstructor
private static Constructor aeDescConstructor
private static Method findFolder
private static Method getFileCreator
private static Method getFileType
private static Method openURL
private static Method makeOSType
private static Method putParameter
private static Method sendNoReply
private static Object kSystemFolderType
private static Integer keyDirectObject
private static Integer kAutoGenerateReturnID
private static Integer kAnyTransactionID
private static Object linkage
private static List _linuxBrowsers
private static final int LINUX
private static final String JDirect_MacOSX
private static final int MRJ_2_0
private static final int MRJ_2_1
private static final int MRJ_3_0
private static final int MRJ_3_1
private static final int MRJ_COCOA
private static final int WINDOWS_NT
private static final int WINDOWS_9x
private static final int OTHER
private static final String FINDER_TYPE
private static final String FINDER_CREATOR
private static final String GURL_EVENT
private static final String FIRST_WINDOWS_PARAMETER
private static final String SECOND_WINDOWS_PARAMETER
private static final String THIRD_WINDOWS_PARAMETER
private static final String NETSCAPE_REMOTE_PARAMETER
private static final String NETSCAPE_OPEN_PARAMETER_START
private static final String NETSCAPE_OPEN_PARAMETER_END
private static String errorMessage
Constructor Detail |
private BrowserLauncher()
Method Detail |
public static void openURL(String url) throws IOException
url
- The URL to open
IOException
- If the web browser could not be located or does not
runprivate static String getEnvironmentBrowser()
private static int ICLaunchURL(int instance, byte[] hint, byte[] data, int len, int[] selectionStart, int[] selectionEnd)
private static int ICStart(int[] instance, int signature)
private static int ICStop(int[] instance)
private static boolean loadClasses()
true
if all intialization succeeded
false
if any portion of the initialization failedprivate static boolean loadMRJ20Classes()
errorMessage
if it fails.
true
if all operations succeeded;
false
otherwiseprivate static Object locateBrowser()
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |