Write the HCM plug-in.

Here, we provide pseudocode from a sample HCM plug-in called CMPISample. The purpose of this sample is to demonstrate the implementation of CMInterface and some of the functions your plug-in will need to provide. CMPISample is designed to read the host credentials from a database that you specify.

top Graphic Image
We recommend that you use the provided CMPIVault class for this function whenever possible. You should only create a custom plug-in like CMPISample if one of the plug-ins provided with Z and I Emulator for Web does not suit your needs.
package com.mycompany;

import java.util.Properties;
import java.util.Enumeration;
import com.ibm.eNetwork.security.sso.*;
import com.ibm.eNetwork.security.sso.cms.CMInterface;


public class CMPISample implements CMInterface {

    private static final String className              
		= "com.mycompany.CMPISample";


    //The following strings match the parameters contained in the 
	web.xml file (see Sample.xml)
    private static final String DB_ADDRESS             = "CMPI_SAMPLE_DB_ADDRESS";
    private static final String DB_TABLE               = "CMPI_SAMPLE_DB_TABLE";
    private static final String TRACE_LEVEL            = "CMPI_SAMPLE_TRACE_LEVEL";
    private static final int    DEFAULT_TRACE_LEVEL    = Ras.TRACE_NONE;
    //TODO:  Add all other parameters needed, such as the database 
		driver, column names, ID and PW, etc

    private String dbAddress;         //url string that provides the 
		address of the database
    private String dbTable;           //database table to use for the query
    private int    traceLevel;        //trace level
    private String logFile;           //trace log file
    //TODO:  Add fields for all other parameters


    private Properties pInit = null;
    private String cmID = null;
    //TODO:  Add any other needed fields, such as the database connection


    private boolean initOK = true;

    /*************************************************************************
     * Called to initialize the Sample Plug-in
     *
     * @param pInit  All of the CredMapper servlet parameters.
     *               These parameters are specified in the web.xml file.
     * @param id     The plug-in being initialized.
     * @return       An int value which indicates the status of the initialization.
     *************************************************************************/
    public int Init(Properties pInit, String id)
    {
         final String methodName = "Init";

         initOK = true;
         this.pInit = pInit;
         this.cmID = id;

         // For debug purposes, add a RAS implementor which logs to stdout and file.
         String traceStr = getProperty(TRACE_LEVEL, false);                  // Optional
         if (traceStr != null)  traceLevel = Integer.parseInt(traceStr);
         else                   traceLevel = DEFAULT_TRACE_LEVEL;
         String logFile  = getProperty(SSOConstants.TRACE_LOG_FILE, false);  // Optional
         if (logFile == null)   logFile    = SSOConstants.DEFAULT_TRACE_LOG_FILE;

         SampleRas sampleRas = new SampleRas(logFile);  //SampleRas classs not provided.  
			This class implements RasInterface
                                                      //and writes to logFile and stdout

         if ((traceLevel > Ras.TRACE_NONE) &&
             (Ras.hasNoImplementations())) {
           Ras.addRasImplementation(sampleRas);
         }

         if (traceLevel >= Ras.TRACE_MINIMUM) {
           //Trace all parameters and their values
           String parameters = "";
           //TODO:  Loop through pInit and build parameters string

           Ras.traceEntry(className, methodName, new String [] {
                          "pInit = {\n\t" + parameters + "\n\t}",
                          "id = " + id
                          } );
         }

        /*************************************************************************
         * Get Sample parameters from the properties object
         * Try to retrieve the value of all parameters and return an error
         * if one or more than one parameter is bad.
         *************************************************************************/
         dbAddress         = getProperty(DB_ADDRESS, true);           //Required
         dbTable           = getProperty(DB_TABLE, true);             //Required
         //TODO:  Read the rest of the properties here

         if (!initOK) return SSOConstants.SSO_INVALID_PARAMETER;

         //TODO:  Check to see if the network database driver exists here, 
			and establish a connection with the database

         if (traceLevel >= Ras.TRACE_MINIMUM)
           Ras.traceExit(className, methodName);

         return SSOConstants.SSO_SUCCESS;
    }

    /*************************************************************************
     * Close the Database connection
     *************************************************************************/
    public void Destroy() {
        final String methodName = "Destroy";
        if (traceLevel >= Ras.TRACE_MINIMUM)
          Ras.traceEntry(className, methodName);

        //TODO:  Close the database connection

        if (traceLevel >= Ras.TRACE_MINIMUM) //@ry3
          Ras.traceExit(className, methodName);

    }

    public CMResponse CMSGetUserCredentials(CMRequest cmreq)
    {
        final String methodName = "CMSGetUserCredentials";
        if (traceLevel >= Ras.TRACE_MINIMUM)
          Ras.traceEntry(className, methodName, new String [] {
                         "Network ID       = " + cmreq.getID(),
                         "Application ID   = " + cmreq.getHostApplID(),
                         "Destination Addr = " + cmreq.getHostDestination()
                         } );

        String netID = cmreq.getID();
        String hostApp = cmreq.getHostApplID();
        String hostDest = cmreq.getHostDestination();

        CMResponse resp = new CMResponse();

        String retid = null;
        String retpw = null;

        //TODO:  Build query statement with netID, hostApp, hostDest, and 
			the values provided in web.xml (table name, column names, etc)
        //TODO:  Execute the statement and set retid and retpw with the results

         if (retid == null) {
          if (traceLevel >= Ras.TRACE_MINIMUM)
            Ras.logMessage(Ras.MSG_ERROR, className, methodName,
                           "DB_USER_ID_ERROR", netID);
            resp.setStatus(SSOConstants.SSO_CMR_USER_ID_NOT_FOUND_IN_DB);
         }
         else {
              resp.setID(retid);
              resp.setPassword(retpw);
              resp.setStatus(SSOConstants.SSO_CMR_SUCCESS);
         }

        if (traceLevel >= Ras.TRACE_MINIMUM) //ry3
          Ras.traceExit(className, methodName);

         return resp;
    }

    /*************************************************************************
     * Retrieve the property value
     *************************************************************************/

    private String getProperty(String propName, boolean required)   //@d01c
    {
        String value = null;
        //TODO:  Retrieve the specified property from pInit

        return value;
    }

    public String getName() {return "Sample Credential Mapper";}
    public String getDescription() {return "Retrieves host credentials 
			from a database";}
    public String getAuthor() {return "My Company";}
    String strParms[] = { DB_ADDRESS, DB_TABLE, TRACE_LEVEL }; //TODO:  
			Fill in the rest of the parameters here
    public String[] getParameters() {return strParms;}
    public Properties getParameterInfo(String strParm)
    {
        Properties p = new Properties();

        if (DB_ADDRESS.equals(strParm))
        {
            p.put(CMInterface.cmiRequired, "true");
        }
        else if (DB_TABLE.equals(strParm))
        {
            p.put(CMInterface.cmiRequired, "true");
        }
        else if (TRACE_LEVEL.equals(strParm))
        {
            p.put(CMInterface.cmiRequired, "false");
            p.put(CMInterface.cmiDefaultValue, Integer.toString(Ras.TRACE_NONE));
        }
        //TODO:  Add ifs for the rest of the parameters
        return p;
    }

} //end class CMPIVault