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

/*
 * @(#)SunRIDeploymentSeparateVM.java	1.3 02/01/17
 */
 
package com.sun.cts.implementation.sun;

import java.rmi.*;
import java.util.*;
import java.io.*;
import java.net.*;
import javasoft.sqe.javatest.*;
import com.sun.enterprise.deployment.*;
import com.sun.enterprise.tools.deployment.main.*;
import com.sun.enterprise.resource.*;
import com.sun.cts.util.*;
import com.sun.cts.porting.*;

/**
 * This is an implementation of the CTSDeploymentInterface.  An implementation 
 * of this class must be supplied by any J2EE implementation wishing to
 * have JavaTest (the harness which drives the CTS tests) automatically deploy 
 * and undeploy test applications.  Providing this functionality enables the 
 * entire test suite to be run without having to manually deploy/undeploy the 
 * J2EE test applications prior to running the tests.  This particular
 * implementation uses the deploy/undeploy semantics of Sun's J2EE
 * reference implementation.
 *  
 * @author	Kyle Grucci
 */	
public class SunRIDeploymentSeparateVM extends SunRIDeployment
{
	private SunRIPortingServerInterface portingServer = null;
	private Process pPortingServer = null;
		
	private void getPortingServer() throws CTSDeploymentException
	{
		String sHost = "";
		String sPort = "2002";
		
		//lookup server here or in suitesynchronizer?
		try
		{
			
			
			sHost = InetAddress.getLocalHost().getHostName();
			try
			{
				sPort = propMgr.getProperty("sun.porting_server.port");
				TestUtil.logHarnessDebug("sun.porting_server.port = " + sPort);
			}
			catch(PropertyNotSetException pe)
			{
			    Log.print(pe.getMessage());
			    Log.print("error looking up property:  sun.porting_server.port");
				Log.print("Defaulting to port #:  " + sPort);
			}
			
			//Do a lookup of our porting server
			portingServer = (SunRIPortingServerInterface)Naming.lookup("rmi://" + sHost + ":" + sPort + "/SunRIPortingServer");
			
			portingServer.init(propMgr.getJteProperties());
			
		}
		catch(java.net.MalformedURLException me)
		{
		    Log.print(me.getMessage());
			me.printStackTrace();
			throw new CTSDeploymentException(me.getMessage(), me);
		}
		catch(java.net.UnknownHostException ue)
		{
		    Log.print(ue.getMessage());
		    Log.print("unknown host:  " + sHost);
			ue.printStackTrace();
			throw new CTSDeploymentException(ue.getMessage(), ue);
		}
		catch(NotBoundException nbe)
		{
		    Log.print(nbe.getMessage());
			nbe.printStackTrace();
			throw new CTSDeploymentException(nbe.getMessage(), nbe);
		}
		catch(java.rmi.RemoteException e)
		{
			TestUtil.logHarness("Failed to deploy using SunRIPortingServer: " + 
						e.getMessage());
			e.printStackTrace();
			throw new CTSDeploymentException(e.getMessage(), e);	
		}
	}
	
	
	/**
	 * This method is called by the test harness to deploy an .ear file into Sun
	 * 's J2EE reference implementation.  We extract such info as the app 
	 * earfile from the provided deployment information.  The following 
	 * properties are available for this method's use:
	 * <p>
	 * generateSQL - "true" if SQL is to be generated for CMP beans
	 * <p>
	 * <p>
	 * deployment_host - the host where this app is to be deployed
	 * <p>
	 *
	 * All additional information is queryable from the DeploymentInfo
	 * interface.
	 * 
	 * @param	info 	Object containing necessary deployment info.
	 * @return		This method should return a string which is formatted such 
	 *				that it can be appended to the classpath.  This 
	 *				implementation returns the fully qualified path to a jar 
	 *				file, which contains the generated ejb stub classes, which 
	 *				are used by any appclient tests (tests whose client directly
	 *				uses an ejb).
	 */
	public String deploy(DeploymentInfo info) throws CTSDeploymentException
	{
		String classpath = null;
		
		try
		{
			if(portingServer == null)
				getPortingServer();
				
			//send deploy info across to proxy		
			classpath = portingServer.deploy(info);
		}
		catch(java.rmi.RemoteException e)
		{
			System.err.println("Failed to deploy using SunRIPortingServer:  " + 
						e.getMessage());
			e.printStackTrace();
			throw new CTSDeploymentException(e.getMessage());	
		}
		
		return classpath;
		
	}

	/**
	 * This method is called by the test harness to check whether or not
	 * an application ear is deployed.  This information is used to determine
	 * whether or not the harness needs to undeploy it.	 The following 
	 * properties are available for this method's use:  
	 * 
	 * ear_file - the fully qualified application (.ear file) 
	 * deployment_host - the host where this app is deployed
	 *
	 * @param	p	Properties specific to the currently running test
	 * @return  	True if the app is deployed.  False if not.
	 */
	public boolean isDeployed(Properties p) throws CTSDeploymentException
	{
		boolean bIsInstalled = false;
		
		try
		{
			if(portingServer == null)
				getPortingServer();
			
			//send deploy info across to proxy		
			bIsInstalled = portingServer.isDeployed(p);
		}
		catch(java.rmi.RemoteException e)
		{
			System.err.println("Failed to check if app was deployed using" + 
					"SunRIPortingServer:  " + e.getMessage());
			e.printStackTrace();
			throw new CTSDeploymentException(e.getMessage());	
		}
		
		return bIsInstalled;
	}

	/**
	 * This method is called by test harness to undeploy an .ear file from Sun's
	 * J2EE reference implementation.  We extract such info as host and app
	 * from these props.  The following properties are available for this method
	 * 's use:  
	 * 
	 * ear_file - the fully qualified application (.ear file) 
	 * deployment_host - the host to undeploy this app from
	 *
	 * @param	p	Properties specific to the currently running test
	 */
	public void undeploy(Properties p) throws CTSDeploymentException
	{
		try
		{
			if(portingServer == null)
				getPortingServer();
			
			//undeploy app		
			portingServer.undeploy(p);
		}
		catch(java.rmi.RemoteException e)
		{
			System.err.println("Failed to undepoy using SunRIPortingServer:  " +
									e.getMessage());
			e.printStackTrace();
			throw new CTSDeploymentException(e.getMessage());	
		}
	}
	
	/**
	 * This method is called by the test harness to get any additional test 	
	 * specific arguments that must be passed to the application client 
	 * container class, which is specified in the cts.jte file in the given 
	 * environment (command.testExecuteAppClient property).  
	 * The additional args should be
	 * appended to the value of (p.getProperty("executeArgs");), and returned.
	 * The following properties are available for this method's use:  
	 * 
	 * executeArgs - the current executeArgs as specified in the jte file 
	 *
	 * @param	p	Properties specific to the currently running test
	 * @return		This method should return a string which represents all
	 *				executeArgs to be used.
	 */
	public String getAppClientArgs(Properties p)
	{
		TestUtil.logHarness("SunRIDeploymentSeparateVM.getAppClientArgs");
		
		String executeArgs = p.getProperty("executeArgs");
		String sApp = p.getProperty("ear_file");
		String sEarName = sApp.substring(sApp.lastIndexOf(File.separator)+1);
		sRIAppName = sHarnessTempDir + File.separator + "two_" + sEarName;
 
		if(executeArgs == null)
			executeArgs = "-client " + sRIAppName + " -name " + p.getProperty("client_name");
		else
			executeArgs += " -client " + sRIAppName + " -name " + p.getProperty("client_name");
	
		return executeArgs;
	}
	
	public Hashtable getInteropJNDINames(DeploymentInfo[] infoArray)
	{
		TestUtil.logHarness("SunRIDeploymentSeparateVM.getInteropJNDINames");
		return super.getInteropJNDINames(infoArray);
	}
}
