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