KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > util > LaunchBrowser


1 /*
2  * FindBugs - Find Bugs in Java programs
3  * Copyright (C) 2006, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307, USA
18  */

19
20 package edu.umd.cs.findbugs.util;
21
22 import java.io.IOException JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.lang.reflect.*;
26
27 import edu.umd.cs.findbugs.SystemProperties;
28 import edu.umd.cs.findbugs.annotations.CheckForNull;
29 import edu.umd.cs.findbugs.annotations.Nullable;
30
31 /**
32  *
33  */

34 public class LaunchBrowser {
35
36     private static final @CheckForNull Method jnlpShowMethod;
37     private static final Object JavaDoc jnlpShowObject; // will not be null if jnlpShowMethod!=null
38
static {
39         // attempt to set the JNLP BasicService object and its showDocument(URL) method
40
Method showMethod = null;
41         Object JavaDoc showObject = null;
42         try {
43             Class JavaDoc serviceManagerClass = Class.forName("javax.jnlp.ServiceManager");
44             Method lookupMethod = serviceManagerClass.getMethod("lookup", new Class JavaDoc[] { String JavaDoc.class });
45             showObject = lookupMethod.invoke(null, new Object JavaDoc[] { "javax.jnlp.BasicService" });
46             showMethod = showObject.getClass().getMethod("showDocument", new Class JavaDoc [] { URL JavaDoc.class });
47         } catch (ClassNotFoundException JavaDoc e) {
48             assert true;
49         } catch (NoSuchMethodException JavaDoc e) {
50             assert true;
51         } catch (IllegalAccessException JavaDoc e) {
52             assert true;
53         } catch (InvocationTargetException e) {
54             assert true;
55         }
56         jnlpShowMethod = showMethod;
57         jnlpShowObject = showObject;
58     }
59
60
61
62     /**
63      * attempt to show the given URL.
64      * will first attempt via the JNLP api, then will try showViaExec().
65      * @param url the URL
66      * @return true on success
67      */

68     public static boolean showDocument(URL JavaDoc url) {
69
70         if (jnlpShowMethod != null) try {
71             Object JavaDoc result = jnlpShowMethod.invoke(jnlpShowObject, new Object JavaDoc [] { url });
72             return (Boolean.TRUE.equals(result));
73         } catch (InvocationTargetException ite) {
74             // do nothing
75
} catch (IllegalAccessException JavaDoc iae) {
76             // do nothing
77
}
78
79         // fallback to exec()
80
return showViaExec( url.toString() ); // or url.toExternalForm()
81
}
82
83
84     /** attempt to show the given URL string.
85      * will first attempt via the JNLP api, then will try showViaExec().
86      * @param url the url srtring
87      * @return true on success
88      */

89     public static boolean showDocument(String JavaDoc url) {
90         URL JavaDoc wrapper = null;
91         if (jnlpShowMethod != null) try {
92             wrapper = new URL JavaDoc(url);
93         } catch (MalformedURLException JavaDoc mue) {
94             wrapper = null;
95         }
96         
97         if (wrapper != null) try {
98             Object JavaDoc result = jnlpShowMethod.invoke(jnlpShowObject, new Object JavaDoc [] { url });
99             System.out.println("jnlp result is "+result);
100             return (Boolean.TRUE.equals(result));
101         } catch (InvocationTargetException ite) {
102             // do nothing
103
} catch (IllegalAccessException JavaDoc iae) {
104             // do nothing
105
}
106
107         // fallback to exec()
108
return showViaExec(url);
109     }
110
111
112     /**
113      * DISABLED -- simply returns false
114      *
115      * Attempts to show the given URL in the OS's web browser.
116      * @param url url to show
117      * @return true if the show operation was successful, false otherwise.
118      */

119     private static boolean showViaExec(String JavaDoc url){
120         return false;
121         /*
122         String os = SystemProperties.getProperty("os.name").toLowerCase();
123         Runtime rt = Runtime.getRuntime();
124         try{
125             if (os.indexOf( "win" ) >= 0) {
126                 // this doesn't support showing urls in the form of "page.html#nameLink"
127                 rt.exec( "rundll32 url.dll,FileProtocolHandler " + url);
128             } else if (os.indexOf( "mac" ) >= 0) {
129                 rt.exec( "open " + url);
130             } else if (os.indexOf( "nix") >=0 || os.indexOf( "nux") >=0) {
131                 // Do a best guess on unix until we get a platform independent way
132                 // Build a list of browsers to try, in this order.
133                 String[] browsers = {"epiphany", "firefox", "mozilla", "konqueror",
134                         "netscape","opera","links","lynx"};
135                 
136                 // Build a command string which looks like "browser1 "url" || browser2 "url" ||..."
137                 StringBuffer cmd = new StringBuffer();
138                 for (int i=0; i<browsers.length; i++)
139                     cmd.append( (i==0 ? "" : " || " ) + browsers[i] +" \"" + url + "\" ");
140                 
141                 rt.exec(new String[] { "sh", "-c", cmd.toString() });
142             } else {
143                 return false;
144             }
145         }catch (IOException e){
146             return false;
147         }
148         return true;
149         */

150     }
151
152 }
153
Popular Tags