1 19 20 package org.netbeans.modules.extbrowser; 21 22 import java.io.IOException ; 23 import java.lang.reflect.InvocationTargetException ; 24 import java.lang.reflect.Method ; 25 import java.net.URI ; 26 import java.net.URISyntaxException ; 27 import java.net.URL ; 28 import java.net.URLEncoder ; 29 import java.util.logging.Level ; 30 import java.util.logging.Logger ; 31 import org.openide.awt.HtmlBrowser; 32 import org.openide.execution.NbProcessDescriptor; 33 import org.openide.util.NbBundle; 34 import org.openide.util.Utilities; 35 36 41 public class SystemDefaultBrowser extends ExtWebBrowser { 42 43 private static final long serialVersionUID = -7317179197254112564L; 44 private static final Logger logger = Logger.getLogger(SystemDefaultBrowser.class.getName()); 45 46 private interface BrowseInvoker { 47 void browse(URI uri) throws IOException ; 48 } 49 private static BrowseInvoker JDK_6_DESKTOP_BROWSE; 50 static { 51 try { 52 if (Boolean.getBoolean("java.net.useSystemProxies") && Utilities.isUnix()) { 53 logger.log(Level.FINE, "Ignoring java.awt.Desktop.browse support to avoid hang from #89540"); 55 } 56 else { 57 Class desktop = Class.forName("java.awt.Desktop"); Method isDesktopSupported = desktop.getMethod("isDesktopSupported", null); Boolean b = (Boolean ) isDesktopSupported.invoke(null, null); 60 logger.log(Level.FINE, "java.awt.Desktop found, isDesktopSupported returned "+b); 61 if (b.booleanValue()) { 62 final Object desktopInstance = desktop.getMethod("getDesktop", null).invoke(null, null); Class desktopAction = Class.forName("java.awt.Desktop$Action"); Method isSupported = desktop.getMethod("isSupported", new Class [] {desktopAction}); Object browseConst = desktopAction.getField("BROWSE").get(null); b = (Boolean ) isSupported.invoke(desktopInstance, new Object [] {browseConst}); 67 logger.log(Level.FINE, "java.awt.Desktop found, isSupported(Action.BROWSE) returned "+b); 68 if (b.booleanValue()) { 69 final Method browse = desktop.getMethod("browse", new Class [] {URI .class}); JDK_6_DESKTOP_BROWSE = new BrowseInvoker() { 71 public void browse(URI uri) throws IOException { 72 try { 73 browse.invoke(desktopInstance, new Object [] {uri}); 74 } catch (InvocationTargetException e) { 75 throw (IOException ) e.getTargetException(); 76 } catch (Exception e) { 77 Logger.getLogger(SystemDefaultBrowser.class.getName()).log(Level.WARNING, null, e); 78 } 79 } 80 }; 81 logger.log(Level.FINE, "java.awt.Desktop.browse support"); 82 } 83 } 84 } 85 } catch (ClassNotFoundException e) { 86 logger.log(Level.FINE, "java.awt.Desktop class not found, disabling JDK 6 browse functionality"); 88 } catch (Exception e) { 89 logger.log(Level.WARNING, null, e); 90 } 91 } 92 93 97 public static Boolean isHidden () { 98 return Boolean.valueOf(!Utilities.isWindows() && JDK_6_DESKTOP_BROWSE == null); 99 } 100 101 102 public SystemDefaultBrowser() { 103 } 104 105 110 public HtmlBrowser.Impl createHtmlBrowserImpl() { 111 if (JDK_6_DESKTOP_BROWSE != null) { 112 return new Jdk6BrowserImpl(); 113 } else if (Utilities.isWindows ()) { 114 return new NbDdeBrowserImpl(this); 115 } else { 116 throw new UnsupportedOperationException (NbBundle.getMessage (SystemDefaultBrowser.class, "MSG_CannotUseBrowser")); 117 } 118 } 119 120 123 public String getName () { 124 if (name == null) { 125 this.name = NbBundle.getMessage(SystemDefaultBrowser.class, "CTL_SystemDefaultBrowserName"); 126 } 127 return name; 128 } 129 130 132 public void setName(String name) { 133 } 135 136 141 protected NbProcessDescriptor defaultBrowserExecutable () { 142 if (!Utilities.isWindows() || JDK_6_DESKTOP_BROWSE != null) { 143 return new NbProcessDescriptor("", ""); } 145 146 String b; 147 String params = ""; try { 149 b = NbDdeBrowserImpl.getDefaultOpenCommand (); 152 String [] args = Utilities.parseParameters (b); 153 154 if (args == null || args.length == 0) { 155 throw new NbBrowserException (); 156 } 157 b = args[0]; 158 params += " {" + ExtWebBrowser.UnixBrowserFormat.TAG_URL + "}"; 159 } catch (NbBrowserException e) { 160 b = ""; } catch (UnsatisfiedLinkError e) { 162 b = "iexplore"; } 165 166 NbProcessDescriptor p = new NbProcessDescriptor (b, params); 167 return p; 168 } 169 170 private static final class Jdk6BrowserImpl extends ExtBrowserImpl { 171 172 public Jdk6BrowserImpl() { 173 assert JDK_6_DESKTOP_BROWSE != null; 174 } 175 public void setURL(URL url) { 176 URL extURL = URLUtil.createExternalURL(url, false); 177 try { 178 extURL = getFullyRFC2396CompliantURL(extURL); 179 URI uri = extURL.toURI(); 180 logger.fine("Calling java.awt.Desktop.browse("+uri+")"); 181 JDK_6_DESKTOP_BROWSE.browse(uri); 182 } catch (URISyntaxException e) { 183 logger.severe("The URL:\n" + extURL + "\nis not fully RFC 2396 compliant and cannot " + "be used with Desktop.browse()."); } catch (IOException e) { 187 logger.log(Level.WARNING, null, e); 189 } 190 } 191 192 199 private URL getFullyRFC2396CompliantURL(URL url){ 200 String urlStr = url.toString(); 201 int ind = urlStr.indexOf('#'); 202 203 if (ind > -1){ 204 String urlWithoutRef = urlStr.substring(0, ind); 205 String anchorOrg = url.getRef(); 206 try { 207 String anchorEscaped = URLEncoder.encode(anchorOrg, "UTF8"); 209 anchorEscaped = anchorEscaped.replaceAll("\\+", "%20"); 212 if (!anchorOrg.equals(anchorEscaped)){ 213 URL escapedURL = new URL (urlWithoutRef + '#' + anchorEscaped); 214 215 logger.warning("The URL:\n" + urlStr + "\nis not fully RFC 2396 compliant and cannot " + "be used with Desktop.browse(). Instead using URL:" + escapedURL); 219 220 return escapedURL; 221 } 222 } catch (IOException e){ 223 logger.log(Level.SEVERE, e.getMessage(), e); 224 } 225 } 226 227 return url; 228 } 229 230 } 231 232 } 233 | Popular Tags |