ctkPluginFramework_p.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 "ctkPluginConstants.h"
  16. #include "ctkPluginContext.h"
  17. #include "ctkPluginContext_p.h"
  18. #include "ctkPluginFramework.h"
  19. #include "ctkPluginFramework_p.h"
  20. #include "ctkPluginFrameworkContext_p.h"
  21. #include "ctkPluginFrameworkUtil_p.h"
  22. #include <QtConcurrentRun>
  23. //----------------------------------------------------------------------------
  24. ctkPluginFrameworkPrivate::ctkPluginFrameworkPrivate(QWeakPointer<ctkPlugin> qq, ctkPluginFrameworkContext* fw)
  25. : ctkPluginPrivate(qq, fw, 0, ctkPluginConstants::SYSTEM_PLUGIN_LOCATION,
  26. ctkPluginConstants::SYSTEM_PLUGIN_SYMBOLICNAME,
  27. // TODO: read version from the manifest resource
  28. ctkVersion(0, 9, 0)),
  29. shuttingDown(0)
  30. {
  31. systemHeaders.insert(ctkPluginConstants::PLUGIN_SYMBOLICNAME, symbolicName);
  32. systemHeaders.insert(ctkPluginConstants::PLUGIN_NAME, location);
  33. systemHeaders.insert(ctkPluginConstants::PLUGIN_VERSION, version.toString());
  34. }
  35. //----------------------------------------------------------------------------
  36. void ctkPluginFrameworkPrivate::init()
  37. {
  38. this->state = ctkPlugin::STARTING;
  39. this->fwCtx->init();
  40. }
  41. //----------------------------------------------------------------------------
  42. void ctkPluginFrameworkPrivate::initSystemPlugin()
  43. {
  44. this->pluginContext.reset(new ctkPluginContext(this));
  45. }
  46. //----------------------------------------------------------------------------
  47. void ctkPluginFrameworkPrivate::uninitSystemPlugin()
  48. {
  49. this->pluginContext->d_func()->invalidate();
  50. }
  51. //----------------------------------------------------------------------------
  52. void ctkPluginFrameworkPrivate::shutdown(bool restart)
  53. {
  54. Locker sync(&lock);
  55. bool wasActive = false;
  56. switch (state)
  57. {
  58. case ctkPlugin::INSTALLED:
  59. case ctkPlugin::RESOLVED:
  60. shutdownDone_unlocked(false);
  61. break;
  62. case ctkPlugin::ACTIVE:
  63. wasActive = true;
  64. // Fall through
  65. case ctkPlugin::STARTING:
  66. if (shuttingDown.fetchAndAddOrdered(0) == 0)
  67. {
  68. try
  69. {
  70. const bool wa = wasActive;
  71. shuttingDown.fetchAndStoreOrdered(1);
  72. QtConcurrent::run(this, &ctkPluginFrameworkPrivate::shutdown0, restart, wa);
  73. }
  74. catch (const ctkException& e)
  75. {
  76. systemShuttingdownDone(ctkPluginFrameworkEvent(ctkPluginFrameworkEvent::PLUGIN_ERROR,
  77. this->q_func(), e));
  78. }
  79. }
  80. break;
  81. case ctkPlugin::STOPPING:
  82. // Shutdown already inprogress, fall through
  83. case ctkPlugin::UNINSTALLED:
  84. break;
  85. }
  86. }
  87. //----------------------------------------------------------------------------
  88. void ctkPluginFrameworkPrivate::shutdown0(bool restart, bool wasActive)
  89. {
  90. try
  91. {
  92. {
  93. Locker sync(&lock);
  94. waitOnOperation(&lock, QString("Framework::") + (restart ? "update" : "stop"), true);
  95. operation = DEACTIVATING;
  96. state = ctkPlugin::STOPPING;
  97. }
  98. fwCtx->listeners.emitPluginChanged(
  99. ctkPluginEvent(ctkPluginEvent::STOPPING, this->q_func()));
  100. if (wasActive)
  101. {
  102. stopAllPlugins();
  103. }
  104. {
  105. Locker sync(&lock);
  106. fwCtx->uninit();
  107. shuttingDown.fetchAndStoreOrdered(0);
  108. shutdownDone_unlocked(restart);
  109. }
  110. if (restart)
  111. {
  112. if (wasActive)
  113. {
  114. q_func().toStrongRef()->start();
  115. }
  116. else
  117. {
  118. init();
  119. }
  120. }
  121. }
  122. catch (const ctkException& e)
  123. {
  124. shuttingDown.fetchAndStoreOrdered(0);
  125. systemShuttingdownDone(ctkPluginFrameworkEvent(ctkPluginFrameworkEvent::PLUGIN_ERROR,
  126. this->q_func(), e));
  127. }
  128. }
  129. //----------------------------------------------------------------------------
  130. void ctkPluginFrameworkPrivate::shutdownDone_unlocked(bool restart)
  131. {
  132. ctkPluginFrameworkEvent::Type t = restart ? ctkPluginFrameworkEvent::FRAMEWORK_STOPPED_UPDATE : ctkPluginFrameworkEvent::FRAMEWORK_STOPPED;
  133. systemShuttingdownDone_unlocked(ctkPluginFrameworkEvent(t, this->q_func()));
  134. }
  135. //----------------------------------------------------------------------------
  136. void ctkPluginFrameworkPrivate::stopAllPlugins()
  137. {
  138. // TODO start level
  139. // if (fwCtx.startLevelController != null)
  140. // {
  141. // fwCtx.startLevelController.shutdown();
  142. // }
  143. // Stop all active plug-ins, in reverse plug-in ID order
  144. // The list will be empty when the start level service is in use.
  145. QList<QSharedPointer<ctkPlugin> > activePlugins = fwCtx->plugins->getActivePlugins();
  146. qSort(activePlugins.begin(), activePlugins.end(), pluginIdLessThan);
  147. QListIterator<QSharedPointer<ctkPlugin> > i(activePlugins);
  148. i.toBack();
  149. while(i.hasPrevious())
  150. {
  151. QSharedPointer<ctkPlugin> p = i.previous();
  152. try
  153. {
  154. if (p->getState() & (ctkPlugin::ACTIVE | ctkPlugin::STARTING))
  155. {
  156. // Stop plugin without changing its autostart setting.
  157. p->stop(ctkPlugin::STOP_TRANSIENT);
  158. }
  159. }
  160. catch (const ctkPluginException& pe)
  161. {
  162. fwCtx->listeners.frameworkError(p, pe);
  163. }
  164. }
  165. QList<QSharedPointer<ctkPlugin> > allPlugins = fwCtx->plugins->getPlugins();
  166. // Set state to INSTALLED and purge any unrefreshed bundles
  167. foreach (QSharedPointer<ctkPlugin> p, allPlugins)
  168. {
  169. if (p->getPluginId() != 0)
  170. {
  171. p->d_func()->setStateInstalled(false);
  172. p->d_func()->purge();
  173. }
  174. }
  175. }
  176. //----------------------------------------------------------------------------
  177. void ctkPluginFrameworkPrivate::systemShuttingdownDone(const ctkPluginFrameworkEvent& fe)
  178. {
  179. Locker sync(&lock);
  180. systemShuttingdownDone_unlocked(fe);
  181. }
  182. //----------------------------------------------------------------------------
  183. void ctkPluginFrameworkPrivate::systemShuttingdownDone_unlocked(const ctkPluginFrameworkEvent& fe)
  184. {
  185. if (state != ctkPlugin::INSTALLED)
  186. {
  187. state = ctkPlugin::RESOLVED;
  188. operation.fetchAndStoreOrdered(IDLE);
  189. lock.wakeAll();
  190. }
  191. stopEvent.isNull = fe.isNull();
  192. stopEvent.type = fe.getType();
  193. }