-
- /**
- * Attempts to open the default web browser to the given URL.
- *
- * @param url
- * The URL to open
- * @throws 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(MessageManager.formatMessage(
- "exception.browser_not_found", new String[] { errorMessage }));
- }
-
- Object browser = locateBrowser();
-
- if (browser == null)
- {
- throw new IOException(MessageManager.formatMessage(
- "exception.browser_unable_to_locate",
- new String[] { errorMessage }));
- }
-
- switch (jvm)
- {
- case MRJ_2_0:
-
- 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(MessageManager.formatMessage(
- "exception.invocation_target_exception_creating_aedesc",
- new String[] { ite.getMessage() }));
- } catch (IllegalAccessException iae)
- {
- throw new IOException(MessageManager.formatMessage(
- "exception.illegal_access_building_apple_evt", new String[]
- { iae.getMessage() }));
- } catch (InstantiationException ie)
- {
- throw new IOException(MessageManager.formatMessage(
- "exception.illegal_access_building_apple_evt", new String[]
- { ie.getMessage() }));
- } finally
- {
- aeDesc = null; // Encourage it to get disposed if it was created
- browser = null; // Ditto
- }
-
- break;
-
- case MRJ_2_1:
- Runtime.getRuntime().exec(new String[] { (String) browser, url });
-
- break;
-
- case MRJ_3_0:
-
- int[] instance = new int[1];
- int result = ICStart(instance, 0);
-
- if (result == 0)
- {
- int[] selectionStart = new int[] { 0 };
- byte[] urlBytes = url.getBytes();
- 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(MessageManager.formatMessage(
- "exception.unable_to_launch_url", new String[] { Integer
- .valueOf(result).toString() }));
- }
- }
- else
- {
- throw new IOException(MessageManager.formatMessage(
- "exception.unable_to_create_internet_config",
- new String[] { Integer.valueOf(result).toString() }));
- }
-
- break;
-
- case MRJ_3_1:
-
- try
- {
- openURL.invoke(null, new Object[] { url });
- } catch (InvocationTargetException ite)
- {
- throw new IOException(MessageManager.formatMessage(
- "exception.invocation_target_calling_url",
- new String[] { ite.getMessage() }));
- } catch (IllegalAccessException iae)
- {
- throw new IOException(MessageManager.formatMessage(
- "exception.illegal_access_calling_url",
- new String[] { iae.getMessage() }));
- }
-
- break;
-
- case WINDOWS_NT:
- case WINDOWS_9x:
-
- // Add quotes around the URL to allow ampersands and other special
- // characters to work.
- Process process = Runtime.getRuntime().exec(
- new String[] { (String) browser, FIRST_WINDOWS_PARAMETER,
- SECOND_WINDOWS_PARAMETER, THIRD_WINDOWS_PARAMETER,
- '"' + url + '"' });
-
- // This avoids a memory leak on some versions of Java on Windows.
- // That's hinted at in
- // <http://developer.java.sun.com/developer/qow/archive/68/>.
- try
- {
- process.waitFor();
- process.exitValue();
- } catch (InterruptedException ie)
- {
- throw new IOException(MessageManager.formatMessage(
- "exception.interrupted_launching_browser",
- new String[] { ie.getMessage() }));
- }
-
- break;
-
- case OTHER:
-
- // Assume that we're on Unix and that Netscape (actually Firefox) is
- // installed
- // First, attempt to open the URL in a currently running session of
- // Netscape
- // JBPNote log debug
-
- /*
- * System.out.println("Executing : "+browser+" "+
- * NETSCAPE_REMOTE_PARAMETER+" "+ NETSCAPE_OPEN_PARAMETER_START + url +
- * NETSCAPE_OPEN_NEW_WINDOW + NETSCAPE_OPEN_PARAMETER_END);
- */
- process = Runtime.getRuntime().exec(
- new String[] {
- (String) browser,
- NETSCAPE_REMOTE_PARAMETER,
-
- NETSCAPE_OPEN_PARAMETER_START + url
- + NETSCAPE_OPEN_NEW_WINDOW
- + NETSCAPE_OPEN_PARAMETER_END });
-
- try
- {
- int exitCode = process.waitFor();
-
- if (exitCode != 0)
- { // if Netscape was not open
- Runtime.getRuntime().exec(new String[] { (String) browser, url });
- }
- } catch (InterruptedException ie)
- {
- throw new IOException(MessageManager.formatMessage(
- "exception.interrupted_launching_browser",
- new String[] { ie.getMessage() }));
- }
-
- break;
-
- default:
-
- // This should never occur, but if it does, we'll try the simplest thing
- // possible
- Runtime.getRuntime().exec(new String[] { (String) browser, url });
-
- break;
- }
- }
-
- /**
- * Methods required for Mac OS X. The presence of native methods does not
- * cause any problems on other platforms.
- */
- private native static int ICStart(int[] instance, int signature);
-
- private native static int ICStop(int[] instance);
-
- private native static int ICLaunchURL(int instance, byte[] hint,
- byte[] data, int len, int[] selectionStart, int[] selectionEnd);
-}