/** * Returns true if the browser is available, ie mdfind command finds it. * * @param logger AbstractLogger * @return boolean */ public boolean isBrowserAvailable(StandardUnixBrowser browser, AbstractLogger logger) { boolean isAvailable = false; try { Process process = Runtime.getRuntime().exec(new String[] {"mdfind", browser.getBrowserApplicationName()}); InputStream errStream = process.getErrorStream(); InputStream inStream = process.getInputStream(); BufferedReader errIn = new BufferedReader(new InputStreamReader(errStream)); BufferedReader in = new BufferedReader(new InputStreamReader(inStream)); String temp; StringBuffer whichOutput = new StringBuffer(); while ((temp = in.readLine()) != null) { // Searching with spotlight (mdfind) will find more than just the app sometimes if (temp.indexOf(browser.getBrowserApplicationName()) > -1) { whichOutput.append(temp); } } StringBuffer whichErrOutput = new StringBuffer(); while ((temp = errIn.readLine()) != null) { whichErrOutput.append(temp); } in.close(); errIn.close(); if (whichOutput.length() > 0) { logger.debug(whichOutput.toString()); // very weak test but should be enough since the Open -a applicationname works very well isAvailable = true; } if (whichErrOutput.length() > 0) { logger.debug(whichErrOutput.toString()); } } catch (IOException ex) { logger.error("io error executing which command", ex); } return isAvailable; }
/** * Uses the which command to find out which browsers are available. The available browsers are put * into the unixBrowsers map using displayName => StandardUnixBrowser mappings. * * @todo what do we do if there are no browsers available? * @throws BrowserLaunchingInitializingException */ public void initialize() throws BrowserLaunchingInitializingException { try { URL configUrl = getClass().getResource(configFileName); if (configUrl == null) { throw new BrowserLaunchingInitializingException( "unable to find config file: " + configFileName); } StringBuffer potentialBrowserNames = new StringBuffer(); Properties configProps = new Properties(); configProps.load(configUrl.openStream()); String sepChar = configProps.getProperty(PROP_KEY_DELIMITER); Iterator keysIter = configProps.keySet().iterator(); while (keysIter.hasNext()) { String key = (String) keysIter.next(); if (key.startsWith(PROP_KEY_BROWSER_PREFIX)) { StandardUnixBrowser browser = new StandardUnixBrowser(sepChar, configProps.getProperty(key)); if (this.isBrowserAvailable(browser, logger)) { unixBrowsers.put(browser.getBrowserDisplayName(), browser); } else { if (potentialBrowserNames.length() > 0) { potentialBrowserNames.append("; "); } potentialBrowserNames.append(browser.getBrowserDisplayName()); } } } if (unixBrowsers.size() == 0) { // no browser installed throw new BrowserLaunchingInitializingException( "one of the supported browsers must be installed: " + potentialBrowserNames); } logger.info(unixBrowsers.keySet().toString()); unixBrowsers = Collections.unmodifiableMap(unixBrowsers); } catch (IOException ioex) { throw new BrowserLaunchingInitializingException(ioex); } }