OW_provinstOpenWBEM_ObjectManager.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_CppProviderIncludes.hpp"
00037 #include "OW_ConfigOpts.hpp"
00038 #include "OW_UUID.hpp"
00039 #include "OW_Thread.hpp"
00040 
00041 namespace OW_NAMESPACE
00042 {
00043 
00044 namespace
00045 {
00046    const String COMPONENT_NAME("ow.provider.OpenWBEM_ObjectManager");
00047    const String CLASS_OpenWBEM_ObjectManager("OpenWBEM_ObjectManager");
00048    const String CLASS_OpenWBEM_InternalData("OpenWBEM_InternalData");
00049    const String Class_OpenWBEM_ComputerSystem("OpenWBEM_UnitaryComputerSystem");
00050    const String dataKey("OpenWBEM_ObjectManager.Name");
00051    const String CLASS_CIM_InstModification("CIM_InstModification");
00052    const String CLASS_OpenWBEM_HostedObjectManager("OpenWBEM_HostedObjectManager");
00053    const CIMName PROP_Antecedent("Antecedent");
00054    const CIMName PROP_Dependent("Dependent");
00055    const CIMName PROP_SystemCreationClassName("SystemCreationClassName");
00056    const CIMName PROP_CreationClassName("CreationClassName");
00057    const CIMName PROP_Name("Name");
00058    const CIMName PROP_SystemName("SystemName");
00059 }
00060 
00061 using namespace WBEMFlags;
00062 class OpenWBEM_ObjectManagerInstProv
00063    : public CppReadOnlyInstanceProviderIFC
00064    , public CppSimpleInstanceProviderIFC
00065    , public CppIndicationProviderIFC
00066 #ifndef OW_DISABLE_ASSOCIATION_TRAVERSAL
00067    , public CppSimpleAssociatorProviderIFC
00068 #endif // #ifndef OW_DISABLE_ASSOCIATION_TRAVERSAL
00069 {
00070 private:
00071    // Only have one Object Manager
00072    CIMInstance m_inst;
00073    bool m_haveSubscriptions;
00074 
00075 public:
00077    OpenWBEM_ObjectManagerInstProv()
00078       : m_inst(CIMNULL)
00079       , m_haveSubscriptions(false)
00080    {
00081    }
00082    
00084    virtual void getInstanceProviderInfo(InstanceProviderInfo& info)
00085    {
00086       info.addInstrumentedClass(CLASS_OpenWBEM_ObjectManager);
00087       info.addInstrumentedClass(CLASS_OpenWBEM_HostedObjectManager);
00088    }
00089 
00090 #ifndef OW_DISABLE_ASSOCIATION_TRAVERSAL
00091 
00092    virtual void getAssociatorProviderInfo(AssociatorProviderInfo& info)
00093    {
00094       info.addInstrumentedClass(CLASS_OpenWBEM_HostedObjectManager);
00095    }
00096 #endif // #ifndef OW_DISABLE_ASSOCIATION_TRAVERSAL
00097 
00099    virtual void getIndicationProviderInfo(IndicationProviderInfo& info)
00100    {
00101       // Add the class(es) we are monitoring for lifecycle indications.
00102       const char* theMonitoredClasses[] =
00103          {
00104             "OpenWBEM_ObjectManager",
00105             0
00106          };
00107 
00108       // all the life-cycle indications that may be generated by this provider
00109       const char* instanceLifeCycleIndicationClassNames[] =
00110          {
00111             "CIM_InstModification",
00112             "CIM_InstIndication",
00113             "CIM_Indication",
00114             0
00115          };
00116 
00117       for (const char** pIndicationClassName = instanceLifeCycleIndicationClassNames;
00118            *pIndicationClassName != 0;
00119            ++pIndicationClassName)
00120       {
00121          const char* indicationClassName = *pIndicationClassName;
00122          IndicationProviderInfoEntry e(indicationClassName);
00123          for (const char** pClassName = theMonitoredClasses;
00124               *pClassName != 0;
00125               ++pClassName)
00126          {
00127             const char* className = *pClassName;
00128             e.classes.push_back(className);
00129          }
00130          info.addInstrumentedClass(e);
00131 
00132       }
00133    }
00134 
00135    void activateFilter(
00136       const ProviderEnvironmentIFCRef& env,
00137       const WQLSelectStatement& filter,
00138       const String& eventType,
00139       const String& nameSpace,
00140       const StringArray& classes,
00141       bool firstActivation
00142       )
00143    {
00144       m_haveSubscriptions = true;
00145    }
00146 
00147    void deActivateFilter(
00148       const ProviderEnvironmentIFCRef& env,
00149       const WQLSelectStatement& filter,
00150       const String& eventType,
00151       const String& nameSpace,
00152       const StringArray& classes,
00153       bool lastActivation
00154       )
00155    {
00156       if (lastActivation)
00157       {
00158          m_haveSubscriptions = false;
00159       }
00160    }
00161 
00163    virtual void initialize(const ProviderEnvironmentIFCRef &env)
00164    {
00165       // retrieve the name from the repository
00166       String interopNS = env->getConfigItem(ConfigOpts::INTEROP_SCHEMA_NAMESPACE_opt, OW_DEFAULT_INTEROP_SCHEMA_NAMESPACE);
00167       CIMOMHandleIFCRef rephdl(env->getRepositoryCIMOMHandle());
00168 
00169       String omName; // will be set to something if we find a previous instance in the repository.
00170       try
00171       {
00172          // get the uuid
00173          CIMObjectPath omNamePath(CLASS_OpenWBEM_InternalData, interopNS);
00174          omNamePath.setKeyValue(PROP_Name, CIMValue(dataKey));
00175          CIMInstance nameInst(rephdl->getInstance(interopNS, omNamePath));
00176          omName = nameInst.getPropertyValue("Value").toString();
00177       }
00178       catch (CIMException& e)
00179       {
00180          if (e.getErrNo() == CIMException::NOT_FOUND)
00181          {
00182 #ifndef OW_DISABLE_INSTANCE_MANIPULATION
00183             omName = OW_PACKAGE_PREFIX ":";
00184             if (omName == ":")
00185             {
00186                // OW_PACKAGE_PREFIX is empty
00187                omName = "OpenWBEM:";
00188             }
00189             omName += UUID().toString();
00190             
00191             // now save it
00192             CIMClass OpenWBEM_InternalData(rephdl->getClass(interopNS, CLASS_OpenWBEM_InternalData));
00193             CIMInstance newData(OpenWBEM_InternalData.newInstance());
00194             newData.updatePropertyValue(PROP_Name, CIMValue(dataKey));
00195             newData.updatePropertyValue("Value", CIMValue(omName));
00196             rephdl->createInstance(interopNS, newData);
00197 #else
00198             throw;
00199 #endif
00200          }
00201          else
00202          {
00203             throw;
00204          }
00205       }
00206 
00207       m_inst = createTheInstance(env, omName);
00208    }
00209 
00211    virtual void shuttingDown(const ProviderEnvironmentIFCRef &env)
00212    {
00213       if (m_haveSubscriptions)
00214       {
00215          // build the indication
00216          String interopNS = env->getConfigItem(ConfigOpts::INTEROP_SCHEMA_NAMESPACE_opt, OW_DEFAULT_INTEROP_SCHEMA_NAMESPACE);
00217          CIMOMHandleIFCRef hdl(env->getCIMOMHandle());
00218          try
00219          {
00220             CIMClass CIM_InstModification(hdl->getClass(interopNS, CLASS_CIM_InstModification));
00221             CIMInstance indicationInst(CIM_InstModification.newInstance());
00222             indicationInst.updatePropertyValue("PreviousInstance", CIMValue(m_inst));
00223             // set our instance to Started=false
00224             m_inst.updatePropertyValue("Started", CIMValue(false));
00225             indicationInst.updatePropertyValue("SourceInstance", CIMValue(m_inst));
00226             indicationInst.setProperty("IndicationTime", CIMValue(CIMDateTime(DateTime::getCurrent())));
00227             hdl->exportIndication(indicationInst, interopNS);
00228             // Wait a bit to give the indication server time to export it.
00229             // Yeah this doesn't guarantee the listeners will receive the indication,
00230             // but shutting down is more important. And if we don't sleep, there's no chance
00231             // they'll ever get it.
00232             Thread::sleep(1000);
00233          }
00234          catch (Exception& e)
00235          {
00236             OW_LOG_ERROR(env->getLogger(COMPONENT_NAME), Format("OpenWBEM_ObjectManager caught exception while exporting indication in shuttingDown(): %1", e));
00237          }
00238             
00239       }
00240    }
00241 
00243    static CIMInstance createTheInstance(const ProviderEnvironmentIFCRef &env, const String& omName)
00244    {
00245       String interopNS = env->getConfigItem(ConfigOpts::INTEROP_SCHEMA_NAMESPACE_opt, OW_DEFAULT_INTEROP_SCHEMA_NAMESPACE);
00246       CIMOMHandleIFCRef hdl(env->getCIMOMHandle());
00247       
00248       CIMClass OpenWBEM_ObjectManager(hdl->getClass(interopNS, CLASS_OpenWBEM_ObjectManager));
00249 
00250       CIMInstance newInst(OpenWBEM_ObjectManager.newInstance());
00251 
00252       // We are weakly associated to the ComputerSystem, we'll get it and use it's keys.
00253       CIMObjectPathArray computerSystems(hdl->enumInstanceNamesA(interopNS, Class_OpenWBEM_ComputerSystem));
00254       if (computerSystems.size() != 1)
00255       {
00256          OW_THROWCIMMSG(CIMException::FAILED, Format("Expected 1 instance of %1, got %2", 
00257             Class_OpenWBEM_ComputerSystem, computerSystems.size()).c_str());
00258       }
00259 
00260       CIMObjectPath& computerSystem(computerSystems[0]);
00261 
00262       newInst.updatePropertyValue("Version", CIMValue(OW_VERSION));
00263       
00264       // This property is a KEY, it must be filled out
00265       newInst.updatePropertyValue(PROP_SystemCreationClassName, computerSystem.getKeyValue(PROP_CreationClassName));
00266       // This property is a KEY, it must be filled out
00267       newInst.updatePropertyValue(PROP_SystemName, computerSystem.getKeyValue(PROP_Name));
00268       // This property is a KEY, it must be filled out
00269       newInst.updatePropertyValue(PROP_CreationClassName, CIMValue(newInst.getClassName()));
00270 
00271       // This property is a KEY, it must be filled out
00272       newInst.updatePropertyValue(PROP_Name, CIMValue(omName));
00273 
00274       newInst.updatePropertyValue("Started", CIMValue(true));
00275       newInst.updatePropertyValue("EnabledState", CIMValue(UInt16(2))); // 2 = Enabled
00276       newInst.updatePropertyValue("Caption", CIMValue("owcimomd"));
00277       newInst.updatePropertyValue("Description", CIMValue("owcimomd"));
00278 
00279       return newInst;
00280    }
00281 
00283    void doSimpleEnumInstances(
00284       const ProviderEnvironmentIFCRef &env,
00285       const String &ns,
00286       const CIMClass &cimClass,
00287       CIMInstanceResultHandlerIFC &result,
00288       EPropertiesFlag propertiesFlag)
00289    {
00290       if (cimClass.getName() == CLASS_OpenWBEM_ObjectManager)
00291       {
00292          result.handle(m_inst);
00293       }
00294       else if (cimClass.getName() == CLASS_OpenWBEM_HostedObjectManager)
00295       {
00296          CIMInstance ci(cimClass.newInstance());
00297          ci.updatePropertyValue(PROP_Dependent, CIMValue(CIMObjectPath(ns, m_inst)));
00298 
00299          String systemCreationClassName(m_inst.getPropertyValue(PROP_SystemCreationClassName).toString());
00300          CIMObjectPath csPath(systemCreationClassName, ns);
00301          csPath.setKeyValue(PROP_CreationClassName, CIMValue(systemCreationClassName));
00302          csPath.setKeyValue(PROP_Name, m_inst.getPropertyValue(PROP_SystemName));
00303 
00304          ci.updatePropertyValue(PROP_Antecedent, CIMValue(csPath));
00305          result.handle(ci);
00306       }
00307    }
00308 
00309 #ifndef OW_DISABLE_ASSOCIATION_TRAVERSAL
00310 
00311    void doReferences(const ProviderEnvironmentIFCRef &env,
00312       CIMInstanceResultHandlerIFC &result,
00313       const String &ns,
00314       const CIMObjectPath &objectName,
00315       const CIMClass &assocClass,
00316       const String &resultClass,
00317       const String &role,
00318       const String &resultRole)
00319    {
00320       doSimpleEnumInstances(env, ns, assocClass, result, E_ALL_PROPERTIES);
00321    }
00322 #endif // #ifndef OW_DISABLE_ASSOCIATION_TRAVERSAL
00323 };
00324 
00325 } // end namespace OW_NAMESPACE
00326 
00327 OW_PROVIDERFACTORY(OpenWBEM::OpenWBEM_ObjectManagerInstProv, owprovinstOpenWBEM_ObjectManager)
00328 

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