OW_CIMNameSpaceUtils.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2003-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 
00035 #include "OW_config.h"
00036 #include "OW_CIMNameSpaceUtils.hpp"
00037 #include "OW_CIMException.hpp"
00038 #include "OW_String.hpp"
00039 #include "OW_CIMClass.hpp"
00040 #include "OW_CIMObjectPath.hpp"
00041 #include "OW_CIMInstanceEnumeration.hpp"
00042 #include "OW_CIMValue.hpp"
00043 #include "OW_CIMProperty.hpp"
00044 #include "OW_CIMQualifier.hpp"
00045 #include "OW_ResultHandlerIFC.hpp"
00046 
00047 namespace OW_NAMESPACE
00048 {
00049 
00050 using namespace WBEMFlags;
00051 namespace CIMNameSpaceUtils
00052 {
00053 namespace
00054 {
00055    CIMClass the__NamespaceClass(CIMNULL);
00056    class StringArrayBuilder : public StringResultHandlerIFC
00057    {
00058    public:
00059       StringArrayBuilder(StringArray& a) : m_a(a)
00060       {
00061       }
00062    protected:
00063       virtual void doHandle(const String &s)
00064       {
00065          m_a.push_back(s);
00066       }
00067    private:
00068       StringArray& m_a;
00069    };
00070    class NamespaceObjectPathToStringHandler : public CIMObjectPathResultHandlerIFC
00071    {
00072    public:
00073       NamespaceObjectPathToStringHandler(StringResultHandlerIFC& result_)
00074          : result(result_)
00075       {}
00076       void doHandle(const CIMObjectPath& op)
00077       {
00078          result.handle(op.getKeyT("Name").getValueT().toString());
00079       }
00080    private:
00081       StringResultHandlerIFC& result;
00082    };
00083 }
00085 String
00086 prepareNamespace(const String& ns_)
00087 {
00088    String ns(ns_);
00089    // translate \\ to /
00090    for (size_t i = 0; i < ns.length(); ++i )
00091    {
00092       if (ns[i] == '\\')
00093       {
00094          ns[i] = '/';
00095       }
00096    }
00097    while (!ns.empty() && ns[0] == '/')
00098    {
00099       ns = ns.substring(1);
00100    }
00101    return ns;
00102 }
00103 #ifndef OW_DISABLE_INSTANCE_MANIPULATION
00104 
00105 void
00106 createCIM_Namespace(CIMOMHandleIFC& hdl, const String& ns_, UInt16 classInfo, const String& descriptionOfClassInfo, const String& interopNs)
00107 {
00108    String ns(prepareNamespace(ns_));
00109    CIMClass theCIM_NamespaceClass = hdl.getClass(interopNs, "CIM_Namespace");
00110    CIMClass theAssocCls = hdl.getClass(interopNs, "CIM_NamespaceInManager");
00111    CIMObjectPathEnumeration e = hdl.enumInstanceNamesE(interopNs, "CIM_ObjectManager");
00112    if (e.numberOfElements() != 1)
00113    {
00114       OW_THROWCIMMSG(CIMException::FAILED, "Failed to get one instance of "
00115          "CIM_ObjectManager.  Unable to create an instance of CIM_Namespace");
00116    }
00117    CIMObjectPath theObjectManager = e.nextElement();
00118    CIMInstance cimInstance = theCIM_NamespaceClass.newInstance();
00119    cimInstance.setProperty(theObjectManager.getKeyT("SystemCreationClassName"));
00120    cimInstance.setProperty(theObjectManager.getKeyT("SystemName"));
00121    cimInstance.setProperty("ObjectManagerCreationClassName", theObjectManager.getKeyT("CreationClassName").getValue());
00122    cimInstance.setProperty("ObjectManagerName", theObjectManager.getKeyT("Name").getValue());
00123    cimInstance.setProperty("CreationClassName", CIMValue("CIM_Namespace"));
00124    cimInstance.setProperty("Name", CIMValue(ns));
00125    cimInstance.setProperty("ClassInfo", CIMValue(classInfo));
00126    if (!descriptionOfClassInfo.empty())
00127    {
00128       cimInstance.setProperty("DescriptionOfClassInfo", CIMValue(descriptionOfClassInfo));
00129    }
00130    CIMObjectPath theNewNsPath = hdl.createInstance(interopNs, cimInstance);
00131    CIMInstance theAssoc = theAssocCls.newInstance();
00132    theAssoc.setProperty("Antecedent", CIMValue(theObjectManager));
00133    theAssoc.setProperty("Dependent", CIMValue(theNewNsPath));
00134    hdl.createInstance(interopNs, theAssoc);
00135 }
00137 void
00138 deleteCIM_Namespace(CIMOMHandleIFC& hdl, const String& ns_, const String& interopNs)
00139 {
00140    String ns(prepareNamespace(ns_));
00141    CIMObjectPathEnumeration e = hdl.enumInstanceNamesE(interopNs, "CIM_ObjectManager");
00142    if (e.numberOfElements() != 1)
00143    {
00144       OW_THROWCIMMSG(CIMException::FAILED, "Failed to get one instance of "
00145          "CIM_ObjectManager.  Unable to create an instance of CIM_Namespace");
00146    }
00147    CIMObjectPath theObjectManager = e.nextElement();
00148    CIMObjectPath nsPath("CIM_Namespace", interopNs);
00149    CIMPropertyArray keys;
00150    keys.push_back(theObjectManager.getKeyT("SystemCreationClassName"));
00151    keys.push_back(theObjectManager.getKeyT("SystemName"));
00152    keys.push_back(CIMProperty("ObjectManagerCreationClassName", theObjectManager.getKeyT("CreationClassName").getValue()));
00153    keys.push_back(CIMProperty("ObjectManagerName", theObjectManager.getKeyT("Name").getValue()));
00154    keys.push_back(CIMProperty("CreationClassName", CIMValue("CIM_Namespace")));
00155    keys.push_back(CIMProperty("Name", CIMValue(ns)));
00156    nsPath.setKeys(keys);
00157 
00158    CIMObjectPath theAssoc("CIM_NamespaceInManager", interopNs);
00159    keys.clear();
00160    keys.push_back(CIMProperty("Antecedent", CIMValue(theObjectManager)));
00161    keys.push_back(CIMProperty("Dependent", CIMValue(nsPath)));
00162    theAssoc.setKeys(keys);
00163 
00164    hdl.deleteInstance(interopNs, theAssoc);
00165    hdl.deleteInstance(interopNs, nsPath);
00166 }
00167 #endif // #ifndef OW_DISABLE_INSTANCE_MANIPULATION
00168 
00169 StringArray
00170 enumCIM_Namespace(CIMOMHandleIFC& hdl, const String& interopNs)
00171 {
00172    StringArray rval;
00173    StringArrayBuilder handler(rval);
00174    enumCIM_Namespace(hdl, handler, interopNs);
00175    return rval;
00176 }
00178 void
00179 enumCIM_Namespace(CIMOMHandleIFC& hdl,
00180    StringResultHandlerIFC& result, const String& interopNs)
00181 {
00182    NamespaceObjectPathToStringHandler handler(result);
00183    hdl.enumInstanceNames(interopNs, "CIM_Namespace", handler);
00184 }
00185 #ifndef OW_DISABLE_INSTANCE_MANIPULATION
00186 
00187 namespace
00188 {
00189 const char* const DEPRECATED__NamespaceClassName = "__Namespace";
00190 }
00191 
00192 void
00193 create__Namespace(CIMOMHandleIFC& hdl, const String& ns_)
00194 {
00195    String ns(prepareNamespace(ns_));
00196    size_t index = ns.lastIndexOf('/');
00197    String parentPath = ns.substring(0, index);
00198    String newNameSpace = ns.substring(index + 1);
00199    if (!the__NamespaceClass)
00200    {
00201       the__NamespaceClass = CIMClass(DEPRECATED__NamespaceClassName);
00202       CIMProperty cimProp(CIMProperty::NAME_PROPERTY);
00203       cimProp.setDataType(CIMDataType::STRING);
00204       cimProp.addQualifier(CIMQualifier::createKeyQualifier());
00205       the__NamespaceClass.addProperty(cimProp);
00206    }
00207    CIMInstance cimInstance = the__NamespaceClass.newInstance();
00208    CIMValue cv(newNameSpace);
00209    cimInstance.setProperty("Name", cv);
00210    hdl.createInstance(parentPath, cimInstance);
00211 }
00213 void
00214 delete__Namespace(CIMOMHandleIFC& hdl, const String& ns_)
00215 {
00216    String ns(prepareNamespace(ns_));
00217    size_t index = ns.lastIndexOf('/');
00218    String parentPath = ns.substring(0,index);
00219    String newNameSpace = ns.substring(index + 1);
00220    CIMPropertyArray v;
00221    CIMValue cv(newNameSpace);
00222    CIMProperty cp("Name", cv);
00223    cp.setDataType(CIMDataType::STRING);
00224    v.push_back(cp);
00225    CIMObjectPath path(DEPRECATED__NamespaceClassName, v);
00226    hdl.deleteInstance(parentPath, path);
00227 }
00228 #endif // #ifndef OW_DISABLE_INSTANCE_MANIPULATION
00229 
00230 StringArray
00231 enum__Namespace(CIMOMHandleIFC& hdl, const String& ns, EDeepFlag deep)
00232 {
00233    StringArray rval;
00234    StringArrayBuilder handler(rval);
00235    enum__Namespace(hdl, ns, handler, deep);
00236    return rval;
00237 }
00238 namespace
00239 {
00241    void
00242    enumNameSpaceAux(CIMOMHandleIFC& hdl,
00243       const String& ns,
00244       StringResultHandlerIFC& result, EDeepFlag deep)
00245    {
00246       // can't use the callback version of enumInstances, because the recursion
00247       // throws a wrench in the works.  Each CIM Method call has to finish
00248       // before another one can begin.
00249       CIMInstanceEnumeration en = hdl.enumInstancesE(ns,
00250          String(DEPRECATED__NamespaceClassName), E_SHALLOW, E_LOCAL_ONLY);
00251       while (en.hasMoreElements())
00252       {
00253          CIMInstance i = en.nextElement();
00254          CIMValue nameVal = i.getPropertyValue("Name");
00255          if (!nameVal || nameVal.getType() != CIMDataType::STRING
00256              || nameVal.isArray())
00257          {
00258              OW_THROWCIMMSG(CIMException::FAILED,
00259                 "Name of namespace not found");
00260          }
00261          String name;
00262          nameVal.get(name);
00263 
00264          result.handle(ns + "/" + name);
00265          if (deep)
00266          {
00267             enumNameSpaceAux(hdl, ns + "/" + name, result, deep);
00268          }
00269 
00270       }
00271    }
00272 }
00274 void
00275 enum__Namespace(CIMOMHandleIFC& hdl, const String& ns_,
00276    StringResultHandlerIFC& result, EDeepFlag deep)
00277 {
00278    String ns(prepareNamespace(ns_));
00279    result.handle(ns);
00280    enumNameSpaceAux(hdl, ns, result, deep);
00281 }
00282 } // end namespace OW_CIMNameSpaceUtils
00283 
00284 } // end namespace OW_NAMESPACE
00285 

Generated on Thu Feb 9 08:47:53 2006 for openwbem by  doxygen 1.4.6