001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2006-2008 Sun Microsystems, Inc.
015 * Portions Copyright 2013-2014 ForgeRock AS.
016 */
017
018package org.opends.quicksetup.util;
019
020
021import static com.forgerock.opendj.util.OperatingSystem.isWindows;
022import static com.forgerock.opendj.util.OperatingSystem.isMacOS;
023import org.forgerock.i18n.LocalizableMessage;
024
025import java.io.IOException;
026import java.lang.reflect.InvocationTargetException;
027import java.lang.reflect.Method;
028
029/**
030 * This is a very basic class used to launch the user web browser.
031 *
032 */
033public class WebBrowserLauncher
034{
035  /**
036   * Tries to launch the user web browser with a given URL.
037   * @param url the url to be used to launch the web browser.
038   * @throws WebBrowserException if launching failed.
039   */
040  public static void openURL(String url) throws WebBrowserException
041  {
042    try
043    {
044      if (isMacOS())
045      {
046        Class<?> fileMgr = Class.forName("com.apple.eio.FileManager");
047        Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[]
048          { String.class });
049        openURL.invoke(null, url);
050
051      } else if (isWindows())
052      {
053        String[] cmd = {"rundll32", "url.dll,FileProtocolHandler", url};
054        Runtime.getRuntime().exec(cmd);
055
056      } else
057      {
058        // assume Unix or Linux
059        String[] browsers =
060              { "firefox", "opera", "konqueror", "epiphany", "mozilla",
061                  "netscape" };
062        String browser = null;
063        for (int count = 0; count < browsers.length && browser == null;
064        count++)
065        {
066          if (Runtime.getRuntime().exec(new String[]
067            { "which", browsers[count] }).waitFor() == 0)
068          {
069            browser = browsers[count];
070          }
071        }
072
073        if (browser == null)
074        {
075          throw new WebBrowserException(url, // TODO: i18n
076                  LocalizableMessage.raw("Could not find web browser"),
077                  null);
078        } else
079        {
080          Runtime.getRuntime().exec(new String[]
081            { browser, url });
082        }
083      }
084    } catch (ClassNotFoundException cnfe)
085    {
086      throw new WebBrowserException(url, // TODO: i18n
087              LocalizableMessage.raw("Class Not Found Exception"), cnfe);
088    } catch (IOException ioe)
089    {
090      throw new WebBrowserException(url, // TODO: i18n
091              LocalizableMessage.raw("IO Exception"), ioe);
092    } catch (InterruptedException ie)
093    {
094      throw new WebBrowserException(url, // TODO: i18n
095              LocalizableMessage.raw("Interrupted Exception"), ie);
096    } catch (NoSuchMethodException nsme)
097    {
098      throw new WebBrowserException(url, // TODO: i18n
099              LocalizableMessage.raw("No Such Method Exception"), nsme);
100    } catch (InvocationTargetException ite)
101    {
102      throw new WebBrowserException(url, // TODO: i18n
103              LocalizableMessage.raw("Invocation Target Exception"), ite);
104    } catch (IllegalAccessException iae)
105    {
106      throw new WebBrowserException(url, // TODO: i18n
107              LocalizableMessage.raw("Illegal Access Exception"), iae);
108    }
109  }
110}