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

/*
 * @(#)SunRIDeployment.java	1.21 02/01/12
 */
 
package com.sun.cts.implementation.sun;

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.tools.packager.*;
import com.sun.enterprise.tools.deployment.main.*;
import com.sun.enterprise.tools.deployment.backend.*;
import com.sun.enterprise.resource.*;
import com.sun.cts.util.*;
import com.sun.cts.porting.*;
import com.sun.ejb.sqlgen.DBInfo;
import com.sun.ejb.sqlgen.SQLGenerator;

/**
 * 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 SunRIDeployment implements CTSDeploymentInterface
{
	protected DeployTool deployTool = new DeployTool(false);
	protected ComponentPackager packager = new ComponentPackager();
	//public static CTSDeployment deployTool = null;
	protected URL[] urls = new URL[1];
	protected PrintWriter Log = null;
	protected Hashtable htDeployedApps = new Hashtable();
	protected CTSPropertyManager propMgr = null;
	protected String slastDeploymentHost = "";
	protected String slastDeploymentPort = "";
	protected File fRIEarFile = null;
	protected String sHarnessTempDir = "";
	protected String sRIAppName = "";
	protected Application standaloneComponent = null;
	protected Hashtable htJNDIRefs = null;
	/**
	 * Initializes a new CTSDeployment instance.  All output should be printed 
	 * to this PrintWriter.  All properties in the cts.jte file are accessible
	 * to this porting implementation class only via the CTSPropertyManager
	 * class.  Please see Sun's implementation of this method (below) for an 
	 * example. 
	 *
	 * @param	writer   The PrintWriter that should be used to log output.
	 */
	public void init(PrintWriter out)
	{
		Log = out;
		propMgr = CTSPropertyManager.getCTSPropertyManager();
		
		try 
		{
		    sHarnessTempDir = propMgr.getProperty("harness.temp.directory");
		}
		catch(PropertyNotSetException pe)
		{
		    Log.print(pe.getMessage());
		    Log.print("error looking up property:  harness.temp.directory");
			pe.printStackTrace();
		}
	}

	private String[] setRunTimeInfo(String s, String sApp) 
		throws CTSDeploymentException
	{
		String[] sRunTimeFiles = null;
		Application toSet = null;
		StringTokenizer st = new StringTokenizer(s);
		int iCount = st.countTokens();
		String sAppName = sApp.substring(sApp.lastIndexOf(File.separator)+1,
				sApp.lastIndexOf("."));
		String sSweptRuntimeFile = "";
		sRunTimeFiles = new String[iCount];
		//sAppNameOnly = (new File(sApp)).getName();
		sRIAppName = sHarnessTempDir + File.separator + (new File(sApp)).getName();

		try 
		{
		    //reset
			fRIEarFile = null;
			
			if(sApp.endsWith(".ear"))
			{
				fRIEarFile = new File(sRIAppName);
				ApplicationManager am = deployTool.getApplicationManager();
		    	toSet = am.openApplication(new File(sApp));
			}
			else
			{
				Log.print("SetRuntimeInfo called with jar/war: " + sApp);
				
				//assume we have a jar or war
				//check to make sure that RI doesn't modify the original file
				standaloneComponent = deployTool.deployStandaloneModule(new File(sApp));
			}
		}
		catch(Exception ioe)
		{
		    Log.print(ioe.getMessage());
		    Log.print("couldn't open application " + sApp);
			throw new CTSDeploymentException(ioe.getMessage());
		}
		
		for(int ii = 0; ii < iCount; ii++)
		{
			//set runtime info for each file
			sRunTimeFiles[ii] = st.nextToken().trim();
						
			try 
			{
			    if(sRunTimeFiles[ii].indexOf("_client.runtime.xml") != -1)
				{
					//if(sRunTimeFiles[ii].indexOf("ejb_vehicle_client.runtime.xml") != -1)
					//{
						//if not an ear, then we must have a jar/war
						if(fRIEarFile != null)
						{
							packager.setClientRuntimeDeploymentInfo(toSet, 
								new File(sRunTimeFiles[ii]), sAppName + "_client", fRIEarFile);
						}
						else
						{
							packager.setClientRuntimeDeploymentInfo(standaloneComponent, 
								new File(sRunTimeFiles[ii]), sAppName + "_client");
						}
					/*}
					else if(sRunTimeFiles[ii].indexOf("appclient_vehicle_client.runtime.xml") != -1)
					{
						//if not an ear, then we must have a jar/war
						if(fRIEarFile != null)
						{
							packager.setClientRuntimeDeploymentInfo(toSet, 
							new File(sRunTimeFiles[ii]), sAppName + "_client", fRIEarFile);
						}
						else
						{
							packager.setClientRuntimeDeploymentInfo(standaloneComponent, 
							new File(sRunTimeFiles[ii]), sAppName + "_client");	
						}
					}
					else
					{
						//if not an ear, then we must have a jar/war
						if(fRIEarFile != null)
						{
							packager.setClientRuntimeDeploymentInfo(toSet, 
							new File(sRunTimeFiles[ii]), sAppName + "_client", fRIEarFile);
						}
						else
						{
							packager.setClientRuntimeDeploymentInfo(standaloneComponent, 
							new File(sRunTimeFiles[ii]), sAppName + "_client");

						}
					}*/
				}
				else
				{
					//if not an ear, then we must have a jar/war
					if(fRIEarFile != null)
					{
						packager.setRuntimeDeploymentInfo(toSet, 
							new File(sRunTimeFiles[ii]), fRIEarFile);
					}
					else
					{
						packager.setRuntimeDeploymentInfo(standaloneComponent, 
							new File(sRunTimeFiles[ii]));	
					}
				}
			}
			catch(Throwable t)
			{
			    Log.print("*****************??????");
				Log.print(t.getMessage());
				Log.print("*****************??????");
				System.out.println("*****************??????");
				t.printStackTrace(Log);
				t.printStackTrace();
				System.out.println("Exception = " + t.getMessage());
				System.out.println("*****************??????");
				Log.print("Warning:  Failed to set runtime info in " +
					sRunTimeFiles[ii] + " for application, " + sApp);
				throw new CTSDeploymentException(t.getMessage());   
			}		
		}
		return sRunTimeFiles;
	}
	
	/**
	 * 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
	{
		Application toDeploy = null;
		String bGenerateSQL = info.getProperty("generateSQL");
		String sDeployHost = info.getProperty("deployment_host");
		String sApp = info.getEarFile();
		String[] sRunTimeFileArray = info.getRuntimeFiles();
		String sRunTimeFiles = "";
		File clientClassesFile = null;
		//URL[] returnURL = new URL[1];
		String sCTSDeploymentDir = sApp.substring(0,
			sApp.lastIndexOf(File.separator)+1) + "cts_dep";	
		File ctsDeployDir = new File(sCTSDeploymentDir);
		boolean bDeployFlag = true;
		sRIAppName = sHarnessTempDir + File.separator + (new File(sApp)).getName();
		standaloneComponent = null;
		
		if(!ctsDeployDir.exists())
		{
			if(!ctsDeployDir.mkdir())
			{
				throw new CTSDeploymentException("Failed to create the cts deployment working directory:  " + sCTSDeploymentDir);
			}
		}

		
		// Set sRunTimeFiles string with contents of runtime files 
		// array:
		if( sRunTimeFileArray != null ) {
		    for( int i = 0; i < sRunTimeFileArray.length; i++ ) {
		        sRunTimeFiles += sRunTimeFileArray[i] + " ";
		    }
		}
		
		//create client jar based on app name
		String sClientJarName = 
				sApp.substring(sApp.lastIndexOf(File.separator)+1,
				sApp.lastIndexOf(".")+1) + "jar";
				
		//set RI specific runtime info for our app	
		if(sRunTimeFiles != null)
			sRunTimeFileArray = setRunTimeInfo(sRunTimeFiles, sApp);
	
		//create the clientclasses directory (if it doesn't exist)
		clientClassesFile = 
			new File(sCTSDeploymentDir + File.separator + sClientJarName);
					
		Log.println("sApp = " + sApp);
		
		if(bGenerateSQL != null)
		{
			if(bGenerateSQL.equals("true"))
			{
				try
				{
					if(standaloneComponent == null)
					{
						deployTool.doGenerateSQL(sRIAppName, sDeployHost, false);
					}
					else
					{
						deployTool.doGenerateSQL(standaloneComponent.getApplicationArchivist().getApplicationFile().getName(), sDeployHost, false);
					}
				}
				catch(Exception e)
				{
					System.err.println("Failed to generate SQL:  " + 
						e.getMessage());
					e.printStackTrace();
					throw new CTSDeploymentException(e.getMessage()); 
				}
			}
		}
		
		try 
		{
			if(standaloneComponent == null)
			{
				fRIEarFile = new File(sRIAppName);
				toDeploy = ApplicationArchivist.open(fRIEarFile);
				TestUtil.logHarness("Just opened app:  " + fRIEarFile.getPath());
			}
			else
			{
				toDeploy = standaloneComponent;
			}
		} 
		catch (Exception ioe) 
		{
			System.err.println(ioe.getMessage());
			ioe.printStackTrace(Log);
			throw new CTSDeploymentException(ioe.getMessage());
		}

		try 
		{
			if (deployTool.getServerManager().isInstalled(toDeploy.getName(),
				 sDeployHost)) 
			{
				//check here for developer flag to disable undeploy/redeploy
				bDeployFlag = (Boolean.valueOf(propMgr.getProperty("undeploy_redeploy_apps"))).booleanValue();
				
				if(!bDeployFlag)
				{
					TestUtil.logHarness("Leaving current app deployed - will not redeploy app");
					return clientClassesFile.toString();
				}
				else
				{
					System.err.println(toDeploy.getName() + " must be uninstalled before it can be re-deployed.");
					throw new CTSDeploymentException(toDeploy.getName() + " must be uninstalled before it can be re-deployed.");
				}	
			}

			
				//deployTool.deploy(toDeploy.getName(), fRIEarFile, sDeployHost, null, clientClassesFile);
				
				//use the old RI api still because we're so close to FCS
				deployTool.deploy(toDeploy, sDeployHost, null, clientClassesFile);			
		} 
		catch (MalformedURLException t) 
		{
			System.err.println(t.getMessage());
			t.printStackTrace(Log);
			throw new CTSDeploymentException(t.getMessage());
		}
		catch (Exception t) 
		{
			System.err.println(t.getMessage());
			t.printStackTrace(Log);
			throw new CTSDeploymentException(t.getMessage());
		}
		
		return clientClassesFile.toString();
	}

	/**
	 * 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;
		String sDeployHost = p.getProperty("deployment_host");
		String sApp = p.getProperty("ear_file");
		Application toDeploy = null;
		sRIAppName = sHarnessTempDir + File.separator + (new File(sApp)).getName();
		
		TestUtil.logHarnessDebug("Deployhost = " + sDeployHost);
		
		try
		{
			TestUtil.logHarnessDebug("isdeploy called with: " + sApp);

			File fApp = new File(sRIAppName);
			
			//if the ri ear does not exist, use the portable ear
			if(!fApp.exists())
				fApp = new File(sApp);
				
			if(sApp.endsWith(".ear"))
			{
				TestUtil.logHarnessDebug("isdeploy called with ear: " + sApp);

				toDeploy = ApplicationArchivist.open(fApp);
				htDeployedApps.put(fApp.getName(), toDeploy.getName());
			}
			else
			{
				TestUtil.logHarnessDebug("isdeploy called with jar/war: " + sApp);
				
				File jar = new File(sApp);
				
				//assume we have a jar or war
				//check to make sure that RI doesn't modify the original file
				toDeploy = deployTool.deployStandaloneModule(jar);
				htDeployedApps.put(jar.getName(), toDeploy.getName());
			}
			
		}
		catch(Exception ioe)
		{
			Log.println("Failed while checking if app was already deployed:  "
							+ sRIAppName);
			ioe.printStackTrace(Log);
			throw new CTSDeploymentException(ioe.getMessage());
		}
		
		try 
		{
			if(deployTool.getServerManager().isInstalled(toDeploy.getName(),
					 sDeployHost)) 
			{
				bIsInstalled = true;	 
		    }
		}
		catch(Exception t) 
		{
			Log.println("Failed while checking if app was already deployed:  "
							+ sApp); 
			throw new CTSDeploymentException(t.getMessage());
		}
		
		TestUtil.logHarnessDebug("isdeploy returning : " + bIsInstalled);

		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
	{
		String sDeployHost = p.getProperty("deployment_host");
		String sApp = p.getProperty("ear_file");
		String sAppName = (new File(sApp)).getName();
		String sCTSDeploymentDir = sApp.substring(0,
			sApp.lastIndexOf(File.separator)+1) + "cts_dep";	
		File ctsDeployDir = new File(sCTSDeploymentDir);
		boolean bDeployFlag = true;
		sRIAppName = sHarnessTempDir + File.separator + (new File(sApp)).getName();
		
		//remove any client jars if they exist
		if(ctsDeployDir.exists())
		{
			File[] fclientFiles = ctsDeployDir.listFiles();
			for(int ii = 0; ii < fclientFiles.length; ii++)
			{
				if(fclientFiles[ii].isFile())
				{
					if(fclientFiles[ii].delete())
						Log.println("Old Client Jar file, " +
							fclientFiles[ii].getName() +
							", was successfully deleted.");
					else
						Log.println("Failed to delete old Client Jar file, " + 
							fclientFiles[ii].getName() + ".");	
				}
			}
		}
		
		sAppName = (String)htDeployedApps.get(sAppName);
		TestUtil.logHarnessDebug("About to try to undeploy Appname:  " + sAppName);
		//sAppName = sAppName.substring(0, sAppName.lastIndexOf("."));
		//Log.println("sAppName = "+ sAppName);
		//Log.println("sDeployHost = "+ sDeployHost);
		
		try 
		{
			//check here for developer flag to disable undeploy/redeploy
			bDeployFlag = (Boolean.valueOf(propMgr.getProperty("undeploy_redeploy_apps"))).booleanValue();
			
			if(!bDeployFlag)
			{
				TestUtil.logHarness("Leaving current app deployed - app will not be undeployed");
				return;
			}
			
			if(deployTool.getServerManager().isInstalled(sAppName, sDeployHost))
			{
				deployTool.getServerManager().undeployApplication(sAppName, sDeployHost);
				Log.print(sAppName + " was uninstalled from " + sDeployHost);
				TestUtil.logHarness(sAppName + " was uninstalled from " + sDeployHost);
		    }
			else
			{
				TestUtil.logHarnessDebug("Undeploy called with jar/war: " + sApp);
				TestUtil.logHarnessDebug(sAppName + " is not deployed on " + sDeployHost);
				Log.print(sAppName + " is not deployed on " + sDeployHost);
				throw new CTSDeploymentException(sAppName +
					" is not deployed on " + sDeployHost);
		    }
			
			//remove temporary ri .ear file
			if(sApp.endsWith(".ear"))
				(new File(sRIAppName)).delete();
		} 
		catch(Exception e)
		{
			Log.print("Undeployment failed!");
			e.printStackTrace(Log);
			e.printStackTrace();
			Log.print(e.getMessage());
			throw new CTSDeploymentException(e.getMessage());
		}
		return;
	}
	
	/**
	 * This method is called to deploy a connector (.rar file) to Sun's
	 * J2EE reference implementation.  We extract such info as deployment_host 
	 * and rar_file from these props.  The following properties are available 
	 * for this method's use:  
	 * 
	 * rar_file - the fully qualified connector file (.rar file) 
	 * deployment_host - the host name of the machine to deploy it to 
	 *
	 * @param	p	Properties specific to the currently running test
	 */

	public void deployConnector(Properties p) throws CTSDeploymentException {
	  String sDeployHost = p.getProperty("deployment_host");
	  String sRarFileName = p.getProperty("rar_file");
	  
	  try {
	    
	    CTSPropertyManager propMgr = CTSPropertyManager.getCTSPropertyManager();
	    
	    String sRarPropName = sRarFileName.substring(sRarFileName.lastIndexOf(File.separator) + 1, sRarFileName.lastIndexOf(".rar"));
	    String sRarName = sRarFileName.substring(sRarFileName.lastIndexOf(File.separator) + 1);
	    String connectionURL = propMgr.getProperty("connector_connectionURL");
	    String xaDataSource = propMgr.getProperty("porting.cts.connector.xaDataSource.class");
	    String serverName = propMgr.getProperty("connector_serverName");
	    String portNumber = propMgr.getProperty("connector_portNumber");
	    String username = propMgr.getProperty("user1");
	    String password = propMgr.getProperty("password1");
	    TestUtil.logHarnessDebug("Connection URL: " + connectionURL);
	    
	    // First, deploy the adapter using the deploytool
	    String args[] = new String[3];
	    args[0] = "-deployConnector";
	    args[1] = sRarFileName;
	    args[2] = sDeployHost;
	    
	    TestUtil.logHarnessDebug("deployConnector: calling deploytool: " + args[0] + " " + args[1] + " " + args[2]);
	    com.sun.enterprise.tools.deployment.main.Main.main(args);
	    TestUtil.logHarness("Connector:  " + sRarFileName + " has been deployed");
	    
	    // Second, create connection factories using admintool
	    // XA rar files have XADataSource, ServerName, PortNumber overridden, and tx files
	    // have ConnectionURL overridden.
	    String sJNDIName = propMgr.getProperty(sRarPropName);
	    sJNDIName = sJNDIName.substring(("java:comp/").length());
	    String ra_args[] = new String[7];
	    ra_args[0] = "-addConnectorFactory";
	    ra_args[1] = sJNDIName;
	    ra_args[2] = sRarName;
	    ra_args[3] = username;
	    ra_args[4] = password;
	    ra_args[5] = "-props";
	    ra_args[6] = "ConnectionURL=" + connectionURL;
	    
	    if (sRarName.indexOf("xa") > 0) {
	      String xara_args[] = new String[9];
	      xara_args[0] = "-addConnectorFactory";
	      xara_args[1] = sJNDIName;
	      xara_args[2] = sRarName;
	      xara_args[3] = username;
	      xara_args[4] = password;
	      xara_args[5] = "-props";
	      xara_args[6] = "XADataSourceName=" + xaDataSource;
	      xara_args[7] = "ServerName=" + serverName;
	      xara_args[8] = "PortNumber=" + portNumber;
	      
	      TestUtil.logHarnessDebug("deployConnector: calling admintool: " + xara_args[0] + " " + xara_args[1] + " " + xara_args[2] + " " + xara_args[3] + " " + xara_args[4] + " " + xara_args[5] + " " + xara_args[6] + " " + xara_args[7] + " " + xara_args[8]);
	      com.sun.enterprise.tools.admin.AdminTool.main(xara_args);
	      TestUtil.logHarness("XA Connection Factory created with JNDI name:  " + xara_args[1]);

	    } else {
	      
	      TestUtil.logHarnessDebug("deployConnector: calling admintool: " + ra_args[0] + " " + ra_args[1] + " " + ra_args[2] + " " + ra_args[3] + " " + ra_args[4] + " " + ra_args[5] + " " + ra_args[6]);
	      com.sun.enterprise.tools.admin.AdminTool.main(ra_args);
	      TestUtil.logHarness("Connection Factory created with JNDI name:  " + ra_args[1]);
	      
	      
	    }
	    
	  }
	  catch (Exception e){
	    TestUtil.logHarness("Deployment of Connector:  " + sRarFileName + " failed");
	    e.printStackTrace();
	    throw new CTSDeploymentException(e.getMessage());
	  }
	}
	
	/**
	 * This method is called to undeploy a connector (.rar file) from Sun's
	 * J2EE reference implementation.  We extract such info as deployment_host 
	 * and rar_file from these props.  The following properties are available 
	 * for this method's use:  
	 * 
	 * rar_file - the fully qualified connector file (.rar file) 
	 * deployment_host - the host name of the machine to undeploy it from 
	 *
	 * @param	p	Properties specific to the currently running test
	 */
	public void undeployConnector(Properties p) throws CTSDeploymentException
	{
		String sDeployHost = p.getProperty("deployment_host");
		String sRarFileName = p.getProperty("rar_file");
		
		try
		{
			String sRarName = sRarFileName.substring(sRarFileName.lastIndexOf(File.separator) + 1);
			String sRarPropName = sRarFileName.substring(sRarFileName.lastIndexOf(File.separator) + 1, sRarFileName.lastIndexOf(".rar"));
			
			CTSPropertyManager propMgr = CTSPropertyManager.getCTSPropertyManager();
			
			//remove connection factories
			String sJNDIName = propMgr.getProperty(sRarPropName);
			sJNDIName = sJNDIName.substring(("java:comp/").length());
			//remove connection factory
			String args2[] = new String[3];
			args2[0] = "-removeConnectionFactory";
			args2[1] = sJNDIName;
			args2[2] = sDeployHost;
			TestUtil.logHarnessDebug("In SunRIDeploy: undeployConnector: calling with args: " + args2[0] + " " + args2[1] + " " + args2[2]);
			//com.sun.enterprise.tools.deployment.Main.main(args);
			com.sun.enterprise.tools.deployment.main.Main.main(args2);
			TestUtil.logHarness("Connection Factory with JNDI name:  " + args2[2] + " was removed.");
			
			TestUtil.logHarnessDebug("In SunRIDeploy: undeployConnector");
			String args[] = new String[3];
			args[0] = "-undeployConnector";
			args[1] = sRarName;
			args[2] = sDeployHost;
			TestUtil.logHarnessDebug("In SunRIDeploy: undeployConnector: calling with args: " + args[0] + " " + args[1] + " " + args[2]);
			//com.sun.enterprise.tools.deployment.Main.main(args);
			com.sun.enterprise.tools.deployment.main.Main.main(args);
			TestUtil.logHarnessDebug("Connector:  " + sRarFileName + " has been deployed");
		}
		catch (Exception e)
		{
			TestUtil.logHarness("Undeployment of Connector:  " + sRarFileName + " failed");
			e.printStackTrace();
			throw new CTSDeploymentException(e.getMessage());
		}
	}
	
	/**
	 * This method is called to check to see if a given connector (.rar file) 
	 * is deployed on Sun's J2EE reference implementation.  We extract such info
	 * as deployment_host and rar_file from these props.  The following 
	 * properties are available for this method's use:  
	 * 
	 * rar_file - the fully qualified connector file (.rar file) 
	 * deployment_host - the host name of the machine to deploy it to 
	 *
	 * @param	p	Properties specific to the currently running test
	 * @return  	True if the app is deployed.  False if not.
	 */
	public boolean isConnectorDeployed(Properties p) throws CTSDeploymentException
	{
		String sDeployHost = p.getProperty("deployment_host");
		String sRarFileName = p.getProperty("rar_file");
        boolean bIsInstalled = false;
		            
		try
		{
			TestUtil.logHarnessDebug("In SunRIDeploy: isConnectorDeployed");
			JarInstaller backend = deployTool.getServerManager().
									getServerForName(sDeployHost);
			ConnectorInfo v = backend.listConnectors();
			
			int size = v.connectors.length;
            	
			for (int ii = 0; ii < size; ii++)
			{
				String msg = "connector.info:  " + 
							(String)v.connectors[ii];
                TestUtil.logHarnessDebug(msg);
				
				String sRarName = sRarFileName.substring(sRarFileName.lastIndexOf(File.separator) + 1);
				TestUtil.logHarnessDebug("In SunRIDeploy: isConnectorDeployed :  sRarName = " + sRarName);
				if( ( (String)v.connectors[ii]).equals(sRarName) )
				{
					bIsInstalled = true;
					TestUtil.logHarnessDebug("Found connector!");
					break;	
				}
            }
			TestUtil.logHarnessDebug("In SunRIDeploy: isConnectorDeployed:  returning" + bIsInstalled);
			return bIsInstalled;							
		}
		catch (Exception e)
		{
			TestUtil.logHarness("Checking if connector:  " + sRarFileName + " is deployed - failed");
			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)
	{
		String executeArgs = p.getProperty("executeArgs");
		String sApp = p.getProperty("ear_file");
		String sEarName = sApp.substring(sApp.lastIndexOf(File.separator)+1);
		sRIAppName = sHarnessTempDir + File.separator + 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)
	{
		htJNDIRefs = new Hashtable();
		
		if((System.getProperty("cts.harness.debug", "false")).equals("true"))
		{
			for(int ii = 0; ii < infoArray.length; ii++)
			{
				TestUtil.logHarnessDebug("**** ii=" + ii + " ***********************************************************************************");
				TestUtil.logHarnessDebug(new RuntimeInfo().getRuntimeInfo(infoArray[ii]));
				TestUtil.logHarnessDebug("***************************************************************************************");

                           
                           Vector resources = 
                              infoArray[ii].getAppClientResources();
                           for(int i = 0; i< resources.size(); i++) {
                              DeploymentInfo.AppClient appResource =
                                (DeploymentInfo.AppClient)
                                resources.elementAt(i);

                              Hashtable jndiChanges =
                                 checkJNDINames(appResource.ejbs);
                              if(!jndiChanges.isEmpty()) {
                                    htJNDIRefs.putAll(jndiChanges);
                              }
                           }

                           /* Web Container */
                           resources = infoArray[ii].getWebResources();
                           for(int i = 0; i< resources.size(); i++) {
                              DeploymentInfo.WebResource webResource =
                                (DeploymentInfo.WebResource)
                                resources.elementAt(i);

                              Hashtable jndiChanges =
                                 checkJNDINames(webResource.ejbs);
                              if(!jndiChanges.isEmpty()) {
                                    htJNDIRefs.putAll(jndiChanges);
                              }
                           }

                           /* EJB Container */
                           Vector ejbJars = infoArray[ii].getEjbJars();
                           for(int y= 0; y< ejbJars.size(); y++) {
                              DeploymentInfo.EJBJar ejbJar =
                                 (DeploymentInfo.EJBJar)ejbJars.elementAt(y);
                              resources = ejbJar.getEjbResources();
                              for(int i = 0; i< resources.size(); i++) {
                                 DeploymentInfo.Ejb ejbResource =
                                   (DeploymentInfo.Ejb) resources.elementAt(i);

                                 Hashtable jndiChanges =
                                    checkJNDINames(ejbResource.ejbs);
                                 if(!jndiChanges.isEmpty()) {
                                       htJNDIRefs.putAll(jndiChanges);
                                 }
                              }
                           }
                           
			}
		}
		
               
		TestUtil.logHarnessDebug("*****Returning the following Hashtable from SunRIDeployment.getInteropJNDINames*****");
			TestUtil.logHarnessDebug("***************************************************************************************");
			
		for (Enumeration e = htJNDIRefs.keys() ; e.hasMoreElements() ;)
		{
 			String sKey = (String)e.nextElement();
			
			TestUtil.logHarnessDebug("Original value:  " + sKey);
			TestUtil.logHarnessDebug("Modified value:  " + (String)htJNDIRefs.get(sKey));	
		}
		TestUtil.logHarnessDebug("***************************************************************************************");
		
		return htJNDIRefs;
	}

	private Hashtable checkJNDINames(Vector ejbRefs) {
           Hashtable jndiChanges = new Hashtable();
           for(int j = 0; j<ejbRefs.size(); j++) {
              DeploymentInfo.EjbReference ref = (DeploymentInfo.EjbReference)
                 ejbRefs.elementAt(j);
              if(ref.jndi.startsWith("corbaname")) {
                 int endPos = ref.jndi.lastIndexOf("#");
                 //String newJNDI = ref.jndi.substring(0,endPos) + "#myfoo/" + 
                 String newJNDI = ref.jndi.substring(0,endPos) + "#" + 
                    ref.jndi.substring(endPos+1);
                 jndiChanges.put(ref.jndi, newJNDI);
		TestUtil.logHarnessDebug("********\nold JNDI name" + ref.jndi
                   + ", new JNDI name" + newJNDI);
              }
           }
           return jndiChanges;
        }

}
