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

Retrieving Class Definitions

The getClass method gets a CIM class. When a class is created, it inherits the methods and properties of the class from which it is derived, and all parent classes in the class hierarchy. The getClass method takes the localOnly Boolean argument.

  • If localOnly is true, getClass returns only non-inherited properties and methods.

  • If localOnly is false, getClass returns all properties in the class.


Example 3-16 Retrieving a Class Definition

This example uses the following methods to retrieve a class definition:

  • CIMNameSpace - Create a new namespace

  • CIMClient - Create a new client connection to the CIM Object Manager

  • CIMObjectPath - Create an object path, which is an object to contain the name of the class to retrieve

  • getClass - Retrieve the class from the CIM Object Manager

import java.rmi.*;
import javax.wbem.client.CIMClient;
import javax.wbem.cim.CIMInstance;
import javax.wbem.cim.CIMValue;
import javax.wbem.cim.CIMProperty;
import javax.wbem.cim.CIMNameSpace;
import javax.wbem.cim.CIMObjectPath;
import javax.wbem.cim.CIMClass;
import javax.wbem.cim.CIMException;
import java.util.Enumeration;
/**
 * Gets the class specified in the command line. Works in the default
 * namespace root\cimv2.
 */
public class GetClass {
    public static void main(String args[]) throws CIMException {
      CIMClient cc = null;
      try {
         CIMNameSpace cns = new CIMNameSpace(args[0]);
         UserPrincipal up = new UserPrincipal("root");
         PasswordCredential pc = new PasswordCredential("root_password");
         cc = new CIMClient(cns);
         CIMObjectPath cop = new CIMObjectPath(args[1]);
             // Returns only the methods and properties that 
             // are local to the specified class (localOnly is true).
         cc.getClass(cop, true);
      } catch (Exception e) {
         System.out.println("Exception: "+e);
      }
      if(cc != null) {
         cc.close();
        }
    }
}


Handling Exceptions

Each CIMClient method throws a CIMException, or error condition. The CIM Object Manager uses Java exception handling and creates a hierarchy of WBEM-specific exceptions. The CIMException class is the base class for CIM exceptions. All other CIM exception classes extend from the CIMException class.

Each class of CIM exceptions defines a particular type of error condition that the API code handles. CIMException has methods to retrieve error codes and parameters relating to the exception. Refer to file:/usr/sadm/lib/wbem/doc/index.html for more information on the CIMException class.

Creating a Namespace

The Solaris WBEM SDK installation compiles the standard CIM Managed Object Format (MOF) files into the default namespaces. If you create a new namespace, you must compile the appropriate CIM .mof files into the new namespace before you create objects in that namespace. For example, if you plan to create classes that use the standard CIM elements, compile the CIM Core Schema into the namespace. If you plan to create classes that extend the CIM Application Schema, compile the CIM Application into the namespace.


Example 3-17 Creating a Namespace

This example uses a two-step process to create a namespace within an existing namespace:

  1. When the namespace is created, the CIMNameSpace method constructs a namespace object that contains the parameters to be passed to the CIM Object Manager.

  2. The CIMClient class connects to the CIM Object Manager and passes the namespace object. The CIM Object Manager creates the namespace, using the parameters contained in the namespace object.

{
    ...
    /* Creates a namespace object on the client, which stores parameters 
    passed to it from the command line. args[0] contains the host 
    name (for example, myhost); args[1] contains the 
    parent namespace (for example, the toplevel directory.) */
     
    CIMNameSpace cns = new CIMNameSpace (args[0], args[1]); 

    UserPrincipal up = new UserPrincipal("root");
    PasswordCredential pc = new PasswordCredential("root_password"); 
     
    /* Connects to the CIM Object Manager and passes it three parameters:
    the namespace object (cns), which contains the host name (args[0]) and
    parent namespace name (args[1]), a user name string (args[3]), and a
    password string (args[4]). */
     
    CIMClient cc = new CIMClient (cns, up, pc);
     
    /* Passes to the CIM Object Manager another namespace object that 
    contains a null string (host name) and args[2], the name of a 
    child namespace (for example, secondlevel). */
     
    CIMNameSpace cop = new CIMNameSpace("", args[2]);
    
    /* Creates a new namespace by the name passed in as args[2] under the
    toplevel namespace on myhost./*
     
    cc.createNameSpace(cop);
    ...
} 


 
 
 
  Previous   Contents   Next