Commit 7dcd35dd4f9253c998dd369853d9eeb42b3ea893

  • avatar
  • Julien Wintz <julien.wintz @m…me.com>
  • Tue Jan 15 22:18:54 GMT 2013
Dependency check ok when it comes to names.
README
(3 / 3)
  
44;; Copyright (C) 2008-2011 - Julien Wintz, Inria.
55;; Created: Tue Jan 15 17:54:13 2013 (+0100)
66;; Version: $Id$
7;; Last-Updated: Tue Jan 15 18:02:02 2013 (+0100)
7;; Last-Updated: Tue Jan 15 21:50:06 2013 (+0100)
88;; By: Julien Wintz
9;; Update #: 10
9;; Update #: 11
1010;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1111
1212Commentary:
3030tst/plugins/tstPluginA: Sample plugin, version 0.0.1, No dependencies
3131tst/plugins/tstPluginB: Sample plugin, version 0.0.1, Depends on "tstPluginA"
3232tst/plugins/tstPluginC: Sample plugin, version 0.0.1, Depends on "tstPluginZ"
33tst/plugins/tstPluginD: Sample plugin, version 0.0.1, Depends on "tstPluginB" and "tstPluginB"
33tst/plugins/tstPluginD: Sample plugin, version 0.0.1, Depends on "tstPluginB" and "tstPluginC"
3434tst/plugins/tstPluginE-0.0.1: Sample plugin, version 0.0.1, No dependencies
3535tst/plugins/tstPluginE-0.0.2: Sample plugin, version 0.0.2, No dependencies
3636tst/plugins/tstPluginF: Sample plugin, version 0.0.1, Depends on "tstPluginE-0.0.2"
app/tstPlugins/main.cpp
(3 / 3)
  
44 * Copyright (C) 2008-2011 - Julien Wintz, Inria.
55 * Created: Tue Jan 15 16:15:32 2013 (+0100)
66 * Version: $Id$
7 * Last-Updated: Tue Jan 15 16:36:20 2013 (+0100)
7 * Last-Updated: Tue Jan 15 23:14:28 2013 (+0100)
88 * By: Julien Wintz
9 * Update #: 21
9 * Update #: 27
1010 */
1111
1212/* Commentary:
2929
3030 tstPluginManager::instance()->initialize();
3131
32 // ...
32 qDebug() << "\nSuccessfully loaded:" << tstPluginManager::instance()->plugins();
3333
3434 tstPluginManager::instance()->uninitialize();
3535
plugins/tstPluginB/tstPluginB.json
(3 / 1)
  
11{
22 "name" : "tstPluginB",
33 "version" : "0.0.1",
4 "dependencies" : []
4 "dependencies" : [
5 { "name" : "tstPluginA", "version" : "0.0.1" }
6 ]
57}
plugins/tstPluginC/tstPluginC.json
(3 / 1)
  
11{
22 "name" : "tstPluginC",
33 "version" : "0.0.1",
4 "dependencies" : []
4 "dependencies" : [
5 { "name" : "tstPluginZ", "version" : "0.0.1" }
6 ]
57}
plugins/tstPluginD/tstPluginD.json
(4 / 1)
  
11{
22 "name" : "tstPluginD",
33 "version" : "0.0.1",
4 "dependencies" : []
4 "dependencies" : [
5 { "name" : "tstPluginB", "version" : "0.0.1" },
6 { "name" : "tstPluginC", "version" : "0.0.1" }
7 ]
58}
src/tstCore/tstPluginManager.cpp
(75 / 29)
  
44 * Copyright (C) 2008-2011 - Julien Wintz, Inria.
55 * Created: Tue Jan 15 16:25:00 2013 (+0100)
66 * Version: $Id$
7 * Last-Updated: Tue Jan 15 17:46:48 2013 (+0100)
7 * Last-Updated: Tue Jan 15 23:17:20 2013 (+0100)
88 * By: Julien Wintz
9 * Update #: 129
9 * Update #: 280
1010 */
1111
1212/* Commentary:
2424#include <QtDebug>
2525
2626// /////////////////////////////////////////////////////////////////
27//
27// tstPluginManagerPrivate
2828// /////////////////////////////////////////////////////////////////
2929
3030class tstPluginManagerPrivate
3131{
3232public:
33 bool check(const QString& path);
34
35public:
36 QHash<QString, QVariant> names;
37 QHash<QString, QVariant> versions;
38 QHash<QString, QVariantList> dependencies;
39
40public:
3341 QHash<QString, QPluginLoader *> loaders;
34 QHash<QString, QString> names;
35 QHash<QString, QString> versions;
36 QHash<QString, QStringList> dependencies;
3742};
3843
44bool tstPluginManagerPrivate::check(const QString& path)
45{
46 bool status = true;
47
48 foreach(QVariant item, this->dependencies.value(path)) {
49
50 QVariantMap mitem = item.toMap();
51 QVariant na_mitem = mitem.value("name");
52 QVariant ve_mitem = mitem.value("version");
53 QString key = this->names.key(na_mitem);
54
55 if(!this->names.values().contains(na_mitem)) {
56 qDebug() << Q_FUNC_INFO << " Missing dependency:" << na_mitem.toString() << "for plugin" << path;
57 status = false;
58 continue;
59 }
60
61 if (this->versions.value(key) != ve_mitem) {
62 qDebug() << Q_FUNC_INFO << " Version mismatch:" << na_mitem.toString() << "version" << this->versions.value(this->names.key(na_mitem)).toString() << "but" << ve_mitem.toString() << "required for plugin" << path;
63 status = false;
64 continue;
65 }
66
67 if(!check(key)) {
68 qDebug() << Q_FUNC_INFO << "Corrupted dependency:" << na_mitem.toString() << "for plugin" << path;
69 status = false;
70 continue;
71 }
72 }
73
74 return status;
75}
76
3977// /////////////////////////////////////////////////////////////////
40//
78// tstPluginManager
4179// /////////////////////////////////////////////////////////////////
4280
4381tstPluginManager *tstPluginManager::instance(void)
9393 path.cd("plugins");
9494
9595 foreach(QFileInfo info, path.entryInfoList(QDir::Files | QDir::NoDotAndDotDot))
96 this->scan(info.absoluteFilePath());
97
98 foreach(QFileInfo info, path.entryInfoList(QDir::Files | QDir::NoDotAndDotDot))
9699 this->load(info.absoluteFilePath());
97100}
98101
105105 this->unload(path);
106106}
107107
108void tstPluginManager::load(const QString& path)
108void tstPluginManager::scan(const QString& path)
109109{
110110 if(!QLibrary::isLibrary(path))
111111 return;
112
112
113113 QPluginLoader *loader = new QPluginLoader(path);
114114
115 if(tstPlugin *plugin = qobject_cast<tstPlugin *>(loader->instance())) {
116 /// Do something smart
117 } else {
118 qDebug() << Q_FUNC_INFO << "Unable to retrieve plugin interface in" << path;
115 d->names.insert(path, loader->metaData().value("MetaData").toObject().value("name").toVariant());
116 d->versions.insert(path, loader->metaData().value("MetaData").toObject().value("version").toVariant());
117 d->dependencies.insert(path, loader->metaData().value("MetaData").toObject().value("dependencies").toArray().toVariantList());
118
119 delete loader;
120}
121
122void tstPluginManager::load(const QString& path)
123{
124 if(!QLibrary::isLibrary(path))
119125 return;
120 }
121126
122 QJsonObject iid = loader->metaData().value("IID").toObject();
123 QJsonObject met = loader->metaData().value("MetaData").toObject();
124 QJsonObject ver = loader->metaData().value("version").toObject();
127 if(!d->check(path))
128 return;
125129
126 qDebug() << Q_FUNC_INFO << "Loaded plugin" << path;
127 qDebug() << Q_FUNC_INFO << "- name:" << met.value("name").toString();
128 qDebug() << Q_FUNC_INFO << "- version:" << met.value("version").toString();
129 qDebug() << Q_FUNC_INFO << "- dependencies:" << met.value("dependencies").toString();
130 QPluginLoader *loader = new QPluginLoader(path);
131
132 if(tstPlugin *plugin = qobject_cast<tstPlugin *>(loader->instance()))
133 d->loaders.insert(path, loader);
134 else
135 delete loader;
130136}
131137
132138void tstPluginManager::unload(const QString& path)
133139{
134140 QPluginLoader *loader = d->loaders.value(path);
135141
136 if(!loader->unload()) {
137 qDebug() << Q_FUNC_INFO << "Unable to unload plugin: " << loader->errorString();
138 return;
142 if(loader->unload()) {
143 d->loaders.remove(path);
144 delete loader;
139145 }
146}
140147
141 d->loaders.remove(path);
142
143 delete loader;
144
145 qDebug() << Q_FUNC_INFO << "Unloaded plugin" << path;
148QStringList tstPluginManager::plugins(void)
149{
150 return d->loaders.keys();
146151}
147152
148153tstPluginManager::tstPluginManager(void) : d(new tstPluginManagerPrivate)
src/tstCore/tstPluginManager.h
(8 / 2)
  
44 * Copyright (C) 2008-2011 - Julien Wintz, Inria.
55 * Created: Tue Jan 15 16:22:48 2013 (+0100)
66 * Version: $Id$
7 * Last-Updated: Tue Jan 15 16:40:57 2013 (+0100)
7 * Last-Updated: Tue Jan 15 23:00:09 2013 (+0100)
88 * By: Julien Wintz
9 * Update #: 27
9 * Update #: 33
1010 */
1111
1212/* Commentary:
3939#pragma mark -
4040#pragma Plugin Management
4141
42 void scan(const QString& path);
4243 void load(const QString& path);
4344 void unload(const QString& path);
45
46#pragma mark -
47#pragma Plugin Queries
48
49 QStringList plugins(void);
4450
4551protected:
4652 tstPluginManager(void);

Comments

Add a new comment:

Login or create an account to post a comment

Add your comment