1 19 20 package org.netbeans.modules.extbrowser; 21 22 import java.awt.*; 23 import java.beans.*; 24 import java.io.*; 25 import java.net.*; 26 import javax.swing.*; 27 28 import org.openide.*; 29 import org.openide.awt.StatusDisplayer; 30 import org.openide.execution.NbProcessDescriptor; 31 import org.openide.util.NbBundle; 32 33 import org.openide.util.RequestProcessor; 34 35 44 public class UnixBrowserImpl extends ExtBrowserImpl { 45 46 49 protected static final int CMD_TIMEOUT = 6; 50 51 57 protected static NbProcessDescriptor createPatchedExecutable (NbProcessDescriptor p) { 58 NbProcessDescriptor newP = null; 59 60 String [] args = org.openide.util.Utilities.parseParameters(p.getArguments()); 61 if (args.length > 1) { 62 if (ExtWebBrowser.getEM().isLoggable(ErrorManager.INFORMATIONAL)) { 63 ExtWebBrowser.getEM().log("Old arguments: " + p.getArguments()); } 65 StringBuffer newArgs = new StringBuffer (); 66 boolean found = false; 67 for (int i=0; i<args.length-1; i++) { 68 if (newArgs.length() > 0) { 69 newArgs.append(" "); } 71 if (args[i].indexOf("-remote") >= 0 && args[i+1].indexOf("openURL(") >=0) { found = true; 74 newArgs.append("\"{URL}\""); } 76 else { 77 newArgs.append("\""+args[i]+"\""); } 79 } 80 if (found) { 81 newP = new NbProcessDescriptor (p.getProcessName(), newArgs.toString(), p.getInfo()); 82 } 83 if (ExtWebBrowser.getEM().isLoggable(ErrorManager.INFORMATIONAL)) { 84 ExtWebBrowser.getEM().log("ProcessName: " + p.getProcessName()); ExtWebBrowser.getEM().log("New arguments: " + newArgs.toString()); } 87 } 88 return newP; 89 } 90 91 92 public UnixBrowserImpl () { 93 this (null); 94 } 95 96 99 public UnixBrowserImpl (ExtWebBrowser extBrowserFactory) { 100 super(); 101 this.extBrowserFactory = extBrowserFactory; 102 if (ExtWebBrowser.getEM().isLoggable(ErrorManager.INFORMATIONAL)) { 103 ExtWebBrowser.getEM().log("UnixBrowserImpl created from factory: " + extBrowserFactory); } 105 } 106 107 117 public void setURL(URL url) { 118 if (SwingUtilities.isEventDispatchThread ()) { 119 final URL newUrl = url; 120 RequestProcessor.getDefault ().post ( 121 new Runnable () { 122 public void run () { 123 UnixBrowserImpl.this.setURL (newUrl); 124 } 125 }); 126 return; 127 } 128 129 NbProcessDescriptor cmd = extBrowserFactory.getBrowserExecutable (); Process p; 131 StatusDisplayer sd = StatusDisplayer.getDefault (); 132 try { 133 url = URLUtil.createExternalURL(url, false); 135 if (ExtWebBrowser.getEM ().isLoggable (ErrorManager.INFORMATIONAL)) { 136 ExtWebBrowser.getEM ().log (ErrorManager.INFORMATIONAL, "External url: " + url); } 138 139 cmd = extBrowserFactory.getBrowserExecutable (); if (ExtWebBrowser.getEM ().isLoggable (ErrorManager.INFORMATIONAL)) { 141 ExtWebBrowser.getEM ().log (ErrorManager.INFORMATIONAL, "Executable: " + cmd); } 143 sd.setStatusText (NbBundle.getMessage (UnixBrowserImpl.class, "MSG_Running_command", cmd.getProcessName ())); 144 p = cmd.exec (new ExtWebBrowser.UnixBrowserFormat (url.toString ())); 145 146 RequestProcessor.getDefault ().post (new Status (cmd, p, url), 1000); 147 148 URL old = this.url; 149 this.url = url; 150 pcs.firePropertyChange (PROP_URL, old, url); 151 } 152 catch (java.io.IOException ex) { 153 DialogDisplayer.getDefault().notify( 155 new NotifyDescriptor.Message ( 156 NbBundle.getMessage (UnixBrowserImpl.class, "MSG_Cant_run_netscape", new Object [] { cmd.getProcessName () }), 157 NotifyDescriptor.Message.WARNING_MESSAGE) 158 ); 159 } 160 catch (NumberFormatException ex) { 161 ErrorManager.getDefault ().notify (ErrorManager.INFORMATIONAL, ex); 162 } 163 catch (java.lang.Exception ex) { 164 ErrorManager.getDefault ().notify (ex); 165 } 166 } 167 168 174 private class Status implements Runnable { 175 176 179 private static final String FAILURE_MSG_BADWINDOW = "BadWindow"; 181 182 private static final String FAILURE_MSG = "No running window found."; 184 185 private NbProcessDescriptor cmd; 186 187 188 private Process p; 189 190 191 private URL url; 192 193 194 private int retries = CMD_TIMEOUT; 195 196 203 public Status (NbProcessDescriptor cmd, Process p, URL url) { 204 this. cmd = cmd; 205 this.p = p; 206 this.url = url; 207 } 208 209 214 public void run () { 215 boolean retried = false; 216 if (ExtWebBrowser.getEM ().isLoggable (ErrorManager.INFORMATIONAL)) { 217 ExtWebBrowser.getEM ().log (ErrorManager.INFORMATIONAL, "Retried: " + retried); } 219 int exitStatus = 1; 220 Reader r = new InputStreamReader (p.getErrorStream ()); 221 try { 222 exitStatus = p.exitValue(); 223 if (ExtWebBrowser.getEM ().isLoggable (ErrorManager.INFORMATIONAL)) { 224 ExtWebBrowser.getEM ().log (ErrorManager.INFORMATIONAL, "Command executed. exitValue = " + exitStatus); } 226 } catch (IllegalThreadStateException ex) { 227 retries--; 228 if (ExtWebBrowser.getEM ().isLoggable (ErrorManager.INFORMATIONAL)) { 229 ExtWebBrowser.getEM ().log (ErrorManager.INFORMATIONAL, "Retries: " + retries); ExtWebBrowser.getEM ().log (ErrorManager.INFORMATIONAL, "Time: " + System.currentTimeMillis()); } 232 if (retries > 0) { 233 RequestProcessor.getDefault().post(this, 1000); 234 return; 235 } else { 236 if (ExtWebBrowser.getEM ().isLoggable (ErrorManager.INFORMATIONAL)) { 237 ExtWebBrowser.getEM ().log (ErrorManager.INFORMATIONAL, "Command not finished yet"); } 239 } 240 } 241 242 if (exitStatus == 0 && org.openide.util.Utilities.getOperatingSystem() == org.openide.util.Utilities.OS_LINUX) { 244 final int LEN = 2048; 245 char [] buff = new char [LEN]; 246 int l; 247 StringBuffer sb = new StringBuffer (); 248 try { 249 while ((l = r.read (buff, 0, LEN)) != -1) { 250 sb.append (buff, 0, l); 251 } 252 if (sb.toString ().indexOf (FAILURE_MSG) >= 0) { 253 if (ExtWebBrowser.getEM ().isLoggable (ErrorManager.INFORMATIONAL)) { 254 ExtWebBrowser.getEM ().log (ErrorManager.INFORMATIONAL, "Browser output: \""+FAILURE_MSG+"\""); } 256 exitStatus = 2; 257 } 258 } catch (java.io.IOException ioe) { 259 ExtWebBrowser.getEM ().notify(ErrorManager.WARNING, ioe); 261 } 262 } 263 264 if (exitStatus == 1 && org.openide.util.Utilities.getOperatingSystem() == org.openide.util.Utilities.OS_LINUX) { 267 final int LEN = 2048; 268 char [] buff = new char [LEN]; 269 int l; 270 StringBuffer sb = new StringBuffer (); 271 try { 272 while ((l = r.read (buff, 0, LEN)) != -1) { 273 sb.append (buff, 0, l); 274 } 275 if (sb.toString ().indexOf (FAILURE_MSG_BADWINDOW) >= 0) { 276 if (ExtWebBrowser.getEM().isLoggable (ErrorManager.INFORMATIONAL)) { 277 ExtWebBrowser.getEM().log (ErrorManager.INFORMATIONAL, "Browser output: \""+FAILURE_MSG_BADWINDOW+"\""); } 279 exitStatus = 0; 280 } 281 } catch (java.io.IOException ioe) { 282 ExtWebBrowser.getEM ().notify(ErrorManager.WARNING, ioe); 284 } 285 } 286 287 if (exitStatus == 2) { 288 try { 289 NbProcessDescriptor startCmd = UnixBrowserImpl.createPatchedExecutable(cmd); 290 if (startCmd != null) { 291 retried = true; 292 StatusDisplayer.getDefault(). 293 setStatusText (NbBundle.getMessage (UnixBrowserImpl.class, "MSG_Running_command", startCmd.getProcessName ())); 294 Process pr = startCmd.exec (new ExtWebBrowser.UnixBrowserFormat (url.toString ())); 295 296 } 299 } 300 catch (java.io.IOException ioe) { 301 ExtWebBrowser.getEM ().notify(ErrorManager.WARNING, ioe); 303 } 304 } 305 306 if (exitStatus != 0 && !retried) { 307 DialogDisplayer.getDefault().notify( 308 new NotifyDescriptor.Message ( 309 NbBundle.getMessage (UnixBrowserImpl.class, "MSG_Cant_run_netscape", new Object [] { cmd.getProcessName () }), 310 NotifyDescriptor.Message.WARNING_MESSAGE) 311 ); 312 return; 313 } 314 315 } 316 } 317 } 318 | Popular Tags |