NCBI C++ ToolKit
driver_mgr.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: driver_mgr.cpp 77792 2017-05-08 13:38:35Z ivanov $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Vladimir Soussov
27  *
28  * File Description: Driver manager
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 #include <corelib/ncbidll.hpp>
34 #include <corelib/ncbireg.hpp>
35 
40 
42 
43 
45 
46 
47 ///////////////////////////////////////////////////////////////////////////////
49 MakePluginManagerParamTree(const string& driver_name, const map<string, string>* attr)
50 {
52  CMemoryRegistry reg;
53  TCIter citer = attr->begin();
54  TCIter cend = attr->end();
55 
56  for ( ; citer != cend; ++citer ) {
57  reg.Set( driver_name, citer->first, citer->second );
58  }
59 
61 
62  return tr;
63 }
64 
65 
66 ///////////////////////////////////////////////////////////////////////////////
69 {
70  unique_ptr<TPluginManagerParamTree> tr(new TPluginManagerParamTree);
71 
72  tr->GetKey() = params.GetDriverName();
73 
74  string param_value;
75 
76  param_value = params.GetParam("reuse_context");
77  if (!param_value.empty()) {
78  tr->AddNode(CConfig::TParamValue(
79  "reuse_context",
80  param_value
81  ));
82  }
83 
84  param_value = params.GetParam("packet");
85  if (!param_value.empty()) {
86  tr->AddNode(CConfig::TParamValue(
87  "packet",
88  param_value
89  ));
90  }
91 
92  param_value = params.GetParam("prog_name");
93  if (!param_value.empty()) {
94  tr->AddNode(CConfig::TParamValue(
95  "prog_name",
96  param_value
97  ));
98  }
99 
100  param_value = params.GetParam("host_name");
101  if (!param_value.empty()) {
102  tr->AddNode(CConfig::TParamValue(
103  "host_name",
104  param_value
105  ));
106  }
107 
108  if (params.GetProtocolVersion() != 0) {
109  tr->AddNode(CConfig::TParamValue("version" ,
111  }
112 
113  switch (params.GetEncoding()) {
114  case eEncoding_UTF8:
115  tr->AddNode(CConfig::TParamValue("client_charset" , "UTF8"));
116  break;
117  default:
118  break;
119  };
120 
121  return tr.release();
122 }
123 
124 
125 ///////////////////////////////////////////////////////////////////////////////
128 {
129  CPluginManager_DllResolver* resolver =
132  kEmptyStr,
135  resolver->SetDllNamePrefix("ncbi");
136  return resolver;
137 }
138 
139 ///////////////////////////////////////////////////////////////////////////////
141 {
142 public:
143  C_xDriverMgr(void);
144  virtual ~C_xDriverMgr(void);
145 
146 public:
147  /// Add path for the DLL lookup
148  void AddDllSearchPath(const string& path);
149  /// Delete all user-installed paths for the DLL lookup (for all resolvers)
150  /// @param previous_paths
151  /// If non-NULL, store the prevously set search paths in this container
152  void ResetDllSearchPath(vector<string>* previous_paths = NULL);
153 
154  /// Specify which standard locations should be used for the DLL lookup
155  /// (for all resolvers). If standard locations are not set explicitelly
156  /// using this method CDllResolver::fDefaultDllPath will be used by default.
159 
160  /// Get standard locations which should be used for the DLL lookup.
161  /// @sa SetDllStdSearchPath
163 
165  const string& driver_name,
166  const TPluginManagerParamTree* const attr = NULL);
167 
169  const string& driver_name,
170  const map<string, string>* attr = NULL);
171 
172 private:
173  struct SDrivers {
174  typedef I_DriverContext* (*FDBAPI_CreateContext)(const map<string,string>* attr);
175 
176  SDrivers(const string& name, FDBAPI_CreateContext func) :
177  drv_name(name),
178  drv_func(func)
179  {
180  }
181 
182  string drv_name;
184  };
185  vector<SDrivers> m_Drivers;
186 
188 
189 private:
192 
194 };
195 
197 {
199 #ifndef NCBI_COMPILER_COMPAQ
200  // For some reason, Compaq's compiler thinks m_ContextManager is
201  // inaccessible here!
203 #endif
204 }
205 
206 
208 {
209 }
210 
211 
212 void
214 {
216 
217  m_ContextManager->AddDllSearchPath( path );
218 }
219 
220 
221 void
222 C_xDriverMgr::ResetDllSearchPath(vector<string>* previous_paths)
223 {
225 
226  m_ContextManager->ResetDllSearchPath( previous_paths );
227 }
228 
229 
232 {
234 
235  return m_ContextManager->SetDllStdSearchPath( standard_paths );
236 }
237 
238 
241 {
243 
244  return m_ContextManager->GetDllStdSearchPath();
245 }
246 
247 
250  const string& driver_name,
251  const TPluginManagerParamTree* const attr)
252 {
253  I_DriverContext* drv = NULL;
254 
255  try {
257 
258  drv = m_ContextManager->CreateInstance(
259  driver_name,
261  attr
262  );
263  }
264  catch( const CPluginManagerException& ) {
265  throw;
266  }
267  catch ( const exception& e ) {
268  DATABASE_DRIVER_ERROR( driver_name + " is not available :: " + e.what(), 300 );
269  }
270  catch ( ... ) {
271  DATABASE_DRIVER_ERROR( driver_name + " was unable to load due an unknown error", 300 );
272  }
273 
274  return drv;
275 }
276 
279  const string& driver_name,
280  const map<string, string>* attr)
281 {
282  unique_ptr<TPluginManagerParamTree> pt;
283  const TPluginManagerParamTree* nd = NULL;
284 
285  if ( attr != NULL ) {
286  pt.reset( MakePluginManagerParamTree(driver_name, attr) );
287  _ASSERT(pt.get());
288  nd = pt->FindNode( driver_name );
289  }
290 
291  return GetDriverContext(driver_name, nd);
292 }
293 
294 ////////////////////////////////////////////////////////////////////////////////
296 
297 
298 ////////////////////////////////////////////////////////////////////////////////
299 C_DriverMgr::C_DriverMgr(unsigned int /* nof_drivers */)
300 {
301 }
302 
303 
305 {
306 }
307 
309  string* /* err_msg */,
310  const map<string,string>* attr)
311 {
312  return s_DrvMgr->GetDriverContext( driver_name, attr );
313 }
314 
315 
316 void
317 C_DriverMgr::AddDllSearchPath(const string& path)
318 {
319  s_DrvMgr->AddDllSearchPath( path );
320 }
321 
322 
323 void
324 C_DriverMgr::ResetDllSearchPath(vector<string>* previous_paths)
325 {
326  s_DrvMgr->ResetDllSearchPath( previous_paths );
327 }
328 
329 
332 {
333  return s_DrvMgr->SetDllStdSearchPath( standard_paths );
334 }
335 
336 
339 {
340  return s_DrvMgr->GetDllStdSearchPath();
341 }
342 
343 
346  const string& driver_name,
347  const TPluginManagerParamTree* const attr)
348 {
349  return s_DrvMgr->GetDriverContext( driver_name, attr );
350 }
351 
352 
355  const string& driver_name,
356  const map<string, string>* attr)
357 {
358  return s_DrvMgr->GetDriverContext( driver_name, attr );
359 }
360 
361 
362 ///////////////////////////////////////////////////////////////////////////////
364 Get_I_DriverContext(const string& driver_name, const map<string, string>* attr)
365 {
366  typedef CPluginManager<I_DriverContext> TReaderManager;
367  typedef CPluginManagerGetter<I_DriverContext> TReaderManagerStore;
368  I_DriverContext* drv = NULL;
369  const TPluginManagerParamTree* nd = NULL;
370 
372  _ASSERT(ReaderManager);
373 
374  try {
375  unique_ptr<TPluginManagerParamTree> pt;
376 
377  if ( attr != NULL ) {
378  pt.reset( MakePluginManagerParamTree(driver_name, attr) );
379 
380  _ASSERT( pt.get() );
381 
382  nd = pt->FindNode( driver_name );
383  }
384  drv = ReaderManager->CreateInstance(
385  driver_name,
387  nd
388  );
389  }
390  catch( const CPluginManagerException& ) {
391  throw;
392  }
393  catch ( const exception& e ) {
394  DATABASE_DRIVER_ERROR( driver_name + " is not available :: " + e.what(), 300 );
395  }
396  catch ( ... ) {
397  DATABASE_DRIVER_ERROR( driver_name + " was unable to load due an unknown error", 300 );
398  }
399 
400  return drv;
401 }
402 
403 
404 ///////////////////////////////////////////////////////////////////////////////
406 {
407  typedef CPluginManager<I_DriverContext> TReaderManager;
408  typedef CPluginManagerGetter<I_DriverContext> TReaderManagerStore;
409  I_DriverContext* drv = NULL;
410 
412  _ASSERT(ReaderManager);
413 
414  try {
415  unique_ptr<TPluginManagerParamTree> pt;
416 
417  pt.reset(MakePluginManagerParamTree(params));
418  _ASSERT( pt.get() );
419 
420  drv = ReaderManager->CreateInstance(
421  params.GetDriverName(),
423  pt.get()
424  );
425  }
426  catch( const CPluginManagerException& ) {
427  throw;
428  }
429  catch ( const exception& e ) {
430  DATABASE_DRIVER_ERROR( params.GetDriverName() + " is not available :: " + e.what(), 300 );
431  }
432  catch ( ... ) {
433  DATABASE_DRIVER_ERROR( params.GetDriverName() + " was unable to load due an unknown error", 300 );
434  }
435 
436  return drv;
437 }
438 
439 
441 
442 
CDBConnParams::
Definition: interfaces.hpp:258
CFastMutex –.
Definition: ncbimtx.hpp:667
CInterfaceVersion<> –.
CMemoryRegistry –.
Definition: ncbireg.hpp:584
CPluginManagerException –.
static TPluginManager * Get(void)
Service class for DLLs resolution.
CPluginManager<> –.
CRef –.
Definition: ncbiobj.hpp:618
CSafeStatic<>::
definition of a Culling tree
Definition: ncbi_tree.hpp:100
CDllResolver::TExtraDllPath SetDllStdSearchPath(CDllResolver::TExtraDllPath standard_paths)
Specify which standard locations should be used for the DLL lookup (for all resolvers).
Definition: driver_mgr.cpp:231
CFastMutex m_Mutex
Definition: driver_mgr.cpp:187
C_xDriverMgr(void)
Definition: driver_mgr.cpp:196
I_DriverContext * GetDriverContext(const string &driver_name, const TPluginManagerParamTree *const attr=NULL)
Definition: driver_mgr.cpp:249
CDllResolver::TExtraDllPath GetDllStdSearchPath(void) const
Get standard locations which should be used for the DLL lookup.
Definition: driver_mgr.cpp:240
CPluginManager< I_DriverContext > TContextManager
Definition: driver_mgr.cpp:190
void AddDllSearchPath(const string &path)
Add path for the DLL lookup.
Definition: driver_mgr.cpp:213
CRef< TContextManager > m_ContextManager
Definition: driver_mgr.cpp:193
CPluginManagerGetter< I_DriverContext > TContextManagerStore
Definition: driver_mgr.cpp:191
virtual ~C_xDriverMgr(void)
Definition: driver_mgr.cpp:207
void ResetDllSearchPath(vector< string > *previous_paths=NULL)
Delete all user-installed paths for the DLL lookup (for all resolvers)
Definition: driver_mgr.cpp:222
vector< SDrivers > m_Drivers
Definition: driver_mgr.cpp:185
const_iterator begin() const
Definition: map.hpp:151
const_iterator end() const
Definition: map.hpp:152
static CSafeStatic< C_xDriverMgr > s_DrvMgr
Definition: driver_mgr.cpp:295
TPluginManagerParamTree * MakePluginManagerParamTree(const string &driver_name, const map< string, string > *attr)
Definition: driver_mgr.cpp:49
#define NULL
Definition: ncbistd.hpp:225
void ResetDllSearchPath(vector< string > *previous_paths=NULL)
Delete all user-installed paths for the DLL lookup (for all resolvers)
Definition: driver_mgr.cpp:324
I_DriverContext * Get_I_DriverContext(const string &driver_name, const map< string, string > *attr)
Definition: driver_mgr.cpp:364
CDllResolver::TExtraDllPath SetDllStdSearchPath(CDllResolver::TExtraDllPath standard_paths)
Specify which standard locations should be used for the DLL lookup (for all resolvers).
Definition: driver_mgr.cpp:331
I_DriverContext * GetDriverContextFromTree(const string &driver_name, const TPluginManagerParamTree *const attr=NULL)
Definition: driver_mgr.cpp:345
virtual ~C_DriverMgr()
Definition: driver_mgr.cpp:304
I_DriverContext * GetDriverContext(const string &driver_name, string *err_msg=0, const map< string, string > *attr=0)
Definition: driver_mgr.cpp:308
I_DriverContext * MakeDriverContext(const CDBConnParams &params)
Definition: driver_mgr.cpp:405
I_DriverContext * GetDriverContextFromMap(const string &driver_name, const map< string, string > *attr=NULL)
Definition: driver_mgr.cpp:354
C_DriverMgr(unsigned int nof_drivers=16)
Definition: driver_mgr.cpp:299
CDllResolver::TExtraDllPath GetDllStdSearchPath(void) const
Get standard locations which should be used for the DLL lookup.
Definition: driver_mgr.cpp:338
void AddDllSearchPath(const string &path)
Add path for the DLL lookup.
Definition: driver_mgr.cpp:317
#define DATABASE_DRIVER_ERROR(message, err_code)
Definition: exception.hpp:740
virtual EEncoding GetEncoding(void) const =0
virtual Uint4 GetProtocolVersion(void) const =0
virtual string GetDriverName(void) const =0
virtual string GetParam(const string &key) const =0
Parameters, which are not listed above explicitly, should be retrieved via SetParam() method.
int TExtraDllPath
Definition: ncbidll.hpp:473
@ eNoAutoUnload
Definition: ncbidll.hpp:145
static TParamTree * ConvertRegToTree(const IRegistry &reg, NStr::ECase use_case=NStr::eNocase)
Reconstruct param tree from the application registry.
virtual void SetDllNamePrefix(const string &prefix)
Set DLL file name prefix.
#define NCBI_INTERFACE_VERSION(iface)
Macro to construct CVersionInfo class using interface name (relies on CInterfaceVersion class)
CPluginManager_DllResolver * operator()(void)
bool Set(const string &section, const string &name, const string &value, TFlags flags=0, const string &comment=kEmptyStr)
Set the configuration parameter value.
Definition: ncbireg.cpp:826
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define kEmptyStr
Definition: ncbistr.hpp:123
static string IntToString(int value, TNumToStringFlags flags=0, int base=10)
Convert int to string.
Definition: ncbistr.hpp:5084
@ eEncoding_UTF8
Definition: ncbistr.hpp:201
const TTreeType * FindNode(const TKeyType &key, TNodeSearchMode sflag=eImmediateAndTop) const
Search for node.
Definition: ncbi_tree.hpp:970
const TYPE & Get(const CNamedParameterList *param)
Static variables safety - create on demand, destroy on application termination.
Define class Dll and for Portable DLL handling.
Process information in the NCBI Registry, including working with configuration files.
Plugin manager (using class factory paradigm).
Helper classes and templates to implement plugins.
Node data template for id-value trees.
Definition: ncbi_tree.hpp:453
FDBAPI_CreateContext drv_func
Definition: driver_mgr.cpp:183
I_DriverContext *(* FDBAPI_CreateContext)(const map< string, string > *attr)
Definition: driver_mgr.cpp:174
SDrivers(const string &name, FDBAPI_CreateContext func)
Definition: driver_mgr.cpp:176
#define _ASSERT
Modified on Sun Feb 25 03:04:01 2024 by modify_doxy.py rev. 669887