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