View Javadoc

1   /*
2    * The contents of this file are subject to the Mozilla Public License
3    * Version 1.1 (the "License"); you may not use this file except in
4    * compliance with the License. You may obtain a copy of the License at
5    * http://www.mozilla.org/MPL/
6    *
7    * Software distributed under the License is distributed on an "AS IS"
8    * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
9    * the License for the specific language governing rights and limitations
10   * under the License.
11   *
12   * The Original Code is the Kowari Metadata Store.
13   *
14   * The Initial Developer of the Original Code is Plugged In Software Pty
15   * Ltd (http://www.pisoftware.com, mailto:info@pisoftware.com). Portions
16   * created by Plugged In Software Pty Ltd are Copyright (C) 2001,2002
17   * Plugged In Software Pty Ltd. All Rights Reserved.
18   *
19   * Contributor(s): N/A.
20   *
21   * [NOTE: The text of this Exhibit A may differ slightly from the text
22   * of the notices in the Source Code files of the Original Code. You
23   * should use the text of this Exhibit A rather than the text found in the
24   * Original Code Source Code for Your Modifications.]
25   *
26   */
27  package net.sf.magicproject.tools;
28  
29  /***
30   * Class used to launch a WebBrowser.
31   * <p>
32   * Extracted from method: EmbeddedKowariServer.LaunchBrowser()
33   * </p>
34   * 
35   * @since 2004-07-23
36   * @author <a href="mailto:robert.turner@tucanatech.com">Robert Turner</a>
37   */
38  public final class WebBrowser {
39  
40  	// Windows config
41  	private static final String ID_WIN = "Windows";
42  
43  	private static final String PATH_WIN = "rundll32";
44  
45  	private static final String FLAG_WIN = "url.dll,FileProtocolHandler";
46  
47  	// MAC config
48  	private static final String ID_MACOSX = "Mac OS X";
49  
50  	// UNIX Config
51  	private static final String FLAG_UNIX = "-remote openURL";
52  
53  	private static final String[] PATHES_UNIX = { "firefox", "opera",
54  			"konqueror", "epiphany", "mozilla", "netscape" };
55  
56  	/***
57  	 * Constructor. This class uses static methods.
58  	 */
59  	private WebBrowser() {
60  		super();
61  	}
62  
63  	/***
64  	 * Launch a browser to display the specified URL.
65  	 * 
66  	 * @param url
67  	 *          url to open.
68  	 * @throws Exception
69  	 */
70  	public static void launchBrowser(String url) throws Exception {
71  
72  		// validate URL
73  		if (url != null && !"".equals(url)) {
74  
75  			// determine OS
76  			String os = System.getProperty("os.name");
77  			boolean windows = os != null && os.startsWith(ID_WIN);
78  			boolean macosx = os != null && os.startsWith(ID_MACOSX);
79  
80  			// execute launch command depending on the OS
81  			if (windows) {
82  
83  				WebBrowser.launchBrowserWindows(url);
84  			} else if (macosx) {
85  
86  				WebBrowser.launchBrowserMac(url);
87  			} else {
88  
89  				// default OS is Unix (eg. Linux, BSD, Solaris)
90  				WebBrowser.launchBrowserUnix(url);
91  			}
92  		}
93  	}
94  
95  	/***
96  	 * Executes a Windows command to launch the Browser.
97  	 * 
98  	 * @param url
99  	 *          String
100 	 * @throws Exception
101 	 */
102 	private static void launchBrowserWindows(String url) throws Exception {
103 
104 		// command = 'rundll32 url.dll,FileProtocolHandler http://...'
105 		Runtime.getRuntime().exec(PATH_WIN + " " + FLAG_WIN + " " + url);
106 	}
107 
108 	/***
109 	 * Executes a Mac command to launch the Browser.
110 	 * 
111 	 * @param url
112 	 *          String
113 	 * @throws Exception
114 	 */
115 	private static void launchBrowserMac(String url) throws Exception {
116 
117 		// command = 'open http://...'
118 		Runtime.getRuntime().exec("open " + url);
119 	}
120 
121 	/***
122 	 * Executes an Unix command to launch the Browser.
123 	 * 
124 	 * @param url
125 	 *          String
126 	 * @throws Exception
127 	 */
128 	private static void launchBrowserUnix(String url) throws Exception {
129 		for (String path : PATHES_UNIX) {
130 			try {
131 				WebBrowser.launchGeckoBrowserUnix(path, url);
132 				return;
133 			} catch (Exception ex) {
134 				// Ignore this error and continue
135 			}
136 		}
137 	}
138 
139 	/***
140 	 * Executes an Unix command to launch the specified Gecko-based Browser.
141 	 * 
142 	 * @param url
143 	 *          String
144 	 * @throws Exception
145 	 */
146 	private static void launchGeckoBrowserUnix(String browser, String url)
147 			throws Exception {
148 
149 		// command = '*browser* -remote openURL(http://www.javaworld.com)'
150 		String command = browser + " " + FLAG_UNIX + "(" + url + ")";
151 
152 		// Under Unix, Browser has to be running for the "-remote" flag to work.
153 		Process process = Runtime.getRuntime().exec(command);
154 
155 		// exit code 0, indicates command worked,
156 		boolean success = (process.waitFor()) == 0;
157 
158 		// Command failed, start up the browser
159 		if (!success) {
160 
161 			// command = '*browser* http://www.javaworld.com'
162 			command = browser + " " + url;
163 			Runtime.getRuntime().exec(command);
164 		}
165 	}
166 
167 }