OWBI1_CIMInstance.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2001-2004 Vintela, Inc. All rights reserved.
00003 *
00004 * Redistribution and use in source and binary forms, with or without
00005 * modification, are permitted provided that the following conditions are met:
00006 *
00007 *  - Redistributions of source code must retain the above copyright notice,
00008 *    this list of conditions and the following disclaimer.
00009 *
00010 *  - Redistributions in binary form must reproduce the above copyright notice,
00011 *    this list of conditions and the following disclaimer in the documentation
00012 *    and/or other materials provided with the distribution.
00013 *
00014 *  - Neither the name of Vintela, Inc. nor the names of its
00015 *    contributors may be used to endorse or promote products derived from this
00016 *    software without specific prior written permission.
00017 *
00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc. OR THE CONTRIBUTORS
00022 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028 * POSSIBILITY OF SUCH DAMAGE.
00029 *******************************************************************************/
00030 
00036 #include "OWBI1_CIMDetail.hpp"
00037 #include "OWBI1_config.h"
00038 #include "OWBI1_CIMInstance.hpp"
00039 #include "OWBI1_CIMName.hpp"
00040 #include "OWBI1_CIMInstanceRep.hpp"
00041 #include "OWBI1_CIMPropertyRep.hpp"
00042 #include "OWBI1_CIMNameRep.hpp"
00043 #include "OWBI1_Array.hpp"
00044 #include "OWBI1_CIMProperty.hpp"
00045 
00046 namespace OWBI1
00047 {
00048 
00049 using namespace OpenWBEM;
00050 using std::ostream;
00051 using std::istream;
00052 using namespace WBEMFlags;
00053 using namespace detail;
00054 
00056 CIMInstance::CIMInstance() :
00057    CIMElement(), m_rep(new CIMInstanceRep)
00058 {
00059 }
00061 CIMInstance::CIMInstance(CIMNULL_t) :
00062    CIMElement(), m_rep(new CIMInstanceRep(OpenWBEM::CIMInstance(OpenWBEM::CIMNULL)))
00063 {
00064 }
00066 CIMInstance::CIMInstance(const char* name) :
00067    CIMElement(), m_rep(new CIMInstanceRep(OpenWBEM::CIMInstance(name)))
00068 {
00069 }
00071 CIMInstance::CIMInstance(const CIMName& name) :
00072    CIMElement(), m_rep(new CIMInstanceRep(OpenWBEM::CIMInstance(name.c_str())))
00073 {
00074 }
00076 CIMInstance::CIMInstance(const CIMInstance& x) :
00077       CIMElement(x), m_rep(x.m_rep)
00078 {
00079 }
00081 CIMInstance::~CIMInstance()
00082 {
00083 }
00085 void
00086 CIMInstance::setNull()
00087 {
00088    m_rep = NULL;
00089 }
00091 CIMInstance& CIMInstance::operator= (const CIMInstance& x)
00092 {
00093    m_rep = x.m_rep;
00094    return *this;
00095 }
00097 CIMInstance&
00098 CIMInstance::setKeys(const CIMPropertyArray& keys)
00099 {
00100    OpenWBEM::CIMPropertyArray owkeys;
00101    unwrapArray(owkeys, keys);
00102    m_rep->inst.setKeys(owkeys);
00103    return *this;
00104 }
00106 CIMName
00107 CIMInstance::getClassName() const
00108 {
00109    return CIMName(new CIMNameRep(m_rep->inst.getClassName()));
00110 }
00112 String
00113 CIMInstance::getLanguage() const
00114 {
00115    return m_rep->inst.getLanguage().c_str();
00116 }
00118 void
00119 CIMInstance::setLanguage(const String& language)
00120 {
00121    m_rep->inst.setLanguage(language.c_str());
00122 }
00124 CIMInstance&
00125 CIMInstance::setClassName(const CIMName& name)
00126 {
00127    m_rep->inst.setClassName(name.getRep()->name);
00128    return *this;
00129 }
00131 CIMPropertyArray
00132 CIMInstance::getProperties(Int32 valueDataType) const
00133 {
00134    OpenWBEM::CIMPropertyArray owpa = m_rep->inst.getProperties(valueDataType);
00135    CIMPropertyArray rv;
00136    unwrapArray(rv, owpa);
00137    return rv;
00138 }
00140 CIMInstance&
00141 CIMInstance::setProperties(const CIMPropertyArray& props)
00142 {
00143    OpenWBEM::CIMPropertyArray owprops;
00144    unwrapArray(owprops, props);
00145    m_rep->inst.setProperties(owprops);
00146    return *this;
00147 }
00149 CIMProperty
00150 CIMInstance::getProperty(const CIMName& name,
00151    const CIMName& originClass) const
00152 {
00153    return CIMProperty(new CIMPropertyRep(m_rep->inst.getProperty(name.getRep()->name, originClass.getRep()->name)));
00154 }
00156 CIMProperty
00157 CIMInstance::getProperty(const CIMName& propertyName) const
00158 {
00159    int tsize = m_rep->m_properties.size();
00160    for (int i = 0; i < tsize; i++)
00161    {
00162       CIMProperty cp = m_rep->m_properties[i];
00163       if (propertyName == cp.getName())
00164       {
00165          return(cp);
00166       }
00167    }
00168    return CIMProperty(CIMNULL);
00169 }
00171 CIMProperty
00172 CIMInstance::getPropertyT(const CIMName& propertyName) const
00173 {
00174    CIMProperty p = getProperty(propertyName);
00175    if (!p)
00176    {
00177       OWBI1_THROW_ERR(NoSuchPropertyException, propertyName.toString().c_str(), E_INSTANCE_HAS_NO_SUCH_PROPERTY);
00178    }
00179    return p;
00180 }
00182 CIMValue
00183 CIMInstance::getPropertyValue(const CIMName& name) const
00184 {
00185    CIMProperty p = this->getProperty(name);
00186    if (p)
00187    {
00188       return p.getValue();
00189    }
00190    return CIMValue(CIMNULL);
00191 }
00193 bool
00194 CIMInstance::propertyHasValue(const CIMName& name) const
00195 {
00196    CIMProperty p = this->getProperty(name);
00197    if (p)
00198    {
00199       CIMValue v = p.getValue();
00200       if (v)
00201       {
00202          return true;
00203       }
00204       else
00205       {
00206          return false;
00207       }
00208    }
00209    return false;
00210 }
00212 // PRIVATE.
00213 void
00214 CIMInstance::_buildKeys()
00215 {
00216    m_rep->m_keys.clear();
00217    int tsize = m_rep->m_properties.size();
00218    for (int i = 0; i < tsize; i++)
00219    {
00220       CIMProperty cp = m_rep->m_properties[i];
00221       if (cp.isKey())
00222       {
00223          m_rep->m_keys.append(cp);
00224       }
00225    }
00226 }
00228 CIMPropertyArray
00229 CIMInstance::getKeyValuePairs() const
00230 {
00231    return m_rep->m_keys;
00232 }
00234 CIMInstance&
00235 CIMInstance::updatePropertyValues(const CIMPropertyArray& props)
00236 {
00237    int tsize = props.size();
00238    for (int i = 0; i < tsize; i++)
00239    {
00240       updatePropertyValue(props[i]);
00241    }
00242    return *this;
00243 }
00245 CIMInstance&
00246 CIMInstance::updatePropertyValue(const CIMProperty& prop)
00247 {
00248    bool buildTheKeys = false;
00249    if (prop)
00250    {
00251       CIMName name = prop.getName();
00252       int tsize = m_rep->m_properties.size();
00253       for (int i = 0; i < tsize; i++)
00254       {
00255          CIMProperty cp = m_rep->m_properties[i];
00256          CIMName rname = cp.getName();
00257          if (rname == name)
00258          {
00259             m_rep->m_properties[i].setValue(prop.getValue());
00260             if (cp.isKey() || prop.isKey())
00261             {
00262                //
00263                // If keys are not null and this is a key
00264                // rebuild the key list when we're done
00265                //
00266                buildTheKeys = true;
00267             }
00268             break;
00269          }
00270       }
00271       if (buildTheKeys)
00272       {
00273          _buildKeys();
00274       }
00275    }
00276    return *this;
00277 }
00278 
00280 CIMInstance&
00281 CIMInstance::updatePropertyValue(const CIMName& name, const CIMValue& value)
00282 {
00283    return updatePropertyValue(CIMProperty(name, value));
00284 }
00285 
00287 CIMInstance&
00288 CIMInstance::setProperty(const CIMName& name, const CIMValue& cv)
00289 {
00290    int tsize = m_rep->m_properties.size();
00291    for (int i = 0; i < tsize; i++)
00292    {
00293       CIMProperty cp = m_rep->m_properties[i];
00294       CIMName rname = cp.getName();
00295       if (rname == name)
00296       {
00297          m_rep->m_properties[i].setValue(cv);
00298          if (cp.isKey())
00299          {
00300             _buildKeys();
00301          }
00302          return *this;
00303       }
00304    }
00305    //
00306    // Not found so add it
00307    //
00308    CIMProperty cp(name);
00309    cp.setValue(cv);
00310    if (cv)
00311    {
00312       cp.setDataType(cv.getCIMDataType());
00313    }
00314    else
00315    {
00316       cp.setDataType(CIMDataType::CIMNULL);
00317    }
00318    m_rep->m_properties.append(cp);
00319    //
00320    // We don't worry about building the keys here, because the
00321    // property doesn't have the key qualifier (or any other for that matter)
00322    //
00323    return *this;
00324 }
00326 CIMInstance&
00327 CIMInstance::setProperty(const CIMProperty& prop)
00328 {
00329    if (prop)
00330    {
00331       CIMName propName = prop.getName();
00332       int tsize = m_rep->m_properties.size();
00333       for (int i = 0; i < tsize; i++)
00334       {
00335          CIMProperty cp = m_rep->m_properties[i];
00336          CIMName rname = cp.getName();
00337          if (rname == propName)
00338          {
00339             m_rep->m_properties[i] = prop;
00340             // If property was a key or is a key, then rebuild the
00341             // key values
00342             if (cp.isKey() || prop.isKey())
00343             {
00344                _buildKeys();
00345             }
00346             return *this;
00347          }
00348       }
00349       //
00350       // Not found so add it
00351       //
00352       m_rep->m_properties.append(prop);
00353       if (prop.isKey())
00354       {
00355          _buildKeys();
00356       }
00357    }
00358    return *this;
00359 }
00361 CIMInstance&
00362 CIMInstance::removeProperty(const CIMName& propName)
00363 {
00364    int tsize = m_rep->m_properties.size();
00365    for (int i = 0; i < tsize; i++)
00366    {
00367       CIMProperty cp = m_rep->m_properties[i];
00368       if (cp.getName() == propName)
00369       {
00370          m_rep->m_properties.remove(i);
00371          // If this property was a key, then rebuild the key values
00372          if (cp.isKey())
00373          {
00374             _buildKeys();
00375          }
00376          break;
00377       }
00378    }
00379    return *this;
00380 }
00382 CIMInstance
00383 CIMInstance::clone(const CIMPropertyList& propertyList) const
00384 {
00385    CIMInstance ci;
00386    ci.m_rep->m_owningClassName = m_rep->m_owningClassName;
00387    ci.m_rep->m_keys = m_rep->m_keys;
00388    ci.m_rep->m_language = m_rep->m_language;
00389    ci.m_rep->m_qualifiers = m_rep->m_qualifiers;
00390    CIMPropertyArray props;
00391    for (size_t i = 0; i < m_rep->m_properties.size(); i++)
00392    {
00393       CIMProperty prop = m_rep->m_properties[i];
00394       if (propertyList.hasProperty(prop.getName()))
00395       {
00396          props.append(prop);
00397       }
00398    }
00399    ci.m_rep->m_properties = props;
00400    return ci;
00401 }
00403 CIMName
00404 CIMInstance::getName() const
00405 {
00406    return m_rep->m_owningClassName;
00407 }
00409 CIMInstance&
00410 CIMInstance::syncWithClass(const CIMClass& theClass)
00411 {
00412    if (!theClass)
00413    {
00414       return *this;
00415    }
00416    CIMName propName;
00417    CIMPropertyArray classProps = theClass.getAllProperties();
00418    CIMPropertyArray instProps = getProperties();
00419    
00420    // Remove properties that are not defined in the class
00421    size_t i = 0;
00422    while (i < instProps.size())
00423    {
00424       propName = instProps[i].getName();
00425       if (!theClass.getProperty(propName))
00426       {
00427          instProps.remove(i);
00428       }
00429       else
00430       {
00431          ++i;
00432       }
00433    }
00434 
00435    // Add missing properties and ensure existing have right class origin,
00436    // and qualifiers
00437    for (size_t i = 0; i < classProps.size(); i++)
00438    {
00439       bool found = false;
00440       CIMProperty cprop = classProps[i];
00441       propName = cprop.getName();
00442       for (size_t j = 0; j < instProps.size(); j++)
00443       {
00444          CIMProperty iprop = instProps[j];
00445          if (iprop.getName() == propName)
00446          {
00447             CIMValue cv = iprop.getValue();
00448             iprop = cprop;
00449             if (cv)
00450             {
00451                if (cv.getType() != iprop.getDataType().getType())
00452                {
00453                   // workaround for the stupid dmtf string/embedded object hack
00454                   if (cv.getType() != CIMDataType::EMBEDDEDCLASS && cv.getType() != CIMDataType::EMBEDDEDINSTANCE)
00455                   {
00456                      cv = CIMValueCast::castValueToDataType(cv,
00457                         iprop.getDataType());
00458                   }
00459                }
00460                iprop.setValue(cv);
00461             }
00462             instProps[j] = iprop;
00463             found = true;
00464             break;
00465          }
00466       }
00467       if (!found)
00468       {
00469          instProps.append(classProps[i]);
00470       }
00471    }
00472    setProperties(instProps);
00473    return *this;
00474 }
00476 CIMInstance
00477 CIMInstance::createModifiedInstance(
00478    const CIMInstance& previousInstance,
00479    const CIMPropertyList& propertyList,
00480    const CIMClass& theClass) const
00481 {
00482    CIMInstance newInst(*this);
00483    newInst.setQualifiers(previousInstance.getQualifiers());
00484    if (!propertyList.isAllProperties())
00485    {
00486       newInst.setProperties(previousInstance.getProperties());
00487       for (CIMPropertyList::const_iterator i = propertyList.begin();
00488           i != propertyList.end(); ++i)
00489       {
00490          CIMProperty p = this->getProperty(*i);
00491          if (p)
00492          {
00493             CIMProperty cp = theClass.getProperty(*i);
00494             if (cp)
00495             {
00496                p.setQualifiers(cp.getQualifiers());
00497             }
00498             newInst.setProperty(p);
00499          }
00500          else
00501          {
00502             CIMProperty cp = theClass.getProperty(*i);
00503             if (cp)
00504             {
00505                newInst.setProperty(cp);
00506             }
00507             else
00508             {
00509                newInst.removeProperty(*i);
00510             }
00511          }
00512       }
00513    }
00514    return newInst;
00515 }
00517 void
00518 CIMInstance::setName(const CIMName& name)
00519 {
00520    m_rep->m_owningClassName = name;
00521 }
00523 void
00524 CIMInstance::readObject(istream &istrm)
00525 {
00526    CIMName owningClassName;
00527    CIMPropertyArray properties;
00528    CIMPropertyArray keys;
00529    CIMQualifierArray qualifiers;
00530    String language;
00531 
00532    UInt32 version = CIMBase::readSig(istrm, OWBI1_CIMINSTANCESIG,
00533       OWBI1_CIMINSTANCESIG_V, CIMInstance::SERIALIZATION_VERSION);
00534 
00535    owningClassName.readObject(istrm);
00536    BinarySerialization::readArray(istrm, keys);
00537    BinarySerialization::readArray(istrm, properties);
00538    BinarySerialization::readArray(istrm, qualifiers);
00539    // If dealing with versioned format, then read language
00540    if (version > 0)
00541    {
00542       language.readObject(istrm);
00543    }
00544    if (!m_rep)
00545    {
00546       m_rep = new INSTData;
00547    }
00548    m_rep->m_owningClassName = owningClassName;
00549    m_rep->m_keys = keys;
00550    m_rep->m_properties = properties;
00551    m_rep->m_qualifiers = qualifiers;
00552    m_rep->m_language = language;
00553 }
00555 void
00556 CIMInstance::writeObject(std::ostream &ostrm) const
00557 {
00558    CIMBase::writeSig(ostrm, OWBI1_CIMINSTANCESIG_V, CIMInstance::SERIALIZATION_VERSION);
00559    m_rep->m_owningClassName.writeObject(ostrm);
00560    BinarySerialization::writeArray(ostrm, m_rep->m_keys);
00561    BinarySerialization::writeArray(ostrm, m_rep->m_properties);
00562    BinarySerialization::writeArray(ostrm, m_rep->m_qualifiers);
00563    m_rep->m_language.writeObject(ostrm);
00564 }
00566 String
00567 CIMInstance::toMOF() const
00568 {
00569    size_t i;
00570    StringBuffer rv;
00571    if (m_rep->m_qualifiers.size() > 0)
00572    {
00573       rv += "[\n";
00574       for (i = 0; i < m_rep->m_qualifiers.size(); i++)
00575       {
00576          if (i > 0)
00577          {
00578             rv += ',';
00579          }
00580          rv += m_rep->m_qualifiers[i].toMOF();
00581       }
00582       rv += "]\n";
00583    }
00584    rv += "INSTANCE OF ";
00585    rv += m_rep->m_owningClassName.toString();
00586    rv += "\n{\n";
00587    for (i = 0; i < m_rep->m_properties.size(); i++)
00588    {
00589       // note that we can't use CIMProperty::toMOF() since it prints out
00590       // the data type.
00591       const CIMProperty& p = m_rep->m_properties[i];
00592       if (p.hasTrueQualifier(CIMQualifier::CIM_QUAL_INVISIBLE))
00593       {
00594          continue;
00595       }
00596       CIMValue v = p.getValue();
00597       if (v)
00598       {
00599          // do qualifiers
00600          CIMQualifierArray qualifiers = p.getQualifiers();
00601          if (qualifiers.size() > 0)
00602          {
00603             rv += "  [";
00604             for (size_t i = 0; i < qualifiers.size(); i++)
00605             {
00606                const CIMQualifier& nq = qualifiers[i];
00607                if (i > 0)
00608                {
00609                   rv += ',';
00610                }
00611                rv += nq.toMOF();
00612             }
00613             rv += "]\n";
00614          }
00615          rv += "  ";
00616          rv += p.getName().toString();
00617          rv += '=';
00618          rv += v.toMOF();
00619          rv += ";\n";
00620       }
00621       else
00622       {
00623          rv += "  ";
00624          rv += p.getName().toString();
00625          rv += "=NULL;\n";
00626       }
00627    }
00628    rv += "};\n";
00629    return rv.releaseString();
00630 }
00632 String
00633 CIMInstance::toString() const
00634 {
00635    size_t i;
00636    StringBuffer temp;
00637    String outVal;
00638    temp += "instance of ";
00639    temp += m_rep->m_owningClassName.toString() + " {\n";
00640    for (i = 0; i < m_rep->m_properties.size(); i++)
00641    {
00642       CIMProperty cp = m_rep->m_properties[i];
00643       if (cp.hasTrueQualifier(CIMQualifier::CIM_QUAL_INVISIBLE))
00644       {
00645          continue;
00646       }
00647 
00648       CIMValue val = cp.getValue();
00649       if (!val)
00650       {
00651          outVal = "null";
00652       }
00653       else
00654       {
00655          outVal = val.toString();
00656       }
00657       temp += cp.getName().toString() + " = " + outVal + ";\n";
00658    }
00659    temp += "}\n";
00660    return temp.releaseString();
00661 }
00663 bool operator<(const CIMInstance& x, const CIMInstance& y)
00664 {
00665    return *x.m_rep < *y.m_rep;
00666 }
00668 bool CIMInstance::propertiesAreEqualTo(const CIMInstance& other) const
00669 {
00670    CIMPropertyArray props1(getProperties());
00671    CIMPropertyArray props2(other.getProperties());
00672    if (props1.size() != props2.size())
00673    {
00674       return false;
00675    }
00676    std::sort(props1.begin(), props1.end());
00677    std::sort(props2.begin(), props2.end());
00678    CIMPropertyArray::iterator i1 = props1.begin();
00679    CIMPropertyArray::iterator i2 = props2.begin();
00680    while (i1 != props1.end())
00681    {
00682       CIMProperty p1 = *i1;
00683       CIMProperty p2 = *i2;
00684       if (p1 != p2) // checks the name
00685       {
00686          return false;
00687       }
00688       if (p1.getValue() != p2.getValue()) // check the value
00689       {
00690          return false;
00691       }
00692       ++i1;
00693       ++i2;
00694    }
00695    return true;
00696 }
00697 
00698 CIMInstance::safe_bool
00699 CIMInstance::operator CIMInstance::safe_bool() const
00700 {  
00701    return m_rep->inst ? &CIMInstance::m_rep : 0; 
00702 }
00703 
00704 bool 
00705 CIMInstance::operator!() const
00706 {  
00707    return !(m_rep->inst); 
00708 }
00709 
00710 CIMInstanceRepRef
00711 CIMInstance::getRep() const
00712 {
00713    return m_rep;
00714 }
00715 
00716 } // end namespace OWBI1
00717 

Generated on Thu Feb 9 08:48:25 2006 for openwbem by  doxygen 1.4.6