- /**
- * The Java virtual machine that we are running on. Actually, in most cases we
- * only care about the operating system, but some operating systems require us
- * to switch on the VM.
- */
- private static int jvm;
-
- /** The browser for the system */
- private static Object browser;
-
- /**
- * Caches whether any classes, methods, and fields that are not part of the
- * JDK and need to be dynamically loaded at runtime loaded successfully.
- * <p>
- * Note that if this is <code>false</code>, <code>openURL()</code> 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 <init>(int) method of com.apple.MacOS.AETarget */
- private static Constructor aeTargetConstructor;
-
- /** The <init>(int, int, int) method of com.apple.MacOS.AppleEvent */
- private static Constructor appleEventConstructor;
-
- /** The <init>(String) method of com.apple.MacOS.AEDesc */
- private static Constructor aeDescConstructor;
-
- /** The findFolder method of com.apple.mrj.MRJFileUtils */
- private static Method findFolder;
-
- /** The getFileCreator method of com.apple.mrj.MRJFileUtils */
- private static Method getFileCreator;
-
- /** The getFileType method of com.apple.mrj.MRJFileUtils */
- private static Method getFileType;
-
- /** The openURL method of com.apple.mrj.MRJFileUtils */
- private static Method openURL;
-
- /** The makeOSType method of com.apple.MacOS.OSUtils */
- private static Method makeOSType;
-
- /** The putParameter method of com.apple.MacOS.AppleEvent */
- private static Method putParameter;
-
- /** The sendNoReply method of com.apple.MacOS.AppleEvent */
- private static Method sendNoReply;
-
- /** Actually an MRJOSType pointing to the System Folder on a Macintosh */
- private static Object kSystemFolderType;
-
- /** The keyDirectObject AppleEvent parameter type */
- private static Integer keyDirectObject;
-
- /** The kAutoGenerateReturnID AppleEvent code */
- private static Integer kAutoGenerateReturnID;
-
- /** The kAnyTransactionID AppleEvent code */
- private static Integer kAnyTransactionID;
-
- /** The linkage object required for JDirect 3 on Mac OS X. */
- private static Object linkage;
-
- /** The framework to reference on Mac OS X */
- private static final String JDirect_MacOSX = "/System/Library/Frameworks/Carbon.framework/Frameworks/HIToolbox.framework/HIToolbox";
-
- /** JVM constant for MRJ 2.0 */
- private static final int MRJ_2_0 = 0;
-
- /** JVM constant for MRJ 2.1 or later */
- private static final int MRJ_2_1 = 1;
-
- /** JVM constant for Java on Mac OS X 10.0 (MRJ 3.0) */
- private static final int MRJ_3_0 = 3;
-
- /** JVM constant for MRJ 3.1 */
- private static final int MRJ_3_1 = 4;
-
- /** JVM constant for any Windows NT JVM */
- private static final int WINDOWS_NT = 5;
-
- /** JVM constant for any Windows 9x JVM */
- private static final int WINDOWS_9x = 6;
-
- /** JVM constant for any other platform */
- private static final int OTHER = -1;
-
- /**
- * The file type of the Finder on a Macintosh. Hardcoding "Finder" would keep
- * non-U.S. English systems from working properly.
- */
- private static final String FINDER_TYPE = "FNDR";
-
- /**
- * The creator code of the Finder on a Macintosh, which is needed to send
- * AppleEvents to the application.
- */
- private static final String FINDER_CREATOR = "MACS";
-
- /** The name for the AppleEvent type corresponding to a GetURL event. */
- private static final String GURL_EVENT = "GURL";
-
- /**
- * The first parameter that needs to be passed into Runtime.exec() to open the
- * default web browser on Windows.
- */
- private static final String FIRST_WINDOWS_PARAMETER = "/c";
-
- /** The second parameter for Runtime.exec() on Windows. */
- private static final String SECOND_WINDOWS_PARAMETER = "start";
-
- /**
- * The third parameter for Runtime.exec() on Windows. This is a "title"
- * parameter that the command line expects. Setting this parameter allows URLs
- * containing spaces to work.
- */
- private static final String THIRD_WINDOWS_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 final String NETSCAPE_REMOTE_PARAMETER = "-remote";
-
- private static final String NETSCAPE_OPEN_PARAMETER_START = "openURL(";
-
- private static final String NETSCAPE_OPEN_NEW_WINDOW = ", new-window";
-
- private static final String NETSCAPE_OPEN_PARAMETER_END = ")";
-
- /**
- * The message from any exception thrown throughout the initialization
- * process.
- */
- private static String errorMessage;
-
- /**
- * An initialization block that determines the operating system and loads the
- * necessary runtime data.
- */
- static
- {
-
- loadedWithoutErrors = true;
-
- if (!Platform.isJS())
- /**
- * Java only
- *
- * @j2sIgnore
- *
- */
- {
- String osName = System.getProperty("os.name");
-
- if (osName.startsWith("Mac OS"))
- {
- String mrjVersion = System.getProperty("mrj.version");
- String majorMRJVersion;
- if (mrjVersion == null)
- {
- // must be on some later build with mrj support
- majorMRJVersion = "3.1";
- }
- else
- {
- majorMRJVersion = mrjVersion.substring(0, 3);
- }
-
- try
- {
- double version = Double.valueOf(majorMRJVersion).doubleValue();
-
- if (version == 2)
- {
- jvm = MRJ_2_0;
- }
- else if ((version >= 2.1) && (version < 3))
- {
- // Assume that all 2.x versions of MRJ work the same. MRJ 2.1 actually
- // works via Runtime.exec() and 2.2 supports that but has an openURL()
- // method
- // as well that we currently ignore.
- jvm = MRJ_2_1;
- }
- else if (version == 3.0)
- {
- jvm = MRJ_3_0;
- }
- else if (version >= 3.1)
- {
- // Assume that all 3.1 and later versions of MRJ work the same.
- jvm = MRJ_3_1;
- }
- else
- {
- loadedWithoutErrors = false;
- errorMessage = "Unsupported MRJ version: " + version;
- }
- } catch (NumberFormatException nfe)
- {
- loadedWithoutErrors = false;
- errorMessage = "Invalid MRJ version: " + mrjVersion;
- }
- }
- else if (osName.startsWith("Windows"))
- {
- if (osName.indexOf("9") != -1)
- {
- jvm = WINDOWS_9x;
- }
- else
- {
- jvm = WINDOWS_NT;
- }
- }
- else
- {
- jvm = OTHER;
- }
-
- if (loadedWithoutErrors)
- { // if we haven't hit any errors yet
- loadedWithoutErrors = loadClasses();
- }
- }
- }
-
- /**
- * This class should be never be instantiated; this just ensures so.
- */
- private BrowserLauncher()
- {
- }
-
- /**
- * Called by a static initializer to load any classes, fields, and methods
- * required at runtime to locate the user's web browser.
- *
- * @return <code>true</code> if all intialization succeeded <code>false</code>
- * if any portion of the initialization failed
- */
- private static boolean loadClasses()
- {
-
- if (!Platform.isJS())
- /**
- * Java only
- *
- * @j2sIgnore
- *
- */
- {
- 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();