Home | History | Annotate | Download | only in srm
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  * or http://www.opensolaris.org/os/licensing.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 /*
     22  * Copyright (c) 2001 by Sun Microsystems, Inc.
     23  * All rights reserved.
     24  *
     25  * SRMProviderDataModel.java
     26  */
     27 
     28 
     29 package com.sun.wbem.solarisprovider.srm;
     30 
     31 import javax.wbem.cim.*;
     32 
     33 import java.util.LinkedHashMap;
     34 import java.util.Iterator;
     35 import java.util.Vector;
     36 
     37 /**
     38  * This is the base class for the SRM provider data models.
     39  * @author Sun Microsystems
     40  */
     41 abstract class SRMProviderDataModel implements SRMProviderProperties {
     42 
     43     /*
     44      * The updated flag is set if this data model has been updated, data model
     45      * be reomoved from their list.that hasn't been
     46      */
     47     private boolean		updated;
     48     private CIMObjectPath	op;   // the CIM object path to 'ci'
     49     protected CIMInstance	ci;   // the CIM instance of this data model
     50     protected Vector		opProperties; // object path properties
     51 
     52     /**
     53      * The keyValTab table contains, for each properties of the CIM class,
     54      * one entry, where the hash key is the key from the RDS protocol and
     55      * the hash value is a object of a class that implements the
     56      * PropertyAccessInterface interface. It knows the type of the property
     57      * and consequently how to access it.
     58      */
     59     protected LinkedHashMap   keyValTab;
     60 
     61     // Computer name, common for all instances
     62     protected static String csName = null;
     63     // OS name, common for all instances
     64     protected static String osName = null;
     65     // provider's key name
     66     protected String name = null;
     67 
     68 
     69     /**
     70      * Constructor, initialize the keyValTab. So, the setProperty() method can
     71      * find the suitable property access object.
     72      */
     73     public SRMProviderDataModel() {
     74 	initKeyValTable();
     75     }
     76 
     77     /**
     78      * Get a cim object path to a Solaris_SRMxxy object.
     79      * @param cc - the class reference
     80      * @return	object path to a Solaris_SRMxxyy instance
     81      *		hosted by this object.
     82      */
     83     public CIMInstance getCIMInstance(CIMClass cc) {
     84 
     85         if (ci == null) {
     86             ci = cc.newInstance();
     87 	    Iterator i = keyValTab.values().iterator();
     88 	    while (i.hasNext()) {
     89 		((PropertyAccessInterface) i.next()).
     90 		    set(ci, PropertyAccessInterface.FLUSH, null);
     91 	    }
     92 	    setCIMInstance(true);
     93         }
     94         return ci;
     95     }
     96 
     97     /**
     98      * Get a CIM object path to the CIM instance hosted by this object.
     99      * @param elementName The name of the instance
    100      * @return object path to the  CIM instance hosted by this object.
    101      */
    102     public CIMObjectPath getCIMObjectPath(String elementName) {
    103 
    104         if (op == null) {
    105             op = new CIMObjectPath(elementName);
    106             op.setNameSpace(NAMESPACE);
    107             if (opProperties == null) {
    108 		opProperties = new Vector();
    109                 setOpPropertiesVector();
    110             }
    111             op.setKeys(opProperties);
    112         }
    113         return op;
    114     }
    115 
    116     /**
    117      * Return all properties of this CIM instance as a string of name and
    118      * values pairs.
    119      * @return	String of properties name and value pairs.
    120      */
    121     public String toString() {
    122 
    123 	Iterator i = keyValTab.values().iterator();
    124 	StringBuffer sb = new StringBuffer();
    125 	while (i.hasNext()) {
    126 	    sb.append(((PropertyAccessInterface) i.next()).toString());
    127 	}
    128 	return sb.toString();
    129     }
    130 
    131     /**
    132      * Return all property values of this CIM instance as a string. The
    133      * values are in order defined in mof file and separated by ' '.
    134      * @return	String of property values separated by ' '.
    135      */
    136     public String toBulkData() {
    137 
    138 	Iterator i = keyValTab.values().iterator();
    139 	StringBuffer sb = new StringBuffer();
    140 	sb.append(name +  ' ');
    141 	while (i.hasNext()) {
    142 	    sb.append(((PropertyAccessInterface) i.next()).getValue() + ' ');
    143 	}
    144 	sb.append("\n");
    145 	return sb.toString();
    146     }
    147 
    148     /**
    149      * Set a property to the given value. Which property and how to access it
    150      * will be find in the keyValTab according to the given key.
    151      * @param	key	hash key value
    152      * @param	val	the property value
    153      */
    154     void setProperty(String key, String val) {
    155 
    156 	PropertyAccessInterface ac;
    157 
    158 	if ((ac = (PropertyAccessInterface) keyValTab.get(key)) == null) {
    159 	    /*
    160 	     * The rds has sent unknown key value pair,
    161 	     * just keep it secret
    162 	     */
    163 	} else {
    164 	    /*
    165 	     * Since, this method can be called before the cim instance has been
    166 	     * created we have to check that.
    167 	     * If ci hasn't been created keep the value in cache, otherwise
    168 	     * write it through the cache into the CIM instance.
    169 	     */
    170 	    ac.set(ci, (ci == null) ? PropertyAccessInterface.CACHE :
    171 		PropertyAccessInterface.CHECK_WTHROUGH, val);
    172 	}
    173 
    174     } // end setProperty
    175 
    176     /**
    177      * If the value v is different form the current property value,
    178      * set a string property.
    179      * @param	b	force to set the property
    180      * @param	n	the name of the property
    181      * @param	v	the property value
    182      */
    183     void setStrProp(boolean b, String n,  String v) {
    184 
    185         if (b || v.equals((String) (ci.getProperty(n).getValue().getValue())))
    186 	    ci.setProperty(n, new CIMValue(v));
    187     }
    188 
    189     /**
    190      * Should be used to mark this data model as updated.
    191      * @param	b	true marks this model as currently updated.
    192      */
    193     void setUpdated(boolean b) {
    194 	updated = b;
    195     }
    196 
    197     /**
    198      * Returns true if this data model has been updated in the last update
    199      * process.
    200      * @return	the updated flag
    201      */
    202     boolean isUpdated() {
    203 	return updated;
    204     }
    205 
    206     /**
    207      * Set additional properties, that can't be set through the setProperty
    208      * method.
    209      * @param	newInstance indicates a new instance and therefore force to
    210      * to set all properties.
    211      * @return	the updated flag
    212      */
    213     abstract protected void setCIMInstance(boolean newInstance);
    214 
    215     /**
    216      * Set the properties of the CIM object path instance. The properties
    217      * are saved in the opProperties class field.
    218      */
    219     abstract protected void setOpPropertiesVector();
    220 
    221     /**
    222      * Set the keyValTab hash table.
    223      * The keyValTab table contains, for each properties of the CIM class,
    224      * one entry, where the hash key is the key from the RDS protocol and
    225      * the hash value is a object of a class that implements the
    226      * PropertyAccessInterface interface. This object knows the type of
    227      * the property and consequently how to access it.
    228      */
    229     abstract protected void initKeyValTable();
    230 
    231 } // end class SRMProviderDataModel
    232