Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "DcEnginesBank.h"
00026
00027 #define FUNC_NAME "GetEngine"
00028 #define INIT_FUNC_NAME "Init"
00029 #define TERM_FUNC_NAME "Term"
00030
00031
00032 typedef bool (__stdcall *cfunc)(IdcEngine** pp_engine,
00033 const char* name,
00034 bool (*pGetParam)(const char*, char*, int));
00035
00036 typedef void (__stdcall *cfunc_void)();
00037
00038 CdcEnginesBank::t_engines_lib_map CdcEnginesBank::mLibs;
00039 CdcMutex CdcEnginesBank::oMutex;
00040 CdcString CdcEnginesBank::sDefaultEngine;
00041
00042 CdcEnginesBank::CdcEnginesBank (IdcRequest* preq, IdcResponse* pres,
00043 CdcUserStatus* pstatus)
00044 : pRequest (preq), pResponse (pres), pAdaptersSet (NULL), pEnginesSet (NULL)
00045 {
00046 if (pstatus)
00047 {
00048 pAdaptersSet = pstatus->GetAdaptersSet ();
00049 pEnginesSet = pstatus->GetEnginesSet ();
00050 }
00051 }
00052
00053 CdcEnginesBank::~CdcEnginesBank ()
00054 {
00055 for (CdcEnginesBank::t_engines_map::iterator pos = mEngines.begin ();
00056 pos != mEngines.end (); ++pos)
00057 if (pos->second)
00058 {
00059 if (pEnginesSet && pos->second->IsStorable () &&
00060 CdcParameters::GetParam ("Engines->" + pos->first + "->Storable")
00061 == "yes")
00062 {
00063 if (pEnginesSet->PutEngine (pos->first, pos->second) == false)
00064 delete pos->second;
00065 }
00066 else
00067 delete pos->second;
00068 }
00069 }
00070
00071
00072
00073
00074 IdcEngine* CdcEnginesBank::GetEngine (const CdcString& name)
00075 {
00076 CdcString eng_name = name;
00077 if (eng_name.IsEmpty () == true)
00078 eng_name = sDefaultEngine;
00079 if (eng_name.IsEmpty () == true)
00080 return NULL;
00081
00082 CdcEnginesBank::t_engines_map::iterator itr;
00083 itr = mEngines.find (eng_name);
00084 if (itr != mEngines.end () && itr->second)
00085 return itr->second;
00086
00087 IdcEngine* peng = NULL;
00088 if (pEnginesSet)
00089 {
00090 peng = pEnginesSet->GetEngine (eng_name);
00091 if (peng)
00092 {
00093 mEngines [eng_name] = peng;
00094 peng->Prepare (pRequest, pResponse, pAdaptersSet);
00095 return peng;
00096 }
00097 }
00098
00099 CdcDynamicLibrary* plib = NULL;
00100 CdcString lib_name = CdcParameters::GetParam ("Engines->" + eng_name +
00101 "->File-Name");
00102 if (lib_name.IsEmpty () == false)
00103 {
00104 CdcEnginesBank::t_engines_lib_map::iterator ditr = mLibs.find (lib_name);
00105 if (ditr != mLibs.end ())
00106 plib = ditr->second;
00107 else
00108 plib = LoadLib (lib_name);
00109 }
00110 if (plib != NULL)
00111 peng = LoadEngine (*plib, eng_name);
00112 mEngines [eng_name] = peng;
00113 return peng;
00114 }
00115
00116 void CdcEnginesBank::DeleteEngine (const CdcString& name)
00117 {
00118 CdcString eng_name = name;
00119 if (eng_name.IsEmpty () == true)
00120 eng_name = sDefaultEngine;
00121 if (eng_name.IsEmpty () == true)
00122 return;
00123
00124 CdcEnginesBank::t_engines_map::iterator itr;
00125 itr = mEngines.find (eng_name);
00126 if (itr != mEngines.end () && itr->second)
00127 {
00128 delete (itr->second);
00129 mEngines.erase (itr);
00130 }
00131 }
00132
00133 IdcEngine* CdcEnginesBank::LoadEngine (CdcDynamicLibrary& lib,
00134 const CdcString& name)
00135 {
00136
00137 cfunc libGetEngine = (cfunc)lib.GetFunctionByName (FUNC_NAME);
00138 if (libGetEngine == NULL)
00139 {
00140 PrintString (lib.GetLastError (), PS_GEN);
00141 return NULL;
00142 }
00143
00144 IdcEngine* pengine = NULL;
00145 libGetEngine (&pengine, name.GetBuffer (), &ExtGetParam);
00146 if (pengine == NULL)
00147 {
00148 PrintString ("Unable to get the engine - " + name, PS_GEN);
00149 return NULL;
00150 }
00151
00152 pengine->Prepare (pRequest, pResponse, pAdaptersSet);
00153 return pengine;
00154 }
00155
00156 CdcDynamicLibrary* CdcEnginesBank::LoadLib (const CdcString& name)
00157 {
00158 oMutex.Lock ();
00159 CdcDynamicLibrary* plib = NULL;
00160
00161 CdcEnginesBank::t_engines_lib_map::iterator itr;
00162 itr = mLibs.find (name);
00163 if (itr == mLibs.end ())
00164 {
00165 plib = new CdcDynamicLibrary (name);
00166 if (plib != NULL)
00167 {
00168 CdcEnginesBank::ExecInit (*plib);
00169 mLibs [name] = plib;
00170 }
00171 }
00172 else
00173 plib = itr->second;
00174 oMutex.Unlock ();
00175 return plib;
00176 }
00177
00178 void CdcEnginesBank::Init ()
00179 {
00180 sDefaultEngine = CdcParameters::GetParam ("Server->Default-Engine");
00181 }
00182
00183 void CdcEnginesBank::Term ()
00184 {
00185
00186 oMutex.Lock ();
00187 for (CdcEnginesBank::t_engines_lib_map::iterator pos = mLibs.begin ();
00188 pos != mLibs.end (); ++pos)
00189 if (pos->second)
00190 {
00191 CdcEnginesBank::ExecTerm (*(pos->second));
00192 delete pos->second;
00193 }
00194 oMutex.Unlock ();
00195 }
00196
00197 void CdcEnginesBank::ExecInit (CdcDynamicLibrary& lib)
00198 {
00199
00200 cfunc_void libInit = (cfunc_void)lib.GetFunctionByName (INIT_FUNC_NAME);
00201 if (libInit != NULL)
00202 libInit ();
00203 }
00204
00205 void CdcEnginesBank::ExecTerm (CdcDynamicLibrary& lib)
00206 {
00207
00208 cfunc_void libTerm = NULL;
00209 libTerm = (cfunc_void)lib.GetFunctionByName (TERM_FUNC_NAME);
00210 if (libTerm != NULL)
00211 libTerm ();
00212 }
00213