ctkPluginFramework_p.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 "ctkPluginFrameworkLauncher.h"
  20. #include "ctkLocationManager_p.h"
  21. #include "ctkPluginFramework_p.h"
  22. #include "ctkPluginFrameworkContext_p.h"
  23. #include "ctkPluginFrameworkUtil_p.h"
  24. #include "ctkPluginFrameworkDebugOptions_p.h"
  25. #include "ctkBasicLocation_p.h"
  26. #include <QtConcurrentRun>
  27. //----------------------------------------------------------------------------
  28. ctkPluginFrameworkPrivate::ctkPluginFrameworkPrivate(QWeakPointer<ctkPlugin> qq, ctkPluginFrameworkContext* fw)
  29. : ctkPluginPrivate(qq, fw, 0, ctkPluginConstants::SYSTEM_PLUGIN_LOCATION,
  30. ctkPluginConstants::SYSTEM_PLUGIN_SYMBOLICNAME,
  31. // TODO: read version from the manifest resource
  32. ctkVersion(0, 9, 0)),
  33. shuttingDown(0)
  34. {
  35. systemHeaders.insert(ctkPluginConstants::PLUGIN_SYMBOLICNAME, symbolicName);
  36. systemHeaders.insert(ctkPluginConstants::PLUGIN_NAME, location);
  37. systemHeaders.insert(ctkPluginConstants::PLUGIN_VERSION, version.toString());
  38. }
  39. //----------------------------------------------------------------------------
  40. void ctkPluginFrameworkPrivate::init()
  41. {
  42. this->state = ctkPlugin::STARTING;
  43. this->fwCtx->init();
  44. }
  45. //----------------------------------------------------------------------------
  46. void ctkPluginFrameworkPrivate::initSystemPlugin()
  47. {
  48. this->pluginContext.reset(new ctkPluginContext(this));
  49. }
  50. //----------------------------------------------------------------------------
  51. void ctkPluginFrameworkPrivate::activate(ctkPluginContext* context)
  52. {
  53. ctkProperties locationProperties;
  54. ctkBasicLocation* location = ctkLocationManager::getUserLocation();
  55. if (location != NULL)
  56. {
  57. locationProperties["type"] = ctkPluginFrameworkLauncher::PROP_USER_AREA;
  58. registrations.push_back(context->registerService<ctkLocation>(location, locationProperties));
  59. }
  60. location = ctkLocationManager::getInstanceLocation();
  61. if (location != NULL)
  62. {
  63. locationProperties["type"] = ctkPluginFrameworkLauncher::PROP_INSTANCE_AREA;
  64. registrations.push_back(context->registerService<ctkLocation>(location, locationProperties));
  65. }
  66. location = ctkLocationManager::getConfigurationLocation();
  67. if (location != NULL)
  68. {
  69. locationProperties["type"] = ctkPluginFrameworkLauncher::PROP_CONFIG_AREA;
  70. registrations.push_back(context->registerService<ctkLocation>(location, locationProperties));
  71. }
  72. location = ctkLocationManager::getInstallLocation();
  73. if (location != NULL)
  74. {
  75. locationProperties["type"] = ctkPluginFrameworkLauncher::PROP_INSTALL_AREA;
  76. registrations.push_back(context->registerService<ctkLocation>(location, locationProperties));
  77. }
  78. location = ctkLocationManager::getCTKHomeLocation();
  79. if (location != NULL)
  80. {
  81. locationProperties["type"] = ctkPluginFrameworkLauncher::PROP_HOME_LOCATION_AREA;
  82. registrations.push_back(context->registerService<ctkLocation>(location, locationProperties));
  83. }
  84. ctkPluginFrameworkDebugOptions* dbgOptions = ctkPluginFrameworkDebugOptions::getDefault();
  85. dbgOptions->start(context);
  86. context->registerService<ctkDebugOptions>(dbgOptions);
  87. }
  88. //----------------------------------------------------------------------------
  89. void ctkPluginFrameworkPrivate::deactivate(ctkPluginContext* /*context*/)
  90. {
  91. foreach(ctkServiceRegistration registration, registrations)
  92. {
  93. if (registration)
  94. {
  95. try
  96. {
  97. registration.unregister();
  98. }
  99. catch (const ctkIllegalStateException&) {}
  100. }
  101. }
  102. }
  103. //----------------------------------------------------------------------------
  104. void ctkPluginFrameworkPrivate::uninitSystemPlugin()
  105. {
  106. this->pluginContext->d_func()->invalidate();
  107. }
  108. //----------------------------------------------------------------------------
  109. void ctkPluginFrameworkPrivate::shutdown(bool restart)
  110. {
  111. Locker sync(&lock);
  112. bool wasActive = false;
  113. switch (state)
  114. {
  115. case ctkPlugin::INSTALLED:
  116. case ctkPlugin::RESOLVED:
  117. shutdownDone_unlocked(false);
  118. break;
  119. case ctkPlugin::ACTIVE:
  120. wasActive = true;
  121. // Fall through
  122. case ctkPlugin::STARTING:
  123. if (shuttingDown.fetchAndAddOrdered(0) == 0)
  124. {
  125. try
  126. {
  127. const bool wa = wasActive;
  128. shuttingDown.fetchAndStoreOrdered(1);
  129. QtConcurrent::run(this, &ctkPluginFrameworkPrivate::shutdown0, restart, wa);
  130. }
  131. catch (const ctkException& e)
  132. {
  133. systemShuttingdownDone(ctkPluginFrameworkEvent(ctkPluginFrameworkEvent::PLUGIN_ERROR,
  134. this->q_func(), e));
  135. }
  136. }
  137. break;
  138. case ctkPlugin::STOPPING:
  139. // Shutdown already inprogress, fall through
  140. case ctkPlugin::UNINSTALLED:
  141. break;
  142. }
  143. }
  144. //----------------------------------------------------------------------------
  145. void ctkPluginFrameworkPrivate::shutdown0(bool restart, bool wasActive)
  146. {
  147. try
  148. {
  149. {
  150. Locker sync(&lock);
  151. waitOnOperation(&lock, QString("Framework::") + (restart ? "update" : "stop"), true);
  152. operation = DEACTIVATING;
  153. state = ctkPlugin::STOPPING;
  154. }
  155. fwCtx->listeners.emitPluginChanged(
  156. ctkPluginEvent(ctkPluginEvent::STOPPING, this->q_func()));
  157. if (wasActive)
  158. {
  159. stopAllPlugins();
  160. deactivate(this->pluginContext.data());
  161. }
  162. {
  163. Locker sync(&lock);
  164. fwCtx->uninit();
  165. shuttingDown.fetchAndStoreOrdered(0);
  166. shutdownDone_unlocked(restart);
  167. }
  168. if (restart)
  169. {
  170. if (wasActive)
  171. {
  172. q_func().toStrongRef()->start();
  173. }
  174. else
  175. {
  176. init();
  177. }
  178. }
  179. }
  180. catch (const ctkException& e)
  181. {
  182. shuttingDown.fetchAndStoreOrdered(0);
  183. systemShuttingdownDone(ctkPluginFrameworkEvent(ctkPluginFrameworkEvent::PLUGIN_ERROR,
  184. this->q_func(), e));
  185. }
  186. }
  187. //----------------------------------------------------------------------------
  188. void ctkPluginFrameworkPrivate::shutdownDone_unlocked(bool restart)
  189. {
  190. ctkPluginFrameworkEvent::Type t = restart ? ctkPluginFrameworkEvent::FRAMEWORK_STOPPED_UPDATE : ctkPluginFrameworkEvent::FRAMEWORK_STOPPED;
  191. systemShuttingdownDone_unlocked(ctkPluginFrameworkEvent(t, this->q_func()));
  192. }
  193. //----------------------------------------------------------------------------
  194. void ctkPluginFrameworkPrivate::stopAllPlugins()
  195. {
  196. // TODO start level
  197. // if (fwCtx.startLevelController != null)
  198. // {
  199. // fwCtx.startLevelController.shutdown();
  200. // }
  201. // Stop all active plug-ins, in reverse plug-in ID order
  202. // The list will be empty when the start level service is in use.
  203. QList<QSharedPointer<ctkPlugin> > activePlugins = fwCtx->plugins->getActivePlugins();
  204. qSort(activePlugins.begin(), activePlugins.end(), pluginIdLessThan);
  205. QListIterator<QSharedPointer<ctkPlugin> > i(activePlugins);
  206. i.toBack();
  207. while(i.hasPrevious())
  208. {
  209. QSharedPointer<ctkPlugin> p = i.previous();
  210. try
  211. {
  212. if (p->getState() & (ctkPlugin::ACTIVE | ctkPlugin::STARTING))
  213. {
  214. // Stop plugin without changing its autostart setting.
  215. p->stop(ctkPlugin::STOP_TRANSIENT);
  216. }
  217. }
  218. catch (const ctkPluginException& pe)
  219. {
  220. fwCtx->listeners.frameworkError(p, pe);
  221. }
  222. }
  223. QList<QSharedPointer<ctkPlugin> > allPlugins = fwCtx->plugins->getPlugins();
  224. // Set state to INSTALLED and purge any unrefreshed bundles
  225. foreach (QSharedPointer<ctkPlugin> p, allPlugins)
  226. {
  227. if (p->getPluginId() != 0)
  228. {
  229. p->d_func()->setStateInstalled(false);
  230. p->d_func()->purge();
  231. }
  232. }
  233. }
  234. //----------------------------------------------------------------------------
  235. void ctkPluginFrameworkPrivate::systemShuttingdownDone(const ctkPluginFrameworkEvent& fe)
  236. {
  237. Locker sync(&lock);
  238. systemShuttingdownDone_unlocked(fe);
  239. }
  240. //----------------------------------------------------------------------------
  241. void ctkPluginFrameworkPrivate::systemShuttingdownDone_unlocked(const ctkPluginFrameworkEvent& fe)
  242. {
  243. if (state != ctkPlugin::INSTALLED)
  244. {
  245. state = ctkPlugin::RESOLVED;
  246. operation.fetchAndStoreOrdered(IDLE);
  247. lock.wakeAll();
  248. }
  249. stopEvent.isNull = fe.isNull();
  250. stopEvent.type = fe.getType();
  251. }