Creating Associations
An association describes a relationship between two or more managed resources such as a computer and its hard disk. This relationship is abstracted in an association class, which is a special type of class that contains an association qualifier. You can add or change an association class without affecting the objects themselves.
Figure 3-1 TeacherStudent Association 1
Figure 3-1 shows two classes, Teacher and Student. Both classes are linked by the TeacherStudent association. The TeacherStudent association has two references:
Teaches, a property that refers to an instance of the Teacher class
TaughtBy, a property that refers to an instance of the Student class
About The Association Methods
The association methods in CIMClient return information about the relationships between classes and instances. These methods are described in the following table.
Table 3-2 Association Methods
Method | Description |
---|---|
associators | Gets the CIM classes or instances that are associated with the specified CIM class or instance. |
associatorNames | Gets the names of the CIM classes or instances that are associated with the specified CIM class or instance. |
references | Gets the association classes or instances that refer to the specified CIM class or instance, respectively. |
referenceNames | Gets the names of the association classes or instances that refer to the specified CIM classes or instances, respectively. |
These methods take one required argument, CIMObjectPath, which is the name of a source CIM class or CIM instance whose associations, associated classes, or instances you want to return. If the CIM Object Manager does not find any associations, associated classes, or instances, it does not return anything.
If CIMObjectpath is a class, the methods return the associated classes and the subclasses of each associated class.
If CIMObjectpath is an instance, the methods return the instances of the associated class and the subclasses of each associated class.
Figure 3-2 TeacherStudent Association 2
In Figure 3-2, the associators and associatorNames methods return information about the classes associated with the Teacher and Student classes. The references and referenceNames methods return information about the associations between the Teacher and Student classes.
Table 3-3 TeacherStudent Methods
Example | Output | Description |
---|---|---|
associators(Teacher, null, null, null, null, false, false, null) | Student class | Returns associated classes.Student is linked to Teacher by the TeacherStudent association. |
associators(MathTeacher, null, null, null, null,,false, false, null) | Student | Returns associated classes. Teacher is linked to Student by the TeacherStudent association. MathTeacher and ArtTeacher inherit the TeacherStudent association from Teacher. |
associatorNames(Teacher, null, null, null, null) | Name of the Student class | Returns the names of the associated classes. Student is linked to Teacher by the TeacherStudentassociation. |
references(Student, null, null. false, false, null) | TeacherStudent | Returns the associations in which Student participates. |
references(Teacher, null, null. false, false, null) | TeacherStudent | Returns the associations in which Teacher participates. |
references(Teacher, null, null, false, false, null) | TeacherStudent | Returns the associations in which Teacher participates. |
referenceNames(Teacher, null, null) | The name of the TeacherStudent class. | Returns the names of the associations in which Teacher participates. |
referenceNames(Teacher, null, null) | The name of the TeacherStudent class. | Returns the names of the associations in which Teacher participates. |
Note - The associatorNames and referenceNames methods do not take the arguments includeQualifiers, includeClassOrigin, and propertyList because these arguments are irrelevant to a method that returns only the names of instances or classes, not their entire contents.
Passing a Class to the Association Methods
To specify the name of a class, you specify its model path. The model path includes the class's namespace, class name, and keys. A key is a property or set of properties that uniquely identify managed resource. Key properties are marked with the key qualifier. This model path:
\\myserver\\root\cimv2\Solaris_ComputerSystem.Name= mycomputer: CreationClassName=Solaris_ComputerSystem |
Specifies the following:
\\myserver\root\cimv2 is the default CIM namespace on host myserver.
Solaris_ComputerSystem is the name of the class from which the instance is derived.
Name=mycomputer, CreationClassName=Solaris_ComputerSystem are two key properties in the format key property=value.
Passing Instances to the Association Methods
You use the enumerateInstances method to return all instances of a given class, and a loop structure to iterate through the instances. In the loop, you can pass each instance to an association method.
Example 3-14 Passing Instances
This example enumerates the instances in the op class and its subclasses, uses a while loop to cast each instance to a CIMObjectPath (op), and passes each instance as the first argument to the associators method.
{ ... Enumeration e = cc.enumerateInstances(op, true); while (e.hasMoreElements()) { op = (CIMObjectPath)e.nextElement(); Enumeration e1 = cc.associators(op, null, null, null, null, false, false, null); ... } |
Using Optional Arguments with the Association Methods
You can use the optional arguments with the association methods to filter the classes and instances that are returned. Each optional parameter value passes its results to the next parameter for filtering until all parameters have been processed.
You can pass values for any one or a combination of the optional parameters. You must enter a value or null for each parameter. The assocClass, resultClass, role, and resultRole parameters filter the classes and instances that are returned. Only the classes and instances that match the values specified for these parameters are returned. The includeQualifiers, includeClassOrigin, and propertyList parameters filter the information that are included in the classes and instances that are returned.
Calling Methods
You use the invokeMethod interface to call a method in a class supported by a provider. To retrieve the signature of a method, an application must first get the definition of the class to which the method belongs. The invokeMethod method returns a CIMValue. The return value is null when the method you invoke does not define a return value.
The invokeMethod interface takes four arguments, as described in the following table.
Table 3-4 invokeMethod Parameters
Parameter | Data Type | Description |
---|---|---|
name | CIMObjectPath | The name of the instance on which the method must be invoked |
methodName | String | The name of the method to call |
inParams | Vector | Input parameters to pass to the method |
outParams | Vector | Output parameters to get from the method |
Example 3-15 Calling a Method
This example gets the instances of the CIM_Service class (services that manage device or software features) and uses the invokeMethod method to stop each service.
{ ... /* Pass the CIM Object Path of the CIM_Service class to the CIM Object Manager. We want to invoke a method defined in this class. */ CIMObjectPath op = new CIMObjectPath("CIM_Service"); /* The CIM Object Manager returns an enumeration of instance object paths, the names of instances of the CIM_Service class. */ Enumeration e = cc.enumerateInstanceNames (op, true); /* Iterate through the enumeration of instance object paths */ while(e.hasMoreElements()) { // Get the instance CIMObjectPath op = (CIMObjectPath) e.nextElement(); //Invoke the Stop Service method to stop the CIM services. cc.invokeMethod("StopService", null, null); } } |