1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.util;
16  
17  import com.liferay.portal.kernel.util.StringBundler;
18  import com.liferay.portal.kernel.util.StringPool;
19  import com.liferay.portal.kernel.util.Validator;
20  
21  import java.net.HttpURLConnection;
22  import java.net.URL;
23  
24  /**
25   * <a href="BrowserLauncher.java.html"><b><i>View Source</i></b></a>
26   *
27   * @author Brian Wing Shun Chan
28   */
29  public class BrowserLauncher implements Runnable {
30  
31      public void run() {
32          if (Validator.isNull(PropsValues.BROWSER_LAUNCHER_URL)) {
33              return;
34          }
35  
36          for (int i = 0; i < 5; i++) {
37              try {
38                  Thread.sleep(3000);
39              }
40              catch (InterruptedException ie) {
41              }
42  
43              try {
44                  URL url = new URL(PropsValues.BROWSER_LAUNCHER_URL);
45  
46                  HttpURLConnection urlc =
47                      (HttpURLConnection)url.openConnection();
48  
49                  int responseCode = urlc.getResponseCode();
50  
51                  if (responseCode == HttpURLConnection.HTTP_OK) {
52                      try {
53                          launchBrowser();
54                      }
55                      catch (Exception e2) {
56                      }
57  
58                      break;
59                  }
60              }
61              catch (Exception e1) {
62              }
63          }
64      }
65  
66      protected void launchBrowser() throws Exception {
67          String os = System.getProperty("os.name").toLowerCase();
68  
69          Runtime runtime = Runtime.getRuntime();
70  
71          if (os.indexOf("mac") >= 0) {
72              launchBrowserApple(runtime);
73          }
74          else if (os.indexOf("win") >= 0) {
75              launchBrowserWindows(runtime);
76          }
77          else {
78              launchBrowserUnix(runtime);
79          }
80      }
81  
82      protected void launchBrowserApple(Runtime runtime) throws Exception {
83          runtime.exec("open " + PropsValues.BROWSER_LAUNCHER_URL);
84      }
85  
86      protected void launchBrowserUnix(Runtime runtime) throws Exception {
87          if (_BROWSERS.length == 0) {
88              runtime.exec(new String[] {"sh", "-c", StringPool.BLANK});
89          }
90  
91          StringBundler sb = new StringBundler(_BROWSERS.length * 5 - 1);
92  
93          for (int i = 0; i < _BROWSERS.length; i++) {
94              if (i != 0) {
95                  sb.append(" || ");
96              }
97  
98              sb.append(_BROWSERS[i]);
99              sb.append(" \"");
100             sb.append(PropsValues.BROWSER_LAUNCHER_URL);
101             sb.append("\" ");
102         }
103 
104         runtime.exec(new String[] {"sh", "-c", sb.toString()});
105     }
106 
107     protected void launchBrowserWindows(Runtime runtime) throws Exception {
108         runtime.exec("cmd.exe /c start " + PropsValues.BROWSER_LAUNCHER_URL);
109     }
110 
111     private static final String[] _BROWSERS = {
112         "firefox", "mozilla", "konqueror", "opera"
113     };
114 
115 }