Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

pluginmgr.h

Go to the documentation of this file.
00001 /*
00002  * pluginmgr.h
00003  *
00004  * Plugin Manager Class Declarations
00005  *
00006  * Portable Windows Library
00007  *
00008  * Contributor(s): Snark at GnomeMeeting
00009  *
00010  * $Log: pluginmgr.h,v $
00011  * Revision 1.16  2004/08/05 03:45:35  csoutheren
00012  * Fixed problems with plugin suffix not being propagated to sudirectories
00013  *
00014  * Revision 1.15  2004/06/24 23:10:27  csoutheren
00015  * Require plugins to have _pwplugin suffix
00016  *
00017  * Revision 1.14  2004/06/01 05:44:57  csoutheren
00018  * Added OnShutdown to allow cleanup on exit
00019  *
00020  * Revision 1.13  2004/05/19 06:54:11  csoutheren
00021  * Removed unused code
00022  *
00023  * Revision 1.12  2004/05/18 06:01:06  csoutheren
00024  * Deferred plugin loading until after main has executed by using abstract factory classes
00025  *
00026  * Revision 1.11  2004/05/17 06:05:20  csoutheren
00027  * Changed "make docs" to use doxygen
00028  * Added new config file and main page
00029  *
00030  * Revision 1.10  2004/04/22 11:43:47  csoutheren
00031  * Factored out functions useful for loading dynamic libraries
00032  *
00033  * Revision 1.9  2004/04/22 07:55:30  csoutheren
00034  * Fix problem with generic plugin manager having pure virtual. Thanks to Ben Lear
00035  *
00036  * Revision 1.8  2004/04/14 11:14:10  csoutheren
00037  * Final fix for generic plugin manager
00038  *
00039  * Revision 1.7  2004/04/14 10:57:38  csoutheren
00040  * Removed multiple definition of statc function in generic plugin functions
00041  *
00042  * Revision 1.6  2004/04/14 10:01:54  csoutheren
00043  * Fixed compile problem on Windows
00044  *
00045  * Revision 1.5  2004/04/14 08:12:02  csoutheren
00046  * Added support for generic plugin managers
00047  *
00048  * Revision 1.4  2004/03/23 04:43:42  csoutheren
00049  * Modified plugin manager to allow code modules to be notified when plugins
00050  * are loaded or unloaded
00051  *
00052  * Revision 1.3  2003/11/12 10:24:35  csoutheren
00053  * Changes to allow operation of static plugins under Windows
00054  *
00055  * Revision 1.2  2003/11/12 03:26:17  csoutheren
00056  * Initial version of plugin code from Snark of GnomeMeeting with changes
00057  *    by Craig Southeren os Post Increment
00058  *
00059  *
00060  */
00061 
00062 #ifndef _PLUGINMGR_H
00063 #define _PLUGINMGR_H
00064 
00065 #define DEFAULT_PLUGINDIR "/usr/lib/pwlib"
00066 
00067 #include <ptlib/plugin.h>
00068 
00069 template <class C>
00070 void PLoadPluginDirectory(C & obj, const PDirectory & directory, const char * suffix = NULL)
00071 {
00072   PDirectory dir = directory;
00073   if (!dir.Open()) {
00074     PTRACE(4, "Cannot open plugin directory " << dir);
00075     return;
00076   }
00077   PTRACE(4, "Enumerating plugin directory " << dir);
00078   do {
00079     PString entry = dir + dir.GetEntryName();
00080     if (dir.IsSubDir())
00081       PLoadPluginDirectory<C>(obj, entry, suffix);
00082     else {
00083       PFilePath fn(entry);
00084       if (
00085            (fn.GetType() *= PDynaLink::GetExtension()) &&
00086            (
00087              (suffix == NULL) || (fn.GetTitle().Right(strlen(suffix)) *= suffix)
00088            )
00089          ) 
00090         obj.LoadPlugin(entry);
00091     }
00092   } while (dir.Next());
00093 }
00094 
00096 //
00097 //  Manager for plugins
00098 //
00099 
00100 class PPluginManager : public PObject
00101 {
00102   PCLASSINFO(PPluginManager, PObject);
00103 
00104   public:
00105     // functions to load/unload a dynamic plugin 
00106     BOOL LoadPlugin (const PString & fileName);
00107     void LoadPluginDirectory (const PDirectory & dir);
00108   
00109     // functions to access the plugins' services 
00110     PStringList GetPluginTypes() const;
00111     PStringList GetPluginsProviding (const PString & serviceType) const;
00112     PPluginServiceDescriptor * GetServiceDescriptor (const PString & serviceName, const PString & serviceType);
00113 
00114     // function to register a service (used by the plugins themselves)
00115     BOOL RegisterService (const PString & serviceName, const PString & serviceType, PPluginServiceDescriptor * descriptor);
00116 
00117     // Get the list of plugin directories
00118     static PStringArray GetPluginDirs();
00119 
00120     // static functions for accessing global instances of plugin managers
00121     static PPluginManager & GetPluginManager();
00122 
00140     void AddNotifier(
00141       const PNotifier & filterFunction,
00142       BOOL existing = FALSE
00143     );
00144 
00145     void RemoveNotifier(
00146       const PNotifier & filterFunction
00147     );
00148 
00149   protected:
00150     void CallNotifier(PDynaLink & dll, INT code);
00151 
00152     PMutex pluginListMutex;
00153     PList<PDynaLink> pluginList;
00154     
00155     PMutex serviceListMutex;
00156     PList<PPluginService> serviceList;
00157 
00158     PMutex notifierMutex;
00159     PList<PNotifier> notifierList;
00160 };
00161 
00163 //
00164 //  Manager for plugin modules
00165 //
00166 
00167 class PPluginModuleManager : public PObject
00168 {
00169   public:
00170     typedef PDictionary<PString, PDynaLink> PluginListType;
00171 
00172     PPluginModuleManager(const char * _signatureFunctionName, PPluginManager * pluginMgr = NULL);
00173 
00174     BOOL LoadPlugin(const PString & fileName)
00175     { if (pluginMgr == NULL) return FALSE; else return pluginMgr->LoadPlugin(fileName); }
00176 
00177     void LoadPluginDirectory(const PDirectory &directory)
00178     { if (pluginMgr != NULL) pluginMgr->LoadPluginDirectory(directory); }
00179 
00180     virtual void OnLoadPlugin(PDynaLink & /*dll*/, INT /*code*/)
00181     { }
00182 
00183     virtual PluginListType GetPluginList() const
00184     { return pluginList; }
00185 
00186     virtual void OnShutdown()
00187     { }
00188 
00189   protected:
00190     PluginListType pluginList;
00191     PDECLARE_NOTIFIER(PDynaLink, PPluginModuleManager, OnLoadModule);
00192 
00193   protected:
00194     const char * signatureFunctionName;
00195     PPluginManager * pluginMgr;
00196 };
00197 
00198 #endif // ifndef _PLUGINMGR_H

Generated on Mon Feb 21 20:43:09 2005 for PWLib by  doxygen 1.4.1