ctkPluginPrivate.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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. // for ctk::msecsTo() - remove after switching to Qt 4.7
  27. #include <ctkUtils.h>
  28. const ctkPlugin::States ctkPluginPrivate::RESOLVED_FLAGS = ctkPlugin::RESOLVED | ctkPlugin::STARTING | ctkPlugin::ACTIVE | ctkPlugin::STOPPING;
  29. //----------------------------------------------------------------------------
  30. void ctkPluginPrivate::LockObject::lock()
  31. {
  32. m_Lock.lock();
  33. }
  34. //----------------------------------------------------------------------------
  35. bool ctkPluginPrivate::LockObject::tryLock()
  36. {
  37. return m_Lock.tryLock();
  38. }
  39. //----------------------------------------------------------------------------
  40. bool ctkPluginPrivate::LockObject::tryLock(int timeout)
  41. {
  42. return m_Lock.tryLock(timeout);
  43. }
  44. //----------------------------------------------------------------------------
  45. void ctkPluginPrivate::LockObject::unlock()
  46. {
  47. m_Lock.unlock();
  48. }
  49. //----------------------------------------------------------------------------
  50. bool ctkPluginPrivate::LockObject::wait(unsigned long time)
  51. {
  52. return m_Condition.wait(&m_Lock, time);
  53. }
  54. //----------------------------------------------------------------------------
  55. void ctkPluginPrivate::LockObject::wakeAll()
  56. {
  57. m_Condition.wakeAll();
  58. }
  59. //----------------------------------------------------------------------------
  60. void ctkPluginPrivate::LockObject::wakeOne()
  61. {
  62. m_Condition.wakeOne();
  63. }
  64. //----------------------------------------------------------------------------
  65. ctkPluginPrivate::ctkPluginPrivate(
  66. QWeakPointer<ctkPlugin> qq,
  67. ctkPluginFrameworkContext* fw,
  68. ctkPluginArchive* pa)
  69. : q_ptr(qq), fwCtx(fw), id(pa->getPluginId()),
  70. location(pa->getPluginLocation().toString()), state(ctkPlugin::INSTALLED),
  71. archive(pa), pluginContext(0), pluginActivator(0), pluginLoader(pa->getLibLocation()),
  72. resolveFailException(0), eagerActivation(false), wasStarted(false)
  73. {
  74. //TODO
  75. //checkCertificates(pa);
  76. // Get library load hints
  77. if (fw->props.contains(ctkPluginConstants::FRAMEWORK_PLUGIN_LOAD_HINTS))
  78. {
  79. QVariant loadHintsVariant = fw->props[ctkPluginConstants::FRAMEWORK_PLUGIN_LOAD_HINTS];
  80. if (loadHintsVariant.isValid())
  81. {
  82. QLibrary::LoadHints loadHints = loadHintsVariant.value<QLibrary::LoadHints>();
  83. pluginLoader.setLoadHints(loadHints);
  84. }
  85. }
  86. checkManifestHeaders();
  87. pluginDir = fwCtx->getDataStorage(id);
  88. // int oldStartLevel = archive->getStartLevel();
  89. try
  90. {
  91. //TODO: StartLevel Service
  92. //if (fwCtx->startLevelController == 0)
  93. //{
  94. archive->setStartLevel(0);
  95. //}
  96. // else
  97. // {
  98. // if (oldStartLevel == -1)
  99. // {
  100. // archive->setStartLevel(fwCtx->startLevelController->getInitialPluginStartLevel());
  101. // }
  102. // }
  103. }
  104. catch (const std::exception& e)
  105. {
  106. qDebug() << "Failed to set start level on #" << id << ":" << e.what();
  107. }
  108. lastModified = archive->getLastModified();
  109. if (lastModified.isNull())
  110. {
  111. modified();
  112. }
  113. // fill require list
  114. QString requireString = archive->getAttribute(ctkPluginConstants::REQUIRE_PLUGIN);
  115. QList<QMap<QString, QStringList> > requireList = ctkPluginFrameworkUtil::parseEntries(ctkPluginConstants::REQUIRE_PLUGIN,
  116. requireString, true, true, false);
  117. QListIterator<QMap<QString, QStringList> > i(requireList);
  118. while (i.hasNext())
  119. {
  120. const QMap<QString, QStringList>& e = i.next();
  121. const QStringList& res = e.value(ctkPluginConstants::RESOLUTION_DIRECTIVE);
  122. const QStringList& version = e.value(ctkPluginConstants::PLUGIN_VERSION_ATTRIBUTE);
  123. ctkRequirePlugin* rp = new ctkRequirePlugin(this, e.value("$key").front(),
  124. res.empty() ? QString() : res.front(),
  125. version.empty() ? QString() : version.front());
  126. require.push_back(rp);
  127. }
  128. }
  129. //----------------------------------------------------------------------------
  130. ctkPluginPrivate::ctkPluginPrivate(QWeakPointer<ctkPlugin> qq,
  131. ctkPluginFrameworkContext* fw,
  132. long id, const QString& loc, const QString& sym, const ctkVersion& ver)
  133. : q_ptr(qq), fwCtx(fw), id(id), location(loc), symbolicName(sym), version(ver),
  134. state(ctkPlugin::INSTALLED), archive(0), pluginContext(0),
  135. pluginActivator(0), resolveFailException(0),
  136. eagerActivation(false), wasStarted(false)
  137. {
  138. modified();
  139. }
  140. //----------------------------------------------------------------------------
  141. ctkPluginPrivate::~ctkPluginPrivate()
  142. {
  143. qDeleteAll(require);
  144. delete archive;
  145. }
  146. //----------------------------------------------------------------------------
  147. ctkPlugin::State ctkPluginPrivate::getUpdatedState()
  148. {
  149. if (state & ctkPlugin::INSTALLED)
  150. {
  151. try
  152. {
  153. // NYI, fix double locking
  154. Locker sync(&operationLock);
  155. if (state == ctkPlugin::INSTALLED)
  156. {
  157. operation.fetchAndStoreOrdered(RESOLVING);
  158. fwCtx->resolvePlugin(this);
  159. state = ctkPlugin::RESOLVED;
  160. // TODO plugin threading
  161. //bundleThread().bundleChanged(new BundleEvent(BundleEvent.RESOLVED, this));
  162. fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::RESOLVED, this->q_func()));
  163. operation.fetchAndStoreOrdered(IDLE);
  164. }
  165. }
  166. catch (const ctkPluginException& pe)
  167. {
  168. if (resolveFailException) delete resolveFailException;
  169. resolveFailException = new ctkPluginException(pe);
  170. this->fwCtx->listeners.frameworkError(this->q_func(), pe);
  171. }
  172. }
  173. return state;
  174. }
  175. //----------------------------------------------------------------------------
  176. QFileInfo ctkPluginPrivate::getDataRoot()
  177. {
  178. return pluginDir;
  179. }
  180. //----------------------------------------------------------------------------
  181. void ctkPluginPrivate::setStateInstalled(bool sendEvent)
  182. {
  183. Locker sync(&operationLock);
  184. // Make sure that the context is invalid
  185. if (pluginContext != 0)
  186. {
  187. pluginContext->d_func()->invalidate();
  188. pluginContext.reset();
  189. }
  190. state = ctkPlugin::INSTALLED;
  191. if (sendEvent)
  192. {
  193. operation.fetchAndStoreOrdered(UNRESOLVING);
  194. // TODO: plugin thread
  195. //bundleThread().bundleChanged(new BundleEvent(BundleEvent.UNRESOLVED, this));
  196. fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::UNRESOLVED, this->q_func()));
  197. }
  198. operation.fetchAndStoreOrdered(IDLE);
  199. }
  200. //----------------------------------------------------------------------------
  201. void ctkPluginPrivate::purge()
  202. {
  203. if (state == ctkPlugin::UNINSTALLED)
  204. {
  205. fwCtx->plugins->remove(location);
  206. }
  207. // Vector fix = oldGenerations;
  208. // if (fix != null) {
  209. // oldGenerations = null;
  210. // for (Iterator i = fix.iterator(); i.hasNext();) {
  211. // ((BundleGeneration)i.next()).purge(true);
  212. // }
  213. // }
  214. }
  215. //----------------------------------------------------------------------------
  216. void ctkPluginPrivate::setAutostartSetting(const ctkPlugin::StartOptions& setting) {
  217. try
  218. {
  219. if (archive)
  220. {
  221. archive->setAutostartSetting(setting);
  222. }
  223. }
  224. catch (const ctkPluginDatabaseException& e)
  225. {
  226. this->fwCtx->listeners.frameworkError(this->q_func(), e);
  227. }
  228. }
  229. //----------------------------------------------------------------------------
  230. void ctkPluginPrivate::ignoreAutostartSetting()
  231. {
  232. try
  233. {
  234. if (archive)
  235. {
  236. archive->setAutostartSetting(-1);
  237. }
  238. }
  239. catch (const ctkPluginDatabaseException& e)
  240. {
  241. this->fwCtx->listeners.frameworkError(this->q_func(), e);
  242. }
  243. }
  244. //----------------------------------------------------------------------------
  245. void ctkPluginPrivate::modified()
  246. {
  247. lastModified = QDateTime::currentDateTime();
  248. if (archive)
  249. {
  250. archive->setLastModified(lastModified);
  251. }
  252. }
  253. //----------------------------------------------------------------------------
  254. void ctkPluginPrivate::checkManifestHeaders()
  255. {
  256. symbolicName = archive->getAttribute(ctkPluginConstants::PLUGIN_SYMBOLICNAME);
  257. if (symbolicName.isEmpty())
  258. {
  259. throw std::invalid_argument(std::string("ctkPlugin has no symbolic name, location=") +
  260. qPrintable(location));
  261. }
  262. QString mpv = archive->getAttribute(ctkPluginConstants::PLUGIN_VERSION);
  263. if (!mpv.isEmpty())
  264. {
  265. try
  266. {
  267. version = ctkVersion(mpv);
  268. }
  269. catch (const std::exception& e)
  270. {
  271. throw std::invalid_argument(std::string("ctkPlugin does not specify a valid ") +
  272. qPrintable(ctkPluginConstants::PLUGIN_VERSION) + " header. Got exception: " + e.what());
  273. }
  274. }
  275. QSharedPointer<ctkPlugin> snp = fwCtx->plugins->getPlugin(symbolicName, version);
  276. // TBD! Should we allow update to same version?
  277. if (!snp.isNull() && snp->d_func() != this)
  278. {
  279. throw std::invalid_argument(std::string("Plugin with same symbolic name and version is already installed (")
  280. + symbolicName.toStdString() + ", " + version.toString().toStdString() + ")");
  281. }
  282. QString ap = archive->getAttribute(ctkPluginConstants::PLUGIN_ACTIVATIONPOLICY);
  283. if (ctkPluginConstants::ACTIVATION_EAGER == ap)
  284. {
  285. eagerActivation = true;
  286. }
  287. }
  288. //----------------------------------------------------------------------------
  289. void ctkPluginPrivate::finalizeActivation()
  290. {
  291. Locker sync(&operationLock);
  292. // 4: Resolve plugin (if needed)
  293. switch (getUpdatedState())
  294. {
  295. case ctkPlugin::INSTALLED:
  296. Q_ASSERT_X(resolveFailException != 0, Q_FUNC_INFO, "no resolveFailException");
  297. throw ctkPluginException(*resolveFailException);
  298. case ctkPlugin::STARTING:
  299. if (operation.fetchAndAddOrdered(0) == ACTIVATING) return; // finalization already in progress.
  300. // Lazy activation; fall through to RESOLVED.
  301. case ctkPlugin::RESOLVED:
  302. {
  303. //6:
  304. state = ctkPlugin::STARTING;
  305. operation.fetchAndStoreOrdered(ACTIVATING);
  306. if (fwCtx->debug.lazy_activation)
  307. {
  308. qDebug() << "activating #" << this->id;
  309. }
  310. //7:
  311. if (!pluginContext)
  312. {
  313. pluginContext.reset(new ctkPluginContext(this));
  314. }
  315. // start dependencies
  316. startDependencies();
  317. //TODO plugin threading
  318. //ctkRuntimeException* e = bundleThread().callStart0(this);
  319. ctkRuntimeException* e = start0();
  320. operation.fetchAndStoreOrdered(IDLE);
  321. operationLock.wakeAll();
  322. if (e)
  323. {
  324. ctkRuntimeException re(*e);
  325. delete e;
  326. throw re;
  327. }
  328. break;
  329. }
  330. case ctkPlugin::ACTIVE:
  331. break;
  332. case ctkPlugin::STOPPING:
  333. // This happens if start is called from inside the ctkPluginActivator::stop method.
  334. // Don't allow it.
  335. throw ctkPluginException("start called from ctkPluginActivator::stop",
  336. ctkPluginException::ACTIVATOR_ERROR);
  337. case ctkPlugin::UNINSTALLED:
  338. throw ctkIllegalStateException("ctkPlugin is in UNINSTALLED state");
  339. }
  340. }
  341. //----------------------------------------------------------------------------
  342. const ctkRuntimeException* ctkPluginPrivate::stop0()
  343. {
  344. wasStarted = state == ctkPlugin::ACTIVE;
  345. // 5:
  346. state = ctkPlugin::STOPPING;
  347. operation.fetchAndStoreOrdered(DEACTIVATING);
  348. // 6-13:
  349. // TODO plugin threading
  350. //const ctkRuntimeException* savedException = pluginThread().callStop1(this);
  351. const ctkRuntimeException* savedException = stop1();
  352. if (state != ctkPlugin::UNINSTALLED)
  353. {
  354. state = ctkPlugin::RESOLVED;
  355. // TODO plugin threading
  356. //bundleThread().bundleChanged(new BundleEvent(BundleEvent.STOPPED, this));
  357. fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::STOPPED, this->q_func()));
  358. operationLock.wakeAll();
  359. operation.fetchAndStoreOrdered(IDLE);
  360. }
  361. return savedException;
  362. }
  363. //----------------------------------------------------------------------------
  364. const ctkRuntimeException* ctkPluginPrivate::stop1()
  365. {
  366. const ctkRuntimeException* res = 0;
  367. //6:
  368. fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::STOPPING, q_func()));
  369. //7:
  370. if (wasStarted && pluginActivator)
  371. {
  372. try
  373. {
  374. pluginActivator->stop(pluginContext.data());
  375. if (state != ctkPlugin::STOPPING)
  376. {
  377. if (state == ctkPlugin::UNINSTALLED)
  378. {
  379. return new ctkIllegalStateException("Plug-in is uninstalled");
  380. }
  381. else
  382. {
  383. return new ctkIllegalStateException("Plug-in changed state because of refresh during stop");
  384. }
  385. }
  386. }
  387. catch (const std::exception& e)
  388. {
  389. res = new ctkPluginException("ctkPlugin::stop: PluginActivator stop failed",
  390. ctkPluginException::ACTIVATOR_ERROR, &e);
  391. }
  392. pluginActivator = 0;
  393. }
  394. if (operation.fetchAndAddOrdered(0) == DEACTIVATING)
  395. {
  396. // Call hooks after we've called PluginActivator::stop(), but before we've
  397. // cleared all resources
  398. if (pluginContext)
  399. {
  400. // TODO service listener hooks
  401. //fwCtx->listeners.serviceListeners.hooksBundleStopped(this);
  402. //8-10:
  403. removePluginResources();
  404. pluginContext->d_func()->invalidate();
  405. pluginContext.reset();
  406. }
  407. }
  408. // This would unload the shared library and delete the activator if
  409. // there are no dependencies. However, objects from the plug-in might
  410. // have been created via C-function symbol lookups. Hence we cannot
  411. // safely unload the DLL. Maybe implement a in-DLL counter later
  412. // (http://stackoverflow.com/questions/460809/c-dll-unloading-issue and
  413. // http://boost.2283326.n4.nabble.com/shared-ptr-A-smarter-smart-pointer-proposal-for-dynamic-libraries-td2649749.html).
  414. // The activator itself will be delete during program termination
  415. // (by the QPluginLoader instance).
  416. //pluginLoader.unload();
  417. return res;
  418. }
  419. //----------------------------------------------------------------------------
  420. void ctkPluginPrivate::waitOnOperation(LockObject* lock, const QString& src, bool longWait)
  421. {
  422. if (operation.fetchAndAddOrdered(0) != IDLE)
  423. {
  424. qint64 left = longWait ? 20000 : 500;
  425. QDateTime waitUntil = QDateTime::currentDateTime().addMSecs(left);
  426. do
  427. {
  428. lock->wait(left);
  429. if (operation.fetchAndAddOrdered(0) == IDLE)
  430. {
  431. return;
  432. }
  433. // TODO use Qt 4.7 QDateTime::msecsTo() API
  434. //left = QDateTime::currentDateTime().msecsTo(waitUntil);
  435. left = ctk::msecsTo(QDateTime::currentDateTime(), waitUntil);
  436. } while (left > 0);
  437. QString op;
  438. switch (operation.fetchAndAddOrdered(0))
  439. {
  440. case IDLE:
  441. // Should not happen!
  442. return;
  443. case ACTIVATING:
  444. op = "start";
  445. break;
  446. case DEACTIVATING:
  447. op = "stop";
  448. break;
  449. case RESOLVING:
  450. op = "resolve";
  451. break;
  452. case UNINSTALLING:
  453. op = "uninstall";
  454. break;
  455. case UNRESOLVING:
  456. op = "unresolve";
  457. break;
  458. case UPDATING:
  459. op = "update";
  460. break;
  461. default:
  462. op = "unknown operation";
  463. break;
  464. }
  465. throw ctkPluginException(src + " called during " + op + " of plug-in",
  466. ctkPluginException::STATECHANGE_ERROR);
  467. }
  468. }
  469. //----------------------------------------------------------------------------
  470. QStringList ctkPluginPrivate::findResourceEntries(const QString& path,
  471. const QString& pattern, bool recurse) const
  472. {
  473. QStringList result;
  474. QStringList resources = archive->findResourcesPath(path);
  475. foreach(QString fp, resources)
  476. {
  477. QString lastComponentOfPath = fp.section('/', -1);
  478. bool isDirectory = fp.endsWith("/");
  479. if (!isDirectory &&
  480. (pattern.isNull() || ctkPluginFrameworkUtil::filterMatch(pattern, lastComponentOfPath)))
  481. {
  482. result << (path + fp);
  483. }
  484. if (isDirectory && recurse)
  485. {
  486. QStringList subResources = findResourceEntries(fp, pattern, recurse);
  487. foreach (QString subResource, subResources)
  488. {
  489. result << (path + subResource);
  490. }
  491. }
  492. }
  493. return result;
  494. }
  495. //----------------------------------------------------------------------------
  496. void ctkPluginPrivate::startDependencies()
  497. {
  498. QListIterator<ctkRequirePlugin*> i(this->require);
  499. while (i.hasNext())
  500. {
  501. ctkRequirePlugin* pr = i.next();
  502. QList<ctkPlugin*> pl = fwCtx->plugins->getPlugins(pr->name, pr->pluginRange);
  503. if (pl.isEmpty())
  504. {
  505. if (pr->resolution == ctkPluginConstants::RESOLUTION_MANDATORY)
  506. {
  507. // We should never get here, since the plugin can only be
  508. // started if all its dependencies could be resolved.
  509. throw ctkPluginException(
  510. QString("Internal error: dependent plugin %1 inside version range %2 is not installed.").
  511. arg(pr->name).arg(pr->pluginRange.toString()));
  512. }
  513. else
  514. {
  515. continue;
  516. }
  517. }
  518. // We take the first plugin in the list (highest version number)
  519. // Immediately start the dependencies (no lazy activation) but do not
  520. // change the autostart setting of the plugin.
  521. pl.front()->start(ctkPlugin::START_TRANSIENT);
  522. }
  523. }
  524. //----------------------------------------------------------------------------
  525. ctkPluginException* ctkPluginPrivate::start0()
  526. {
  527. ctkPluginException* res = 0;
  528. fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::STARTING, this->q_func()));
  529. ctkPluginException::Type error_type = ctkPluginException::MANIFEST_ERROR;
  530. try {
  531. pluginLoader.load();
  532. if (!pluginLoader.isLoaded())
  533. {
  534. error_type = ctkPluginException::ACTIVATOR_ERROR;
  535. throw ctkPluginException(QString("Loading plugin %1 failed: %2").arg(pluginLoader.fileName()).arg(pluginLoader.errorString()),
  536. ctkPluginException::ACTIVATOR_ERROR);
  537. }
  538. pluginActivator = qobject_cast<ctkPluginActivator*>(pluginLoader.instance());
  539. if (!pluginActivator)
  540. {
  541. throw ctkPluginException(QString("Creating ctkPluginActivator instance from %1 failed: %2").arg(pluginLoader.fileName()).arg(pluginLoader.errorString()),
  542. ctkPluginException::ACTIVATOR_ERROR);
  543. }
  544. pluginActivator->start(pluginContext.data());
  545. if (state != ctkPlugin::STARTING)
  546. {
  547. error_type = ctkPluginException::STATECHANGE_ERROR;
  548. if (ctkPlugin::UNINSTALLED == state)
  549. {
  550. throw ctkPluginException("ctkPlugin uninstalled during start()", ctkPluginException::STATECHANGE_ERROR);
  551. }
  552. else
  553. {
  554. throw ctkPluginException("ctkPlugin changed state because of refresh during start()", ctkPluginException::STATECHANGE_ERROR);
  555. }
  556. }
  557. state = ctkPlugin::ACTIVE;
  558. }
  559. catch (const std::exception& e)
  560. {
  561. res = new ctkPluginException("ctkPlugin start failed", error_type, &e);
  562. }
  563. if (fwCtx->debug.lazy_activation)
  564. {
  565. qDebug() << "activating #" << id << "completed.";
  566. }
  567. if (res == 0)
  568. {
  569. //10:
  570. fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::STARTED, this->q_func()));
  571. }
  572. else if (operation.fetchAndAddOrdered(0) == ACTIVATING)
  573. {
  574. // 8:
  575. state = ctkPlugin::STOPPING;
  576. fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::STOPPING, this->q_func()));
  577. removePluginResources();
  578. pluginContext->d_func()->invalidate();
  579. pluginContext.reset();
  580. state = ctkPlugin::RESOLVED;
  581. fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::STOPPED, this->q_func()));
  582. }
  583. return res;
  584. }
  585. //----------------------------------------------------------------------------
  586. void ctkPluginPrivate::removePluginResources()
  587. {
  588. // automatic disconnect due to Qt signal slot
  589. //fwCtx->listeners.removeAllListeners(this);
  590. QList<ctkServiceRegistration> srs = fwCtx->services->getRegisteredByPlugin(this);
  591. QMutableListIterator<ctkServiceRegistration> i(srs);
  592. while (i.hasNext())
  593. {
  594. try
  595. {
  596. i.next().unregister();
  597. }
  598. catch (const std::logic_error& /*ignore*/)
  599. {
  600. // Someone has unregistered the service after stop completed.
  601. // This should not occur, but we don't want get stuck in
  602. // an illegal state so we catch it.
  603. }
  604. }
  605. QList<ctkServiceRegistration> s = fwCtx->services->getUsedByPlugin(q_func());
  606. QListIterator<ctkServiceRegistration> i2(s);
  607. while (i2.hasNext())
  608. {
  609. i2.next().getReference().d_func()->ungetService(q_func(), false);
  610. }
  611. }