001
014
015 package com.liferay.portal.util;
016
017 import com.liferay.portal.kernel.util.OSDetector;
018 import com.liferay.portal.kernel.util.StringBundler;
019 import com.liferay.portal.kernel.util.StringPool;
020 import com.liferay.portal.kernel.util.Validator;
021
022 import java.net.HttpURLConnection;
023 import java.net.URL;
024
025
028 public class BrowserLauncher implements Runnable {
029
030 public void run() {
031 if (Validator.isNull(PropsValues.BROWSER_LAUNCHER_URL)) {
032 return;
033 }
034
035 for (int i = 0; i < 5; i++) {
036 try {
037 Thread.sleep(3000);
038 }
039 catch (InterruptedException ie) {
040 }
041
042 try {
043 URL url = new URL(PropsValues.BROWSER_LAUNCHER_URL);
044
045 HttpURLConnection urlc =
046 (HttpURLConnection)url.openConnection();
047
048 int responseCode = urlc.getResponseCode();
049
050 if (responseCode == HttpURLConnection.HTTP_OK) {
051 try {
052 launchBrowser();
053 }
054 catch (Exception e2) {
055 }
056
057 break;
058 }
059 }
060 catch (Exception e1) {
061 }
062 }
063 }
064
065 protected void launchBrowser() throws Exception {
066 Runtime runtime = Runtime.getRuntime();
067
068 if (OSDetector.isApple()) {
069 launchBrowserApple(runtime);
070 }
071 else if (OSDetector.isWindows()) {
072 launchBrowserWindows(runtime);
073 }
074 else {
075 launchBrowserUnix(runtime);
076 }
077 }
078
079 protected void launchBrowserApple(Runtime runtime) throws Exception {
080 runtime.exec("open " + PropsValues.BROWSER_LAUNCHER_URL);
081 }
082
083 protected void launchBrowserUnix(Runtime runtime) throws Exception {
084 if (_BROWSERS.length == 0) {
085 runtime.exec(new String[] {"sh", "-c", StringPool.BLANK});
086 }
087
088 StringBundler sb = new StringBundler(_BROWSERS.length * 5 - 1);
089
090 for (int i = 0; i < _BROWSERS.length; i++) {
091 if (i != 0) {
092 sb.append(" || ");
093 }
094
095 sb.append(_BROWSERS[i]);
096 sb.append(" \"");
097 sb.append(PropsValues.BROWSER_LAUNCHER_URL);
098 sb.append("\" ");
099 }
100
101 runtime.exec(new String[] {"sh", "-c", sb.toString()});
102 }
103
104 protected void launchBrowserWindows(Runtime runtime) throws Exception {
105 runtime.exec("cmd.exe /c start " + PropsValues.BROWSER_LAUNCHER_URL);
106 }
107
108 private static final String[] _BROWSERS = {
109 "firefox", "mozilla", "konqueror", "opera"
110 };
111
112 }