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