/* Launch the user's default web browser. Copyright (c) 2002-2014 The Regents of the University of California. All rights reserved. Permission is hereby granted, without written agreement and without license or royalty fees, to use, copy, modify, and distribute this software and its documentation for any purpose, provided that the above copyright notice and the following two paragraphs appear in all copies of this software. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. PT_COPYRIGHT_VERSION_2 COPYRIGHTENDKEY */ package ptolemy.actor.gui; import java.io.File; import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.net.URL; import ptolemy.util.StringUtilities; /** 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 Firefox via a shell command.
This code is Copyright 1999-2001 by Eric Albert (ejalbert@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 is more out of curiosity than anything and is not required. This software includes no warranty. The author is not responsible 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, and Ron Rabakukk
On November 3, 2003, BrowserLauncher was downloaded from
http://browserlauncher.sourceforge.net/.
That URL no longer exists, there is a replacement package which is LGPL'd.
See http://web.archive.org/web/20031028032907/http://browserlauncher.sourceforge.net/
for the original web page.
For example, to open the user's default browser on * http://www.eecs.berkeley.edu *
* java -classpath $PTII ptolemy.actor.gui.BrowserLauncher http://www.eecs.berkeley.edu ** @param args An array of command line arguments. The first * argument names a URL to be opened. If there is no first * argument, then open http://ptolemy.eecs.berkeley.edu. Second * and subsequent arguments are ignored. * @exception Exception If there is a problem launching the browser. */ public static void main(String[] args) throws Exception { if (args.length >= 1) { // Ignore any arguments after the first one. BrowserLauncher.openURL(args[0]); } else { BrowserLauncher.openURL("http://ptolemy.eecs.berkeley.edu"); } if (BrowserLauncher.delayExit) { System.out.println("Delaying exit for 10 seconds because we" + "may have copied a jar: file"); try { Thread.sleep(10000); } catch (InterruptedException e) { } StringUtilities.exit(0); } } /** * Attempts to open the default web browser to the given URL. * *
We use the following strategy to find URLs that may be inside
* jar files:
*
If the string does not start with "http": see if it is a
* file.
*
If the file cannot be found, look it up in the classpath.
*
If the file can be found in the classpath then use the
* found file instead of the given URL.
*
If the file cannot be found in the classpath, then pass the
* original given URL to the browser.
*
If the ptolemy.ptII.browser property is set, then its value
* is used as the value of the browser.
*
To always use Internet Explorer, one might invoke Ptolemy
* with:
*
* java -classpath $PTII -Dptolemy.ptII.browser=c:\\Program\ Files\\Internet\ Explorer\\iexplore.exe ptolemy.vergil.VergilApplication **
To always use Firefox: *
* java -classpath $PTII -Dptolemy.ptII.browser=c:\\Program\ Files\\Mozilla\ Firefox\\firefox ptolemy.vergil.VergilApplication ** *
To preserve your browser choice set the ptolemy.ptII.browser
* property in $PTII/lib/ptII.properties
. Note that
* each time $PTII/bin/configure
is run,
* $PTII/lib/ptII.properties.in
is read and
* $PTII/lib/ptII.properties
is overwritten, so you
* may want to add your changes to
co * $PTII/lib/ptII.properties.in
.
*
* @param url The URL to open.
* It is best if the first argument is an absolute URL
* as opposed to a relative URL.
* @exception IOException If the web browser could not be located or
* does not run
*/
public static void openURL(String url) throws IOException {
if (!loadedWithoutErrors) {
throw new IOException("Exception in finding browser: "
+ errorMessage);
}
if (!url.startsWith("http:") && !url.startsWith("https:")) {
// If the url does not start with http:, then look it up
// as a regular file and then possibly in the classpath.
File urlFile = null;
try {
urlFile = new File(url);
} catch (Exception ex) {
// Ignored, we try to fix this below.
urlFile = null;
}
if (urlFile == null || !urlFile.exists()) {
// The file could not be found, so try the search path mambo.
// We might be in the Swing Event thread, so
// Thread.currentThread().getContextClassLoader()
// .getResource(entry) probably will not work.
String refClassName = "ptolemy.kernel.util.NamedObj";
try {
Class refClass = Class.forName(refClassName);
URL entryURL = refClass.getClassLoader().getResource(url);
if (entryURL != null && !url.startsWith("jar:")) {
System.out.println("BrowserLauncher: Could not "
+ "find '" + url + "', but '" + entryURL
+ "' was found.");
url = entryURL.toString();
} else {
if (url.startsWith("jar:")) {
// If the URL begins with jar: then we are
// inside Web Start and we should get the
// resource, write it to a temporary file
// and pass that value to the browser
// Save the jar file as a temporary file
// in the default platform dependent
// directory with the same suffix as that
// of the jar URL
// FIXME: we should probably cache this
// copy somehow.
String old = url;
String temporaryURL = JNLPUtilities
.saveJarURLInClassPath(url);
if (temporaryURL != null) {
url = temporaryURL;
} else {
url = JNLPUtilities.saveJarURLAsTempFile(url,
"tmp", null, null);
delayExit = true;
}
System.out.println("BrowserLauncher: Could not "
+ "find '" + old + "', but jar url'" + url
+ "' was found.");
}
}
} catch (ClassNotFoundException ex) {
System.err.println("BrowserLauncher: Internal error, "
+ " Could not find " + refClassName);
}
}
}
if (!StringUtilities.getProperty("ptolemy.ptII.browser").equals("")) {
Runtime.getRuntime()
.exec(new String[] {
"\""
+ StringUtilities
.getProperty("ptolemy.ptII.browser")
+ "\"", url });
return;
}
Object browser = locateBrowser();
if (browser == null) {
throw new IOException("Unable to locate browser: " + errorMessage);
}
String args[];
switch (jvm) {
case MRJ_2_0:
errorMessage = "Command was a call to aeDescConstructor(" + url
+ ")";
Object aeDesc = null;
try {
aeDesc = aeDescConstructor.newInstance(new Object[] { url });
putParameter.invoke(browser, new Object[] { keyDirectObject,
aeDesc });
sendNoReply.invoke(browser, new Object[] {});
} catch (InvocationTargetException ite) {
throw new IOException("InvocationTargetException while "
+ "creating AEDesc: " + ite.getMessage());
} catch (IllegalAccessException iae) {
throw new IOException("IllegalAccessException while "
+ "building AppleEvent: " + iae.getMessage());
} catch (InstantiationException ie) {
throw new IOException("InstantiationException while "
+ "creating AEDesc: " + ie.getMessage());
} finally {
// Encourage it to get disposed if it was created
aeDesc = null;
// Ditto
browser = null;
}
break;
case MRJ_2_1:
args = new String[] { (String) browser, url };
errorMessage = "Command was: " + args[0] + " " + args[1];
Runtime.getRuntime().exec(args);
break;
case MRJ_3_0:
errorMessage = "Command was a call to ICLaunchURL(" + url + ")";
int[] instance = new int[1];
int result = ICStart(instance, 0);
if (result == 0) {
int[] selectionStart = new int[] { 0 };
byte[] urlBytes = url.getBytes("UTF-8");
int[] selectionEnd = new int[] { urlBytes.length };
result = ICLaunchURL(instance[0], new byte[] { 0 }, urlBytes,
urlBytes.length, selectionStart, selectionEnd);
if (result == 0) {
// Ignore the return value; the URL was launched
// successfully regardless of what happens here.
ICStop(instance);
} else {
throw new IOException("Unable to launch URL: " + result);
}
} else {
throw new IOException("Unable to create an Internet "
+ "Config instance: " + result);
}
break;
case MRJ_3_1:
if (!url.startsWith("file:") && !url.startsWith("http:")
&& !url.startsWith("https:")) {
// Needed by Web Start file:
// ptII/ptolemy/domains/ct/demo/CartPendulum/CartPendulum.jnlp
url = new File(url).toURI().toURL().toString();
}
errorMessage = "Command was a call to openURL(" + url + ")";
try {
openURL.invoke(null, new Object[] { url });
} catch (InvocationTargetException ite) {
throw new IOException("InvocationTargetException while "
+ "calling openURL() on " + url + " "
+ ite.getMessage());
} catch (IllegalAccessException iae) {
throw new IOException("IllegalAccessException while "
+ "calling openURL() on: " + url + " "
+ iae.getMessage());
}
break;
case WINDOWS_NT:
case WINDOWS_9x:
// Add quotes around the URL to allow ampersands and other special
// characters to work.
args = new String[] { (String) browser, FIRST_WINDOWS_PARAMETER,
SECOND_WINDOWS_PARAMETER, THIRD_WINDOWS_PARAMETER,
'"' + url + '"' };
Process process = Runtime.getRuntime().exec(args);
errorMessage = "Command was: "
+ args[0]
+ " "
+ args[1]
+ " "
+ args[2]
+ " "
+ args[3]
+ " "
+ args[4]
+ "\nNote: Under Windows, make sure that the file named by "
+ "the url is executable.";
// The return code returned by process.waitFor()
// 0 usually indicates normal execution.
int exitCode = 0;
// This avoids a memory leak on some versions of Java on Windows.
// That's hinted at in
//
Note that if this is false
,
* openURL()
will always return an IOException.
*/
private static boolean loadedWithoutErrors;
/** The com.apple.mrj.MRJFileUtils class */
private static Class mrjFileUtilsClass;
/** The com.apple.mrj.MRJOSType class */
private static Class mrjOSTypeClass;
/** The com.apple.MacOS.AEDesc class */
private static Class aeDescClass;
/** The true
if all initialization succeeded
* false
if any portion of the initialization failed
*/
private static boolean loadClasses() {
switch (jvm) {
case MRJ_2_0:
try {
Class aeTargetClass = Class.forName("com.apple.MacOS.AETarget");
Class osUtilsClass = Class.forName("com.apple.MacOS.OSUtils");
Class appleEventClass = Class
.forName("com.apple.MacOS.AppleEvent");
Class aeClass = Class.forName("com.apple.MacOS.ae");
aeDescClass = Class.forName("com.apple.MacOS.AEDesc");
aeTargetConstructor = aeTargetClass
.getDeclaredConstructor(new Class[] { int.class });
appleEventConstructor = appleEventClass
.getDeclaredConstructor(new Class[] { int.class,
int.class, aeTargetClass, int.class, int.class });
aeDescConstructor = aeDescClass
.getDeclaredConstructor(new Class[] { String.class });
makeOSType = osUtilsClass.getDeclaredMethod("makeOSType",
new Class[] { String.class });
putParameter = appleEventClass.getDeclaredMethod(
"putParameter", new Class[] { int.class, aeDescClass });
sendNoReply = appleEventClass.getDeclaredMethod("sendNoReply",
new Class[] {});
Field keyDirectObjectField = aeClass
.getDeclaredField("keyDirectObject");
keyDirectObject = (Integer) keyDirectObjectField.get(null);
Field autoGenerateReturnIDField = appleEventClass
.getDeclaredField("kAutoGenerateReturnID");
kAutoGenerateReturnID = (Integer) autoGenerateReturnIDField
.get(null);
Field anyTransactionIDField = appleEventClass
.getDeclaredField("kAnyTransactionID");
kAnyTransactionID = (Integer) anyTransactionIDField.get(null);
} catch (ClassNotFoundException cnfe) {
errorMessage = cnfe.getMessage();
return false;
} catch (NoSuchMethodException nsme) {
errorMessage = nsme.getMessage();
return false;
} catch (NoSuchFieldException nsfe) {
errorMessage = nsfe.getMessage();
return false;
} catch (IllegalAccessException iae) {
errorMessage = iae.getMessage();
return false;
}
break;
case MRJ_2_1:
try {
mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils");
mrjOSTypeClass = Class.forName("com.apple.mrj.MRJOSType");
Field systemFolderField = mrjFileUtilsClass
.getDeclaredField("kSystemFolderType");
kSystemFolderType = systemFolderField.get(null);
findFolder = mrjFileUtilsClass.getDeclaredMethod("findFolder",
new Class[] { mrjOSTypeClass });
getFileCreator = mrjFileUtilsClass.getDeclaredMethod(
"getFileCreator", new Class[] { File.class });
getFileType = mrjFileUtilsClass.getDeclaredMethod(
"getFileType", new Class[] { File.class });
} catch (ClassNotFoundException cnfe) {
errorMessage = cnfe.getMessage();
return false;
} catch (NoSuchFieldException nsfe) {
errorMessage = nsfe.getMessage();
return false;
} catch (NoSuchMethodException nsme) {
errorMessage = nsme.getMessage();
return false;
} catch (SecurityException se) {
errorMessage = se.getMessage();
return false;
} catch (IllegalAccessException iae) {
errorMessage = iae.getMessage();
return false;
}
break;
case MRJ_3_0:
try {
Class linker = Class.forName("com.apple.mrj.jdirect.Linker");
Constructor constructor = linker
.getConstructor(new Class[] { Class.class });
/*linkage = */constructor
.newInstance(new Object[] { BrowserLauncher.class });
} catch (ClassNotFoundException cnfe) {
errorMessage = cnfe.getMessage();
return false;
} catch (NoSuchMethodException nsme) {
errorMessage = nsme.getMessage();
return false;
} catch (InvocationTargetException ite) {
errorMessage = ite.getMessage();
return false;
} catch (InstantiationException ie) {
errorMessage = ie.getMessage();
return false;
} catch (IllegalAccessException iae) {
errorMessage = iae.getMessage();
return false;
}
break;
case MRJ_3_1:
try {
mrjFileUtilsClass = Class.forName("com.apple.mrj.MRJFileUtils");
openURL = mrjFileUtilsClass.getDeclaredMethod("openURL",
new Class[] { String.class });
} catch (ClassNotFoundException cnfe) {
errorMessage = cnfe.getMessage();
return false;
} catch (NoSuchMethodException nsme) {
errorMessage = nsme.getMessage();
return false;
}
break;
default:
break;
}
return true;
}
/**
* Attempts to locate the default web browser on the local system.
* Caches results so it only locates the browser once for each use
* of this class per JVM instance.
*
* @return The browser for the system. Note that this may not be
* what you would consider to be a standard web browser;
* instead, it's the application that gets called to open the
* default web browser. In some cases, this will be a non-String
* object that provides the means of calling the default browser.
*
*/
private static Object locateBrowser() {
if (browser != null) {
return browser;
}
switch (jvm) {
case MRJ_2_0:
try {
Integer finderCreatorCode = (Integer) makeOSType.invoke(null,
new Object[] { FINDER_CREATOR });
Object aeTarget = aeTargetConstructor
.newInstance(new Object[] { finderCreatorCode });
Integer gurlType = (Integer) makeOSType.invoke(null,
new Object[] { GURL_EVENT });
Object appleEvent = appleEventConstructor
.newInstance(new Object[] { gurlType, gurlType,
aeTarget, kAutoGenerateReturnID,
kAnyTransactionID });
// Don't set browser = appleEvent because then the
// next time we call locateBrowser(), we'll get the
// same AppleEvent, to which we'll already have added
// the relevant parameter. Instead, regenerate the
// AppleEvent every time. There's probably a way to
// do this better; if any has any ideas, please let me
// know.
return appleEvent;
} catch (IllegalAccessException iae) {
browser = null;
errorMessage = iae.getMessage();
return browser;
} catch (InstantiationException ie) {
browser = null;
errorMessage = ie.getMessage();
return browser;
} catch (InvocationTargetException ite) {
browser = null;
errorMessage = ite.getMessage();
return browser;
}
case MRJ_2_1:
File systemFolder;
try {
systemFolder = (File) findFolder.invoke(null,
new Object[] { kSystemFolderType });
} catch (IllegalArgumentException iare) {
browser = null;
errorMessage = iare.getMessage();
return browser;
} catch (IllegalAccessException iae) {
browser = null;
errorMessage = iae.getMessage();
return browser;
} catch (InvocationTargetException ite) {
browser = null;
errorMessage = ite.getTargetException().getClass() + ": "
+ ite.getTargetException().getMessage();
return browser;
}
String[] systemFolderFiles = systemFolder.list();
// Avoid a FilenameFilter because
// that can't be stopped mid-list
for (String systemFolderFile : systemFolderFiles) {
try {
File file = new File(systemFolder, systemFolderFile);
if (!file.isFile()) {
continue;
}
// We're looking for a file with a creator code of
// 'MACS' and a type of 'FNDR'. Only requiring
// the type results in non-Finder applications
// being picked up on certain Mac OS 9 systems,
// especially German ones, and sending a GURL
// event to those applications results in a logout
// under Multiple Users.
Object fileType = getFileType.invoke(null,
new Object[] { file });
if (FINDER_TYPE.equals(fileType.toString())) {
Object fileCreator = getFileCreator.invoke(null,
new Object[] { file });
if (FINDER_CREATOR.equals(fileCreator.toString())) {
// Actually the Finder, but that's OK
browser = file.toString();
return browser;
}
}
} catch (IllegalArgumentException iare) {
errorMessage = iare.getMessage();
return null;
} catch (IllegalAccessException iae) {
browser = null;
errorMessage = iae.getMessage();
return browser;
} catch (InvocationTargetException ite) {
browser = null;
errorMessage = ite.getTargetException().getClass() + ": "
+ ite.getTargetException().getMessage();
return browser;
}
}
browser = null;
break;
case MRJ_3_0:
case MRJ_3_1:
browser = ""; // Return something non-null
break;
case WINDOWS_NT:
browser = "cmd.exe";
break;
case WINDOWS_9x:
browser = "command.com";
break;
case OTHER:
default:
browser = "firefox";
break;
}
return browser;
}
}