Sun Microsystems, Inc.
spacerspacer
spacer www.sun.com docs.sun.com |
spacer
black dot
 
 
4.  Writing a Provider Program Implementing the Provider Interfaces Writing an Instance Provider  Previous   Contents   Next 
   
 

Writing a Method Provider

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.


Example 4-2 Method Provider

...
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
	...
}

Writing an Associator Provider


Note - 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.


Example 4-3 CIMAssociator Provider

...

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

 
 
 
  Previous   Contents   Next