Sun Microsystems, Inc.
spacerspacer
spacer www.sun.com docs.sun.com |
spacer
black dot
 
 
3.  Writing a Client Program Performing Basic Client Operations Enumerating Objects Enumerating Objects  Previous   Contents   Next 
   
 

The following example program performs a deep and shallow (deep=false) enumeration of classes and instances. The localOnly flag returns the contents of the classes and instances instead of the names of the classes and instances.

import java.rmi.*;
import java.util.Enumeration;

import javax.wbem.client.CIMClient;
import javax.wbem.cim.CIMClass;
import javax.wbem.cim.CIMException;
import javax.wbem.cim.CIMInstance;
import javax.wbem.cim.CIMNameSpace;
import javax.wbem.cim.CIMObjectPath;

import javax.wbem.client.UserPrincipal;
import javax.wbem.client.PasswordCredential;


/** 
 * This example enumerates classes and instances. It does deep and 
 * shallow enumerations on a class that is passed from the command line
 */
public class ClientEnum {

    public static void main(String args[]) throws CIMException {
        CIMClient cc = null;
        CIMObjectPath cop = null;
        if (args.length < 4) {
            System.out.println("Usage: ClientEnum host user passwd " + 
                               "classname");
            System.exit(1);
   }
   try {
       CIMNameSpace cns = new CIMNameSpace(args[0]);
       UserPrincipal up = new UserPrincipal(args[1]);
       PasswordCredential pc = new PasswordCredential(args[2]);
       cc = new CIMClient(cns, up, pc);


       // Get the class name from the command line    
       cop = new CIMObjectPath(args[3]);    
       // Do a deep enumeration of the class
       Enumeration e = cc.enumerateClasses(cop, true, true, true, 
                                           true);
       // Will print out all the subclasses of the class.
       while (e.hasMoreElements()) {
           System.out.println(e.nextElement());
       }
       System.out.println("+++++");
       // Do a shallow enumeration of the class
       e = cc.enumerateClasses(cop, false, true, true, true);
       // Will print out the first level subclasses.
       while (e.hasMoreElements()) {
           System.out.println(e.nextElement());
       }
       System.out.println("+++++");
       // Do a deep enumeration of the instances of the class
       e = cc.enumerateInstances(cop, false, true, true, true, null);
       // Will print out all the instances of the class and its 
       // subclasses.
       while (e.hasMoreElements()) {
           System.out.println(e.nextElement());
       }
       System.out.println("+++++");
       // Do a shallow enumeration of the instances of the class
       e = cc.enumerateInstances(cop, false, false, true, true, null);
       // Will print out all the instances of the class.
       while (e.hasMoreElements()) {
           System.out.println(e.nextElement());
       }
       System.out.println("+++++");

       e = cc.enumerateInstanceNames(cop);
       while (e.hasMoreElements()) {
           System.out.println(e.nextElement());
       }
       System.out.println("+++++");

       e = cc.enumerateInstanceNames(cop);
       while (e.hasMoreElements()) {
           CIMObjectPath opInstance = (CIMObjectPath)e.nextElement();
           CIMInstance ci = cc.getInstance(opInstance, false, 
                                           true, true, null);
           System.out.println(ci); 
       }
       System.out.println("+++++");

  }
  catch (Exception e) {
      System.out.println("Exception: "+e);
  }

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


Example 3-12 Enumerating Class Names

The following example program returns a list of class names and subclass names.

...
{
    /* Creates a CIMObjectPath object and initializes it 
    with the name of the CIM class to be enumerated (myclass). */
    CIMObjectPath cop = new CIMObjectPath(myclass); 
    
    /* This enumeration contains the names of the classes and subclasses
    in the enumerated class. */
    Enumeration e = cc.enumerateClassNames(cop, true);
}
... 



Example 3-13 Enumerating Namespaces

This example program uses the enumNameSpace method in the CIMClient class to print the names of the namespace and all the namespaces contained within the namespace.

 
import java.rmi.*;
import java.util.Enumeration;

import javax.wbem.cim.CIMClass;
import javax.wbem.cim.CIMException;
import javax.wbem.cim.CIMInstance;
import javax.wbem.cim.CIMNameSpace;
import javax.wbem.cim.CIMObjectPath;

import javax.wbem.client.CIMClient;
import javax.wbem.client.PasswordCredential;
import javax.wbem.client.UserPrincipal;


/** 
 *
 */
public class EnumNameSpace {
    public static void main(String args[]) throws CIMException {
        CIMClient cc = null;
        // if not four arguments, show usage and exit
        if (args.length < 4) {
            System.out.println("Usage: EnumNameSpace host username " +
                               "password namespace");
            System.exit(1);
            }
        try {
            // args[0] contains the hostname. We create a CIMNameSpace 
            // (cns) pointing to the specified namespace on the 
            // specified host
            CIMNameSpace cns = new CIMNameSpace(args[0], "");

           // args[1] and args[2] contain the username and password.
           // We create a UserPrincipal (up) using the username and
           // a PasswordCredential using the password.
           UserPrincipal up = new UserPrincipal(args[1]);
           PasswordCredential pc = new PasswordCredential(args[2]);

           // Connect to the CIM Object Manager and pass it the
           // CIMNameSpace, UserPrincipal and PasswordCredential objects
           // we created. 
           cc = new CIMClient(cns, up, pc);

           // Use the namespace (args[3]) to create a CIMObjectPath
           CIMObjectPath cop = new CIMObjectPath("", args[3]);

           // Enumerate the namespace
           Enumeration e = cc.enumNameSpace(cop);
           while (e.hasMoreElements()) {
               System.out.println((CIMObjectPath)e.nextElement());
           } // end while 

           } catch (Exception e) {
              // is we have an exception, catch it and print it out.
              System.out.println("Exception: "+ e);
           } // end catch

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


 
 
 
  Previous   Contents   Next