1 11 package org.eclipse.ui.internal.browser.browsers; 12 13 import java.io.*; 14 import java.net.URL ; 15 16 import org.eclipse.core.runtime.Platform; 17 import org.eclipse.ui.browser.AbstractWebBrowser; 18 import org.eclipse.ui.internal.browser.WebBrowserUIPlugin; 19 23 public class MozillaBrowser extends AbstractWebBrowser { 24 protected static final int DELAY = 5000; 27 28 protected long browserFullyOpenedAt = 0; 29 30 private BrowserThread lastBrowserThread = null; 31 32 protected String executable; 33 34 protected boolean firstLaunch = true; 35 36 42 public MozillaBrowser(String id, String executable) { 43 super(id); 44 this.executable = executable; 45 } 46 47 50 public void openURL(URL url2) { 51 String url = url2.toExternalForm(); 52 if (lastBrowserThread != null) 53 lastBrowserThread.exitRequested = true; 54 55 lastBrowserThread = new BrowserThread(url); 56 lastBrowserThread.setDaemon(true); 57 lastBrowserThread.start(); 58 } 59 60 private class BrowserThread extends Thread { 61 public boolean exitRequested = false; 62 63 private String url; 64 65 public BrowserThread(String urlName) { 66 this.url = urlName; 67 } 68 69 73 private int openBrowser(String browserCmd) { 74 try { 75 Process pr = Runtime.getRuntime().exec(browserCmd); 76 StreamConsumer outputs = new StreamConsumer(pr.getInputStream()); 77 (outputs).start(); 78 StreamConsumer errors = new StreamConsumer(pr.getErrorStream()); 79 (errors).start(); 80 pr.waitFor(); 81 int ret = pr.exitValue(); 82 83 if (ret == 0 && errorsInOutput(outputs, errors)) { 84 return -1; 85 } 86 return ret; 87 } catch (InterruptedException e) { 88 } catch (IOException e) { 90 WebBrowserUIPlugin.logError("Launching " + executable + " has failed.", e); 98 return 0; 100 } 101 return -1; 102 } 103 104 113 private boolean errorsInOutput(StreamConsumer outputs, 114 StreamConsumer errors) { 115 try { 116 outputs.join(1000); 117 if (outputs.getLastLine() != null 118 && (outputs.getLastLine().indexOf( 119 "No running window found") >= 0 || outputs.getLastLine().indexOf( 121 "not running on display") >= 0)) { 123 return true; 124 } 125 errors.join(1000); 126 if (errors.getLastLine() != null 127 && (errors.getLastLine().indexOf( 128 "No running window found") >= 0 || errors.getLastLine().indexOf( 130 "not running on display") >= 0)) { 132 return true; 133 } 134 } catch (InterruptedException ie) { 135 } 137 return false; 138 } 139 140 public void run() { 141 waitForBrowser(); 143 if (exitRequested) 144 return; 145 if (firstLaunch && Platform.OS_WIN32.equals(Platform.getOS())) { 146 if (openBrowser(executable + " " + url) == 0) return; 148 browserFullyOpenedAt = System.currentTimeMillis() + DELAY; 149 return; 150 } 151 if (openBrowser(executable + " -remote openURL(" + url + ")") == 0) 153 return; 154 155 if (exitRequested) 156 return; 157 browserFullyOpenedAt = System.currentTimeMillis() + DELAY; 158 openBrowser(executable + " " + url); } 160 161 private void waitForBrowser() { 162 while (System.currentTimeMillis() < browserFullyOpenedAt) 163 try { 164 if (exitRequested) 165 return; 166 Thread.sleep(100); 167 } catch (InterruptedException ie) { 168 } 170 } 171 } 172 } | Popular Tags |