OWBI1_ProviderIFC.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 "OWBI1_ProviderIFC.hpp"
00038 #include "OW_SharedLibraryException.hpp"
00039 #include "OW_SharedLibraryLoader.hpp"
00040 #include "OW_Format.hpp"
00041 #include "OW_SignalScope.hpp"
00042 #include "OW_ConfigOpts.hpp"
00043 #include "OW_FileSystem.hpp"
00044 #include "OWBI1_ProxyProvider.hpp"
00045 #include "OW_NoSuchProviderException.hpp"
00046 #include "OW_Assertion.hpp"
00047 #include "OW_IntrusiveCountableBase.hpp"
00048 #include "OW_NonRecursiveMutex.hpp"
00049 #include "OW_NonRecursiveMutexLock.hpp"
00050 #include "OW_Condition.hpp"
00051 #include "OW_ExceptionIds.hpp"
00052 #include "OWBI1_ProviderEnvironmentIFC.hpp"
00053 #include "OWBI1_OperationContext.hpp"
00054 #include "OW_OperationContext.hpp"
00055 #include "OWBI1_InstanceProviderInfo.hpp"
00056 #include "OWBI1_SecondaryInstanceProviderInfo.hpp"
00057 #include "OWBI1_AssociatorProviderInfo.hpp"
00058 #include "OWBI1_MethodProviderInfo.hpp"
00059 #include "OWBI1_IndicationProviderInfo.hpp"
00060 #include "OWBI1_CIMOMHandleIFC.hpp"
00061 #include "OWBI1_Logger.hpp"
00062 #include "OWBI1_SessionLanguage.hpp"
00063 
00064 namespace OWBI1
00065 {
00066 
00067 //using namespace OpenWBEM;
00068 
00069 OW_DECLARE_EXCEPTION(BI1ProviderIFC);
00070 OW_DEFINE_EXCEPTION_WITH_ID(BI1ProviderIFC);
00071 
00072 typedef BI1ProviderBaseIFC* (*ProviderCreationFunc)();
00073 typedef const char* (*versionFunc_t)();
00074 namespace
00075 {
00076    const OpenWBEM::String COMPONENT_NAME("ow.provider.cpp.ifc");
00077 
00078    class OperationContextWrapper : public OperationContext
00079    {
00080    public:
00081       OperationContextWrapper(OpenWBEM::OperationContext& oc)
00082          : m_oc(oc)
00083       {
00084       }
00090       virtual void removeData(const String& key)
00091       {
00092          m_oc.removeData(key.c_str());
00093       }
00094 
00100       virtual bool keyHasData(const String& key) const
00101       {
00102          return m_oc.keyHasData(key.c_str());
00103       }
00104 
00109       virtual void setStringData(const String& key, const String& str)
00110       {
00111          m_oc.setStringData(key.c_str(), str.c_str());
00112       }
00113 
00117       virtual String getStringData(const String& key) const
00118       {
00119          return m_oc.getStringData(key.c_str()).c_str();
00120       }
00121 
00125       virtual String getStringDataWithDefault(const String& key, const String& def = String() ) const
00126       {
00127          return m_oc.getStringDataWithDefault(key.c_str(), def.c_str()).c_str();
00128       }
00129 
00130    private:
00131       OpenWBEM::OperationContext& m_oc;
00132    };
00133 
00134    // hmm this needs it's own file, since the Proxy classes will use it too.
00135    // TODO: Write it.
00136    class ProvEnvWrapper : public OWBI1::ProviderEnvironmentIFC
00137    {
00138    public:
00139       ProvEnvWrapper(const OpenWBEM::ProviderEnvironmentIFCRef& env)
00140          : m_env(env)
00141          , m_context(new OperationContextWrapper(m_env->getOperationContext()))
00142       {
00143       }
00144 
00145       virtual CIMOMHandleIFCRef getCIMOMHandle() const
00146       {
00147          return CIMOMHandleIFCRef();
00148       }
00149       virtual LoggerRef getLogger(const String& componentName) const
00150       {
00151          return LoggerRef();
00152       }
00153       virtual String getConfigItem(const String &name, const String& defRetVal="") const
00154       {
00155          return m_env->getConfigItem(name.c_str(), defRetVal.c_str()).c_str();
00156       }
00157       virtual String getUserName() const
00158       {
00159          return "";
00160       }
00161       virtual OperationContext& getOperationContext()
00162       {
00163          return *m_context;
00164       }
00165       virtual SessionLanguageRef getSessionLanguage() const
00166       {
00167          return SessionLanguageRef();
00168       }
00169 
00170    private:
00171       OpenWBEM::ProviderEnvironmentIFCRef m_env;
00172       IntrusiveReference<OperationContext> m_context;
00173    };
00174 
00175    OWBI1::ProviderEnvironmentIFCRef wrapProvEnv(const OpenWBEM::ProviderEnvironmentIFCRef& env)
00176    {
00177       return OWBI1::ProviderEnvironmentIFCRef(new ProvEnvWrapper(env));
00178    }
00179 
00180    OpenWBEM::InstanceProviderInfo convertInstanceProviderInfo(const OWBI1::InstanceProviderInfo& in)
00181    {
00182       //TODO:
00183       return OpenWBEM::InstanceProviderInfo();
00184    }
00185    OpenWBEM::SecondaryInstanceProviderInfo convertSecondaryInstanceProviderInfo(const OWBI1::SecondaryInstanceProviderInfo& in)
00186    {
00187       //TODO:
00188       return OpenWBEM::SecondaryInstanceProviderInfo();
00189    }
00190 #ifndef OW_DISABLE_ASSOCIATION_TRAVERSAL
00191    OpenWBEM::AssociatorProviderInfo convertAssociatorProviderInfo(const OWBI1::AssociatorProviderInfo& in)
00192    {
00193       //TODO:
00194       return OpenWBEM::AssociatorProviderInfo();
00195    }
00196 #endif
00197    OpenWBEM::MethodProviderInfo convertMethodProviderInfo(const OWBI1::MethodProviderInfo& in)
00198    {
00199       //TODO:
00200       return OpenWBEM::MethodProviderInfo();
00201    }
00202    OpenWBEM::IndicationProviderInfo convertIndicationProviderInfo(const OWBI1::IndicationProviderInfo& in)
00203    {
00204       //TODO:
00205       return OpenWBEM::IndicationProviderInfo();
00206    }
00207 
00208 }
00209 const char* const BI1ProviderIFC::CREATIONFUNC = "createProvider";
00210 
00212 class BI1ProviderIFC::BI1ProviderInitializationHelper : public IntrusiveCountableBase
00213 {
00214 public:
00215 
00216    explicit BI1ProviderInitializationHelper(const BI1ProviderBaseIFCRef& provider)
00217       : m_initialized(false)
00218       , m_initializeFailed(false)
00219       , m_provider(provider)
00220    {
00221 
00222    }
00223 
00224    // returns false if initialization failed
00225    bool waitUntilInitialized() const
00226    {
00227       NonRecursiveMutexLock l(m_initializedGuard);
00228       while (!m_initialized && !m_initializeFailed)
00229       {
00230          m_initializedCond.wait(l);
00231       }
00232       return !m_initializeFailed;
00233    }
00234    
00235    void initialize(const ProviderEnvironmentIFCRef& env)
00236    {
00237       try
00238       {
00239          m_provider->initialize(env);
00240       }
00241       catch (...)
00242       {
00243          OpenWBEM::NonRecursiveMutexLock l(m_initializedGuard);
00244          m_initializeFailed = true;
00245          m_initializedCond.notifyAll();
00246          throw;
00247       }
00248 
00249       OpenWBEM::NonRecursiveMutexLock l(m_initializedGuard);
00250       m_initialized = true;
00251       m_initializedCond.notifyAll();
00252    }
00253 
00254    BI1ProviderBaseIFCRef getProvider() const
00255    {
00256       return m_provider;
00257    }
00258 private:
00259    bool m_initialized;
00260    bool m_initializeFailed;
00261    mutable OpenWBEM::NonRecursiveMutex m_initializedGuard;
00262    mutable OpenWBEM::Condition m_initializedCond;
00263    BI1ProviderBaseIFCRef m_provider;
00264 };
00265 
00267 BI1ProviderIFC::BI1ProviderIFC()
00268    : m_loadDone(false)
00269 {
00270 }
00272 BI1ProviderIFC::~BI1ProviderIFC()
00273 {
00274    try
00275    {
00276       ProviderMap::iterator it = m_provs.begin();
00277       while (it != m_provs.end())
00278       {
00279          it->second = 0;
00280          it++;
00281       }
00282    
00283       m_provs.clear();
00284       m_indicationExportProviders.clear();
00285       m_polledProviders.clear();
00286    }
00287    catch (...)
00288    {
00289       // don't let exceptions escape
00290    }
00291 }
00293 void
00294 BI1ProviderIFC::doInit(const OpenWBEM::ProviderEnvironmentIFCRef& env,
00295    OpenWBEM::InstanceProviderInfoArray& i,
00296    OpenWBEM::SecondaryInstanceProviderInfoArray& si,
00297 #ifndef OW_DISABLE_ASSOCIATION_TRAVERSAL
00298    OpenWBEM::AssociatorProviderInfoArray& a,
00299 #endif
00300    OpenWBEM::MethodProviderInfoArray& m,
00301    OpenWBEM::IndicationProviderInfoArray& ind)
00302 {
00303    loadProviders(env, i,
00304       si,
00305 #ifndef OW_DISABLE_ASSOCIATION_TRAVERSAL
00306       a,
00307 #endif
00308       m,
00309       ind);
00310 }
00312 OpenWBEM::InstanceProviderIFCRef
00313 BI1ProviderIFC::doGetInstanceProvider(const OpenWBEM::ProviderEnvironmentIFCRef& env,
00314    const char* provIdString)
00315 {
00316    BI1ProviderBaseIFCRef pProv = getProvider(env, provIdString);
00317    if (pProv)
00318    {
00319       BI1InstanceProviderIFC* pIP = pProv->getInstanceProvider();
00320       if (pIP)
00321       {
00322          OW_LOG_DEBUG(env->getLogger(COMPONENT_NAME), 
00323             Format("OWBI1ProviderIFC found instance provider %1", provIdString));
00324          BI1InstanceProviderIFCRef ipRef(pProv.getLibRef(), pIP);
00325          return OpenWBEM::InstanceProviderIFCRef(new BI1InstanceProviderProxy(ipRef));
00326       }
00327       OW_LOG_ERROR(env->getLogger(COMPONENT_NAME), Format("Provider %1 is not an instance provider",
00328          provIdString));
00329    }
00330    OW_THROW(NoSuchProviderException, provIdString);
00331 }
00333 OpenWBEM::SecondaryInstanceProviderIFCRef
00334 BI1ProviderIFC::doGetSecondaryInstanceProvider(const OpenWBEM::ProviderEnvironmentIFCRef& env,
00335    const char* provIdString)
00336 {
00337    BI1ProviderBaseIFCRef pProv = getProvider(env, provIdString);
00338    if (pProv)
00339    {
00340       BI1SecondaryInstanceProviderIFC* pIP = pProv->getSecondaryInstanceProvider();
00341       if (pIP)
00342       {
00343          OW_LOG_DEBUG(env->getLogger(COMPONENT_NAME), 
00344             Format("OWBI1ProviderIFC found secondary instance provider %1", provIdString));
00345          BI1SecondaryInstanceProviderIFCRef ipRef(pProv.getLibRef(), pIP);
00346          return OpenWBEM::SecondaryInstanceProviderIFCRef(new BI1SecondaryInstanceProviderProxy(ipRef));
00347       }
00348       OW_LOG_ERROR(env->getLogger(COMPONENT_NAME), Format("Provider %1 is not a secondary instance provider",
00349          provIdString));
00350    }
00351    OW_THROW(NoSuchProviderException, provIdString);
00352 }
00354 OpenWBEM::IndicationExportProviderIFCRefArray
00355 BI1ProviderIFC::doGetIndicationExportProviders(const OpenWBEM::ProviderEnvironmentIFCRef&)
00356 {
00357    return m_indicationExportProviders;
00358    /*
00359    OpenWBEM::IndicationExportProviderIFCRefArray rvra;
00360    for (size_t i = 0; i < m_noUnloadProviders.size(); i++)
00361    {
00362       BI1ProviderBaseIFCRef pProv = m_noUnloadProviders[i];
00363       BI1IndicationExportProviderIFC* pIEP =
00364          pProv->getIndicationExportProvider();
00365       if (pIEP)
00366       {
00367          BI1IndicationExportProviderIFCRef iepRef(pProv.getLibRef(), pIEP);
00368          rvra.append(OpenWBEM::IndicationExportProviderIFCRef(new BI1IndicationExportProviderProxy(iepRef)));
00369       }
00370    }
00371    return rvra;
00372    */
00373 }
00375 OpenWBEM::PolledProviderIFCRefArray
00376 BI1ProviderIFC::doGetPolledProviders(const OpenWBEM::ProviderEnvironmentIFCRef&)
00377 {
00378    return m_polledProviders;
00379    /*
00380    OpenWBEM::PolledProviderIFCRefArray rvra;
00381    for (size_t i = 0; i < m_noUnloadProviders.size(); i++)
00382    {
00383       BI1ProviderBaseIFCRef pProv = m_noUnloadProviders[i];
00384       BI1PolledProviderIFC* pPP = pProv->getPolledProvider();
00385       if (pPP)
00386       {
00387          BI1PolledProviderIFCRef ppRef(pProv.getLibRef(), pPP);
00388          rvra.append(OpenWBEM::PolledProviderIFCRef(new BI1PolledProviderProxy(ppRef)));
00389       }
00390    }
00391    return rvra;
00392    */
00393 }
00395 OpenWBEM::MethodProviderIFCRef
00396 BI1ProviderIFC::doGetMethodProvider(const OpenWBEM::ProviderEnvironmentIFCRef& env,
00397    const char* provIdString)
00398 {
00399    BI1ProviderBaseIFCRef pProv = getProvider(env, provIdString);
00400    if (pProv)
00401    {
00402       BI1MethodProviderIFC* pMP = pProv->getMethodProvider();
00403       if (pMP)
00404       {
00405          OW_LOG_DEBUG(env->getLogger(COMPONENT_NAME), Format("OWBI1ProviderIFC found method provider %1",
00406             provIdString));
00407          BI1MethodProviderIFCRef mpRef(pProv.getLibRef(), pMP);
00408          return OpenWBEM::MethodProviderIFCRef(new BI1MethodProviderProxy(mpRef));
00409       }
00410       OW_LOG_ERROR(env->getLogger(COMPONENT_NAME), Format("Provider %1 is not a method provider",
00411          provIdString));
00412    }
00413    OW_THROW(NoSuchProviderException, provIdString);
00414 }
00415 #ifndef OW_DISABLE_ASSOCIATION_TRAVERSAL
00416 
00417 OpenWBEM::AssociatorProviderIFCRef
00418 BI1ProviderIFC::doGetAssociatorProvider(const OpenWBEM::ProviderEnvironmentIFCRef& env,
00419    const char* provIdString)
00420 {
00421    BI1ProviderBaseIFCRef pProv = getProvider(env, provIdString);
00422    if (pProv)
00423    {
00424       BI1AssociatorProviderIFC* pAP = pProv->getAssociatorProvider();
00425       if (pAP)
00426       {
00427          OW_LOG_DEBUG(env->getLogger(COMPONENT_NAME), Format("OWBI1ProviderIFC found associator provider %1",
00428             provIdString));
00429          BI1AssociatorProviderIFCRef apRef(pProv.getLibRef(), pAP);
00430          return OpenWBEM::AssociatorProviderIFCRef(new BI1AssociatorProviderProxy(apRef));
00431       }
00432       OW_LOG_ERROR(env->getLogger(COMPONENT_NAME), Format("Provider %1 is not an associator provider",
00433          provIdString));
00434    }
00435    OW_THROW(NoSuchProviderException, provIdString);
00436 }
00437 #endif
00438 
00439 OpenWBEM::IndicationProviderIFCRef
00440 BI1ProviderIFC::doGetIndicationProvider(const OpenWBEM::ProviderEnvironmentIFCRef& env,
00441    const char* provIdString)
00442 {
00443    BI1ProviderBaseIFCRef pProv = getProvider(env, provIdString);
00444    if (pProv)
00445    {
00446       BI1IndicationProviderIFC* pAP = pProv->getIndicationProvider();
00447       if (pAP)
00448       {
00449          OW_LOG_DEBUG(env->getLogger(COMPONENT_NAME), Format("OWBI1ProviderIFC found indication provider %1",
00450             provIdString));
00451 
00452          IndicationProviderMap::const_iterator ci = m_indicationProviders.find(provIdString);
00453          if (ci != m_indicationProviders.end())
00454          {
00455             return ci->second;
00456          }
00457 
00458          BI1IndicationProviderIFCRef apRef(pProv.getLibRef(), pAP);
00459          OpenWBEM::IndicationProviderIFCRef rv(new BI1IndicationProviderProxy(apRef));
00460          
00461          m_indicationProviders.insert(IndicationProviderMap::value_type(provIdString, rv));
00462 
00463          return rv;
00464       }
00465       OW_LOG_ERROR(env->getLogger(COMPONENT_NAME), Format("Provider %1 is not an indication provider",
00466          provIdString));
00467    }
00468    OW_THROW(NoSuchProviderException, provIdString);
00469 }
00471 void
00472 BI1ProviderIFC::loadProviders(const OpenWBEM::ProviderEnvironmentIFCRef& env,
00473    OpenWBEM::InstanceProviderInfoArray& instanceProviderInfo,
00474    OpenWBEM::SecondaryInstanceProviderInfoArray& secondaryInstanceProviderInfo,
00475 #ifndef OW_DISABLE_ASSOCIATION_TRAVERSAL
00476    OpenWBEM::AssociatorProviderInfoArray& associatorProviderInfo,
00477 #endif
00478    OpenWBEM::MethodProviderInfoArray& methodProviderInfo,
00479    OpenWBEM::IndicationProviderInfoArray& indicationProviderInfo)
00480 {
00481    MutexLock ml(m_guard);
00482    if (m_loadDone)
00483    {
00484       return;
00485    }
00486    m_loadDone = true;
00487 
00488    // Create the Shared library loader
00489 
00490    SharedLibraryLoaderRef ldr =
00491       SharedLibraryLoader::createSharedLibraryLoader();
00492    if (!ldr)
00493    {
00494       const char* msg = "OWBI1 provider ifc failed to get shared lib loader";
00495       OW_LOG_ERROR(env->getLogger(COMPONENT_NAME), msg);
00496       OW_THROW(BI1ProviderIFCException, msg);
00497    }
00498 
00499    OpenWBEM::StringArray paths = env->getMultiConfigItem(
00500       ConfigOpts::OWBI1IFC_PROV_LOCATION_opt, 
00501       String(OW_DEFAULT_OWBI1IFC_PROV_LOCATION).tokenize(OW_PATHNAME_SEPARATOR),
00502       OW_PATHNAME_SEPARATOR);
00503 
00504    for (OpenWBEM::StringArray::size_type i1 = 0; i1 < paths.size(); i1++)
00505    {
00506       OpenWBEM::StringArray dirEntries;
00507       if (!FileSystem::getDirectoryContents(paths[i1], dirEntries))
00508       {
00509          OpenWBEM::String msg(Format("OWBI1 provider ifc failed getting contents of directory: %1", paths[i1]));
00510          OW_LOG_INFO(env->getLogger(COMPONENT_NAME), msg);
00511       }
00512       for (size_t i = 0; i < dirEntries.size(); i++)
00513       {
00514          if (!dirEntries[i].endsWith(OW_SHAREDLIB_EXTENSION))
00515          {
00516             continue;
00517          }
00518          OpenWBEM::String libName = paths[i1];
00519          libName += OW_FILENAME_SEPARATOR;
00520          libName += dirEntries[i];
00521 
00522 #ifdef OW_DARWIN
00523          if (!FileSystem::isLink(libName))
00524          {
00525             continue;
00526          }
00527 #endif // OW_DARWIN
00528    
00529          // try and load it
00530 
00531          SharedLibraryRef theLib = ldr->loadSharedLibrary(libName,
00532             env->getLogger(COMPONENT_NAME));
00533          if (!theLib)
00534          {
00535             OpenWBEM::String msg(Format("OWBI1 provider ifc failed to load library: %1", libName));
00536             OW_LOG_ERROR(env->getLogger(COMPONENT_NAME), "****************************************");
00537             OW_LOG_ERROR(env->getLogger(COMPONENT_NAME), msg);
00538             OW_LOG_ERROR(env->getLogger(COMPONENT_NAME), "****************************************");
00539             OW_THROW(BI1ProviderIFCException, msg.c_str());
00540          }
00541 
00542          // chop off lib and .so
00543          OpenWBEM::String providerid = dirEntries[i].substring(3, providerid.length() - (strlen(OW_SHAREDLIB_EXTENSION) + 3));
00544 
00545          BI1ProviderBaseIFCRef p = getProvider(env, providerid.c_str(),
00546             dontStoreProvider, dontInitializeProvider);
00547 
00548          if (!p)
00549          {
00550             OpenWBEM::String msg(Format("OWBI1 provider ifc: Libary %1 does not load", libName));
00551             OW_LOG_DEBUG(env->getLogger(COMPONENT_NAME), "****************************************");
00552             OW_LOG_DEBUG(env->getLogger(COMPONENT_NAME), msg);
00553             OW_LOG_DEBUG(env->getLogger(COMPONENT_NAME), "****************************************");
00554             OW_THROW(BI1ProviderIFCException, msg.c_str());
00555          }
00556 
00557          BI1ProviderInitializationHelperRef p2;
00558          // The named provider may also be an indication export or a
00559          // polled provider. If so, we'll store a reference to it in
00560          // the m_polledProviders
00561          BI1PolledProviderIFC* p_polledProv = p->getPolledProvider();
00562          if (p_polledProv)
00563          {
00564             OW_LOG_DEBUG(env->getLogger(COMPONENT_NAME), 
00565                Format("OWBI1 provider ifc loaded polled provider from lib: %1 - initializing", libName));
00566 
00567             if (!p2)
00568             {
00569                p2 = BI1ProviderInitializationHelperRef(new BI1ProviderInitializationHelper(p));
00570                p2->initialize(wrapProvEnv(env));
00571                m_provs.insert(std::make_pair(providerid, p2));
00572             }
00573             BI1PolledProviderIFCRef ipRef(theLib, p_polledProv);
00574             m_polledProviders.push_back(OpenWBEM::PolledProviderIFCRef(new BI1PolledProviderProxy(ipRef)));
00575          }
00576 
00577          BI1IndicationExportProviderIFC* p_indExpProv =
00578             p->getIndicationExportProvider();
00579          if (p_indExpProv)
00580          {
00581             OW_LOG_DEBUG(env->getLogger(COMPONENT_NAME), 
00582                Format("OWBI1 provider ifc loaded indication export provider from lib: %1 - initializing", libName));
00583 
00584             if (!p2)
00585             {
00586                BI1ProviderInitializationHelperRef p2(new BI1ProviderInitializationHelper(p));
00587                p2->initialize(wrapProvEnv(env));
00588                m_provs.insert(std::make_pair(providerid, p2));
00589             }
00590             BI1IndicationExportProviderIFCRef iepRef(theLib, p_indExpProv);
00591             m_indicationExportProviders.push_back(OpenWBEM::IndicationExportProviderIFCRef(new BI1IndicationExportProviderProxy(iepRef)));
00592          }
00593 
00594          BI1InstanceProviderIFC* p_ip = p->getInstanceProvider();
00595          if (p_ip)
00596          {
00597             OWBI1::InstanceProviderInfo info;
00598             info.setProviderName(providerid.c_str());
00599             p_ip->getInstanceProviderInfo(info);
00600             instanceProviderInfo.push_back(convertInstanceProviderInfo(info));
00601          }
00602 
00603          BI1SecondaryInstanceProviderIFC* p_sip =
00604             p->getSecondaryInstanceProvider();
00605 
00606          if (p_sip)
00607          {
00608             OWBI1::SecondaryInstanceProviderInfo info;
00609             info.setProviderName(providerid.c_str());
00610             p_sip->getSecondaryInstanceProviderInfo(info);
00611             secondaryInstanceProviderInfo.push_back(convertSecondaryInstanceProviderInfo(info));
00612          }
00613 
00614 #ifndef OW_DISABLE_ASSOCIATION_TRAVERSAL
00615          BI1AssociatorProviderIFC* p_ap = p->getAssociatorProvider();
00616          if (p_ap)
00617          {
00618             OWBI1::AssociatorProviderInfo info;
00619             info.setProviderName(providerid.c_str());
00620             p_ap->getAssociatorProviderInfo(info);
00621             associatorProviderInfo.push_back(convertAssociatorProviderInfo(info));
00622          }
00623 #endif
00624 
00625          BI1MethodProviderIFC* p_mp = p->getMethodProvider();
00626          if (p_mp)
00627          {
00628             OWBI1::MethodProviderInfo info;
00629             info.setProviderName(providerid.c_str());
00630             p_mp->getMethodProviderInfo(info);
00631             methodProviderInfo.push_back(convertMethodProviderInfo(info));
00632          }
00633          BI1IndicationProviderIFC* p_indp = p->getIndicationProvider();
00634          if (p_indp)
00635          {
00636             OWBI1::IndicationProviderInfo info;
00637             info.setProviderName(providerid.c_str());
00638             p_indp->getIndicationProviderInfo(info);
00639             indicationProviderInfo.push_back(convertIndicationProviderInfo(info));
00640          }
00641       }
00642    }
00643 }
00644 
00646 //static
00647 BI1ProviderBaseIFCRef
00648 BI1ProviderIFC::loadProvider(const OpenWBEM::String& libName, OpenWBEM::LoggerRef logger)
00649 {
00650    OpenWBEM::String provId = libName.substring(libName.lastIndexOf(OW_FILENAME_SEPARATOR)+1);
00651    // chop of lib and .so
00652    provId = provId.substring(3, provId.length() - (strlen(OW_SHAREDLIB_EXTENSION) + 3));
00653 
00654    SharedLibraryLoaderRef ldr = SharedLibraryLoader::createSharedLibraryLoader();
00655    if (!ldr)
00656    {
00657       OW_LOG_ERROR(logger, "OWBI1 provider ifc FAILED to get shared lib loader");
00658       return BI1ProviderBaseIFCRef();
00659    }
00660 
00661    OW_LOG_DEBUG(logger, Format("BI1ProviderIFC::loadProvider loading library: %1", libName));
00662 
00663    SharedLibraryRef theLib = ldr->loadSharedLibrary(libName, logger);
00664 
00665    versionFunc_t versFunc;
00666    if (!theLib->getFunctionPointer("getOWVersion", versFunc))
00667    {
00668       OW_LOG_ERROR(logger, Format("OWBI1 provider ifc failed getting function pointer to \"getOWVersion\" from library %1.", libName));
00669       return BI1ProviderBaseIFCRef();
00670    }
00671    const char* strVer = (*versFunc)();
00672    if (strcmp(strVer, OW_VERSION))
00673    {
00674       OW_LOG_ERROR(logger, "OWBI1 provider ifc got invalid version from provider");
00675       OW_LOG_ERROR(logger, Format("OWBI1 provider ifc version: %1  provider version: %2  library: %3",
00676                OW_VERSION, strVer, libName));
00677       return BI1ProviderBaseIFCRef();
00678    }
00679 
00680    ProviderCreationFunc createProvider;
00681    OpenWBEM::String creationFuncName = OpenWBEM::String(CREATIONFUNC) + provId;
00682    if (!theLib->getFunctionPointer(creationFuncName, createProvider))
00683    {
00684       OW_LOG_ERROR(logger, Format("OWBI1 provider ifc: Libary %1 does not contain %2 function.",
00685          libName, creationFuncName));
00686       return BI1ProviderBaseIFCRef();
00687    }
00688 
00689    BI1ProviderBaseIFC* pProv = (*createProvider)();
00690 
00691    if (!pProv)
00692    {
00693       OW_LOG_ERROR(logger, Format("OWBI1 provider ifc: Libary %1 -"
00694          " %2 returned null provider. Not loaded.", libName, creationFuncName));
00695       return BI1ProviderBaseIFCRef();
00696    }
00697 
00698    BI1ProviderBaseIFCRef rval(theLib, pProv);
00699 
00700    OW_LOG_DEBUG(logger, Format("OWBI1 provider ifc successfully loaded library %1 for provider %2", libName, provId));
00701 
00702    return rval;
00703 }
00704 
00706 BI1ProviderBaseIFCRef
00707 BI1ProviderIFC::getProvider(
00708    const OpenWBEM::ProviderEnvironmentIFCRef& env, const char* provIdString,
00709    StoreProviderFlag storeP, InitializeProviderFlag initP)
00710 {
00711    OW_ASSERT((initP == initializeProvider && storeP == storeProvider) || (initP == dontInitializeProvider && storeP == dontStoreProvider));
00712 
00713    MutexLock ml(m_guard);
00714 
00715    OpenWBEM::String provId(provIdString);
00716    ProviderMap::iterator it = m_provs.find(provId);
00717    if (it != m_provs.end())
00718    {
00719       // make a copy in case the map gets modified when we unlock m_guard
00720       BI1ProviderInitializationHelperRef prov(it->second);
00721       // do this to prevent a deadlock
00722       ml.release();
00723 
00724       // another thread may be initializing the provider, wait until it's done.
00725       if (prov->waitUntilInitialized())
00726       {
00727          return prov->getProvider();
00728       }
00729       else
00730       {
00731          // initialization failed, it's now out of the map.
00732          return BI1ProviderBaseIFCRef();
00733       }
00734    }
00735 
00736    OpenWBEM::String libName;
00737    BI1ProviderBaseIFCRef rval;
00738 
00739    OpenWBEM::StringArray paths = env->getMultiConfigItem(
00740       ConfigOpts::OWBI1IFC_PROV_LOCATION_opt, 
00741       String(OW_DEFAULT_OWBI1IFC_PROV_LOCATION).tokenize(OW_PATHNAME_SEPARATOR),
00742       OW_PATHNAME_SEPARATOR);
00743 
00744    for (OpenWBEM::StringArray::size_type i = 0; i < paths.size(); i++)
00745    {
00746       libName = paths[i];
00747       libName += OW_FILENAME_SEPARATOR;
00748       libName += "lib";
00749       libName += provId;
00750       libName += OW_SHAREDLIB_EXTENSION;
00751 
00752       if (!FileSystem::exists(libName))
00753       {
00754          continue;
00755       }
00756 
00757       rval = loadProvider(libName, env->getLogger(COMPONENT_NAME));
00758 
00759       if (rval)
00760       {
00761          break;
00762       }
00763 
00764    }
00765 
00766    if (!rval)
00767    {
00768       OW_LOG_ERROR(env->getLogger(COMPONENT_NAME), 
00769          Format("OWBI1 provider ifc failed to load library: %1 for provider id %2. Skipping.", libName, provId));
00770       return rval;
00771    }
00772 
00773    if (initP == initializeProvider && storeP == storeProvider)
00774    {
00775       BI1ProviderInitializationHelperRef provInitHelper(new BI1ProviderInitializationHelper(rval));
00776 
00777       m_provs[provId] = provInitHelper;
00778       
00779       // now it's in the map, we can unlock the mutex protecting the map
00780       ml.release();
00781 
00782       OW_LOG_DEBUG(env->getLogger(COMPONENT_NAME), 
00783          Format("OWBI1 provider ifc calling initialize for provider %1", provId));
00784 
00785       try
00786       {
00787          provInitHelper->initialize(wrapProvEnv(env)); // Let provider initialize itself
00788       }
00789       catch (...)
00790       {
00791          // provider initialization failed, we need to take it out of the map
00792          MutexLock lock(m_guard);
00793          m_provs.erase(provId);
00794          throw;
00795       }
00796 
00797       OW_LOG_DEBUG(env->getLogger(COMPONENT_NAME), 
00798          Format("OWBI1 provider ifc: provider %1 loaded and initialized", provId));
00799 
00800    }
00801    else
00802    {
00803       OW_LOG_DEBUG(env->getLogger(COMPONENT_NAME), 
00804          Format("OWBI1 provider ifc: provider %1 loaded but not initialized", provId));
00805    }
00806 
00807    return rval;
00808 }
00810 void
00811 BI1ProviderIFC::doUnloadProviders(const OpenWBEM::ProviderEnvironmentIFCRef& env)
00812 {
00813    OpenWBEM::String timeWindow = env->getConfigItem(ConfigOpts::OWBI1IFC_PROV_TTL_opt, OW_DEFAULT_OWBI1IFC_PROV_TTL);
00814    Int32 iTimeWindow;
00815    try
00816    {
00817       iTimeWindow = timeWindow.toInt32();
00818    }
00819    catch (const StringConversionException&)
00820    {
00821       iTimeWindow = String(OW_DEFAULT_OWBI1IFC_PROV_TTL).toInt32();
00822    }
00823    if (iTimeWindow < 0)
00824    {
00825       return;
00826    }
00827    DateTime dt;
00828    dt.setToCurrent();
00829    OpenWBEM::MutexLock l(m_guard);
00830    for (ProviderMap::iterator iter = m_provs.begin();
00831         iter != m_provs.end();)
00832    {
00833       DateTime provDt = iter->second->getProvider()->getLastAccessTime();
00834       provDt.addMinutes(iTimeWindow);
00835       if (provDt < dt && iter->second->getProvider()->canUnload())
00836       {
00837          OW_LOG_INFO(env->getLogger(COMPONENT_NAME), Format("Unloading Provider %1", iter->first));
00838          iter->second = 0;
00839          m_provs.erase(iter++);
00840          continue;
00841       }
00842 
00843       ++iter;
00844    }
00845 }
00846 
00847 void BI1ProviderIFC::doShuttingDown(const OpenWBEM::ProviderEnvironmentIFCRef& env)
00848 {
00849    // Need to lock m_guard while accessing m_provs, but we can't leave the mutex locked
00850    // while calling shuttingDown(), since that might cause a deadlock, so make a copy.
00851    OpenWBEM::MutexLock l(m_guard);
00852    ProviderMap provsCopy(m_provs);
00853    l.release();
00854 
00855    ProviderEnvironmentIFCRef bi1env = wrapProvEnv(env);
00856    ProviderMap::iterator it, itend = provsCopy.end();
00857    for (it = provsCopy.begin(); it != itend; ++it)
00858    {
00859       try
00860       {
00861          it->second->getProvider()->shuttingDown(bi1env);
00862       }
00863       catch (Exception& e)
00864       {
00865          OW_LOG_ERROR(env->getLogger(COMPONENT_NAME), Format("Caught exception while calling shuttingDown() on provider: %1", e));
00866       }
00867    }
00868 
00869 }
00870 
00871 } // end namespace OWBI1
00872 
00873 OW_PROVIDERIFCFACTORY(OWBI1::BI1ProviderIFC, owowbi1provifc)
00874 

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