package tests.ClientUnitTests;

import java.util.Enumeration;
import java.util.Vector;
import java.rmi.*;
import java.rmi.server.*;
import java.rmi.Naming;
import java.rmi.Remote;
import java.net.*;
import org.snia.wbemcmd.utils.SimpleEnum;
import org.snia.wbem.client.CIMClient;
import org.snia.wbem.cim.CIMNameSpace;
import org.snia.wbem.cim.CIMObjectPath;
import org.snia.wbem.cim.CIMClass;
import org.snia.wbem.cim.CIMProperty;
import org.snia.wbem.cim.CIMInstance;
import org.snia.wbem.cim.CIMValue;
import org.snia.wbem.cim.CIMQualifierType;
import org.snia.wbem.cim.CIMException;
import org.snia.wbem.cim.CIMException;

class Test01 {

    private void runTest(String [] args)
        throws CIMException 
    {

        CIMProperty valProp;
        CIMInstance currentInstance;
        CIMObjectPath currentInstancePath;
        CIMValue value = null;
        CIMNameSpace clientNameSpace;
        String hostName = "localhost";          // host for cimom
        String nameSpace = "root/cimv2";        // namespace
        CIMClient cc=null;
        int portNo=CIMNameSpace.DEFAULT_PORT;
        String hostURL;

        if (args.length==3) {
            hostName=args[0];
            nameSpace=args[1];
            portNo=Integer.parseInt(args[2]);
        } else if (args.length!=0) {
            System.err.println("SetClass [hostname namespace portNo]");
            System.exit(1);
        }

        System.err.println("Connecting to "+hostName+
                            " for namespace "+nameSpace);
        hostURL="http://"+hostName+":"+portNo;
        try {
            clientNameSpace = new CIMNameSpace(hostURL, nameSpace);
            cc = new CIMClient(clientNameSpace, "guest", "guest", CIMClient.XML);
        } catch (CIMException e) {
            System.err.println("Failed to access CIMOM: "+e);
            System.exit(1);
        }

        CIMNameSpace ns = cc.getNameSpace();
        System.out.println("----------------------------------------------------------------");
        System.out.println("Host      = " + ns.getHost());
        System.out.println("NameSpace = " + ns.getNameSpace());
        System.out.println("Protocol  = " + ns.getProtocol());
        System.out.println("Port      = " + ns.getPortNumber());
        System.out.println("Class     = " + ns.getClass());
        System.out.println("Host URL  = " + ns.getHostURL());
        System.out.println("----------------------------------------------------------------");
        System.err.println("Client created");

/*
        CIMObjectPath path = new CIMObjectPath("HP_NParPowerCoolingDomain");
        //CIMObjectPath path = new CIMObjectPath("PG_OperatingSystem");
        path.addKey("ID", new CIMValue( (Object)new Integer(0)));
        currentInstance = cc.getInstance(path, false);

        valProp = currentInstance.getProperty("SlotCount");
        value = valProp.getValue();
        if (value != null && value.getValue() != null) {
            System.out.println(value.toString());
        }
        else {
            System.out.println("null");
        }

*/
        // works..
        Enumeration instances = cc.enumInstances(new CIMObjectPath("PG_UnixProcess", nameSpace), CIMClient.DEEP);
        while( instances.hasMoreElements() ) {
            Object obj = instances.nextElement();
            System.out.println("Getting properties for " + obj.toString());
            currentInstancePath=(CIMObjectPath)obj;
            currentInstance=cc.getInstance(currentInstancePath,false);

            Vector props = currentInstance.getProperties();
            for( int i = 0; i < props.size(); ++i) {
                valProp = (CIMProperty)props.elementAt(i);
                value = valProp.getValue();
                if (value != null && value.getValue() != null) {
                    System.out.println(value.toString());
                }
                else {
                    System.out.println("null");
                }
            }
        }

        if( cc != null ) {
            cc.close();
        }
    }

    public static void main(String [] args) {
        Test01 t = new Test01();
        try {
            t.runTest(args);
        }
        catch (CIMException e) {
            e.printStackTrace();
            System.exit(1);
        }
    
        System.out.println("It Passed!");
    }
}
