ctkPluginFrameworkContext.cxx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) 2010 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 "ctkPluginFrameworkPrivate_p.h"
  17. #include "ctkPluginArchive_p.h"
  18. #include "ctkPluginConstants.h"
  19. namespace ctk {
  20. QMutex PluginFrameworkContext::globalFwLock;
  21. int PluginFrameworkContext::globalId = 1;
  22. PluginFrameworkContext::PluginFrameworkContext(
  23. const PluginFrameworkFactory::Properties& initProps)
  24. : plugins(0), /*services(this),*/ systemPlugin(this),
  25. storage(this), props(initProps)
  26. {
  27. {
  28. QMutexLocker lock(&globalFwLock);
  29. id = globalId++;
  30. }
  31. log() << "created";
  32. }
  33. void PluginFrameworkContext::init()
  34. {
  35. log() << "initializing";
  36. // if (Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT
  37. // .equals(props.getProperty(Constants.FRAMEWORK_STORAGE_CLEAN))) {
  38. // deleteFWDir();
  39. // // Must remove the storage clean property since it should not be
  40. // // used more than once!
  41. // props.removeProperty(Constants.FRAMEWORK_STORAGE_CLEAN);
  42. // }
  43. // props.save();
  44. PluginFrameworkPrivate* const systemPluginPrivate = systemPlugin.d_func();
  45. systemPluginPrivate->initSystemPlugin();
  46. plugins = new Plugins(this);
  47. plugins->load();
  48. log() << "inited";
  49. log() << "Installed plugins:";
  50. // Use the ordering in the plugin storage to get a sorted list of plugins.
  51. QList<PluginArchive*> allPAs = storage.getAllPluginArchives();
  52. for (int i = 0; i < allPAs.size(); ++i)
  53. {
  54. PluginArchive* pa = allPAs[i];
  55. Plugin* p = plugins->getPlugin(pa->getPluginLocation().toString());
  56. log() << " #" << p->getPluginId() << " " << p->getSymbolicName() << ":"
  57. << p->getVersion() << " location:" << p->getLocation();
  58. }
  59. }
  60. void PluginFrameworkContext::uninit()
  61. {
  62. log() << "uninit";
  63. //PluginFrameworkPrivate* const systemPluginPrivate = systemPlugin.d_func();
  64. //systemPluginPrivate->uninitSystemBundle();
  65. plugins->clear();
  66. delete plugins;
  67. plugins = 0;
  68. storage.close();
  69. }
  70. int PluginFrameworkContext::getId() const
  71. {
  72. return id;
  73. }
  74. void PluginFrameworkContext::checkOurPlugin(Plugin* plugin) const
  75. {
  76. PluginPrivate* pp = plugin->d_func();
  77. if (this != pp->fwCtx)
  78. {
  79. throw std::invalid_argument("Plugin does not belong to this framework: " + plugin->getSymbolicName().toStdString());
  80. }
  81. }
  82. QDebug PluginFrameworkContext::log() const
  83. {
  84. QDebug dbg(qDebug());
  85. dbg << "Framework instance " << getId() << ": ";
  86. return dbg;
  87. }
  88. void PluginFrameworkContext::resolvePlugin(PluginPrivate* plugin)
  89. {
  90. qDebug() << "resolve:" << plugin->symbolicName << "[" << plugin->id << "]";
  91. // If we enter with tempResolved set, it means that we already have
  92. // resolved plugins. Check that it is true!
  93. if (tempResolved.size() > 0 && !tempResolved.contains(plugin))
  94. {
  95. PluginException pe("resolve: InternalError1!", PluginException::RESOLVE_ERROR);
  96. listeners.frameworkError(plugin->q_func(), pe);
  97. throw pe;
  98. }
  99. tempResolved.clear();
  100. tempResolved.insert(plugin);
  101. checkRequirePlugin(plugin);
  102. tempResolved.clear();
  103. qDebug() << "resolve: Done for" << plugin->symbolicName << "[" << plugin->id << "]";
  104. }
  105. void PluginFrameworkContext::checkRequirePlugin(PluginPrivate *plugin)
  106. {
  107. if (!plugin->require.isEmpty())
  108. {
  109. qDebug() << "checkRequirePlugin: check requiring plugin" << plugin->id;
  110. QListIterator<RequirePlugin*> i(plugin->require);
  111. while (i.hasNext())
  112. {
  113. RequirePlugin* pr = i.next();
  114. QList<Plugin*> pl = plugins->getPlugins(pr->name, pr->pluginRange);
  115. PluginPrivate* ok = 0;
  116. for (QListIterator<Plugin*> pci(pl); pci.hasNext() && ok == 0; )
  117. {
  118. PluginPrivate* p2 = pci.next()->d_func();
  119. if (tempResolved.contains(p2))
  120. {
  121. ok = p2;
  122. }
  123. else if (PluginPrivate::RESOLVED_FLAGS & p2->state)
  124. {
  125. ok = p2;
  126. }
  127. else if (p2->state == Plugin::INSTALLED) {
  128. QSet<PluginPrivate*> oldTempResolved = tempResolved;
  129. tempResolved.insert(p2);
  130. checkRequirePlugin(p2);
  131. tempResolved = oldTempResolved;
  132. ok = p2;
  133. }
  134. }
  135. if (!ok && pr->resolution == PluginConstants::RESOLUTION_MANDATORY)
  136. {
  137. tempResolved.clear();
  138. qDebug() << "checkRequirePlugin: failed to satisfy:" << pr->name;
  139. throw PluginException(QString("Failed to resolve required plugin: %1").arg(pr->name));
  140. }
  141. }
  142. }
  143. }
  144. }