KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > apollo > dev > browser > spencer > BrowserLauncher


1 /*
2 ** Apollo - Test Skeleton Toolkit for Web Start/JNLP
3 ** Copyright (c) 2001, 2002, 2003 by Gerald Bauer
4 **
5 ** This program is free software.
6 **
7 ** You may redistribute it and/or modify it under the terms of the GNU
8 ** General Public License as published by the Free Software Foundation.
9 ** Version 2 of the license should be included with this distribution in
10 ** the file LICENSE, as well as License.html. If the license is not
11 ** included with this distribution, you may find a copy at the FSF web
12 ** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
13 ** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
14 **
15 ** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,
16 ** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR
17 ** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY
18 ** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR
19 ** REDISTRIBUTION OF THIS SOFTWARE.
20 **
21 */

22
23 package apollo.dev.browser.spencer;
24
25 import java.io.*;
26 import java.net.*;
27 import houston.*;
28
29 public class BrowserLauncher
30 {
31
32    /**
33     * The flag to display a url.
34     */

35    private final static String JavaDoc UNIX_FLAG = "-remote openURL";
36
37    /**
38     * The default browser under unix.
39     */

40    private final static String JavaDoc UNIX_PATH = "netscape";
41
42    /**
43     * The flag to display a url.
44     */

45    private final static String JavaDoc WIN_FLAG = "url.dll,FileProtocolHandler";
46
47    /**
48     * Used to identify the windows platform.
49     */

50    private final static String JavaDoc WIN_ID = "Windows";
51    /**
52     * The default system browser under windows.
53     */

54    private final static String JavaDoc WIN_PATH = "rundll32";
55
56    public static boolean isWindowsPlatform()
57    {
58       String JavaDoc os = System.getProperty( "os.name" );
59
60       if( os != null && os.startsWith( WIN_ID ) )
61          return true;
62       else
63          return false;
64    }
65
66    public static void showDocument( String JavaDoc url )
67    {
68       if( url == null )
69          return;
70
71       boolean windows = isWindowsPlatform();
72       String JavaDoc cmd = null;
73
74       try
75       {
76          if( windows )
77          {
78             // cmd = 'rundll32 url.dll,FileProtocolHandler http://...'
79
cmd = WIN_PATH + " " + WIN_FLAG + " " + url;
80             Process JavaDoc p = Runtime.getRuntime().exec( cmd );
81          }
82          else
83          {
84             // Under Unix, Netscape has to be running for the "-remote"
85
// command to work. So, we try sending the command and
86
// check for an exit value. If the exit command is 0,
87
// it worked, otherwise we need to start the browser.
88

89             // cmd = 'netscape -remote openURL(http://www.javaworld.com)'
90
cmd = UNIX_PATH + " " + UNIX_FLAG + "(" + url + ")";
91             Process JavaDoc p = Runtime.getRuntime().exec( cmd );
92
93             try
94             {
95                // wait for exit code -- if it's 0, command worked,
96
// otherwise we need to start the browser up.
97
int exitCode = p.waitFor();
98
99                if( exitCode != 0 )
100                {
101                   // Command failed, start up the browser
102
// cmd = 'netscape http://www.javaworld.com'
103
cmd = UNIX_PATH + " " + url;
104                   p = Runtime.getRuntime().exec( cmd );
105                }
106             }
107             catch( InterruptedException JavaDoc x )
108             {
109                Status.error( "*** failed to bring up browser using command '" + cmd + "': " + x.toString() );
110             }
111          }
112       }
113       catch( IOException x )
114       {
115          // couldn't exec browser
116
Status.error( "*** failed to bring up browser using command '" + cmd + "': " + x.toString() );
117       }
118    }
119 }
120
Popular Tags