ctkPluginPrivate.cpp 11 KB

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