ctkPluginPrivate.cpp 13 KB

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