001: package hero.client.manager;
002:
003: import java.io.IOException;
004:
005: /**
006: * A simple, static class to display a URL in the system browser.
007:
008:
009: *
010: * Under Unix, the system browser is hard-coded to be 'netscape'.
011: * Netscape must be in your PATH for this to work. This has been
012: * tested with the following platforms: AIX, HP-UX and Solaris.
013:
014:
015: *
016: * Under Windows, this will bring up the default browser under windows,
017: * usually either Netscape or Microsoft IE. The default browser is
018: * determined by the OS. This has been tested under Windows 95/98/NT.
019:
020:
021: *
022: * Examples:
023:
024:
025: *
026: BrowserControl.displayURL("http://www.javaworld.com")
027: *
028: BrowserControl.displayURL("file://c:\\docs\\index.html")
029: *
030: BrowserContorl.displayURL("file:///user/joe/index.html");
031: *
032:
033: * Note - you must include the url type -- either "http://" or
034: * "file://".
035: */
036: public class BrowserControl {
037:
038: static java.util.ResourceBundle resource = java.util.ResourceBundle
039: .getBundle("resources.Traduction")/*#BundleType=List*/;
040:
041: /**
042: * Display a file in the system browser. If you want to display a
043: * file, you must include the absolute path name.
044: *
045: * @param url the file's url (the url must start with either "http://"
046: or
047: * "file://").
048: */
049: public static void displayURL(String url) {
050: boolean windows = isWindowsPlatform();
051: String cmd = null;
052: try {
053: if (windows) {
054: // cmd = 'rundll32 url.dll,FileProtocolHandler http://...'
055: cmd = WIN_PATH + " " + WIN_FLAG + " " + url;
056: Process p = Runtime.getRuntime().exec(cmd);
057: } else {
058: // Under Unix, Netscape has to be running for the "-remote"
059: // command to work. So, we try sending the command and
060: // check for an exit value. If the exit command is 0,
061: // it worked, otherwise we need to start the browser.
062: // cmd = 'netscape -remote openURL(http://www.javaworld.com)'
063: cmd = UNIX_PATH + " " + UNIX_FLAG + "(" + url + ")";
064: Process p = Runtime.getRuntime().exec(cmd);
065: try {
066: // wait for exit code -- if it's 0, command worked,
067: // otherwise we need to start the browser up.
068: int exitCode = p.waitFor();
069: if (exitCode != 0) {
070: // Command failed, start up the browser
071: // cmd = 'netscape http://www.javaworld.com'
072: cmd = UNIX_PATH + " " + url;
073: p = Runtime.getRuntime().exec(cmd);
074: }
075: } catch (InterruptedException x) {
076: System.err.println(resource
077: .getString("browsercontrol.error")
078: + cmd + "'");
079: System.err.println(resource
080: .getString("browsercontrol.caught")
081: + x);
082: }
083: }
084: } catch (IOException x) {
085: // couldn't exec browser
086: System.err.println(resource
087: .getString("browsercontrol.notinvoke")
088: + cmd);
089: System.err.println(resource
090: .getString("browsercontrol.caught")
091: + x);
092: }
093: }
094:
095: /**
096: * Try to determine whether this application is running under Windows
097: * or some other platform by examing the "os.name" property.
098: *
099: * @return true if this application is running under a Windows OS
100: */
101: public static boolean isWindowsPlatform() {
102: String os = System.getProperty("os.name");
103: if (os != null && os.startsWith(WIN_ID))
104: return true;
105: else
106: return false;
107:
108: }
109:
110: /**
111: * Simple example.
112: */
113: public static void main(String[] args) {
114: displayURL("http://www.javaworld.com");
115: }
116:
117: // Used to identify the windows platform.
118: private static final String WIN_ID = "Windows";
119: // The default system browser under windows.
120: private static final String WIN_PATH = "rundll32";
121: // The flag to display a url.
122: private static final String WIN_FLAG = "url.dll,FileProtocolHandler";
123: // The default browser under unix.
124: private static final String UNIX_PATH = "netscape";
125: // The flag to display a url.
126: private static final String UNIX_FLAG = "-remote openURL";
127: }
|