/*
 * FindBugs - Find Bugs in Java programs
 * Copyright (C) 2006, University of Maryland
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston MA 02111-1307, USA
 */

package edu.umd.cs.findbugs.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import edu.umd.cs.findbugs.annotations.CheckForNull;

/**
 * 
 */
public class LaunchBrowser {

	private static final @CheckForNull Method jnlpShowMethod;
	private static final Object jnlpShowObject; // will not be null if jnlpShowMethod!=null
	
	private static Object desktopObject;
	private static Method desktopBrowseMethod;
	static {
		try {
			Class <?> desktopClass = Class.forName("java.awt.Desktop");
			desktopObject = desktopClass.getMethod("getDesktop").invoke(null);
			desktopBrowseMethod = desktopClass.getMethod("browse", URI.class);
		}  catch (Exception e) {
			assert true;
		} 
		// attempt to set the JNLP BasicService object and its showDocument(URL) method
		Method showMethod = null;
		Object showObject = null;
		try {
			Class<?> serviceManagerClass = Class.forName("javax.jnlp.ServiceManager");
			Method lookupMethod = serviceManagerClass.getMethod("lookup", new Class[] { String.class });
			showObject = lookupMethod.invoke(null, new Object[] { "javax.jnlp.BasicService" });
			showMethod = showObject.getClass().getMethod("showDocument", new Class [] { URL.class });
		} catch (ClassNotFoundException e) {
			assert true;
		} catch (NoSuchMethodException e) {
			assert true;
		} catch (IllegalAccessException e) {
			assert true;
		} catch (InvocationTargetException e) {
			assert true;
		}
		jnlpShowMethod = showMethod;
		jnlpShowObject = showObject;
	}



	/** 
	 * attempt to show the given URL.
	 * will first attempt via the JNLP api, then will try showViaExec().
	 * @param url the URL
	 * @return true on success
	 */
	public static boolean showDocument(URL url) {
		
		if (desktopObject != null && desktopBrowseMethod != null) try { 
			 desktopBrowseMethod.invoke(desktopObject, url.toURI());
			 return true;
		} catch (InvocationTargetException ite) {
			assert true;
		} catch (IllegalAccessException iae) {
			assert true;
		} catch (IllegalArgumentException e) {
			assert true;
        } catch (URISyntaxException e) {
        	assert true;
        }
		if (jnlpShowMethod != null) try {
			Object result = jnlpShowMethod.invoke(jnlpShowObject,  url );
			return (Boolean.TRUE.equals(result));
		} catch (InvocationTargetException ite) {
			assert true;
		} catch (IllegalAccessException iae) {
			assert true;
		}
		return false;
	}


	

	
}

@@ -862,16 +862,44 @@
 .util;%0A%0A
+import java.io.IOException;%0A
 import j
@@ -979,47 +979,8 @@
 od;%0A
-import java.net.MalformedURLException;%0A
 impo
@@ -1054,16 +1054,61 @@
 t.URL;%0A%0A
+import edu.umd.cs.findbugs.SystemProperties;%0A
 import e
@@ -1345,16 +1345,117 @@
 =null%0A%09%0A
+%09private static final boolean launchFirefox = SystemProperties.getBoolean(%22findbugs.launchFirefox%22);%0A
 %09private
@@ -1528,16 +1528,71 @@
 Method;%0A
+%09private static boolean launchViaExecFailed = false;%0A%09%0A
 %09static 
@@ -1823,32 +1823,56 @@
 (Exception e) %7B%0A
+%09%09%09e.printStackTrace();%0A
 %09%09%09assert true;%0A
@@ -3067,32 +3067,36 @@
 xception ite) %7B%0A
+%09%09%09%0A
 %09%09%09assert true;%0A
@@ -3545,16 +3545,564 @@
 ue;%0A%09%09%7D%0A
+%09if (launchFirefox && !launchViaExecFailed) %7B%0A%09%09%09Runtime r = Runtime.getRuntime();%0A%09%09%09String a%5B%5D = new String%5B%5D %7B %22firefox%22, url.toString() %7D;%0A%09%09%09try %7B%0A%09%09%09%09Process p = r.exec(a);%0A%09%09%09%09Thread.sleep(20);%0A%09%09%09%09int exitValue = p.exitValue();%0A%09%09%09%09if (exitValue != 0) %7B%0A%09%09%09%09%09launchViaExecFailed = true;%0A%09%09%09%09%09System.out.println(%22exit value : %22 + exitValue);%0A%09%09%09%09%09return false;%0A%09%09%09%09%7D %0A%09%09%09%09return true;%0A%09%09%09%7D catch (IllegalThreadStateException e) %7B%0A%09%09%09%09assert true;%0A%09%09%09%7D catch (Exception e) %7B%0A%09%09%09%09launchViaExecFailed = true;%0A%09%09%09%09e.printStackTrace();%0A%09%09%09%7D%0A%09%09%7D%0A%0A
 %09%09return

allow setting of a propery to specify firefox launches via Runtime.exec