OW_GenericHDBRepository.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 "OW_config.h"
00037 #include "OW_GenericHDBRepository.hpp"
00038 #include "OW_Assertion.hpp"
00039 #include "OW_CIMException.hpp"
00040 #include "OW_DataStreams.hpp"
00041 #include "OW_IOException.hpp"
00042 #include "OW_Format.hpp"
00043 
00044 namespace OW_NAMESPACE
00045 {
00046 
00047 namespace
00048 {
00049    const char NS_SEPARATOR_C(':');
00050    const int HDL_NOTINUSE = -1;  
00051    const int HDL_NOTCACHED = -2;
00052 }
00053 
00054 const String GenericHDBRepository::COMPONENT_NAME("ow.repository.hdb");
00056 GenericHDBRepository::GenericHDBRepository()
00057    : m_opened(false)
00058 {
00059 }
00060 
00062 void
00063 GenericHDBRepository::init(const ServiceEnvironmentIFCRef& env)
00064 {
00065    m_env = env;
00066 }
00067 
00069 GenericHDBRepository::~GenericHDBRepository()
00070 {
00071    try
00072    {
00073       close();
00074    }
00075    catch (...)
00076    {
00077       // don't let exceptions escape
00078    }
00079 }
00081 HDBHandle
00082 GenericHDBRepository::getHandle()
00083 {
00084    MutexLock ml(m_guard);
00085    for (int i = 0; i < int(m_handles.size()); i++)
00086    {
00087       if (m_handles[i].getUserValue() == HDL_NOTINUSE)
00088       {
00089          m_handles[i].setUserValue(i);    // Set user value to index
00090          return m_handles[i];
00091       }
00092    }
00093    HDBHandle hdl = m_hdb.getHandle();
00094    if (m_handles.size() < MAXHANDLES)
00095    {
00096       hdl.setUserValue(m_handles.size());
00097       m_handles.append(hdl);
00098    }
00099    else
00100    {
00101       hdl.setUserValue(HDL_NOTCACHED);
00102    }
00103    return hdl;
00104 }
00106 void
00107 GenericHDBRepository::freeHandle(HDBHandle& hdl)
00108 {
00109    MutexLock ml(m_guard);
00110    Int32 uv = hdl.getUserValue();
00111    if (uv > HDL_NOTINUSE && uv < Int32(m_handles.size()))
00112    {
00113       // Handle is from the cache, so flag it as not in use.
00114       hdl.flush();
00115       m_handles[uv].setUserValue(HDL_NOTINUSE);
00116    }
00117 }
00119 void
00120 GenericHDBRepository::open(const String& path)
00121 {
00122    MutexLock ml(m_guard);
00123    close();
00124    m_hdb.open(path.c_str());
00125    m_opened = true;
00126 #if !defined(OW_DISABLE_INSTANCE_MANIPULATION) && !defined(OW_DISABLE_NAMESPACE_MANIPULATION)
00127    // Create root namespace
00128    createNameSpace("root");
00129 #endif
00130    //HDBHandleLock hdl(this, getHandle());
00131    //String contk("root");
00132    //contk.toLowerCase();
00133    //HDBNode node = hdl->getNode(contk);
00134    //if (!node)
00135    //{
00136    // node = HDBNode(contk, contk.length()+1,
00137    //    reinterpret_cast<const unsigned char*>(contk.c_str()));
00138    // hdl->turnFlagsOn(node, HDBNSNODE_FLAG);
00139    // hdl->addRootNode(node);
00140    //}
00141 }
00143 void
00144 GenericHDBRepository::close()
00145 {
00146    MutexLock ml(m_guard);
00147    if (!m_opened)
00148    {
00149       return;
00150    }
00151    m_opened = false;
00152    for (int i = 0; i < int(m_handles.size()); i++)
00153    {
00154       if (m_handles[i].getUserValue() > HDL_NOTINUSE)
00155       {
00156          //cerr << "GenericHDBRepository::close HANDLES ARE STILL IN USE!!!!"
00157          // << endl;
00158          break;
00159       }
00160    }
00161    m_handles.clear();
00162    m_hdb.close();
00163 }
00165 HDBNode
00166 GenericHDBRepository::getNameSpaceNode(HDBHandleLock& hdl,
00167    String ck)
00168 {
00169    if (ck.empty())
00170    {
00171       return HDBNode();
00172    }
00173    HDBNode node = hdl->getNode(ck);
00174    if (node)
00175    {
00176       if (!node.areAllFlagsOn(HDBNSNODE_FLAG))
00177       {
00178          OW_THROW(IOException, "logic error. Expected namespace node");
00179       }
00180    }
00181    return node;
00182 }
00183 #if !defined(OW_DISABLE_INSTANCE_MANIPULATION) && !defined(OW_DISABLE_NAMESPACE_MANIPULATION)
00184 
00185 int
00186 GenericHDBRepository::createNameSpace(const String& ns)
00187 {
00188    throwIfNotOpen();
00189    HDBHandleLock hdl(this, getHandle());
00190    HDBNode node;
00191    if (ns.empty())
00192    {
00193       return -1;
00194    }
00195    
00196    node = hdl->getNode(ns);
00197    if (!node)
00198    {
00199       // create the namespace
00200       node = HDBNode(ns, ns.length()+1,
00201          reinterpret_cast<const unsigned char*>(ns.c_str()));
00202       hdl->turnFlagsOn(node, HDBNSNODE_FLAG);
00203       hdl->addRootNode(node);
00204       OW_LOG_DEBUG(m_env->getLogger(COMPONENT_NAME), Format("created namespace %1", ns));
00205    }
00206    else
00207    {
00208       // it already exists, return -1.
00209       if (!node.areAllFlagsOn(HDBNSNODE_FLAG))
00210       {
00211          OW_THROW(IOException,
00212             "logic error. read namespace node that is not a namespace");
00213       }
00214       return -1;
00215    }
00216    return 0;
00217 }
00219 void
00220 GenericHDBRepository::deleteNameSpace(const String& key)
00221 {
00222    throwIfNotOpen();
00223    if (key.equals("root"))
00224    {
00225       OW_THROWCIMMSG(CIMException::FAILED,
00226          "cannot delete root namespace");
00227    }
00228    HDBHandleLock hdl(this, getHandle());
00229    HDBNode node = hdl->getNode(key);
00230    if (node)
00231    {
00232       if (!node.areAllFlagsOn(HDBNSNODE_FLAG))
00233       {
00234          OW_THROW(IOException, "logic error. deleting non-namespace node");
00235       }
00236       hdl->removeNode(node);
00237    }
00238    else
00239    {
00240       OW_THROWCIMMSG(CIMException::FAILED, Format("Unable to delete namespace %1", key).c_str());
00241    }
00242 }
00243 #endif
00244 
00245 bool
00246 GenericHDBRepository::nameSpaceExists(const String& key)
00247 {
00248    throwIfNotOpen();
00249    HDBHandleLock hdl(this, getHandle());
00250    HDBNode node = hdl->getNode(key);
00251    if (node)
00252    {
00253       if (!node.areAllFlagsOn(HDBNSNODE_FLAG))
00254       {
00255          return false;
00256       }
00257       return true;
00258    }
00259    return false;
00260 }
00262 void
00263 GenericHDBRepository::nodeToCIMObject(CIMBase& cimObj,
00264    const HDBNode& node)
00265 {
00266    if (node)
00267    {
00268       DataIStream istrm(node.getDataLen(), node.getData());
00269       cimObj.readObject(istrm);
00270    }
00271    else
00272    {
00273       cimObj.setNull();
00274    }
00275 }
00277 void
00278 GenericHDBRepository::getCIMObject(CIMBase& cimObj, const String& key,
00279    HDBHandle hdl)
00280 {
00281    nodeToCIMObject(cimObj, hdl.getNode(key));
00282 }
00284 void
00285 GenericHDBRepository::updateCIMObject(const CIMBase& cimObj,
00286    HDBNode& node, HDBHandle hdl)
00287 {
00288    DataOStream ostrm;
00289    cimObj.writeObject(ostrm);
00290    hdl.updateNode(node, ostrm.length(), ostrm.getData());
00291 }
00293 void
00294 GenericHDBRepository::addCIMObject(const CIMBase& cimObj,
00295    const String& key, HDBNode& parentNode, HDBHandle hdl,
00296    UInt32 nodeFlags)
00297 {
00298    DataOStream ostrm;
00299    cimObj.writeObject(ostrm);
00300    HDBNode node(key, ostrm.length(), ostrm.getData());
00301    node.turnFlagsOn(hdl, nodeFlags);
00302    hdl.addChild(parentNode, node);
00303 }
00305 void
00306 GenericHDBRepository::addCIMObject(const CIMBase& cimObj,
00307    const String& key, HDBHandle hdl, UInt32 nodeFlags)
00308 {
00309    DataOStream ostrm;
00310    cimObj.writeObject(ostrm);
00311    HDBNode node(key, ostrm.length(), ostrm.getData());
00312    node.turnFlagsOn(hdl, nodeFlags);
00313    hdl.addRootNode(node);
00314 }
00315 
00316 } // end namespace OW_NAMESPACE
00317 

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