1 19 20 package org.netbeans.modules.extbrowser; 21 22 import java.awt.*; 23 import java.awt.event.*; 24 import java.beans.*; 25 import java.net.*; 26 import javax.swing.*; 27 import java.util.Timer ; 28 import java.util.TimerTask ; 29 import java.util.Vector ; 30 31 import org.openide.DialogDisplayer; 32 import org.openide.NotifyDescriptor; 33 import org.openide.ErrorManager; 34 import org.openide.awt.StatusDisplayer; 35 import org.openide.execution.NbProcessDescriptor; 36 import org.openide.util.Utilities; 37 import org.openide.util.NbBundle; 38 39 import java.io.File ; 40 import java.io.FileWriter ; 41 import java.io.IOException ; 42 43 44 64 public class NbDdeBrowserImpl extends ExtBrowserImpl { 65 66 67 private static final String WWW_ACTIVATE = "WWW_Activate"; private static final String WWW_OPEN_URL = "WWW_OpenURL"; 70 private static final String EXTBROWSER_DLL = "extbrowser"; private static final String EXTBROWSER_DLL_64BIT = "extbrowser64"; 73 static { 74 if (ExtWebBrowser.getEM().isLoggable(ErrorManager.INFORMATIONAL)) { 75 ExtWebBrowser.getEM().log(ErrorManager.INFORMATIONAL, "" + System.currentTimeMillis() + "> NbDdeBrowser: static initializer: "); 76 } 77 try { 78 if (org.openide.util.Utilities.isWindows()) { 79 80 String sunDataModel = System.getProperty("sun.arch.data.model"); if (sunDataModel != null) { 83 if ("64".equals(sunDataModel)) { System.loadLibrary(EXTBROWSER_DLL_64BIT); 85 } else { 86 System.loadLibrary(EXTBROWSER_DLL); 87 } 88 } else { 89 String javaVMName = System.getProperty("java.vm.name"); if ((javaVMName != null) && (javaVMName.indexOf("64") > -1)) { System.loadLibrary(EXTBROWSER_DLL_64BIT); 92 } else { 93 System.loadLibrary(EXTBROWSER_DLL); 94 } 95 } 96 97 } 98 } catch (Exception e) { 99 DialogDisplayer.getDefault ().notify ( 100 new NotifyDescriptor.Message(NbBundle.getMessage(NbDdeBrowserImpl.class, "ERR_cant_locate_dll"), 101 NotifyDescriptor.INFORMATION_MESSAGE) 102 ); 103 } 104 } 105 106 107 private static Thread nativeThread = null; 108 109 110 private static NbDdeBrowserImpl.URLDisplayer nativeRunnable = null; 111 112 113 public NbDdeBrowserImpl (ExtWebBrowser extBrowserFactory) { 114 super (); 115 this.extBrowserFactory = extBrowserFactory; 116 if (ExtWebBrowser.getEM().isLoggable(ErrorManager.INFORMATIONAL)) { 117 ExtWebBrowser.getEM().log("" + System.currentTimeMillis() + "NbDdeBrowserImpl created with factory: " + extBrowserFactory); } 119 } 120 121 native private byte [] reqDdeMessage (String srv, String topic, String item, int timeout) throws NbBrowserException; 122 123 124 public native static String getBrowserPath (String browser) throws NbBrowserException; 125 126 129 public native static String getDefaultOpenCommand() throws NbBrowserException; 130 131 135 public synchronized void setURL(final URL url) { 136 if (ExtWebBrowser.getEM().isLoggable(ErrorManager.INFORMATIONAL)) { 137 ExtWebBrowser.getEM().log("" + System.currentTimeMillis() + "NbDdeBrowserImpl.setUrl: " + url); } 139 if (nativeThread == null) { 140 nativeRunnable = new NbDdeBrowserImpl.URLDisplayer (); 141 nativeThread = new Thread (nativeRunnable, "URLdisplayer"); nativeThread.start (); 143 } 144 nativeRunnable.postTask (new DisplayTask (url, this)); 145 } 146 147 151 private String realDDEServer () { 152 if (ExtWebBrowser.getEM().isLoggable(ErrorManager.INFORMATIONAL)) { 153 ExtWebBrowser.getEM().log("" + System.currentTimeMillis() + "NbDdeBrowserImpl.realDDEServer"); } 155 String srv = extBrowserFactory.getDDEServer (); 156 if (srv != null) { 157 return srv; 158 } 159 160 try { 161 String cmd = getDefaultOpenCommand (); 162 if (cmd != null) { 163 if (cmd.toUpperCase ().indexOf (ExtWebBrowser.IEXPLORE) >= 0) { 164 return ExtWebBrowser.IEXPLORE; 165 } 166 167 if (cmd.toUpperCase ().indexOf ("NETSCP") >= 0) { return ExtWebBrowser.NETSCAPE6; 169 } 170 171 if (cmd.toUpperCase ().indexOf (ExtWebBrowser.NETSCAPE) >= 0) { 172 return ExtWebBrowser.NETSCAPE; 173 } 174 175 if (cmd.toUpperCase ().indexOf (ExtWebBrowser.MOZILLA) >= 0) { 176 return ExtWebBrowser.MOZILLA; 177 } 178 179 if (cmd.toUpperCase ().indexOf (ExtWebBrowser.FIREFOX) >= 0) { 180 return ExtWebBrowser.FIREFOX; 181 } 182 } 183 } catch (Exception ex) { 184 ErrorManager.getDefault ().notify (ErrorManager.INFORMATIONAL, ex); 186 } 187 return ExtWebBrowser.IEXPLORE; 189 } 190 191 195 public int getActivateTimeout() { 196 return extBrowserFactory.getActivateTimeout(); 197 } 198 199 203 public int getOpenUrlTimeout() { 204 return extBrowserFactory.getOpenurlTimeout(); 205 } 206 207 210 static class URLDisplayer implements Runnable { 212 private static final int ADDITIONAL_WAIT_TIMEOUT = 6000; 213 214 215 Vector tasks; 216 217 218 boolean doProcessing = true; 219 220 223 boolean isDisplaying = false; 224 225 private URLDisplayer () { 226 tasks = new Vector (); 227 } 228 229 private void postTask (DisplayTask task) { 230 synchronized (this) { 231 boolean shouldNotify = tasks.isEmpty (); 232 tasks.add (task); 233 if (shouldNotify) { 234 notifyAll(); 235 } 236 } 237 } 238 239 243 private synchronized DisplayTask getNextTask() throws InterruptedException { 244 do { 245 246 if (!tasks.isEmpty ()) { 247 return (DisplayTask)tasks.remove(0); 248 } 249 wait(); 250 251 } while (true); 252 } 253 254 public void run() { 255 if (ExtWebBrowser.getEM().isLoggable(ErrorManager.INFORMATIONAL)) { 256 ExtWebBrowser.getEM().log("" + System.currentTimeMillis() + "NbDdeBrowserImpl.run"); } 258 while (doProcessing) { 259 try { 260 261 DisplayTask task = getNextTask (); 262 263 isDisplaying = true; 264 Timer timer = new Timer (); 265 timer.schedule (new TimerTask () { 266 public void run() { 267 if (isDisplaying) { 268 NbDdeBrowserImpl.nativeThread.interrupt(); 269 if (ExtWebBrowser.getEM().isLoggable(ErrorManager.INFORMATIONAL)) { 270 ExtWebBrowser.getEM().log("interrupted in URLDisplayer.run.TimerTask.run()"); } 272 DialogDisplayer.getDefault().notify( 273 new NotifyDescriptor.Message(NbBundle.getMessage(NbDdeBrowserImpl.class, "MSG_win_browser_invocation_failed"), 274 NotifyDescriptor.INFORMATION_MESSAGE) 275 ); 276 } 277 } 278 }, ADDITIONAL_WAIT_TIMEOUT); 279 dispatchURL (task); 280 timer.cancel(); 281 } catch (InterruptedException ex) { 282 ExtWebBrowser.getEM().log("interrupted in run(): " + ex); } finally { 285 isDisplaying = false; 286 } 287 } 288 } 289 290 public void dispatchURL (DisplayTask task) { 291 if (ExtWebBrowser.getEM().isLoggable(ErrorManager.INFORMATIONAL)) { 292 ExtWebBrowser.getEM().log("" + System.currentTimeMillis() + " NbDdeBrowserImpl.dispatchURL: " + task); } 294 try { 295 296 URL url = task.url; 297 if (ExtWebBrowser.getEM().isLoggable(ErrorManager.INFORMATIONAL)) { 298 ExtWebBrowser.getEM().log("" + System.currentTimeMillis() + " URLDispatcher.url: " + url); } 300 301 url = URLUtil.createExternalURL(url, URLUtil.browserHandlesJarURLs(task.browser.realDDEServer())); 304 if (ExtWebBrowser.getEM().isLoggable(ErrorManager.INFORMATIONAL)) { 305 ExtWebBrowser.getEM().log("" + System.currentTimeMillis() + " url: " + url); } 307 308 String urlStr = url.toString(); 309 310 boolean triedStart = false; 311 final int MAX_URL_LENGTH = 199; 312 313 if ((urlStr != null) && (urlStr.length() > MAX_URL_LENGTH)) { 314 urlStr = getFileUrl(urlStr); 315 } 316 317 if (ExtWebBrowser.getEM().isLoggable(ErrorManager.INFORMATIONAL)) { 318 ExtWebBrowser.getEM().log("" + System.currentTimeMillis() + " urlstr: " + urlStr); } 320 if (!win9xHack(task.browser.realDDEServer())) { 321 StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage (NbDdeBrowserImpl.class, "MSG_activatingBrowser")); 322 try { 323 task.browser.reqDdeMessage(task.browser.realDDEServer(),WWW_ACTIVATE,"-1,0x0",task.browser.getActivateTimeout()); 324 } catch (NbBrowserException ex) { 325 if (ExtWebBrowser.getEM().isLoggable(ErrorManager.INFORMATIONAL)) { 326 ExtWebBrowser.getEM().log("" + System.currentTimeMillis() + "Exception, gonna start browser: " + ex); } 328 triedStart = true; 329 startBrowser(task.browser.extBrowserFactory.getBrowserExecutable(), urlStr); 330 } 331 } 332 if (ExtWebBrowser.getEM().isLoggable(ErrorManager.INFORMATIONAL)) { 333 ExtWebBrowser.getEM().log("" + System.currentTimeMillis() + " firstpart"); } 335 336 if (!triedStart) { 337 StatusDisplayer.getDefault().setStatusText(NbBundle.getMessage(NbDdeBrowserImpl.class, "MSG_openingURLInBrowser", urlStr)); 338 String args1 = "\""+urlStr+"\",,-1,0x1,,,"; 340 try { 341 Thread.sleep(500); task.browser.reqDdeMessage(task.browser.realDDEServer(),WWW_OPEN_URL,args1,task.browser.getOpenUrlTimeout()); 343 } catch (NbBrowserException ex) { 344 if (ExtWebBrowser.getEM().isLoggable(ErrorManager.INFORMATIONAL)) { 345 ExtWebBrowser.getEM().log("Restarting browser."); } 347 startBrowser(task.browser.extBrowserFactory.getBrowserExecutable(), urlStr); 348 } catch (InterruptedException ex) { 349 } 351 } 352 if (ExtWebBrowser.getEM().isLoggable(ErrorManager.INFORMATIONAL)) { 353 ExtWebBrowser.getEM().log("" + System.currentTimeMillis() + " secondpart"); } 355 356 URL oldUrl = task.browser.url; 357 task.browser.url = url; 358 task.browser.pcs.firePropertyChange(PROP_URL, oldUrl, url); 359 360 } catch (Exception ex) { 361 final Exception ex1 = ex; 362 if (ExtWebBrowser.getEM().isLoggable(ErrorManager.INFORMATIONAL)) { 363 ExtWebBrowser.getEM().log("" + System.currentTimeMillis() + " Interrupted in URLDisplayer.dispatchURL.end"); } 365 ErrorManager.getDefault ().annotate(ex1, NbBundle.getMessage(NbDdeBrowserImpl.class, "MSG_win_browser_invocation_failed")); 366 SwingUtilities.invokeLater(new Runnable () { 367 public void run() { 368 ErrorManager.getDefault ().notify (ex1); 369 } 370 }); 371 } 372 } 373 374 375 378 private String getFileUrl(String url) { 379 if (ExtWebBrowser.getEM().isLoggable(ErrorManager.INFORMATIONAL)) { 380 ExtWebBrowser.getEM().log("" + System.currentTimeMillis() + "Gonna get redirect file for long url: " + url); 381 } 382 String newurl = null; 383 FileWriter fw = null; 384 File f = null; 385 386 int retries = 10; 387 388 while ((f == null) && (retries > 0)) { 389 retries--; 390 try { 391 f = File.createTempFile("extbrowser", ".html"); if (ExtWebBrowser.getEM().isLoggable(ErrorManager.INFORMATIONAL)) { 393 ExtWebBrowser.getEM().log("file: " + f); } 395 if (f != null) { 396 fw = new FileWriter (f); 397 if (f.canWrite()) { 398 String s1 = org.openide.util.NbBundle.getMessage(NbDdeBrowserImpl.class, "TXT_RedirectURL1"); String s2 = org.openide.util.NbBundle.getMessage(NbDdeBrowserImpl.class, "TXT_RedirectURL2"); String s = s1.concat(url).concat(s2); 401 fw.write(s); 402 fw.flush(); 403 } 404 newurl = "file:/" + f.getAbsolutePath(); } 406 } catch (IOException ioe) { 407 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "" + System.currentTimeMillis() + ioe.toString()); 408 } finally { 409 if (fw != null) { 410 try { 411 fw.close(); 412 } catch (IOException ioe) { 413 ErrorManager.getDefault().log(ErrorManager.INFORMATIONAL, "" + System.currentTimeMillis() + ioe.toString()); 414 } 415 } 416 } 417 } 418 if (newurl != null) { 419 if (ExtWebBrowser.getEM().isLoggable(ErrorManager.INFORMATIONAL)) { 420 ExtWebBrowser.getEM().log("" + System.currentTimeMillis() + "New URL: " + newurl); } 422 return newurl; 423 } 424 return url; 425 } 426 427 430 private boolean win9xHack (String browser) { 431 return browser.equals(ExtWebBrowser.IEXPLORE) 432 && (Utilities.getOperatingSystem() == Utilities.OS_WIN98 433 || Utilities.getOperatingSystem() == Utilities.OS_WIN95); 434 } 435 436 441 private void startBrowser(NbProcessDescriptor cmd, String url) throws java.io.IOException { 442 StatusDisplayer.getDefault ().setStatusText (NbBundle.getMessage(NbDdeBrowserImpl.class, "MSG_startingBrowser", url)); 443 cmd.exec(new ExtWebBrowser.UnixBrowserFormat(url)); 444 } 445 } 446 447 448 private static class DisplayTask { 449 URL url; 450 NbDdeBrowserImpl browser; 451 452 DisplayTask (URL url, NbDdeBrowserImpl browser) { 453 this.url = url; 454 this.browser = browser; 455 } 456 } 457 } 458 | Popular Tags |