Sun WBEM SDK Developer's Guide

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();
    }
}