When you write a provider, you must determine the interfaces that your provider will support. You must implement all the methods of each interface that your provider supports. In addition, every provider must implement the CIMProvider interface, which has two methods:
initialize(CIMOMHandle cimom) – If your provider stores data in the CIM Object Manager Repository, it must assign the passed CIM Object Manager handle to the CIM Object Manager handle that it will use to contact the CIM Object Manager. For example:
private CIMOMHandle cimom = null; ... public void initialize(CIMOMHandle cimom) throws CIMException { this.cimom = (CIMOMHandle) cimom; |
The provider creates instances or manipulates associations in the CIM Object Manager Repository, it must first cast the passed CIM Object Manager handle to the subclass ProviderCIMOMHandle, and then fetch an internal instance or association provider. For example:
private ProviderCIMOMHandle cimom = null; private CIMAssociatorProvider ap = null; ... public void initialize(CIMOMHandle cimom) throws CIMException { this.cimom = (ProviderCIMOMHandle) cimom; ap = pcimom.getInternalProvider(); |
The initialize command automatically runs each time a provider is initialized after the CIM Object Manager restarts.
cleanup() – Currently acts as a placeholder.
This following sample code implements the enumerateInstances and getInstance interfaces for the Ex_SimpleCIMInstanceProvider class. For brevity, this example implements the deleteInstance, createInstance, setInstance, and execQuery interfaces by throwing a CIMException.
For information on implementing the execQuery method, see “Parsing Queries”.
/* * "@(#)SimpleCIMInstanceProvider.java" */ import javax.wbem.cim.*; import javax.wbem.client.*; import javax.wbem.provider.CIMProvider; import javax.wbem.provider.CIMInstanceProvider; import javax.wbem.provider.MethodProvider; import java.util.*; import java.io.*; public class SimpleCIMInstanceProvider implements CIMInstanceProvider{ static int loop = 0; public void initialize(CIMOMHandle cimom) throws CIMException { } public void cleanup() throws CIMException { } public CIMObjectPath[] enumerateInstanceNames(CIMObjectPath op, CIMClass cc) throws CIMException { return null; } /* * enumerateInstances: * The entire instances and not just the names are returned. */ public CIMInstance[] enumerateInstances(CIMObjectPath op, boolean localOnly,boolean includeQualifiers, boolean includeClassOrigin,String[] propertyList, CIMClass cc) throws CIMException { if (op.getObjectName().equalsIgnoreCase\ ("Ex_SimpleCIMInstanceProvider")) { Vector instances = new Vector(); CIMInstance ci = cc.newInstance(); if (loop == 0){ ci.setProperty("First", new CIMValue("red")); ci.setProperty("Last", new CIMValue("apple")); // only include the properties that were requested ci = ci.filterProperties(propertyList, includeQualifier, includeClassOrigin); instances.addElement(ci); loop += 1; } else { ci.setProperty("First", new CIMValue("red")); ci.setProperty("Last", new CIMValue("apple")); // only include the properties that were requested ci = ci.filterProperties(propertyList, includeQualifier, includeClassOrigin); instances.addElement(ci); ci = cc.newInstance(); ci.setProperty("First", new CIMValue("green")); ci.setProperty("Last", new CIMValue("apple")); // only include the properties that were requested ci = ci.filterProperties(propertyList, includeQualifier, includeClassOrigin); instances.addElement(ci); } return (CIMInstance[])instances.toArray(); } throw new CIMException(CIM_ERR_INVALID_CLASS); } public CIMInstance getInstance(CIMObjectPath op, boolean localOnly, boolean includeQualifiers, boolean includeClassOrigin, String[] propertyList, CIMClass cc) ) throws CIMException { if (op.getObjectName().equalsIgnoreCase ("Ex_SimpleCIMInstanceProvider")) { CIMInstance ci = cc.newInstance(); // we need to get the keys from the passed in object path, this // will uniqeuly identify the instance we want to get java.util.Vector keys = cop.getKeys(); // Since this is a contrived example we will simply place the keys // into the instance and be done. ci.setProperties(keys); // if we had other non-key properties we should add them here. // only include the properties that were requested ci = ci.filterProperties(propertyList, includeQualifiers, includeClassOrigin); return ci; } throw new CIMException(CIM_ERR_INVALID_CLASS); } public CIMInstance[] execQuery(CIMObjectPath op, \ String query, String ql, CIMClass cc) throws CIMException { throw(new CIMException(CIMException.CIM_ERR_NOT_SUPPORTED)); } public void setInstance(CIMObjectPath op, CIMInstance ci, boolean includeQualifiers, String[] propertyList) throws CIMException { throw(new CIMException(CIMException.CIM_ERR_NOT_SUPPORTED)); } public CIMObjectPath createInstance(CIMObjectPath op, CIMInstance ci) throws CIMException { throw(new CIMException(CIMException.CIM_ERR_NOT_SUPPORTED)); } public void deleteInstance(CIMObjectPath cp) throws CIMException { throw(new CIMException(CIMException.CIM_ERR_NOT_SUPPORTED)); } }
The method invokeMethod is the only way that a client program can call the methods of Solaris WBEM providers, whether those providers are:
Built–in – The “platform-free” CIM_* providers or the Solaris-specific Solaris_* providers.
Added by developers – For example, a method provider, whether it supplies provider or non-WBEM methods, is created by implementing the MethodProvider interface.
The following sample code creates the Solaris_ComputerSystem provider class that routes requests from the CIM Object Manager to one or more specialized providers. These providers service requests for dynamic data for a particular type of managed object. For example, the Solaris_Package provider services requests to execute methods in the Solaris_Package class.
The method provider implements a single method, invokeMethod, that calls the appropriate provider to either reboot a system, shut down a system, or delete a serial port.
... public class Solaris_ComputerSystem implements MethodProvider { ProviderCIMOMHandle pch = null; public void initialize(CIMOMHandle ch) throws CIMExcepiton { pch = (ProviderCIMOMHandle)ch; } public void cleanup() throws CIMException { } public CIMValue invokeMethod(CIMObjectPath op, String methodName, Vector inParams, Vector outParams) throws CIMException { if (op.getObjectName().equalsIgnoreCase("solaris_computersystem")) { if (methodName.equalsIgnoreCase("reboot")) { // call helper function, not shown here return new CIMValue(rebootSystem()); } if (methodName.equalsIgnoreCase("shutdown")) { // call helper function, not shown here return new CIMValue(shutdownSystem()); } } if (op.getObjectName().equalsIgnoreCase("solaris_serialport")) { if (methodName.equalsIgnoreCase("disableportservice")) { // call helper function, not shown here return new CIMValue(deletePort(op)); } } // error if we get here throw new CIMException(CIMException.CIM_ERR_NOT_SUPPORTED, "The requested function does not exist"); } // helper functions would be defined below ... }
The objectName argument in each of the association methods called by your client program, that is, CIMObjectPath, must be the object path of an instance, not a class.
Unless the CIM Object Manager sees the object path of an instance, it assumes that the client wants to see the class definitions of the association (the templates from which the association's member instances are derived) in the CIM Object Manager Repository, and will use the client API's association method and not that of the provider's.
The most important part of designing and coding an association is the association class itself. Your association will only be as complex as the contents of the association class. The number of members of the association equals the number of references in the association class. Roles can be used to model more complicated associations. Following are some sample association classes:
An asymmetrical pair relationship, such as a one-to-one relationship between a teacher and a student, with two roles defined (teaches and taughtby):
class TeacherStudent { Teacher REF teaches; Student REF taughtby; }; |
A one-to-many relationship:
class Classroom { Teacher REF teaches; Student1 REF taughtby; Student2 REF taughtby; Student3 REF taughtby; Student4 REF taughtby; }; |
A many-to-many relationship:
class TeachingAssistants { Assistant1 REF assists; Assistant2 REF assists; Student1 REF assistedby; Student2 REF assistedby; Student3 REF assistedby; Student4 REF assistedby; Student5 REF assistedby; }; |
An association of more than two members of equal standing:
class Club { Member1 REF; Member2 REF; Member3 REF; }; |
The following code sample implements the associators method. The CIM Object Manager passes values for associatorNames, objectName, role, resultRole, includeQualifiers, includeClassOrigin, and propertyList to the association provider. In addition, the code prints the name of the CIM associator class and the CIM class or instance whose associated objects are to be returned. This provider handles instances of example_teacher and example_student classes.
... public CIMInstance[] associators(CCIMObjectPath assocName, CIMObjectPath objectName, String resultClass, String role, String resultRole, boolean includeQualifiers, boolean includeClassOrigin, String[] propertyList) throws CIMException { System.out.println("Associators "+assocName+" "+objectName); if (objectName.getObjectName()equalsIgnoreCase("example_teacher")) { Vector v = new Vector(); if ((role != null) && (!role.equalsIgnoreCase("teaches"))) { // Teachers only play the teaches role. return v; } if ((resultRole != null) && (!resultRole.equalsIgnoreCase ("taughtby"))) { // Teachers only result in taughtby role return v; } // Get the associators of a teacher CIMProperty nameProp = (CIMProperty)objectName.getKeys().elementAt (0); String name = (String)nameProp.getValue().getValue(); // Get the student class CIMObjectPath tempOp = new CIMObjectPath("example_student"); tempOp.setNameSpace(assocName.getNameSpace()); CIMClass cc = cimom.getClass(tempOp, false); // Test the instance name passed by objectName // and return the associated instances of the student class. if(name.equals("teacher1")) { // Get students for teacher1 CIMInstance ci = cc.newInstance(); ci.setProperty("name", new CIMValue("student1")); v.addElement(ci.filterProperties(propertyList, includeQualifiers, includeClassOrigin)); ci = cc.newInstance(); ci.setProperty("name", new CIMValue("student2")); v.addElement(ci.filterProperties(propertyList, includeQualifiers, includeClassOrigin)); return v; } }
To generate an indication for a CIM event, do the following:
Use the methods in the EventProvider interface to detect when to start and stop delivering indications of the CIM event.
Create an instance of one or more subclasses of the CIM_Indication class to store information about the CIM event that occurred.
Use the deliverEvent method in the ProviderCIMOMHandle interface to deliver indications to the CIM Object Manager.
Implement the EventProvider interface.
For example:
public class sampleEventProvider implements InstanceProvider EventProvider{ // Reference for provider to contact the CIM Object Manager private ProviderCIMOMHandle cimom; }
Execute each of the methods listed in Table 4–2 for each instance indication that the provider handles.
Create an indication for each create, modify, and delete instance event type.
For example, in the createInstance method:
public CIMObjectPath createInstance(CIMObjectPath op, CIMInstance ci) throws CIMException { CIMObjectpath newop = ip.createInstance(op, ci); CIMInstance indication = new CIMInstance(); indication.setClassName("CIM_InstCreation"); CIMProperty cp = new CIMProperty(); cp.setName("SourceInstance"); cp.setValue(new CIMValue(ci)); Vector v = new Vector(); v.addElement(cp); indication.setProperties(v); ... }
Deliver the event indication to the CIM Object Manager:
cimom.deliverEvent(op.getNameSpace(), indication);
An event provider implements the EventProvider interface. This interface contains methods that the CIM Object Manager uses to notify the provider when a client has subscribed for indications of CIM events, and when a client has cancelled the subscription for CIM events. These methods also allow the provider to indicate whether or not the CIM Object Manager should poll for some event indications and whether or not the provider should authorize the return of an indication to a handler.
The following table lists the methods in the EventProvider interface that must be implemented by an event provider.
Table 4-2 EventProvider Methods
Method |
Description |
---|---|
activateFilter |
When a client creates a subscription, the CIM Object Manager calls this method to ask the provider to check for CIM events. |
authorizeFilter |
When a client creates a subscription, the CIM Object Manager calls this method to test if the specified filter expression is allowed. |
deActivateFilter |
When a client removes a subscription, the CIM Object Manager calls this method to ask the provider to deactivate the specified event filter. |
mustPoll |
When a client creates a subscription, the CIM Object Manager calls this method to test if the specified filter expression is allowed by the provider, and if it must be polled. |
The CIM Object Manager passes values for the following arguments to all methods:
filter – SelectExp that specifies the CIM events for which indications must be generated.
eventType – String that specifies the type of CIM event, which can also be extracted from the FROM clause of the select expression.
classPath – CIMObjectPath that specifies the name of the class for which the event is required.
In addition, the activateFilter method takes the boolean firstActivation, indicating that this is the first filter for this event type. The deActivateFilter method takes the boolean lastActivation, indicating that this is the last filter for this event type.
When a client application subscribes for indications of CIM events by creating an instance of the CIM_IndicationSubscription class, the CIM Object Manager forwards the request to the appropriate provider. If the provider implements the EventProvider interface, the CIM Object Manager notifies the provider when to start sending indications for the specified events by calling the provider's activateFilter method. In addition, the CIM Object Manager notifies the provider when to stop sending indications for the specified events by calling the provider's deActivateFilter method.
The provider responds to the CIM Object Manager's requests by creating and delivering an indication each time the provider creates, modifies, or deletes an instance. A provider typically defines a flag variable that is set when the CIM Object Manager calls the activateFilter method that is cleared when the CIM Object Manager calls the deActivateFilter method. Then in each method that creates, modifies, or deletes an instance, the provider checks the status of the activate filter flag. If the flag is set, the provider creates an indication containing the created CIM instance object and uses the deliverEvent method to return the indication to the CIM Object Manager. If the flag is not set, the provider does not create and deliver an indication of the event.
A provider starts delivering indications when the activateFilter method is called. The provider creates instances of concrete subclasses of CIM_Indication and invokes the ProviderCIMOMHandled.deliverIndication method. The CIM Object Manager receives the indication and delivers the indication to the appropriate indication handlers. A provider can handle multiple event types. For example, in the case of life cycle indications, a provider can handle CIM_InstCreation, CIM_InstDeletion, and CIM_InstModification.
To keep track of types that have subscriber interest, the provider can use the firstActivation and lastActivation flags passed in the activateFilter and deActivateFilter calls, respectively. The firstActivation flag is true when the subscription is the first one for the particular event type. Similarly, lastActivation is true when the last subscription for the particular event type is removed. By checking these flags, the provider can easily allocate or deallocate resources to monitor the specified event types.
A provider that handles sensitive data can check authorizations for requests for indications. The provider must implement the Authorizable interface to indicate that it handles authorization checking. The provider also implements the authorizeFilter method. The CIM Object Manager calls this method to test if the owner (UID) of an event handler is authorized to receive the indications that result from evaluating a filter expression. The UID for the owner of the event destination (event handler) can be different than the owner of the client application requesting the filter activation.
Providers get information from and set information on managed devices. A native provider is a program specifically written for a particular managed device. For example, a provider that accesses data on a Solaris system usually includes C functions to query the system.
The common reasons for writing a native provider are as follows:
Efficiency – You may want to implement a small portion of time-critical code in a lower-level programming language, such as Assembly, and then have your Java application call these functions.
Need to access platform-specific features – The standard Java class library might not support the platform-dependent features required by your application.
Legacy code – You want to continue to use your legacy code with a Java provider.
The Java Native Interface is part of the JDK software. By writing programs using the Java Native Interface, you ensure that your code is completey portable across all platforms.The Java Native Interface enables Java code that runs within a Java virtual machine to operate with applications and libraries written in other languages, such as C, C++, and assembly.
For more information on writing and integrating Java programs with native methods, visit the Java Web site at http://java.sun.com.