ctkPluginPrivate.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 "ctkPluginPrivate_p.h"
  16. #include "ctkPluginConstants.h"
  17. #include "ctkPluginDatabaseException.h"
  18. #include "ctkPluginArchive_p.h"
  19. #include "ctkPluginFrameworkContext_p.h"
  20. #include "ctkPluginFrameworkUtil_p.h"
  21. #include "ctkPluginActivator.h"
  22. #include "ctkPluginContext_p.h"
  23. #include "ctkServices_p.h"
  24. #include "ctkServiceReferencePrivate.h"
  25. #include "ctkServiceRegistration.h"
  26. const ctkPlugin::States ctkPluginPrivate::RESOLVED_FLAGS = ctkPlugin::RESOLVED | ctkPlugin::STARTING | ctkPlugin::ACTIVE | ctkPlugin::STOPPING;
  27. ctkPluginPrivate::ctkPluginPrivate(
  28. ctkPlugin& qq,
  29. ctkPluginFrameworkContext* fw,
  30. ctkPluginArchive* pa)
  31. : q_ptr(&qq), fwCtx(fw), id(pa->getPluginId()),
  32. location(pa->getPluginLocation().toString()), state(ctkPlugin::INSTALLED),
  33. archive(pa), pluginContext(0), pluginActivator(0), pluginLoader(pa->getLibLocation()),
  34. lastModified(0), eagerActivation(false), activating(false), deactivating(false)
  35. {
  36. //TODO
  37. //checkCertificates(pa);
  38. checkManifestHeaders();
  39. // fill require list
  40. QString requireString = archive->getAttribute(ctkPluginConstants::REQUIRE_PLUGIN);
  41. QList<QMap<QString, QStringList> > requireList = ctkPluginFrameworkUtil::parseEntries(ctkPluginConstants::REQUIRE_PLUGIN,
  42. requireString, true, true, false);
  43. QListIterator<QMap<QString, QStringList> > i(requireList);
  44. while (i.hasNext())
  45. {
  46. const QMap<QString, QStringList>& e = i.next();
  47. const QStringList& res = e.value(ctkPluginConstants::RESOLUTION_DIRECTIVE);
  48. const QStringList& version = e.value(ctkPluginConstants::PLUGIN_VERSION_ATTRIBUTE);
  49. ctkRequirePlugin* rp = new ctkRequirePlugin(this, e.value("$key").front(),
  50. res.empty() ? QString() : res.front(),
  51. version.empty() ? QString() : version.front());
  52. require.push_back(rp);
  53. }
  54. }
  55. ctkPluginPrivate::ctkPluginPrivate(ctkPlugin& qq,
  56. ctkPluginFrameworkContext* fw,
  57. long id, const QString& loc, const QString& sym, const ctkVersion& ver)
  58. : q_ptr(&qq), fwCtx(fw), id(id), location(loc), symbolicName(sym), version(ver),
  59. state(ctkPlugin::INSTALLED), archive(0), pluginContext(0),
  60. pluginActivator(0), lastModified(0),
  61. eagerActivation(false), activating(false), deactivating(false)
  62. {
  63. }
  64. ctkPluginPrivate::~ctkPluginPrivate()
  65. {
  66. qDeleteAll(require);
  67. }
  68. ctkPlugin::State ctkPluginPrivate::getUpdatedState()
  69. {
  70. if (state & ctkPlugin::INSTALLED)
  71. {
  72. try
  73. {
  74. if (state == ctkPlugin::INSTALLED)
  75. {
  76. fwCtx->resolvePlugin(this);
  77. state = ctkPlugin::RESOLVED;
  78. fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::RESOLVED, this->q_func()));
  79. }
  80. }
  81. catch (const ctkPluginException& pe)
  82. {
  83. Q_Q(ctkPlugin);
  84. this->fwCtx->listeners.frameworkError(q, pe);
  85. throw;
  86. }
  87. }
  88. return state;
  89. }
  90. void ctkPluginPrivate::setAutostartSetting(const ctkPlugin::StartOptions& setting) {
  91. try
  92. {
  93. if (archive)
  94. {
  95. archive->setAutostartSetting(setting);
  96. }
  97. }
  98. catch (const ctkPluginDatabaseException& e)
  99. {
  100. Q_Q(ctkPlugin);
  101. this->fwCtx->listeners.frameworkError(q, e);
  102. }
  103. }
  104. void ctkPluginPrivate::ignoreAutostartSetting()
  105. {
  106. try
  107. {
  108. if (archive)
  109. {
  110. archive->setAutostartSetting(-1);
  111. }
  112. }
  113. catch (const ctkPluginDatabaseException& e)
  114. {
  115. Q_Q(ctkPlugin);
  116. this->fwCtx->listeners.frameworkError(q, e);
  117. }
  118. }
  119. void ctkPluginPrivate::checkManifestHeaders()
  120. {
  121. symbolicName = archive->getAttribute(ctkPluginConstants::PLUGIN_SYMBOLICNAME);
  122. if (symbolicName.isEmpty())
  123. {
  124. throw std::invalid_argument(std::string("ctkPlugin has no symbolic name, location=") +
  125. qPrintable(location));
  126. }
  127. QString mpv = archive->getAttribute(ctkPluginConstants::PLUGIN_VERSION);
  128. if (!mpv.isEmpty())
  129. {
  130. try
  131. {
  132. version = ctkVersion(mpv);
  133. }
  134. catch (const std::exception& e)
  135. {
  136. throw std::invalid_argument(std::string("ctkPlugin does not specify a valid ") +
  137. qPrintable(ctkPluginConstants::PLUGIN_VERSION) + " header. Got exception: " + e.what());
  138. }
  139. }
  140. QString ap = archive->getAttribute(ctkPluginConstants::PLUGIN_ACTIVATIONPOLICY);
  141. if (ctkPluginConstants::ACTIVATION_EAGER == ap)
  142. {
  143. eagerActivation = true;
  144. }
  145. }
  146. void ctkPluginPrivate::finalizeActivation()
  147. {
  148. switch (getUpdatedState())
  149. {
  150. case ctkPlugin::INSTALLED:
  151. // we shouldn't be here, getUpdatedState should have thrown
  152. // an exception during resolving the plugin
  153. throw ctkPluginException("Internal error: expected exception on plugin resolve not thrown!");
  154. case ctkPlugin::STARTING:
  155. if (activating) return; // finalization already in progress.
  156. // Lazy activation; fall through to RESOLVED.
  157. case ctkPlugin::RESOLVED:
  158. //6:
  159. state = ctkPlugin::STARTING;
  160. activating = true;
  161. qDebug() << "activating #" << this->id;
  162. //7:
  163. if (!pluginContext)
  164. {
  165. pluginContext = new ctkPluginContext(this);
  166. }
  167. try
  168. {
  169. // start dependencies
  170. startDependencies();
  171. //TODO maybe call this in its own thread
  172. start0();
  173. }
  174. catch (...)
  175. {
  176. //8:
  177. state = ctkPlugin::STOPPING;
  178. // NYI, call outside lock
  179. fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::STOPPING, this->q_func()));
  180. removePluginResources();
  181. delete pluginContext;
  182. state = ctkPlugin::RESOLVED;
  183. // NYI, call outside lock
  184. fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::STOPPED, this->q_func()));
  185. activating = false;
  186. throw;
  187. }
  188. activating = false;
  189. break;
  190. case ctkPlugin::ACTIVE:
  191. break;
  192. case ctkPlugin::STOPPING:
  193. // This happens if start is called from inside the ctkPluginActivator::stop method.
  194. // Don't allow it.
  195. throw ctkPluginException("start called from ctkPluginActivator::stop",
  196. ctkPluginException::ACTIVATOR_ERROR);
  197. case ctkPlugin::UNINSTALLED:
  198. throw std::logic_error("ctkPlugin is in UNINSTALLED state");
  199. }
  200. }
  201. void ctkPluginPrivate::stop0(bool wasStarted)
  202. {
  203. //5:
  204. state = ctkPlugin::STOPPING;
  205. deactivating = true;
  206. //6-13:
  207. const std::exception* savedException = 0;
  208. try
  209. {
  210. //6:
  211. fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::STOPPING, q_func()));
  212. //7:
  213. if (wasStarted && pluginActivator)
  214. {
  215. try
  216. {
  217. pluginActivator->stop(pluginContext);
  218. }
  219. catch (const std::exception& e)
  220. {
  221. savedException = new ctkPluginException("ctkPlugin::stop: PluginActivator stop failed",
  222. ctkPluginException::ACTIVATOR_ERROR, &e);
  223. }
  224. if (state == ctkPlugin::UNINSTALLED)
  225. {
  226. throw std::logic_error("Plugin is uninstalled");
  227. }
  228. pluginActivator = 0;
  229. }
  230. // Call hooks after we've called PluginActivator::stop(), but before we've cleared all resources
  231. // TODO service listener hooks
  232. //fwCtx->listeners.serviceListeners.hooksBundleStopped(this);
  233. if (pluginContext)
  234. {
  235. pluginContext->d_func()->invalidate();
  236. pluginContext = 0;
  237. }
  238. //8-10:
  239. removePluginResources();
  240. }
  241. catch (const std::exception* exc)
  242. {
  243. savedException = exc;
  244. }
  245. if (state != ctkPlugin::UNINSTALLED)
  246. {
  247. state = ctkPlugin::RESOLVED;
  248. deactivating = false;
  249. //fwCtx.packages.notifyAll();
  250. }
  251. if (savedException)
  252. {
  253. throw savedException;
  254. }
  255. }
  256. void ctkPluginPrivate::startDependencies()
  257. {
  258. QListIterator<ctkRequirePlugin*> i(this->require);
  259. while (i.hasNext())
  260. {
  261. ctkRequirePlugin* pr = i.next();
  262. QList<ctkPlugin*> pl = fwCtx->plugins->getPlugins(pr->name, pr->pluginRange);
  263. if (pl.isEmpty())
  264. {
  265. if (pr->resolution == ctkPluginConstants::RESOLUTION_MANDATORY)
  266. {
  267. // We should never get here, since the plugin can only be
  268. // started if all its dependencies could be resolved.
  269. throw ctkPluginException(
  270. QString("Internal error: dependent plugin %1 inside version range %2 is not installed.").
  271. arg(pr->name).arg(pr->pluginRange.toString()));
  272. }
  273. else
  274. {
  275. continue;
  276. }
  277. }
  278. // We take the first plugin in the list (highest version number)
  279. pl.front()->start();
  280. }
  281. }
  282. void ctkPluginPrivate::start0()
  283. {
  284. fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::STARTING, this->q_func()));
  285. try {
  286. pluginLoader.load();
  287. if (!pluginLoader.isLoaded())
  288. {
  289. throw ctkPluginException(QString("Loading plugin %1 failed: %2").arg(pluginLoader.fileName()).arg(pluginLoader.errorString()),
  290. ctkPluginException::ACTIVATOR_ERROR);
  291. }
  292. pluginActivator = qobject_cast<ctkPluginActivator*>(pluginLoader.instance());
  293. if (!pluginActivator)
  294. {
  295. throw ctkPluginException(QString("Creating ctkPluginActivator instance from %1 failed: %2").arg(pluginLoader.fileName()).arg(pluginLoader.errorString()),
  296. ctkPluginException::ACTIVATOR_ERROR);
  297. }
  298. pluginActivator->start(pluginContext);
  299. if (ctkPlugin::UNINSTALLED == state)
  300. {
  301. throw ctkPluginException("ctkPlugin uninstalled during start()", ctkPluginException::STATECHANGE_ERROR);
  302. }
  303. state = ctkPlugin::ACTIVE;
  304. }
  305. catch (const std::exception& e)
  306. {
  307. throw ctkPluginException("ctkPlugin start failed", ctkPluginException::ACTIVATOR_ERROR, &e);
  308. }
  309. qDebug() << "activating #" << id << "completed.";
  310. //10:
  311. fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::STARTED, this->q_func()));
  312. }
  313. void ctkPluginPrivate::removePluginResources()
  314. {
  315. // automatic disconnect due to Qt signal slot
  316. //fwCtx->listeners.removeAllListeners(this);
  317. QList<ctkServiceRegistration*> srs = fwCtx->services->getRegisteredByPlugin(this);
  318. QListIterator<ctkServiceRegistration*> i(srs);
  319. while (i.hasNext())
  320. {
  321. try
  322. {
  323. i.next()->unregister();
  324. }
  325. catch (const std::logic_error& ignore)
  326. {
  327. // Someone has unregistered the service after stop completed.
  328. // This should not occur, but we don't want get stuck in
  329. // an illegal state so we catch it.
  330. }
  331. }
  332. QList<ctkServiceRegistration*> s = fwCtx->services->getUsedByPlugin(q_func());
  333. QListIterator<ctkServiceRegistration*> i2(s);
  334. while (i2.hasNext())
  335. {
  336. i2.next()->getReference().d_func()->ungetService(q_func(), false);
  337. }
  338. }