View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2007 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // utgb-shell Project
18  //
19  // WebBrowser.java
20  // Since: 2007/11/23
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.shell;
26  
27  import java.lang.reflect.Method;
28  
29  import org.xerial.util.log.Logger;
30  
31  /**
32   * Open a given URL via OS's default web browser.
33   * 
34   * This code is created by modifying the following public code: (BareBones library)
35   * http://www.centerkey.com/java/browser/myapp/BareBonesBrowserLaunch.java
36   * 
37   * @author leo
38   * 
39   */
40  public class WebBrowser {
41  
42  	private static Logger _logger = Logger.getLogger(WebBrowser.class);
43  
44  	public static void openURL(String url) {
45  		try {
46  			OSType osType = OSType.getOSType();
47  			switch (osType) {
48  			case MacOS:
49  				Class fileMgr = Class.forName("com.apple.eio.FileManager");
50  				Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] { String.class });
51  				openURL.invoke(null, new Object[] { url });
52  				break;
53  			case Windows:
54  				Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
55  				break;
56  			default:
57  				// assume Unix or Linux
58  				String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" };
59  				String browser = null;
60  				for (int count = 0; count < browsers.length && browser == null; count++)
61  					if (Runtime.getRuntime().exec(new String[] { "which", browsers[count] }).waitFor() == 0)
62  						browser = browsers[count];
63  				if (browser == null)
64  					throw new Exception("Could not find web browser");
65  				else
66  					Runtime.getRuntime().exec(new String[] { browser, url });
67  				break;
68  			}
69  		}
70  		catch (Exception e) {
71  			_logger.error(e);
72  		}
73  	}
74  
75  }