1
22
23 package com.liferay.portal.util;
24
25 import com.liferay.portal.kernel.util.Validator;
26
27 import java.net.HttpURLConnection;
28 import java.net.URL;
29
30
35 public class BrowserLauncher implements Runnable {
36
37 public void run() {
38 if (Validator.isNull(PropsValues.BROWSER_LAUNCHER_URL)) {
39 return;
40 }
41
42 for (int i = 0; i < 5; i++) {
43 try {
44 Thread.sleep(3000);
45 }
46 catch (InterruptedException ie) {
47 }
48
49 try {
50 URL url = new URL(PropsValues.BROWSER_LAUNCHER_URL);
51
52 HttpURLConnection urlc =
53 (HttpURLConnection)url.openConnection();
54
55 int responseCode = urlc.getResponseCode();
56
57 if (responseCode == HttpURLConnection.HTTP_OK) {
58 try {
59 launchBrowser();
60 }
61 catch (Exception e2) {
62 }
63
64 break;
65 }
66 }
67 catch (Exception e1) {
68 }
69 }
70 }
71
72 protected void launchBrowser() throws Exception {
73 String os = System.getProperty("os.name").toLowerCase();
74
75 Runtime runtime = Runtime.getRuntime();
76
77 if (os.indexOf("mac") >= 0) {
78 launchBrowserApple(runtime);
79 }
80 else if (os.indexOf("win") >= 0) {
81 launchBrowserWindows(runtime);
82 }
83 else {
84 launchBrowserUnix(runtime);
85 }
86 }
87
88 protected void launchBrowserApple(Runtime runtime) throws Exception {
89 runtime.exec("open " + PropsValues.BROWSER_LAUNCHER_URL);
90 }
91
92 protected void launchBrowserUnix(Runtime runtime) throws Exception {
93 StringBuilder sb = new StringBuilder();
94
95 for (int i = 0; i < _BROWSERS.length; i++) {
96 if (i != 0) {
97 sb.append(" || ");
98 }
99
100 sb.append(_BROWSERS[i]);
101 sb.append(" \"");
102 sb.append(PropsValues.BROWSER_LAUNCHER_URL);
103 sb.append("\" ");
104 }
105
106 runtime.exec(new String[] {"sh", "-c", sb.toString()});
107 }
108
109 protected void launchBrowserWindows(Runtime runtime) throws Exception {
110 runtime.exec("cmd.exe /c start " + PropsValues.BROWSER_LAUNCHER_URL);
111 }
112
113 private static final String[] _BROWSERS = {
114 "firefox", "mozilla", "konqueror", "opera"
115 };
116
117 }