ctkPluginFrameworkContext.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #include "ctkPluginFrameworkContext_p.h"
  16. #include "ctkPluginFrameworkUtil_p.h"
  17. #include "ctkPluginFrameworkPrivate_p.h"
  18. #include "ctkPluginArchive_p.h"
  19. #include "ctkPluginStorageSQL_p.h"
  20. #include "ctkPluginConstants.h"
  21. #include "ctkServices_p.h"
  22. #include "ctkUtils.h"
  23. //----------------------------------------------------------------------------
  24. QMutex ctkPluginFrameworkContext::globalFwLock;
  25. int ctkPluginFrameworkContext::globalId = 1;
  26. //----------------------------------------------------------------------------
  27. ctkPluginFrameworkContext::ctkPluginFrameworkContext(
  28. const ctkProperties& initProps)
  29. : plugins(0), listeners(this), services(0), systemPlugin(new ctkPluginFramework()),
  30. storage(0), firstInit(true), props(initProps), debug(props),
  31. initialized(false)
  32. {
  33. {
  34. QMutexLocker lock(&globalFwLock);
  35. id = globalId++;
  36. systemPlugin->ctkPlugin::init(new ctkPluginFrameworkPrivate(systemPlugin, this));
  37. }
  38. initProperties();
  39. log() << "created";
  40. }
  41. //----------------------------------------------------------------------------
  42. ctkPluginFrameworkContext::~ctkPluginFrameworkContext()
  43. {
  44. if (initialized)
  45. {
  46. this->uninit();
  47. }
  48. }
  49. //----------------------------------------------------------------------------
  50. void ctkPluginFrameworkContext::initProperties()
  51. {
  52. props[ctkPluginConstants::FRAMEWORK_VERSION] = "0.9";
  53. props[ctkPluginConstants::FRAMEWORK_VENDOR] = "CommonTK";
  54. }
  55. //----------------------------------------------------------------------------
  56. void ctkPluginFrameworkContext::init()
  57. {
  58. log() << "initializing";
  59. if (firstInit && ctkPluginConstants::FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT
  60. == props[ctkPluginConstants::FRAMEWORK_STORAGE_CLEAN])
  61. {
  62. deleteFWDir();
  63. firstInit = false;
  64. }
  65. ctkPluginFrameworkPrivate* const systemPluginPrivate = systemPlugin->d_func();
  66. systemPluginPrivate->initSystemPlugin();
  67. storage = new ctkPluginStorageSQL(this);
  68. dataStorage = ctkPluginFrameworkUtil::getFileStorage(this, "data");
  69. services = new ctkServices(this);
  70. plugins = new ctkPlugins(this);
  71. // Pre-load libraries
  72. // This may speed up installing new plug-ins if they have dependencies on
  73. // one of these libraries. It prevents repeated loading and unloading of the
  74. // pre-loaded libraries during caching of the plug-in meta-data.
  75. if (props[ctkPluginConstants::FRAMEWORK_PRELOAD_LIBRARIES].isValid())
  76. {
  77. QStringList preloadLibs = props[ctkPluginConstants::FRAMEWORK_PRELOAD_LIBRARIES].toStringList();
  78. QLibrary::LoadHints loadHints;
  79. QVariant loadHintsVariant = props[ctkPluginConstants::FRAMEWORK_PLUGIN_LOAD_HINTS];
  80. if (loadHintsVariant.isValid())
  81. {
  82. loadHints = loadHintsVariant.value<QLibrary::LoadHints>();
  83. }
  84. foreach(QString preloadLib, preloadLibs)
  85. {
  86. QLibrary lib(preloadLib);
  87. lib.setLoadHints(loadHints);
  88. log() << "Pre-loading library" << preloadLib << "with hints [" << static_cast<int>(loadHints) << "]";
  89. if (!lib.load())
  90. {
  91. qWarning() << "Pre-loading library" << preloadLib << "failed";
  92. }
  93. }
  94. }
  95. plugins->load();
  96. log() << "inited";
  97. initialized = true;
  98. log() << "Installed plugins:";
  99. // Use the ordering in the plugin storage to get a sorted list of plugins.
  100. QList<QSharedPointer<ctkPluginArchive> > allPAs = storage->getAllPluginArchives();
  101. foreach (QSharedPointer<ctkPluginArchive> pa, allPAs)
  102. {
  103. QSharedPointer<ctkPlugin> plugin = plugins->getPlugin(pa->getPluginLocation().toString());
  104. log() << " #" << plugin->getPluginId() << " " << plugin->getSymbolicName() << ":"
  105. << plugin->getVersion() << " location:" << plugin->getLocation();
  106. }
  107. }
  108. //----------------------------------------------------------------------------
  109. void ctkPluginFrameworkContext::uninit()
  110. {
  111. if (!initialized) return;
  112. log() << "uninit";
  113. ctkPluginFrameworkPrivate* const systemPluginPrivate = systemPlugin->d_func();
  114. systemPluginPrivate->uninitSystemPlugin();
  115. plugins->clear();
  116. delete plugins;
  117. plugins = 0;
  118. delete storage; // calls storage->close()
  119. storage = 0;
  120. delete services;
  121. services = 0;
  122. initialized = false;
  123. }
  124. //----------------------------------------------------------------------------
  125. int ctkPluginFrameworkContext::getId() const
  126. {
  127. return id;
  128. }
  129. //----------------------------------------------------------------------------
  130. QFileInfo ctkPluginFrameworkContext::getDataStorage(long id)
  131. {
  132. return QFileInfo(dataStorage.absolutePath() + '/' + QString::number(id) + '/');
  133. }
  134. //----------------------------------------------------------------------------
  135. void ctkPluginFrameworkContext::checkOurPlugin(ctkPlugin* plugin) const
  136. {
  137. ctkPluginPrivate* pp = plugin->d_func();
  138. if (this != pp->fwCtx)
  139. {
  140. throw std::invalid_argument("ctkPlugin does not belong to this framework: " + plugin->getSymbolicName().toStdString());
  141. }
  142. }
  143. //----------------------------------------------------------------------------
  144. QDebug ctkPluginFrameworkContext::log() const
  145. {
  146. static QString nirvana;
  147. nirvana.clear();
  148. if (debug.framework)
  149. return qDebug() << "Framework instance " << getId() << ": ";
  150. else
  151. return QDebug(&nirvana);
  152. }
  153. //----------------------------------------------------------------------------
  154. void ctkPluginFrameworkContext::resolvePlugin(ctkPluginPrivate* plugin)
  155. {
  156. if (debug.resolve)
  157. {
  158. qDebug() << "resolve:" << plugin->symbolicName << "[" << plugin->id << "]";
  159. }
  160. // If we enter with tempResolved set, it means that we already have
  161. // resolved plugins. Check that it is true!
  162. if (tempResolved.size() > 0 && !tempResolved.contains(plugin))
  163. {
  164. ctkPluginException pe("resolve: InternalError1!", ctkPluginException::RESOLVE_ERROR);
  165. listeners.frameworkError(plugin->q_func(), pe);
  166. throw pe;
  167. }
  168. tempResolved.clear();
  169. tempResolved.insert(plugin);
  170. checkRequirePlugin(plugin);
  171. tempResolved.clear();
  172. if (debug.resolve)
  173. {
  174. qDebug() << "resolve: Done for" << plugin->symbolicName << "[" << plugin->id << "]";
  175. }
  176. }
  177. //----------------------------------------------------------------------------
  178. void ctkPluginFrameworkContext::checkRequirePlugin(ctkPluginPrivate *plugin)
  179. {
  180. if (!plugin->require.isEmpty())
  181. {
  182. if (debug.resolve)
  183. {
  184. qDebug() << "checkRequirePlugin: check requiring plugin" << plugin->id;
  185. }
  186. QListIterator<ctkRequirePlugin*> i(plugin->require);
  187. while (i.hasNext())
  188. {
  189. ctkRequirePlugin* pr = i.next();
  190. QList<ctkPlugin*> pl = plugins->getPlugins(pr->name, pr->pluginRange);
  191. ctkPluginPrivate* ok = 0;
  192. for (QListIterator<ctkPlugin*> pci(pl); pci.hasNext() && ok == 0; )
  193. {
  194. ctkPluginPrivate* p2 = pci.next()->d_func();
  195. if (tempResolved.contains(p2))
  196. {
  197. ok = p2;
  198. }
  199. else if (ctkPluginPrivate::RESOLVED_FLAGS & p2->state)
  200. {
  201. ok = p2;
  202. }
  203. else if (p2->state == ctkPlugin::INSTALLED) {
  204. QSet<ctkPluginPrivate*> oldTempResolved = tempResolved;
  205. tempResolved.insert(p2);
  206. // TODO check if operation locking is correct in case of
  207. // multi-threaded plug-in start up. Maybe refactor out the dependency
  208. // checking (use the "package" lock)
  209. ctkPluginPrivate::Locker sync(&p2->operationLock);
  210. p2->operation.fetchAndStoreOrdered(ctkPluginPrivate::RESOLVING);
  211. checkRequirePlugin(p2);
  212. tempResolved = oldTempResolved;
  213. p2->state = ctkPlugin::RESOLVED;
  214. listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::RESOLVED, p2->q_func()));
  215. p2->operation.fetchAndStoreOrdered(ctkPluginPrivate::IDLE);
  216. ok = p2;
  217. }
  218. }
  219. if (!ok && pr->resolution == ctkPluginConstants::RESOLUTION_MANDATORY)
  220. {
  221. tempResolved.clear();
  222. if (debug.resolve)
  223. {
  224. qDebug() << "checkRequirePlugin: failed to satisfy:" << pr->name;
  225. }
  226. throw ctkPluginException(QString("Failed to resolve required plugin: %1").arg(pr->name));
  227. }
  228. }
  229. }
  230. }
  231. //----------------------------------------------------------------------------
  232. void ctkPluginFrameworkContext::deleteFWDir()
  233. {
  234. QString d = ctkPluginFrameworkUtil::getFrameworkDir(this);
  235. QFileInfo fwDirInfo(d);
  236. if (fwDirInfo.exists())
  237. {
  238. if(fwDirInfo.isDir())
  239. {
  240. log() << "deleting old framework directory.";
  241. bool bOK = ctk::removeDirRecursively(fwDirInfo.absoluteFilePath());
  242. if(!bOK)
  243. {
  244. qDebug() << "Failed to remove existing fwdir" << fwDirInfo.absoluteFilePath();
  245. }
  246. }
  247. }
  248. }