package tests.ProviderClientTest;

import org.snia.wbem.client.CIMClient;
import org.snia.wbem.cim.CIMException;
import org.snia.wbem.cim.*;
import org.snia.wbemcmd.xml.XMLNameSpace;
import org.snia.wbemcmd.cimom.CIMOMHandleXML;
import org.snia.wbemcmd.*;
import org.snia.wbemcmd.utils.SimpleEnum;
import javax.swing.*;

public class XMLConnect
{
	int localPort;
	String localManagedSystem = null;
	CIMClient cc = null;
	String nameSpace = null;
	String protocol = null;

	public XMLConnect()
	{
	}

	public void connectSystem()
	{
		String className = null;
		CIMNameSpace clientNameSpace;
 		String attachName;

		//New Pegasus from bapu
		System.out.println("Connecting to: "+localManagedSystem);
		try
		{

                       if ( protocol != null && protocol.equalsIgnoreCase("ssl") )
			  attachName="https://"+localManagedSystem+":"+localPort;
                       else
			  attachName="http://"+localManagedSystem+":"+localPort;

			clientNameSpace = new CIMNameSpace(attachName, nameSpace);
			System.out.println("attachName= "+attachName);
			//
			//these are constants....do not change
			//
			//String urlText = "/cimom";
			//CIMOMHandleXML.portNumber=localPort;
			//CIMOMHandleXML.url=urlText;
			cc = new CIMClient(clientNameSpace, "guest", "guest", CIMClient.XML);
			//System.out.println("Got CIMClient:"+cc);

		}
		catch (CIMException e)
		{
			//System.err.println("Failed to access CIMOM: "+e);
			//System.err.println("Exiting...");
			System.out.println("Error: "+e);
			//System.exit(1);
		}

		//old Pegasus
		/*System.out.println("Connecting to: "+localManagedSystem);
		try
		{
			clientNameSpace = new CIMNameSpace(localManagedSystem, nameSpace);
			//
			//these are constants....do not change
			//
			String urlText = "/cimom";
			CIMOMHandleXML.portNumber=localPort;
			CIMOMHandleXML.url=urlText;
			cc = new CIMClient(clientNameSpace, null, null, CIMClient.XML);
			//System.out.println("Got CIMClient:"+cc);

		}
		catch (CIMException e)
		{
			//System.err.println("Failed to access CIMOM: "+e);
			//System.err.println("Exiting...");
			System.out.println("Error: "+e);
			//System.exit(1);
		}*/
	}

	public void changeNamespace(String ns)
	{
		nameSpace = ns;
		connectSystem();
		//return(cc);
	}


	public void disconnect()
	{
		try
		{
			cc.close();
		}
		catch (CIMException ex)
		{
			System.out.println("Error closing connection, exiting program");
			//System.exit(1);
		}
	}

	public void setPort(int port)
	{
		localPort = port;
		//System.out.println("Local Port Set: "+localPort);
	}

	public int getPort()
	{
		return(localPort);
	}

	public void setManagedSystem(String managedSystem)
	{
		localManagedSystem = managedSystem;
		//System.out.println("Managed System Set: "+localManagedSystem);
	}

	public String getManagedSystem()
	{
		return (localManagedSystem);
	}

	public CIMClient getConnectHandle()
	{
		return(cc);
	}

	public void setNamespace(String ns)
	{
		nameSpace = ns;
	}

	public String getNamespace()
	{
		return(nameSpace);
	}

	public void setProtocol(String proto)
	{
		protocol = proto;
	}
	public String getProtocol()
	{
		return(protocol);
	}

}
