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