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
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
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
00098
00099
00100 class PPluginManager : public PObject
00101 {
00102 PCLASSINFO(PPluginManager, PObject);
00103
00104 public:
00105
00106 BOOL LoadPlugin (const PString & fileName);
00107 void LoadPluginDirectory (const PDirectory & dir);
00108
00109
00110 PStringList GetPluginTypes() const;
00111 PStringList GetPluginsProviding (const PString & serviceType) const;
00112 PPluginServiceDescriptor * GetServiceDescriptor (const PString & serviceName, const PString & serviceType);
00113
00114
00115 BOOL RegisterService (const PString & serviceName, const PString & serviceType, PPluginServiceDescriptor * descriptor);
00116
00117
00118 static PStringArray GetPluginDirs();
00119
00120
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
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 & , INT )
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