001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.util;
016    
017    import com.liferay.portal.kernel.util.StringBundler;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.Validator;
020    
021    import java.net.HttpURLConnection;
022    import java.net.URL;
023    
024    /**
025     * @author Brian Wing Shun Chan
026     */
027    public class BrowserLauncher implements Runnable {
028    
029            public void run() {
030                    if (Validator.isNull(PropsValues.BROWSER_LAUNCHER_URL)) {
031                            return;
032                    }
033    
034                    for (int i = 0; i < 5; i++) {
035                            try {
036                                    Thread.sleep(3000);
037                            }
038                            catch (InterruptedException ie) {
039                            }
040    
041                            try {
042                                    URL url = new URL(PropsValues.BROWSER_LAUNCHER_URL);
043    
044                                    HttpURLConnection urlc =
045                                            (HttpURLConnection)url.openConnection();
046    
047                                    int responseCode = urlc.getResponseCode();
048    
049                                    if (responseCode == HttpURLConnection.HTTP_OK) {
050                                            try {
051                                                    launchBrowser();
052                                            }
053                                            catch (Exception e2) {
054                                            }
055    
056                                            break;
057                                    }
058                            }
059                            catch (Exception e1) {
060                            }
061                    }
062            }
063    
064            protected void launchBrowser() throws Exception {
065                    String os = System.getProperty("os.name").toLowerCase();
066    
067                    Runtime runtime = Runtime.getRuntime();
068    
069                    if (os.indexOf("mac") >= 0) {
070                            launchBrowserApple(runtime);
071                    }
072                    else if (os.indexOf("win") >= 0) {
073                            launchBrowserWindows(runtime);
074                    }
075                    else {
076                            launchBrowserUnix(runtime);
077                    }
078            }
079    
080            protected void launchBrowserApple(Runtime runtime) throws Exception {
081                    runtime.exec("open " + PropsValues.BROWSER_LAUNCHER_URL);
082            }
083    
084            protected void launchBrowserUnix(Runtime runtime) throws Exception {
085                    if (_BROWSERS.length == 0) {
086                            runtime.exec(new String[] {"sh", "-c", StringPool.BLANK});
087                    }
088    
089                    StringBundler sb = new StringBundler(_BROWSERS.length * 5 - 1);
090    
091                    for (int i = 0; i < _BROWSERS.length; i++) {
092                            if (i != 0) {
093                                    sb.append(" || ");
094                            }
095    
096                            sb.append(_BROWSERS[i]);
097                            sb.append(" \"");
098                            sb.append(PropsValues.BROWSER_LAUNCHER_URL);
099                            sb.append("\" ");
100                    }
101    
102                    runtime.exec(new String[] {"sh", "-c", sb.toString()});
103            }
104    
105            protected void launchBrowserWindows(Runtime runtime) throws Exception {
106                    runtime.exec("cmd.exe /c start " + PropsValues.BROWSER_LAUNCHER_URL);
107            }
108    
109            private static final String[] _BROWSERS = {
110                    "firefox", "mozilla", "konqueror", "opera"
111            };
112    
113    }