ctkPluginPrivate.cpp 12 KB

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