CPluginHandler.cpp [plain text]
#include <CoreFoundation/CFPlugIn.h>
#include <CoreFoundation/CFString.h>
#include <CoreFoundation/CFURL.h>
#include <CoreFoundation/CFPriv.h>
#include "CPluginHandler.h"
#include "CPlugInList.h"
#include "ServerControl.h"
#include "DirServiceMain.h"
#include "CString.h"
#include "COSUtils.h"
#include "CLog.h"
#include "CServerPlugin.h"
DSEventSemaphore *gKickPluginHndlrThread = nil;
CPluginHandler *gPluginHandler = nil;
CPluginHandler::CPluginHandler ( void )
: DSCThread(kTSPlugInHndlrThread )
{
fThreadSignature = kTSPlugInHndlrThread;
if ( gKickPluginHndlrThread == nil )
{
gKickPluginHndlrThread = new DSEventSemaphore();
if ( gKickPluginHndlrThread == nil ) throw((sInt32)eMemoryAllocError);
}
}
CPluginHandler::~CPluginHandler()
{
}
void CPluginHandler::WakupHandlers ( void )
{
gKickPluginHndlrThread->Signal();
}
void CPluginHandler::StartThread ( void )
{
if ( this == nil ) throw((sInt32)eMemoryError);
this->Resume();
SetThreadRunState( kThreadRun );
}
void CPluginHandler::StopThread ( void )
{
if ( this == nil ) throw((sInt32)eMemoryError);
SetThreadRunState( kThreadStop );
}
long CPluginHandler::ThreadMain ( void )
{
uInt32 uiPluginCnt = 0;
uiPluginCnt = LoadPlugins();
if ( uiPluginCnt == 0 )
{
ERRORLOG( kLogApplication, "*** WARNING: No Plugins loaded ***" );
}
else
{
DBGLOG1( kLogApplication, "%d Plugins loaded or processed.", uiPluginCnt );
DBGLOG( kLogApplication, "Initializing plugins." );
gPlugins->InitPlugIns();
}
return( 0 );
}
sInt32 CPluginHandler::LoadPlugins ( void )
{
sInt32 status = eDSNoErr;
uInt32 uiCount = 0;
uInt32 uiMask = kCFSystemDomainMask;
CString cSubPath( COSUtils::GetStringFromList( kAppStringsListID, kStrPlugInsFolder ) );
CString cOtherSubPath( COSUtils::GetStringFromList( kAppStringsListID, kStrOtherPlugInsFolder ) );
CString cPlugExt( COSUtils::GetStringFromList( kAppStringsListID, kStrPluginExtension ) );
register CFIndex i;
CFArrayRef aPaths = nil;
CFStringRef sType = nil;
char string [ PATH_MAX ];
status = CServerPlugin::ProcessConfigurePlugin();
if (status == eDSNoErr)
{
uiCount++;
}
aPaths = ::CFCopySearchPathForDirectoriesInDomains( kCFLibraryDirectory, (CFSearchPathDomainMask)uiMask, true );
if ( aPaths != nil )
{
i = ::CFArrayGetCount( aPaths );
if ( i != 0 )
{
sType = ::CFStringCreateWithCString( kCFAllocatorDefault, cPlugExt.GetData(), kCFStringEncodingMacRoman );
while ( i-- )
{
CFURLRef urlPath = (CFURLRef)::CFArrayGetValueAtIndex( aPaths, i );
CFStringRef sBase, sPath;
CFArrayRef aBundles;
sBase = ::CFURLCopyFileSystemPath( urlPath, kCFURLPOSIXPathStyle );
sPath = ::CFStringCreateWithFormat( kCFAllocatorDefault, NULL, CFSTR( "%@/%s" ), sBase, cSubPath.GetData() );
::CFRelease( sBase );
sBase = nil;
::CFStringGetCString( sPath, string, sizeof( string ), kCFStringEncodingMacRoman );
DBGLOG( kLogApplication, "Checking for plugins in:" );
DBGLOG1( kLogApplication, " %s", string );
urlPath = ::CFURLCreateWithFileSystemPath( kCFAllocatorDefault, sPath, kCFURLPOSIXPathStyle, true );
::CFRelease( sPath );
sPath = nil;
aBundles = ::CFBundleCopyResourceURLsOfTypeInDirectory( urlPath, sType, NULL );
::CFRelease( urlPath );
urlPath = nil;
if ( aBundles == nil )
{
continue;
}
for ( register CFIndex j = 0; j < ::CFArrayGetCount( aBundles ); ++j )
{
status = CServerPlugin::ProcessURL( (CFURLRef)::CFArrayGetValueAtIndex( aBundles, j ) );
if ( status == eDSNoErr )
{
uiCount++;
}
}
if ( aBundles != nil )
{
::CFRelease( aBundles );
aBundles = nil;
}
}
}
}
if ( sType != nil )
{
::CFRelease( sType );
sType = nil;
}
if ( aPaths != nil )
{
::CFRelease( aPaths );
aPaths = nil;
}
uiMask = kCFLocalDomainMask;
i = 0;
aPaths = ::CFCopySearchPathForDirectoriesInDomains( kCFLibraryDirectory, (CFSearchPathDomainMask)uiMask, true );
if ( aPaths != nil )
{
i = ::CFArrayGetCount( aPaths );
if ( i != 0 )
{
sType = ::CFStringCreateWithCString( kCFAllocatorDefault, cPlugExt.GetData(), kCFStringEncodingMacRoman );
while ( i-- )
{
CFURLRef urlPath = (CFURLRef)::CFArrayGetValueAtIndex( aPaths, i );
CFStringRef sBase, sPath;
CFArrayRef aBundles;
sBase = ::CFURLCopyFileSystemPath( urlPath, kCFURLPOSIXPathStyle );
sPath = ::CFStringCreateWithFormat( kCFAllocatorDefault, NULL, CFSTR( "%@/%s" ), sBase, cOtherSubPath.GetData() );
::CFRelease( sBase );
sBase = nil;
::CFStringGetCString( sPath, string, sizeof( string ), kCFStringEncodingMacRoman );
DBGLOG( kLogApplication, "Checking for plugins in:" );
DBGLOG1( kLogApplication, " %s", string );
urlPath = ::CFURLCreateWithFileSystemPath( kCFAllocatorDefault, sPath, kCFURLPOSIXPathStyle, true );
::CFRelease( sPath );
sPath = nil;
aBundles = ::CFBundleCopyResourceURLsOfTypeInDirectory( urlPath, sType, NULL );
::CFRelease( urlPath );
urlPath = nil;
if ( aBundles == nil )
{
continue;
}
register CFIndex j = ::CFArrayGetCount( aBundles );
while ( j-- )
{
status = CServerPlugin::ProcessURL( (CFURLRef)::CFArrayGetValueAtIndex( aBundles, j ) );
if ( status == eDSNoErr )
{
uiCount++;
}
}
if ( aBundles != nil )
{
::CFRelease( aBundles );
aBundles = nil;
}
}
}
}
if ( sType != nil )
{
::CFRelease( sType );
sType = nil;
}
if ( aPaths != nil )
{
::CFRelease( aPaths );
aPaths = nil;
}
return( uiCount );
}