Sun WBEM SDK Developer's Guide

Advanced Programming Topics

This section describes advanced programming operations and operations that you would use less frequently.

Creating a Namespace

The installation compiles the standard CIM MOF files into the default namespaces, root\cimv2 and root\security. If you create a new namespace, you must compile the appropriate CIM MOF files into the new namespace before creating objects in it. 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 — Creating a Namespace

The code segment in Example 4–20 uses a two-step process to create a namespace within an existing namespace.


Example 4–20 Creating a Namespace (CIMNameSpace)

{
    ...
    /* 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]); 
     
    /* 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, "root", "secret");	
     
    /* 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 called secondlevel under the
    toplevel namespace on myhost./*
     
    cc.createNameSpace(cop);
    ...
} 


Deleting a Namespace

Use the deleteNameSpace method to delete a namespace.

Example — Deleting a Namespace

The sample program in Example 4–21, deletes the specified namespace on the specified host. The program takes five required string arguments (host name, parent namespace, child namespace, username, and password). The user running this program must specify the username and password for an account that has write permission to the namespace to be deleted.


Example 4–21 Deleting a Namespace (deleteNameSpace)

{
import java.rmi.*;
import com.sun.wbem.client.CIMClient;
import com.sun.wbem.cim.CIMInstance;
import com.sun.wbem.cim.CIMValue;
import com.sun.wbem.cim.CIMProperty;
import com.sun.wbem.cim.CIMNameSpace;
import com.sun.wbem.cim.CIMObjectPath;
import com.sun.wbem.cim.CIMClass;
import com.sun.wbem.cim.CIMException;
import java.util.Enumeration;
 /**
 * This example program deletes the specified namespace on the
 * specified host. The user running this program must specify 
 * the username and password for a user account that has write
 * permission for the specified namespace.
 */
 public class DeleteNameSpace {
    public static void main(String args[]) throws CIMException {
        // Initialize an instance of the CIM Client class
				CIMClient cc = null;
        // Requires 5 command-line arguments. If not all entered,
        // prints command string.
				if(args.length != 5) {
	    		    System.out.println("Usage: DeleteNameSpace host parentNS 
					          childNS username password"); 
	    			  System.exit(1);
				}
				try {
		         /**
						 * Creates a namespace object (cns), which stores the host 
						 * name and parent namespace.
						 */ 
	      			 CIMNameSpace cns = new CIMNameSpace(args[0], args[1]);
	     			/** 
						 * Connects to the CIM Object Manager, and passes it the
						 * namespace object (cns) and the username and password
	     			 * command line arguments. 
	     			 */
	     			 cc = new CIMClient(cns, args[3], args[4]);
	   			  /**
						 * Creates another namespace object (cop), which stores the
						 * a null string for the host name and a string for the
						 * child namespace (from the command line arguments).
						 */
		   			 CIMNameSpace cop = new CIMNameSpace("",args[2]);
	 					/**
						 * Deletes the child name space under the parent namespace.
	     		   */ 
	    			   cc.deleteNameSpace(cop);
				} catch (Exception e) {
	    		    System.out.println("Exception: "+e);
				}
 				// Close the session
				if(cc != null) {
	   		    cc.close();
				}
   	 }
}


Creating a Base Class

Applications can create classes using either the MOF language or the client APIs. If you are familiar with MOF syntax, use a text editor to create a MOF file and then use the MOF Compiler to compile it into Java classes. This section describes how to use the client APIs to create a base class.

Use the CIMClass class to create a Java class representing a CIM class. To declare the most basic class, you need only specify the class name. Most classes include properties that describe the data of the class. To declare a property, include the property's data type, name, and an optional default value. The property data type must be an instance of CIMDataType (one of the predefined CIM data types).

A property can have a key qualifier, which identifies it as a key property. A key property uniquely defines the instances of the class. Only keyed classes can have instances. Therefore, if you do not define a key property in a class, the class can only be used as an abstract class.

If you define a key property in a class in a new namespace, you must first compile the core MOF files into the namespace. The core MOF files contain the declarations of the standard CIM qualifiers, such as the key qualifier.

Class definitions can be more complicated, including such MOF features as aliases, qualifiers, and qualifier flavors.

Example — Creating a CIM Class

The example in Example 4–22 creates a new CIM class in the default namespace (root\cimv2) on the local host. This class has two properties, one of which is the key property for the class. The example then uses the newInstance method to create an instance of the new class.


Example 4–22 Creating a CIM Class (CIMClass)

{
...
    /* Connect to the root\cimv2 namespace 
    on the local host and create a new class called myclass */

    // Connect to the default namespace on local host. 
    CIMClient cc = new CIMClient();
 
    // Construct a new CIMClass object 
    CIMClass cimclass = new CIMClass();

    // Set CIM class name to myclass. 
    cimclass.setName("myclass"); 

    // Construct a new CIM property object
    CIMProperty cp = new CIMProperty(); 

    // Set property name
    cp.setName("keyprop"); 
     
    // Set property type to one of the predefined CIM data types.
    cp.setType(CIMDatatype.getPredefinedType(CIMDataType.STRING)); 
     
    // Construct a new CIM Qualifier object
    CIMQualifier cq = new CIMQualifier(); 
     
    // Set the qualifier name
    cq.setName("key"); 
     
    // Add the new key qualifier to the property
    cp.addQualfier(cq); 
     
    /* Create an integer property initialized to 10 */
     
    // Construct a new CIM property object
    CIMProperty mp = new CIMProperty();
     
    // Set property name to myprop
    mp.setName("myprop"); 
     
    // Set property type to one of the predefined CIM data types.
    mp.setType(CIMDatatype.getPredefinedType(CIMDataType.SINT16)); 
     
    // Initialize mp to a CIMValue that is a new Integer object 
    // with the value 10. The CIM Object Manager converts this 
    // CIMValue to the CIM Data Type (SINT16) specified for the 
    // property in the mp.setType statement in the line above. 
    // If the CIMValue (Integer 10) does not fall within the range 
    // of values allowed for the CIM Data Type of the property
    // (SINT16), the CIM Object Manager throws an exception.  
    mp.setValue(new CIMValue(new Integer(10)));
     
    /* Add the new properties to myclass and call 
    the CIM Object Manager to create the class. */
     
    // Add the key property to class object
    cimclass.addProperty(cp); 
     
    // Add the integer property to class object
    cimclass.addProperty(mp); 
     
    /* Connect to the CIM Object Manager and pass the new class */
    cc.createClass(new CIMObjectPath(),cimclass);
     
    // Create a new CIM instance of myclass
    ci = cc.newInstance(); 
     
    // If the client connection is open, close it.
		if(cc != null) {
        cc.close();
    }
}


Deleting a Class

Use the CIMClient deleteClass method to delete a class. Deleting a class removes the class, its subclasses, and all instances of the class; it does not delete any associations that refer to the deleted class.

Example — Deleting a Class

The example in Example 4–23 uses the deleteClass method to delete a class in the default namespace root\cimv2. This program takes four required string arguments (host name, class name, username, and password). The user running this program must specify the username and password for an account that has write permission to the root\cimv2namespace.


Example 4–23 Deleting a Class (deleteClass)

 
		import java.rmi.*;
		import com.sun.wbem.client.CIMClient;
		import com.sun.wbem.cim.CIMInstance;
		import com.sun.wbem.cim.CIMValue;
		import com.sun.wbem.cim.CIMProperty;
		import com.sun.wbem.cim.CIMNameSpace;
		import com.sun.wbem.cim.CIMObjectPath;
		import com.sun.wbem.cim.CIMClass;
		import com.sun.wbem.cim.CIMException;
		import java.util.Enumeration;
	 /**
		* Deletes the class specified in the command line. Works in the default
		* namespace root\cimv2.
		*/
		public class DeleteClass {
	      public static void main(String args[]) throws CIMException {
			      CIMClient cc = null;
			      if(args.length != 4) {
	   		 	      System.out.println("Usage: 
                        DeleteClass host className username password"); 
	   		 	      System.exit(1);
				    }
				    try {
	   			      /**
	     		       * Creates a namespace object (cns), which stores the host 
	     		       * name.
					       */ 
						    CIMNameSpace cns = new CIMNameSpace(args[0]);
 
	   			     /** 
						    * Connects to the CIM Object Manager, and passes it the
						    * namespace object (cns) and the username and password
	     		      * command line arguments. 
	     		      */
						    cc = new CIMClient(cns, args[2], args[3]);
 
					      /** 
						     * Create an object (CIMObjectPath) that 
						     * contains the name of the class specified in args[1].
						     */
						    CIMObjectPath cop = new CIMObjectPath(args[1]);
 
						    /**
						     * Delete the class referenced by the CIM object path.
						     */
	  		  			    cc.deleteClass(cop);
					   } catch (Exception e) {
	 		  			    System.out.println("Exception: "+e);
					   }
					   if(cc != null) {
	    				    cc.close();
				     }
  	    }
}

Working with Qualifier Types and Qualifiers

A CIM qualifier is an element that characterizes a CIM class, instance, property, method, or parameter. Qualifiers have the following attributes:

In Managed Object Format syntax, each CIM qualifier must have a CIM qualifier type declared in the same MOF file. Qualifiers do not have a scope attribute. Scope indicates which CIM elements can use the qualifier. Scope can only be defined in the qualifier type declaration; it cannot be changed in a qualifier.

The following sample code shows the MOF syntax for a CIM qualifier type declaration. This statement defines a qualifier type named key, with a Boolean data type (default value false), which can describe only a property and a reference to an object. The DisableOverride flavor means that key qualifiers cannot change their value.

Qualifier Key : boolean = false, Scope(property, reference), 
								Flavor(DisableOverride);

The following sample code shows the MOF syntax for a CIM qualifier. In this sample MOF file, key and description are qualifiers for the property test. The property data type is an integer with the value a.

{
[key, Description("test")]
int a
}

Example — Getting CIM Qualifiers

The code segment in Example 4–24 uses the CIMQualifier class to identify the CIM qualifiers in a vector of CIM elements. The example returns the property name, value, and type for each CIM Qualifier.

A qualifier flavor is a flag that governs the use of a qualifier. Flavors describe rules that specify whether a qualifier can be propagated to derived classes and instances and whether or not a derived class or instance can override the qualifier's original value.


Example 4–24 Getting CIM Qualifiers (CIMQualifier)

	{
    ...
    } else if (tableType == QUALIFIER_TABLE) {
		     CIMQualifier prop = (CIMQualifier)cimElements.elementAt(row);
		     if (prop != null) {
		         if (col == nameColumn) {
			       return prop.getName();
		      } else if (col == typeColumn) {
			        CIMValue cv = prop.getValue();
			    if (cv != null) {
			        return cv.getType().toString();
			    } else {
			        return "NULL";			
          }
	} 
... 


Example — Setting CIM Qualifiers

Example 4–25 is a code segment that sets a list of CIM qualifiers for a new class to the qualifiers in its superclass.


Example 4–25 Set Qualifiers (setQualifiers)

{   
    ...
	   try {
	       cimSuperClass = cimClient.getClass(new CIMObjectPath(scName));
	    	    Vector v = new Vector();
	    	    for (Enumeration e = cimSuperClass.getQualifiers().elements();
					        e.hasMoreElements();) { 
					    CIMQualifier qual = (CIMQualifier)((CIMQualifier)e.nextElement()).clone();
					    v.addElement(qual);
	    	    }
	    	    cimClass.setQualifiers(v); 
          } catch (CIMException exc) {
	    	        return;
		      }
    }
}
...