KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > extbrowser > SystemDefaultBrowser


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.extbrowser;
21
22 import java.io.IOException JavaDoc;
23 import java.lang.reflect.InvocationTargetException JavaDoc;
24 import java.lang.reflect.Method JavaDoc;
25 import java.net.URI JavaDoc;
26 import java.net.URISyntaxException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.net.URLEncoder JavaDoc;
29 import java.util.logging.Level JavaDoc;
30 import java.util.logging.Logger JavaDoc;
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 /**
37  * Browser factory for System default Win browser.
38  *
39  * @author Martin Grebac
40  */

41 public class SystemDefaultBrowser extends ExtWebBrowser {
42
43     private static final long serialVersionUID = -7317179197254112564L;
44     private static final Logger JavaDoc logger = Logger.getLogger(SystemDefaultBrowser.class.getName());
45     
46     private interface BrowseInvoker {
47         void browse(URI JavaDoc uri) throws IOException JavaDoc;
48     }
49     private static BrowseInvoker JDK_6_DESKTOP_BROWSE;
50     static {
51         try {
52           if (Boolean.getBoolean("java.net.useSystemProxies") && Utilities.isUnix()) {
53               // remove this check if JDK's bug 6496491 is fixed or if we can assume ORBit >= 2.14.2 and gnome-vfs >= 2.16.1
54
logger.log(Level.FINE, "Ignoring java.awt.Desktop.browse support to avoid hang from #89540");
55           }
56           else {
57             Class JavaDoc desktop = Class.forName("java.awt.Desktop"); // NOI18N
58
Method JavaDoc isDesktopSupported = desktop.getMethod("isDesktopSupported", null); // NOI18N
59
Boolean JavaDoc b = (Boolean JavaDoc) isDesktopSupported.invoke(null, null);
60             logger.log(Level.FINE, "java.awt.Desktop found, isDesktopSupported returned "+b);
61             if (b.booleanValue()) {
62                 final Object JavaDoc desktopInstance = desktop.getMethod("getDesktop", null).invoke(null, null); // NOI18N
63
Class JavaDoc desktopAction = Class.forName("java.awt.Desktop$Action"); // NOI18N
64
Method JavaDoc isSupported = desktop.getMethod("isSupported", new Class JavaDoc[] {desktopAction}); // NOI18N
65
Object JavaDoc browseConst = desktopAction.getField("BROWSE").get(null); // NOI18N
66
b = (Boolean JavaDoc) isSupported.invoke(desktopInstance, new Object JavaDoc[] {browseConst});
67                 logger.log(Level.FINE, "java.awt.Desktop found, isSupported(Action.BROWSE) returned "+b);
68                 if (b.booleanValue()) {
69                     final Method JavaDoc browse = desktop.getMethod("browse", new Class JavaDoc[] {URI JavaDoc.class}); // NOI18N
70
JDK_6_DESKTOP_BROWSE = new BrowseInvoker() {
71                         public void browse(URI JavaDoc uri) throws IOException JavaDoc {
72                             try {
73                                 browse.invoke(desktopInstance, new Object JavaDoc[] {uri});
74                             } catch (InvocationTargetException JavaDoc e) {
75                                 throw (IOException JavaDoc) e.getTargetException();
76                             } catch (Exception JavaDoc 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 JavaDoc e) {
86             // JDK 5, ignore
87
logger.log(Level.FINE, "java.awt.Desktop class not found, disabling JDK 6 browse functionality");
88         } catch (Exception JavaDoc e) {
89             logger.log(Level.WARNING, null, e);
90         }
91     }
92     
93     /** Determines whether the browser should be visible or not
94      * @return true when OS is Windows.
95      * false in all other cases.
96      */

97     public static Boolean JavaDoc isHidden () {
98         return Boolean.valueOf(!Utilities.isWindows() && JDK_6_DESKTOP_BROWSE == null);
99     }
100     
101     /** Creates new ExtWebBrowser */
102     public SystemDefaultBrowser() {
103     }
104
105     /**
106      * Returns a new instance of BrowserImpl implementation.
107      * @throws UnsupportedOperationException when method is called and OS is not Windows.
108      * @return browserImpl implementation of browser.
109      */

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 JavaDoc (NbBundle.getMessage (SystemDefaultBrowser.class, "MSG_CannotUseBrowser"));
117         }
118     }
119     
120     /** Getter for browser name
121      * @return name of browser
122      */

123     public String JavaDoc getName () {
124         if (name == null) {
125             this.name = NbBundle.getMessage(SystemDefaultBrowser.class, "CTL_SystemDefaultBrowserName");
126         }
127         return name;
128     }
129     
130     /** Setter for browser name
131      */

132     public void setName(String JavaDoc name) {
133         // system default browser name shouldn't be changed
134
}
135
136     /** Default command for browser execution.
137      * Can be overriden to return browser that suits to platform and settings.
138      *
139      * @return process descriptor that allows to start browser.
140      */

141     protected NbProcessDescriptor defaultBrowserExecutable () {
142         if (!Utilities.isWindows() || JDK_6_DESKTOP_BROWSE != null) {
143             return new NbProcessDescriptor("", ""); // NOI18N
144
}
145         
146         String JavaDoc b;
147         String JavaDoc params = ""; // NOI18N
148
try {
149             // finds HKEY_CLASSES_ROOT\\".html" and respective HKEY_CLASSES_ROOT\\<value>\\shell\\open\\command
150
// we will ignore all params here
151
b = NbDdeBrowserImpl.getDefaultOpenCommand ();
152             String JavaDoc [] 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 = ""; // NOI18N
161
} catch (UnsatisfiedLinkError JavaDoc e) {
162             // someone is customizing this on non-Win platform
163
b = "iexplore"; // NOI18N
164
}
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 JavaDoc url) {
176             URL JavaDoc extURL = URLUtil.createExternalURL(url, /* to be safe */false);
177             try {
178                 extURL = getFullyRFC2396CompliantURL(extURL);
179         URI JavaDoc uri = extURL.toURI();
180                 logger.fine("Calling java.awt.Desktop.browse("+uri+")");
181                 JDK_6_DESKTOP_BROWSE.browse(uri);
182             } catch (URISyntaxException JavaDoc e) {
183                 logger.severe("The URL:\n" + extURL //NOI18N
184
+ "\nis not fully RFC 2396 compliant and cannot " //NOI18N
185
+ "be used with Desktop.browse()."); //NOI18N
186
} catch (IOException JavaDoc e) {
187                 // Report in GUI?
188
logger.log(Level.WARNING, null, e);
189             }
190         }
191         
192         /**
193          * We used to allow URLs that do not fully conform the spec
194          * (specifically those containing reserved characters),
195          * the JVM 6's Desktop.browse() method wouldn't work with them.
196          * This method addresses numerous compatibility issues by
197          * attemting to convert those to "kosher" URLs
198          */

199         private URL JavaDoc getFullyRFC2396CompliantURL(URL JavaDoc url){
200             String JavaDoc urlStr = url.toString();
201             int ind = urlStr.indexOf('#');
202             
203             if (ind > -1){
204                 String JavaDoc urlWithoutRef = urlStr.substring(0, ind);
205                 String JavaDoc anchorOrg = url.getRef();
206                 try {
207                     String JavaDoc anchorEscaped = URLEncoder.encode(anchorOrg, "UTF8"); //NOI18N
208

209                     // browsers seems to like %20 more...
210
anchorEscaped = anchorEscaped.replaceAll("\\+", "%20"); // NOI18N
211

212                     if (!anchorOrg.equals(anchorEscaped)){
213                         URL JavaDoc escapedURL = new URL JavaDoc(urlWithoutRef + '#' + anchorEscaped);
214                         
215                         logger.warning("The URL:\n" + urlStr //NOI18N
216
+ "\nis not fully RFC 2396 compliant and cannot " //NOI18N
217
+ "be used with Desktop.browse(). Instead using URL:" //NOI18N
218
+ escapedURL);
219                         
220                         return escapedURL;
221                     }
222                 } catch (IOException JavaDoc e){
223                     logger.log(Level.SEVERE, e.getMessage(), e);
224                 }
225             }
226             
227             return url;
228         }
229         
230     }
231
232 }
233
Popular Tags