/** * Opens a url using a specific browser. * * <p>If the specified browser is not available, the method will fall through to calling the * default openUrl method. * * @param browser String * @param urlString String * @throws UnsupportedOperatingSystemException * @throws BrowserLaunchingExecutionException * @throws BrowserLaunchingInitializingException */ public void openUrl(String browser, String urlString) throws UnsupportedOperatingSystemException, BrowserLaunchingExecutionException, BrowserLaunchingInitializingException { if (IBrowserLaunching.BROWSER_DEFAULT.equals(browser) || browser == null) { logger.info("default or null browser target; falling through to non-targetted openUrl"); openUrl(urlString); } else { Map browserMap = getBrowserMap(); WindowsBrowser winBrowser = (WindowsBrowser) browserMap.get(browser); if (winBrowser == null) { logger.info("the available browsers list does not contain: " + browser); logger.info("falling through to non-targetted openUrl"); openUrl(urlString); } else { String protocol = null; try { protocol = getProtocol(urlString); } catch (MalformedURLException malrulex) { throw new BrowserLaunchingExecutionException(malrulex); } boolean successfullLaunch = openUrlWithBrowser(winBrowser, protocol, urlString); if (!successfullLaunch) { logger.debug("falling through to non-targetted openUrl"); openUrl(urlString); } } } }
/** * Attempts to open a url with the specified browser. This is a utility method called by the * openUrl methods. * * @param winBrowser WindowsBrowser * @param protocol String * @param urlString String * @return boolean * @throws BrowserLaunchingExecutionException */ private boolean openUrlWithBrowser(WindowsBrowser winBrowser, String protocol, String urlString) throws BrowserLaunchingExecutionException { boolean success = false; try { logger.info(winBrowser.getBrowserDisplayName()); logger.info(urlString); logger.info(protocol); String args = getCommandArgs(protocol, winBrowser, urlString, forceNewWindow); if (logger.isDebugEnabled()) { logger.debug(args); } Process process = Runtime.getRuntime().exec(args); // 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/>. process.waitFor(); // some browsers (mozilla, firefox) return 1 if you attempt to // open a url and an instance of that browser is already running // not clear why because the call is succeeding, ie the browser // opens the url. // If we don't say 1 is also a success, we get two browser // windows or tabs opened to the url. // // We could make this check smarter in the future if we run // into problems. the winBrowser object could handle the // check to make it browser specific. int exitValue = process.exitValue(); success = exitValue == 0 || exitValue == 1; } // Runtimes may throw InterruptedException // want to catch every possible exception and wrap it catch (Exception e) { throw new BrowserLaunchingExecutionException(e); } return success; }
/** * Opens a url using the default browser. * * @param urlString String * @throws UnsupportedOperatingSystemException * @throws BrowserLaunchingExecutionException * @throws BrowserLaunchingInitializingException */ public void openUrl(String urlString) throws UnsupportedOperatingSystemException, BrowserLaunchingExecutionException, BrowserLaunchingInitializingException { try { logger.info(urlString); String protocol = getProtocol(urlString); logger.info(protocol); // try the system prop first boolean successfullSystemPropLaunch = false; String browserName = System.getProperty(IBrowserLaunching.BROWSER_SYSTEM_PROPERTY, null); if (browserName != null) { Map browserMap = getBrowserMap(); WindowsBrowser winBrowser = (WindowsBrowser) browserMap.get(browserName); if (winBrowser != null) { logger.debug("using browser from system property"); successfullSystemPropLaunch = openUrlWithBrowser(winBrowser, protocol, urlString); } } if (!successfullSystemPropLaunch) { String[] args = getCommandArgs(protocol, urlString); if (logger.isDebugEnabled()) { logger.debug(getArrayAsString(args)); } Process process = Runtime.getRuntime().exec(args); // 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/>. process.waitFor(); process.exitValue(); } } catch (Exception e) { logger.error("fatal exception", e); throw new BrowserLaunchingExecutionException(e); } }
/** * Checks that the windows key is valid. * * @param logger AbstractLogger * @param windowsKey String */ public WindowsBrowserLaunching(AbstractLogger logger, String windowsKey) { if (windowsKey == null) { throw new IllegalArgumentException("windowsKey cannot be null"); } if (Arrays.binarySearch(WIN_KEYS, windowsKey) < 0) { throw new IllegalArgumentException(windowsKey + " is invalid"); } this.logger = logger; this.windowsKey = windowsKey; logger.info(windowsKey); }