File: ModuleFactory.cpp

package info (click to toggle)
pinot 0.95-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 6,860 kB
  • ctags: 4,511
  • sloc: cpp: 39,387; sh: 9,973; ansic: 4,174; makefile: 657; xml: 372; python: 260
file content (523 lines) | stat: -rw-r--r-- 11,914 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
/*
 *  Copyright 2007-2008 Fabrice Colin
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#ifdef HAVE_DLFCN_H
#include <dlfcn.h>
#endif
#include <utility>
#include <iostream>

#ifdef HAVE_DBUS
#include "DBusIndex.h"
#endif
#include "PluginWebEngine.h"
#include "ModuleFactory.h"

#ifdef HAVE_DLFCN_H
#ifdef __CYGWIN__
#define DLOPEN_FLAGS RTLD_NOW
#else
#define DLOPEN_FLAGS (RTLD_NOW|RTLD_LOCAL)
#endif
#endif

#define GETMODULEPROPERTIESFUNC	"getModuleProperties"
#define OPENORCREATEINDEXFUNC	"openOrCreateIndex"
#define MERGEINDEXESFUNC	"mergeIndexes"
#define GETINDEXFUNC		"getIndex"
#define GETSEARCHENGINEFUNC	"getSearchEngine"
#define CLOSEALLFUNC		"closeAll"

typedef ModuleProperties *(getModulePropertiesFunc)(void);
typedef bool (openOrCreateIndexFunc)(const string &, bool &, bool, bool);
typedef bool (mergeIndexesFunc)(const string &, const string &, const string &);
typedef IndexInterface *(getIndexFunc)(const string &);
typedef SearchEngineInterface *(getSearchEngineFunc)(const string &);
typedef void (closeAllFunc)(void);

using std::cout;
using std::cerr;
using std::endl;
using std::string;
using std::map;
using std::set;
using std::pair;

LoadableModule::LoadableModule(ModuleProperties *pProperties,
	const string &location, void *pHandle) :
	m_pProperties(pProperties),
	m_location(location),
	m_canSearch(false),
	m_canIndex(false),
	m_pHandle(pHandle)
{
}

LoadableModule::LoadableModule(const LoadableModule &other) :
	m_pProperties(NULL),
	m_location(other.m_location),
	m_canSearch(other.m_canSearch),
	m_canIndex(other.m_canIndex),
	m_pHandle(other.m_pHandle)
{
	if (other.m_pProperties != NULL)
	{
		m_pProperties = new ModuleProperties(*other.m_pProperties);
	}
}

LoadableModule::~LoadableModule()
{
	if (m_pProperties != NULL)
	{
		delete m_pProperties;
	}
}

LoadableModule &LoadableModule::operator=(const LoadableModule &other)
{
	if (this != &other)
	{
		if (m_pProperties != NULL)
		{
			delete m_pProperties;
			m_pProperties = NULL;
		}
		m_pProperties = other.m_pProperties;
		m_location = other.m_location;
		m_canSearch = other.m_canSearch;
		m_canIndex = other.m_canIndex;
		m_pHandle = other.m_pHandle;
	}

	return *this;
}

map<string, LoadableModule> ModuleFactory::m_types;

ModuleFactory::ModuleFactory()
{
}

ModuleFactory::~ModuleFactory()
{
}

IndexInterface *ModuleFactory::getLibraryIndex(const string &type, const string &option)
{
	map<string, LoadableModule>::iterator typeIter = m_types.find(type);
	if ((typeIter == m_types.end()) ||
		(typeIter->second.m_canIndex == false))
	{
		// We don't know about this type, or doesn't support indexes
		return false;
	}

	void *pHandle = typeIter->second.m_pHandle;
	if (pHandle == NULL)
	{
		return NULL;
	}

#ifdef HAVE_DLFCN_H
	getIndexFunc *pFunc = (getIndexFunc *)dlsym(pHandle,
		GETINDEXFUNC);
	if (pFunc != NULL)
	{
		return (*pFunc)(option);
	}
#endif
#ifdef DEBUG
	cout << "ModuleFactory::getLibraryIndex: couldn't find export getIndex" << endl;
#endif

	return NULL;
}

SearchEngineInterface *ModuleFactory::getLibrarySearchEngine(const string &type, const string &option)
{
	map<string, LoadableModule>::iterator typeIter = m_types.find(type);
	if (typeIter == m_types.end())
	{
		// We don't know about this type
		return NULL;
	}

	void *pHandle = typeIter->second.m_pHandle;
	if (pHandle == NULL)
	{
		return NULL;
	}

#ifdef HAVE_DLFCN_H
	getSearchEngineFunc *pFunc = (getSearchEngineFunc *)dlsym(pHandle,
		GETSEARCHENGINEFUNC);
	if (pFunc != NULL)
	{
		return (*pFunc)(option);
	}
#endif
#ifdef DEBUG
	cout << "ModuleFactory::getLibrarySearchEngine: couldn't find export getSearchEngine" << endl;
#endif

	return NULL;
}

unsigned int ModuleFactory::loadModules(const string &directory)
{
	unsigned int count = 0;
#ifdef HAVE_DLFCN_H
	struct stat fileStat;

	if (directory.empty() == true)
	{
		return 0;
	}

	// Is it a directory ?
	if ((stat(directory.c_str(), &fileStat) == -1) ||
		(!S_ISDIR(fileStat.st_mode)))
	{
		cerr << "ModuleFactory::loadModules: " << directory << " is not a directory" << endl;
		return 0;
	}

	// Scan it
	DIR *pDir = opendir(directory.c_str());
	if (pDir == NULL)
	{
		return 0;
	}

	// Iterate through this directory's entries
	struct dirent *pDirEntry = readdir(pDir);
	while (pDirEntry != NULL)
	{
		char *pEntryName = pDirEntry->d_name;
		if (pEntryName != NULL)
		{
			string fileName = pEntryName;
			string::size_type extPos = fileName.find_last_of(".");

			if ((extPos == string::npos) ||
				(fileName.substr(extPos) != ".so"))
			{
				// Next entry
				pDirEntry = readdir(pDir);
				continue;
			}

			fileName = directory;
			fileName += "/";
			fileName += pEntryName;

			// Check this entry
			if ((stat(fileName.c_str(), &fileStat) == 0) &&
				(S_ISREG(fileStat.st_mode)))
			{
				void *pHandle = dlopen(fileName.c_str(), DLOPEN_FLAGS);
				if (pHandle != NULL)
				{
					// What type does this export ?
					getModulePropertiesFunc *pPropsFunc = (getModulePropertiesFunc *)dlsym(pHandle,
						GETMODULEPROPERTIESFUNC);
					if (pPropsFunc != NULL)
					{
						LoadableModule module((*pPropsFunc)(), fileName, pHandle);

						if (module.m_pProperties != NULL)
						{
							string moduleType(module.m_pProperties->m_name);

							// Can it search ?
							getSearchEngineFunc *pSearchFunc = (getSearchEngineFunc *)dlsym(pHandle,
								GETSEARCHENGINEFUNC);
							if (pSearchFunc != NULL)
							{
								module.m_canSearch = true;
							}

							// Can it index ?
							getIndexFunc *pIndexFunc = (getIndexFunc *)dlsym(pHandle,
								GETINDEXFUNC);
							if (pIndexFunc != NULL)
							{
								module.m_canIndex = true;
							}

							// Add a record for this module
							m_types.insert(pair<string, LoadableModule>(moduleType, module));
#ifdef DEBUG
							cout << "ModuleFactory::loadModules: " << moduleType
								<< " is supported by " << pEntryName << endl;
#endif
						}
					}
					else cerr << "ModuleFactory::loadModules: " << dlerror() << endl;
				}
				else cerr << "ModuleFactory::loadModules: " << dlerror() << endl;
			}
#ifdef DEBUG
			else cout << "ModuleFactory::loadModules: "
				<< pEntryName << " is not a file" << endl;
#endif
		}

		// Next entry
		pDirEntry = readdir(pDir);
	}
	closedir(pDir);
#endif

	return count;
}

bool ModuleFactory::openOrCreateIndex(const string &type, const string &option,
	bool &obsoleteFormat, bool readOnly, bool overwrite)
{
	map<string, LoadableModule>::iterator typeIter = m_types.find(type);
	if ((typeIter == m_types.end()) ||
		(typeIter->second.m_canIndex == false))
	{
		// We don't know about this type, or doesn't support indexes
		return false;
	}

	void *pHandle = typeIter->second.m_pHandle;
	if (pHandle == NULL)
	{
		return false;
	}

#ifdef HAVE_DLFCN_H
	openOrCreateIndexFunc *pFunc = (openOrCreateIndexFunc *)dlsym(pHandle,
		OPENORCREATEINDEXFUNC);
	if (pFunc != NULL)
	{
		return (*pFunc)(option, obsoleteFormat, readOnly, overwrite);
	}
#endif
#ifdef DEBUG
	cout << "ModuleFactory::openOrCreateIndex: couldn't find export openOrCreateIndex" << endl;
#endif

	return false;
}

bool ModuleFactory::mergeIndexes(const string &type, const string &option0,
	const string &option1, const string &option2)
{
	map<string, LoadableModule>::iterator typeIter = m_types.find(type);
	if ((typeIter == m_types.end()) ||
		(typeIter->second.m_canIndex == false))
	{
		// We don't know about this type, or doesn't support indexes
		return false;
	}

	void *pHandle = typeIter->second.m_pHandle;
	if (pHandle == NULL)
	{
		return false;
	}

#ifdef HAVE_DLFCN_H
	mergeIndexesFunc *pFunc = (mergeIndexesFunc *)dlsym(pHandle,
		MERGEINDEXESFUNC);
	if (pFunc != NULL)
	{
		return (*pFunc)(option0, option1, option2);
	}
#endif
#ifdef DEBUG
	cout << "ModuleFactory::mergeIndexes: couldn't find export mergeIndexes" << endl;
#endif

	return false;
}

IndexInterface *ModuleFactory::getIndex(const string &type, const string &option)
{
	IndexInterface *pIndex = NULL;

	// Choice by type
	// Do we need to nest it in a DBusIndex ?
	if (type.substr(0, 5) == "dbus-")
	{
#ifdef DEBUG
		cout << "ModuleFactory::mergeIndexes: sub-type " << type.substr(5) << endl;
#endif
		pIndex = getLibraryIndex(type.substr(5), option);
		if (pIndex != NULL)
		{
#ifdef HAVE_DBUS
			return new DBusIndex(pIndex);
#else
			return pIndex;
#endif
		}

		return NULL;
	}

	return getLibraryIndex(type, option);
}

SearchEngineInterface *ModuleFactory::getSearchEngine(const string &type, const string &option)
{
	SearchEngineInterface *pEngine = NULL;

	// Choice by type
	if (
#ifdef HAVE_BOOST_SPIRIT
		(type == "sherlock") ||
#endif
		(type == "opensearch"))
	{
		pEngine = new PluginWebEngine(option);
	}

	if (pEngine != NULL)
	{
		return pEngine;
	}

	return getLibrarySearchEngine(type, option);
}

string ModuleFactory::getSearchEngineName(const string &type, const string &option)
{
	if (
#ifdef HAVE_BOOST_SPIRIT
		(type == "sherlock") ||
#endif
		(type == "opensearch"))
	{
		SearchPluginProperties properties;

		if (PluginWebEngine::getDetails(option, properties) == true)
		{
			return properties.m_name;
		}

		return "";
	}
	else
	{
		return option;
	}

	return type;
}

void ModuleFactory::getSupportedEngines(map<ModuleProperties, bool> &engines)
{
	engines.clear();

	// Built-in engines
#ifdef HAVE_BOOST_SPIRIT
	engines.insert(pair<ModuleProperties, bool>(ModuleProperties("sherlock", "Sherlock", "", ""), false));
#endif
	engines.insert(pair<ModuleProperties, bool>(ModuleProperties("opensearch", "OpenSearch", "", ""), false));

	// Library-handled engines
	for (map<string, LoadableModule>::iterator typeIter = m_types.begin();
		typeIter != m_types.end(); ++typeIter)
	{
		ModuleProperties *pProps = typeIter->second.m_pProperties;

		if (pProps != NULL)
		{
			engines.insert(pair<ModuleProperties, bool>(*pProps, true));
		}
	}
}

bool ModuleFactory::isSupported(const string &type, bool asIndex)
{
	if (asIndex == true)
	{
		// Only backends implement index functionality
		map<string, LoadableModule>::const_iterator typeIter = m_types.find(type);
		if (typeIter != m_types.end())
		{
			return typeIter->second.m_canIndex;
		}

		return false;
	}

	if (
#ifdef HAVE_BOOST_SPIRIT
		(type == "sherlock") ||
#endif
		(type == "opensearch"))
	{
		return true;
	}
	else
	{
		// Does this backend implement search functionality ?
		map<string, LoadableModule>::const_iterator typeIter = m_types.find(type);
		if (typeIter != m_types.end())
		{
			return typeIter->second.m_canSearch;
		}
	}

	return false;	
}

void ModuleFactory::unloadModules(void)
{
	for (map<string, LoadableModule>::iterator typeIter = m_types.begin(); typeIter != m_types.end(); ++typeIter)
	{
		void *pHandle = typeIter->second.m_pHandle;
		if (pHandle == NULL)
		{
			continue;
		}

#ifdef HAVE_DLFCN_H
		closeAllFunc *pFunc = (closeAllFunc *)dlsym(pHandle, CLOSEALLFUNC);
		if (pFunc != NULL)
		{
			(*pFunc)();
		}
#ifdef DEBUG
		else cout << "ModuleFactory::unloadModules: couldn't find export closeAll" << endl;
#endif

		if (dlclose(pHandle) != 0)
		{
#ifdef DEBUG
			cout << "ModuleFactory::unloadModules: failed on " << typeIter->first << endl;
#endif
		}
#endif
	}

	m_types.clear();
}