00001 /* 00002 Copyright 2007 Erez Bibi (erezbibi@users.sourceforge.net) 00003 This file is part of Beesnest. 00004 00005 Beesnest is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or 00008 (at your option) any later version. 00009 00010 Beesnest is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with Beesnest; if not, write to the Free Software 00017 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00018 */ 00019 00020 /* DcEnginesBank.h: interface for the CdcEnginesBank class. */ 00021 00022 #ifndef CDC_ENGINES_BANK 00023 #define CDC_ENGINES_BANK 00024 00025 #include "DcGlobals.h" 00026 #include "DcParameters.h" 00027 #include "DcEnginesSet.h" 00028 #include "DcUserStatus.h" 00029 #include "Interfaces/DcEngine.h" 00030 #include "Interfaces/DcRequest.h" 00031 #include "Interfaces/DcResponse.h" 00032 #include "Interfaces/DcAdaptersSet.h" 00033 00034 #include _GET_INCLUDE_PATH (DcDynamicLibrary.h) 00035 00036 /** 00037 * The Engines Bank class is responsible for loading the scripts engines from 00038 * their libraries, and passing them to the CdcConnectionThread object so it can 00039 * execute scripts on the buffer that is going back to the client. This class 00040 * loads an engine library on the first request for this engine. Then it 00041 * initializes the engine, and stores the loaded library. The library will stay 00042 * loaded until Beesnest exits. EngineBank can store engines in the user 00043 * EnginesSet object (if the user is logged in so he has an Engine Set). When 00044 * CdcConnectionThread asks for an engine the class first looks if an engine 00045 * from this type (engine name) is in the user Engines Set. If not, it creates 00046 * the engine from it's library, and passes it down. For the same page though, 00047 * EnginesBank will store the engines it creates, so the engine can process 00048 * different sections of scripts under the same environment. But, as mentioned, 00049 * if the user is logged in, Engine Bank can store an engine over multiple 00050 * pages. That means a programmer can create a script that will start to run in 00051 * one page, and continue on another one (with all the data still in place). 00052 * <p> 00053 * This class built in a very smiler way to CdcAdaptersBank. Both works on the 00054 * exact same principle, But here, unlike in the Adapters Bank, the EnginesSet 00055 * is a server for this class. 00056 * <p> 00057 * This class is thread safe. 00058 * 00059 * <p> 00060 * @author Erez Bibi 00061 */ 00062 class CdcEnginesBank 00063 { 00064 private: 00065 typedef map <CdcString, CdcDynamicLibrary*> t_engines_lib_map; 00066 /** The static LIBs map. */ 00067 static t_engines_lib_map mLibs; 00068 00069 /** Static Mutex for this class. */ 00070 static CdcMutex oMutex; 00071 00072 typedef map <CdcString, IdcEngine*> t_engines_map; 00073 /** Map to store loaded engines. */ 00074 t_engines_map mEngines; 00075 00076 /** Pointer to the current Request interface. */ 00077 IdcRequest* pRequest; 00078 00079 /** Pointer to the current Response interface. */ 00080 IdcResponse* pResponse; 00081 00082 /** Pointer to the current Adapters Set interface (if exists!). */ 00083 IdcAdaptersSet* pAdaptersSet; 00084 /** Pointer to the current Engines Set (if exists!). */ 00085 CdcEnginesSet* pEnginesSet; 00086 00087 /** The default engine name. */ 00088 static CdcString sDefaultEngine; 00089 00090 /** 00091 * This function loads an engine from it's library. 00092 * <p> 00093 * @param lib Reference to the library to load the engine from. 00094 * @param name The name of the engine (just so I can pass it to the engine 00095 * itself). 00096 * @return (IdcEngine*) Pointer to this engine or NULL.\ 00097 */ 00098 IdcEngine* LoadEngine (CdcDynamicLibrary& lib, const CdcString& name); 00099 00100 /** 00101 * Loads a library by its name. 00102 * <p> 00103 * @param name The library name. 00104 * @return (CdcDynamicLibrary*) Pointer to the library object. This pointer 00105 * will not be NULL. 00106 */ 00107 CdcDynamicLibrary* LoadLib (const CdcString& name); 00108 00109 /** 00110 * This function executes an Init function in a library, if it exists. 00111 * <p> 00112 * @param oLib Reference to the library to load the function from. 00113 */ 00114 static void ExecInit (CdcDynamicLibrary& olib); 00115 00116 /** 00117 * This function executes a Term function in a library, if it exists. 00118 * <p> 00119 * @param oLib Reference to the library to load the function from. 00120 */ 00121 static void ExecTerm (CdcDynamicLibrary& olib); 00122 00123 public: 00124 /** 00125 * Constructor, initialize an EnginesBank object with pointers to the 00126 * current Request, Response and AdaptersSet objects. This class does 00127 * nothing with these objects apart of passing them to every engine it loads. 00128 * Note: pstatus will be NULL if the user is not logged in! 00129 */ 00130 CdcEnginesBank (IdcRequest* preq, IdcResponse* pres, CdcUserStatus* pstatus); 00131 00132 /** 00133 * Destructor, stores or releases every engine this object loaded. 00134 */ 00135 ~CdcEnginesBank (); 00136 00137 /** 00138 * This function will look for a loaded engine from the type 'name' in the 00139 * engines map. If it founds one, it returns it's pointer, if not it loads 00140 * the engine from it's library, puts it in the map, and returns the pointer. 00141 * <p> 00142 * @param name The name of the engine to load. An empty string will load the 00143 * default engine. 00144 * @return (IdcEngine*) Pointer to the engine, or NULL if it cannot be found. 00145 */ 00146 IdcEngine* GetEngine (const CdcString& name); 00147 00148 /** 00149 * This function will delete an loaded engine (if it exists). I use this 00150 * function to prevent the class from storing a "Storable" engine in the 00151 * Engines Set. Beesnest will call this function if the script ends with 00152 * "[delete]%>" 00153 * <p> 00154 * @param name The name of the engine to delete. An empty string will delete 00155 * the default engine. 00156 */ 00157 void DeleteEngine (const CdcString& name); 00158 00159 /** 00160 * Just set the default engine name. Getting it from the configuration file. 00161 */ 00162 static void Init (); 00163 00164 /** 00165 * Static function to terminate engines. Every engine library may have a 00166 * termination function, this function will call it. 00167 */ 00168 static void Term (); 00169 }; 00170 #endif /* CDC_ENGINES_BANK */