1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
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  /**
29   * <a href="BrowserLauncher.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   *
33   */
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 }