/*
 * IBM Confidential 
 *   
 * (c) Copyright IBM Corp. 2004  All Rights Reserved.
 *
 * The source code for this program is not published or otherwise 
 * divested of its trade secrets, irrespective of what has been 
 * deposited with the U.S. Copyright Office.
 */
import java.applet.Applet;

public class FtInfo extends Applet
{
	private static Object htmlDomain = null;
	
	public void init()
	{
   	}

	private Object getHtmlTestDomain()
	{
		try
		{
			com.rational.test.ft.bootstrap.Bootstrap bs = com.rational.test.ft.bootstrap.Bootstrap.getBootstrap();
			if (bs != null)
			{
				Class cls = bs.loadClass("com.rational.test.ft.sys.TestContext");
				if (cls != null)
				{
					Object tc = com.rational.test.ft.bootstrap.BootstrapReflection.invokeStaticMethod("getRunningTestContext", cls);
					if (tc != null)
					{
						java.util.Enumeration domains = (java.util.Enumeration)com.rational.test.ft.bootstrap.BootstrapReflection.invokeMethod("getDomainImplementations", tc);
						while (domains.hasMoreElements()) 
						{
							Object domain = domains.nextElement();
							if (com.rational.test.ft.bootstrap.BootstrapReflection.isInstanceOf(domain, "com.rational.test.ft.domain.html.HtmlTestDomainImplementation"))
							{
								htmlDomain = domain;
								return htmlDomain;
							}
						}
					}

					// On Unix/Linux, the above code doesn't work, because the
					// HTML domain isn't in the applet's JVM.  So we do this
					// workaround to see if we can find an HTML domain whose
					// PID matches our parent PID (presumably the browser's
					// PID)
					Integer ppid = (Integer)com.rational.test.ft.bootstrap.BootstrapReflection.invokeStaticMethod("getParentProcessIdAsInteger", cls);

					return( com.rational.test.ft.bootstrap.BootstrapReflection.invokeStaticMethod("getTcRefMatchingPid",
										cls, 
										new Object[] {ppid},
										new Class[] {java.lang.Integer.class}) );
				}
			}	
		}
		catch(Exception e)
		{
		}
		catch(Error e)
		{
		}
		return null;
	}

	public boolean bootstrapLoaded()
	{
		try
        		{
			Class cls = Class.forName("com.rational.test.ft.bootstrap.BootstrapReflection");
			if (cls != null)
			{
				return true;
			}
        		}
	        	catch (Exception e)
        		{	
		}
		return false;
	}

	public boolean javaTestDomainLoaded()
	{
		try
		{
			Object domain = com.rational.test.ft.bootstrap.Bootstrap.getBootstrapDomain();				
			if (domain != null)
			{
				return true;
			}
		}
		catch(Exception e)
		{
		}		
		return false;
	}

	public boolean htmlDomainLoaded()
	{
		if (htmlDomain == null)
		{ 
			htmlDomain = getHtmlTestDomain();
		}
		if (htmlDomain != null)
		{
			return true;
		}
		else
		{
			return false;		
		}
	}	
	
	public String getHtmlDomainImplementationName(String navigatorAppName)
	{
		if (htmlDomain == null)
		{ 
			htmlDomain = getHtmlTestDomain();
		}
		if (htmlDomain != null)
		{
			try
			{
				if( htmlDomain.getClass().getName().equals("com.rational.test.ft.domain.html.HtmlTestDomainImplementation") )
				{
					String domainName = (String)com.rational.test.ft.bootstrap.BootstrapReflection.invokeMethod("getImplementationName", htmlDomain);
					if (domainName != null && domainName.length() > 0)
					{
						return domainName;
					}
				}
				else
				{
					// if we get here, then we're on Unix/Linux, and the returned
					// object is not an HTML domain, but rather the test context
					// reference.  We also know we're using Netscape, so we just
					// return the string that's used as a fallback in NSHtmlDomain.
					// Because of security access issues (see Frank's comments in
					// AppletHelper.java), we can't easily load DomainTestObject
					// in order to get the precise domain implementation name, so
					// we just do this for now.
					Object tcRef = htmlDomain;
					Object args[] = { tcRef, "Html" };
					Class clss[] = { tcRef.getClass(), String.class };
					com.rational.test.ft.bootstrap.Bootstrap bs = com.rational.test.ft.bootstrap.Bootstrap.getBootstrap();
					if (bs != null)
					{
						Class cls = bs.loadClass("com.rational.test.ft.sys.TestContext");
						if (cls != null)
						{
						if ((( Boolean )com.rational.test.ft.bootstrap.BootstrapReflection.invokeStaticMethod("isDomainSupported", cls, args, clss)).booleanValue() == true)
						{
						args[1] = navigatorAppName;
						String implName = (( String )com.rational.test.ft.bootstrap.BootstrapReflection.invokeStaticMethod("getHtmlImplNameForTcRef", cls, args, clss));
						return implName;						

						}
						}
					}
				}
			}
			catch (Exception e)
			{
			}
			catch (Error e)
			{
			}
		}
		return "Unknown";
	}						
}
