Sun Microsystems, Inc.
spacerspacer
spacer www.sun.com docs.sun.com |
spacer
black dot
 
 
Chapter 2, Creating JavaBeans Using the MOF Compiler How CIM Maps to Java   Previous   Contents   Next 
   
 

Generating JavaBeans Example

Following is an example that shows the JavaBeans that are produced when you use the mofcomp command with the -j option.

You must run the mofcomp command as root or as a user with write access to the namespace in which you are compiling.


Note - Avoid specifying both the -u (user) -p (password) options when running the mofcomp command, as you must type in your password directly on the command line. Instead, specify only the -u option so that you are prompted to specify an encrypted password.



Example 2-1 Generating JavaBeans

/usr/sadm/bin/mofcomp -u root -p mypassword -o /tmp 
-j /tmp/bean.txt /usr/sadm/mof/Simple.mof

Following is the content of /usr/sadm/mof/Simple.mof:

/usr/sadm/mof/Simple.mof
-------------------
#pragma include ("CIM_Core26.mof")

class Simple_Class {

      [Key, Description ("Name of the class.") ]
   string Name;

      [Description ("Method to print the contents of the class.") ]
   string printClass();

};

Following is the content of /tmp/bean.txt:

/tmp/bean/txt
----------
PACKAGE=foo.com
IMPORTS=java.lang.Exception
EXCEPTIONS=Exception

Following is the content of CIMBean.java:

package foo.com;

import javax.wbem.cim.CIMException;
import javax.wbem.client.CIMOMHandle;
import javax.wbem.cim.CIMInstance;

/**
 * This Interface defines the methods that must be implemented by 
 *  CIMBeanImpl and its subclasses. CIMBeanImpl constitutes the base
 *  class of the Java source generated by 'mofcomp -j'. 
 */
public interface CIMBean {

    /**
     * This method returns the CIMBean's CIMOMHandle.
     * 
     * @return	CIMOMHandle	handle to the CIMOM
     */
    public CIMOMHandle getCIMOMHandle();

    /**
     * This method sets the CIMBean's CIMOMHandle to the specifed value.
     * 
     * @param	CIMOMHandle	handle to the CIMOM
     */
    public void setCIMOMHandle(CIMOMHandle handle);

    /**
     * This method returns the CIMBean's CIMInstance.
     * 
     * @return	CIMInstance	handle to the CIMInstance being managed
     */
    public CIMInstance getCIMInstance();

    /**
     * This method sets the CIMBean's CIMInstance to the specified value.
     * 
     * @param	CIMInstance	handle to the CIMInstance being managed
     */
    public void setCIMInstance(CIMInstance instance);

    /**
     * This method makes the remote call to update the CIMInstance in the 
     * CIMOM. 
     */
    public void update() throws CIMException;

    /**
     * This method makes the remote call to update the specified 
     * CIMProperty of the CIMInstance in the CIMOM. 
     * 
     * @param	String	property name to update in the CIMInstance
     * @param	Object	property value to update in the CIMProperty
     */
    public void update(String propName, Object value) throws CIMException;

    /**
     * This method makes the remote call to delete the CIMInstance in the 
     * CIMOM. 
     */
    public void delete() throws CIMException;

    /**
     * This method returns a string array of the Key qualified property 
     * name(s) in the CIMInstance. This is needed to build the 
     * CIMObjectPath for the CIMInstance if it does not contain any 
     * qualifier information. 
     * 
     * @return String Version qualifier value or "-1" if there isn't 
     * one
     */
    public String[] getBeanKeys();

    /**
     * This method returns the CIM class's Version qualifier value or 
     * '-1'if it does not have this qualifier. 
     * 
     * @return	String[]	array of the key qualified property names
     */
    public String getBeanVersion();

    /**
     * This method returns a string representation of the CIMBean. 
     * This method is intended for debug purposes and the format of the 
     * string may vary from implementation to implementation. The string 
     * returned may be empty, but may not be null. 
     * 
     * @return	String	string representation of the Bean
     */
    public String toString();

} // Interface CIMBean

Following is the content of CIMBeanImpl.java:

package foo.com;

import java.io.Serializable;
import java.util.*;
import javax.wbem.client.CIMOMHandle;
import javax.wbem.cim.CIMException;
import javax.wbem.cim.CIMInstance;
import javax.wbem.cim.CIMObjectPath;
import javax.wbem.cim.CIMValue;
import javax.wbem.client.CIMOMHandle;

/**
 * This Class implements the CIMBean Interface. It is the base Class 
 * of the Java source code generated by 'mofcomp -j'. 
 */
public class CIMBeanImpl implements CIMBean, Serializable {

    private CIMInstance	cimInstance = null;
    private CIMOMHandle	cimomHandle = null;

    /**
     * This default constructor takes no parameters and creates an empty 
     * instance of CIMBeanImpl. 
     */
    public CIMBeanImpl() {

	super();

    } // constructor

    /**
     * This constructor takes the specified CIMOMHandle and CIMInstance and 
     * creates a CIMBeanImpl. 
     * 
     * @param	CIMOMHandle	handle to the CIMOM
     * @param	CIMInstance	handle to the CIMInstance being managed
     */
    public CIMBeanImpl(CIMOMHandle handle, CIMInstance instance) {

	super();
	cimomHandle = handle;
	cimInstance = instance;

    } // constructor

    /**
     * This method returns the CIMBean's CIMOMHandle.
     * 
     * @return	CIMOMHandle	handle to the CIMOM
     */
    public CIMOMHandle getCIMOMHandle() {

	return (cimomHandle);

    } // getCIMOMHandle

    /**
     * This method sets the CIMBean's CIMOMHandle to the specifed value.
     * 
     * @param	CIMOMHandle	handle to the CIMOM
     */
    public void setCIMOMHandle(CIMOMHandle handle) {

	this.cimomHandle = handle;

    } // setCIMOMHandle

    /**
     * This method returns the CIMBean's CIMInstance.
     * 
     * @return	CIMInstance	handle to the CIMInstance being managed
     */
    public CIMInstance getCIMInstance() {

	return (cimInstance);

    } // getCIMInstance

    /**
     * This method sets the CIMBean's CIMInstance to the specified 
     * value.
     * 
     * @param	CIMInstance	handle to the CIMInstance being managed
     */
    public void setCIMInstance(CIMInstance instance) {

	this.cimInstance = instance;

    } // setCIMInstance

    /**
     * This method makes the remote call to update the CIMInstance in 
     * the CIMOM. 
     */
    public void update() throws CIMException {

	cimomHandle.setInstance(getObjectPath(), cimInstance);

} // update

    /**
     * This method makes the remote call to update the specified 
     * CIMProperty of the CIMInstance in the CIMOM. 
     * 
     * @param	String	property name to update in the CIMInstance
     * @param	Object	property value to update in the CIMProperty
     */
    public void update(String propName, Object value) throws CIMException {

	cimomHandle.setProperty(getObjectPath(), propName, new CIMValue(value));

    } // update

    /**
     * This method makes the remote call to delete the CIMInstance in the 
     * CIMOM. 
     */
    public void delete() throws CIMException {

	cimomHandle.deleteInstance(getObjectPath());

    } // delete

    /**
     * This is a convenience method for use by subclasses to get the 
     * Object contained in the given CIMProperty's CIMValue. 
     * NOTE: The Object returned may be null. 
     * 
     * @param	String	property name whose value should be retrieved
     * @return	Object	object contained in the CIMProperty's CIMValue
     */
    protected Object getProperty(String propName) {

	try {

	    return (cimInstance.getProperty(propName).getValue().getValue());

	} catch (NullPointerException npe) {
	}
	return ((Object)null);

    } // getProperty

    /**
     * This is a convenience method for use by subclasses to get 
     * the String[] equivalent to the Vector contained in the given 
     * CIMProperty's CIMValue. 
     * NOTE: The String[] returned may be null. 
     * 
     * @param	String	property name to get the value for
     * @param	String[]	property Values qualifier data
     * @param	Object[]	property ValueMap qualifier data
     * @return	String[]	container of constants for property value
     */
    protected String[] getArrayProperty(String propName, String[] 
    valueArr,	Object[] valueMapArr) {

	List propList = null;
	try {

	    propList =
		((List)cimInstance.getProperty(propName).getValue().getValue());

	} catch (NullPointerException npe) {
	}

	if (propList != null) {

	    String[] returnArr;
	    returnArr = new String[propList.size()];
	    ListIterator listIterator = propList.listIterator();
	    int counter = 0;
	    while (listIterator.hasNext()) {

		returnArr[counter] = valueArr[getArrayIndex(valueMapArr,
		    listIterator.next())];
		counter++;

	    }
	    return (returnArr);

	}
	return ((String[])null);

    } // getArrayProperty

    /**
     * This method gets the CIMInstance referenced by the property 
     * value (i.e., the object path specified) and sets it in the 
     * specified Bean. This method is used by accessor methods of 
     * Association properties. 
     * 
     * @param	CIMObjectPath	object path for the CIMInstance
     * @param	CIMBeanImpl	Bean container for CIMInstance retrieved
     */
    protected void getAssociationProperty(CIMObjectPath cop, 
    CIMBeanImp	bean) throws CIMException {

	cop.setNameSpace("");
	CIMInstance ci = cimomHandle.getInstance(cop, false, true, true,
	    (String[])null);
	bean.setCIMInstance(ci);
	bean.setCIMOMHandle(cimomHandle);

    } // getAssociationProperty

    /**
     * This is a convenience method for use by subclasses to set a 
     * CIMValue containing the specified Object value in the 
     * CIMProperty of the specified name. 
     * 
     * @param	String	property name to set a new value for
     * @param	Object	property value to update in the CIMInstance
     */
    protected void setProperty(String propName, Object propValue)throws
	IllegalArgumentException {

	cimInstance.setProperty(propName, new CIMValue(propValue));

    } // setProperty

    /**
     * This is a convenience method for use by subclasses to set a 
     * CIMValue containing a Vector equivalent to the specified 
     * String[] in the CIMProperty of the specified name. 
     * 
     * @param	String	property name to get the value for
     * @param	String[]	property Values qualifier data
     * @param	Object[]	property ValueMap qualifier data
     * @param	String[]	property value to set in the CIMInstance
     */
    protected void setArrayProperty(String propName, String[] valueArr,
	Object[] valueMapArr, String[] propValues) {

	Vector vPropValue = new Vector(propValues.length);
	for (int i = 0; i < propValues.length; i++) {

	    vPropValue.addElement(valueMapArr[getArrayIndex(valueArr,
		propValues[i])]);

	}
	setProperty(propName, vPropValue);

    } // setArrayProperty

    /**
     * This method returns a string array of the Key qualified property 
     * name(s) in the CIMInstance. This is needed to build the 
     * CIMObjectPath for the CIMInstance if it does not contain any 
     * qualifier information. 
     * 
     * @return	String[]	array of the key qualified property names
     */
    public String[] getBeanKeys() {

	return ((String[])null);

    } // getBeanKeys

    /**
     * This method returns the CIMObjectPath of the class's CIMInstance.
     * 
     * @return	CIMObjectPath	object path for the CIMInstance
     */
    protected CIMObjectPath getObjectPath() {

	CIMObjectPath cop = new CIMObjectPath(cimInstance.getClassName());
	Vector vKeys = cimInstance.getKeyValuePairs();
	if ((vKeys != null) && (vKeys.size() > 0)) {

	    cop.setKeys(vKeys);

	} else {

	    String[] keyArr = getBeanKeys();
	    if (keyArr != null) {

		String keyProperty;
		for (int i = 0; i < keyArr.length; i++) {

		    keyProperty = keyArr[i];
		    cop.addKey(keyProperty,
			(cimInstance.getProperty(keyProperty)).getValue());

		}

	    }

	}
	return (cop);

    } // getObjectPath

    /**
     * This convenience method returns the index of the specified 
     * object in the specified array, or -1 if the object is not 
     * contained in the array. 
     * 
     * @param	Object[]	Object array to find index of Object in
     * @param	Object	Object to find index of in Object array
     * @return	int	index of Object in Object array
     */
    protected int getArrayIndex(Object[] objArr, Object obj) {

	List arrList = Arrays.asList(objArr);
	return (arrList.indexOf(obj));

    } // getArrayIndex

    /**
     * This method returns the CIM class's Version qualifier value, or 
     * '-1'if it does not have this qualifier. 
     * 
     * @return	String	Version qualifier value or "-1" if there isn't 
     * one
     */
    public String getBeanVersion() {

	return ("-1");

    } // getBeanVersion

    /**
     * This method returns a string representation of the CIMBean. 
     * This method is intended for debug purposes and the format of 
     * the string may vary from implementation to implementation. 
     * The string returned may be empty, but may not be null. 
     * 
     * @return	String	string representation of the Bean
     */
    public String toString() {

	return (cimInstance.toString());

    } // toString

} // Class CIMBeanImpl
 
 
 
  Previous   Contents   Next