/*
 * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

/*
 * @(#)SunRILauncher.java	1.5 02/01/12
 */

package com.sun.cts.implementation.sun;

import java.io.*;
import com.sun.cts.harness.ExecCTSTestCmd; // for ExecCTSTestCmd
import com.sun.cts.util.*;
import com.sun.cts.porting.*;
import java.util.*;
import java.awt.*;
import javax.swing.*;

/**
 * CTSLauncher provides implemenation specific code for launching 
 * applications for interactive tests or other needs.  Platforms that do
 * not support Runtime.exec() can use this class as a hook to instruct the
 * user which applications to launch via a dialog box or another mechanism.
 *
 * @author Mark Roth
 */
public class SunRILauncher implements CTSLauncherInterface
{

    /** Properties object for use by launcher methods */
    private Properties properties;

    /**
     * Creates a new launcher.
     *
     * @param prop A properties table for use by the launcher methods 
     *     however they choose.  The cts.env environment is made available
     *     through this properties object.
     */
    public void init( Properties prop ) {
	this.properties = prop;
    }

    /**
     * Launch a web browser to the given URL.  The web browser launched must
     * start in a new process and must be capable of SSL 3.0, such as 
     * Netscape Navigator 4.51.  The call must not return until the web 
     * browser is closed by the user.
     * <p>
     * Expects the property key "browser" to contain the value of the
     * path to the browser on this system.
     *
     * @param url The fully qualified URL to launch the browser to.
     */
    public void launchBrowser( String url ) {

	// If set to true, instead of launching the browser directly, 
	// a dialog will be displayed notifying the user that they should
	// launch their favorite web browser to the given url and click okay
	// when done.
	boolean dialogMode = false;

	if( dialogMode ) {
	    JOptionPane.showInputDialog( 
		new Frame(),
		"Please launch an SSL 3.0 capable web browser in a new " +
		"process to the following URL.  Click OK only after you " +
		"have closed the web browser.",
                "Launch Browser",
		JOptionPane.INFORMATION_MESSAGE,
		null,
		null,
		url );
	}
	else {
	    String cmd[] = new String[2];
	    String env[] = null;

	    ExecCTSTestCmd command = new ExecCTSTestCmd();

	    // cmd[0] = path to browser
	    // cmd[1] = url
	    // env = inherited from parent environment.  On Solaris,
	    // the variables DISPLAY and HOME must be set.  On NT, the
	    // variables SYSTEMROOT and WINDIR must be set.
	    cmd[0] = properties.getProperty( "browser" );
	    cmd[1] = url;
	    command.exec( cmd, env, 
		new PrintWriter( System.out ), 
		new PrintWriter( System.err ) );
	}
    }

}
