ctkPlugin.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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 <QStringList>
  16. #include "ctkPlugin.h"
  17. #include "ctkPlugin_p.h"
  18. #include "ctkPluginContext.h"
  19. #include "ctkPluginContext_p.h"
  20. #include "ctkPluginFrameworkUtil_p.h"
  21. #include "ctkPluginArchive_p.h"
  22. #include "ctkPluginFrameworkContext_p.h"
  23. #include "ctkServices_p.h"
  24. #include "ctkUtils.h"
  25. //----------------------------------------------------------------------------
  26. ctkPlugin::ctkPlugin()
  27. : d_ptr(0)
  28. {
  29. }
  30. //----------------------------------------------------------------------------
  31. void ctkPlugin::init(ctkPluginPrivate* dd)
  32. {
  33. if (d_ptr) throw ctkIllegalStateException("ctkPlugin already initialized");
  34. d_ptr = dd;
  35. }
  36. //----------------------------------------------------------------------------
  37. void ctkPlugin::init(const QWeakPointer<ctkPlugin>& self,
  38. ctkPluginFrameworkContext* fw,
  39. QSharedPointer<ctkPluginArchive> pa)
  40. {
  41. if (d_ptr) throw ctkIllegalStateException("ctkPlugin already initialized");
  42. d_ptr = new ctkPluginPrivate(self, fw, pa);
  43. }
  44. //----------------------------------------------------------------------------
  45. ctkPlugin::~ctkPlugin()
  46. {
  47. delete d_ptr;
  48. }
  49. //----------------------------------------------------------------------------
  50. ctkPlugin::State ctkPlugin::getState() const
  51. {
  52. Q_D(const ctkPlugin);
  53. return d->state;
  54. }
  55. //----------------------------------------------------------------------------
  56. void ctkPlugin::start(const StartOptions& options)
  57. {
  58. Q_D(ctkPlugin);
  59. if (d->state == UNINSTALLED)
  60. {
  61. throw ctkIllegalStateException("ctkPlugin is uninstalled");
  62. }
  63. // Initialize the activation; checks initialization of lazy
  64. // activation.
  65. //TODO 1: If activating or deactivating, wait a litle
  66. // we don't use mutliple threads to start plugins for now
  67. //waitOnActivation(lock, "ctkPlugin::start", false);
  68. //2: start() is idempotent, i.e., nothing to do when already started
  69. if (d->state == ACTIVE)
  70. {
  71. return;
  72. }
  73. //3: Record non-transient start requests.
  74. if ((options & START_TRANSIENT) == 0)
  75. {
  76. d->setAutostartSetting(options);
  77. }
  78. //4: Resolve plugin (if needed)
  79. d->getUpdatedState();
  80. //5: Eager?
  81. if ((options & START_ACTIVATION_POLICY) && !d->eagerActivation )
  82. {
  83. if (STARTING == d->state) return;
  84. d->state = STARTING;
  85. d->pluginContext.reset(new ctkPluginContext(this->d_func()));
  86. ctkPluginEvent pluginEvent(ctkPluginEvent::LAZY_ACTIVATION, d->q_ptr);
  87. d->fwCtx->listeners.emitPluginChanged(pluginEvent);
  88. }
  89. else
  90. {
  91. d->finalizeActivation();
  92. }
  93. }
  94. //----------------------------------------------------------------------------
  95. void ctkPlugin::stop(const StopOptions& options)
  96. {
  97. Q_D(ctkPlugin);
  98. const ctkRuntimeException* savedException = 0;
  99. //1:
  100. if (d->state == UNINSTALLED)
  101. {
  102. throw ctkIllegalStateException("ctkPlugin is uninstalled");
  103. }
  104. // 2: If an operation is in progress, wait a little
  105. d->waitOnOperation(&d->operationLock, "Plugin::stop", false);
  106. //3:
  107. if ((options & STOP_TRANSIENT) == 0)
  108. {
  109. d->ignoreAutostartSetting();
  110. }
  111. switch (d->state)
  112. {
  113. case INSTALLED:
  114. case RESOLVED:
  115. case STOPPING:
  116. case UNINSTALLED:
  117. //4:
  118. return;
  119. case ACTIVE:
  120. case STARTING: // Lazy start...
  121. savedException = d->stop0();
  122. break;
  123. };
  124. if (savedException != 0)
  125. {
  126. if (const ctkPluginException* pluginExc = dynamic_cast<const ctkPluginException*>(savedException))
  127. {
  128. ctkPluginException pe(*pluginExc);
  129. delete savedException;
  130. throw pe;
  131. }
  132. else
  133. {
  134. ctkRuntimeException re(*savedException);
  135. delete savedException;
  136. throw re;
  137. }
  138. }
  139. }
  140. //----------------------------------------------------------------------------
  141. void ctkPlugin::update(const QUrl& updateLocation)
  142. {
  143. Q_D(ctkPlugin);
  144. ctkPluginPrivate::Locker sync(&d->operationLock);
  145. const bool wasActive = d->state == ACTIVE;
  146. switch (d->getUpdatedState_unlocked())
  147. {
  148. case ACTIVE:
  149. stop(STOP_TRANSIENT);
  150. // Fall through
  151. case RESOLVED:
  152. case INSTALLED:
  153. // Load new plugin
  154. //secure.callUpdate0(this, in, wasActive);
  155. d->update0(updateLocation, wasActive);
  156. break;
  157. case STARTING:
  158. // Wait for RUNNING state, this doesn't happen now
  159. // since we are synchronized.
  160. throw ctkIllegalStateException("Plugin is in STARTING state");
  161. case STOPPING:
  162. // Wait for RESOLVED state, this doesn't happen now
  163. // since we are synchronized.
  164. throw ctkIllegalStateException("Plugin is in STOPPING state");
  165. case UNINSTALLED:
  166. throw ctkIllegalStateException("Plugin is in UNINSTALLED state");
  167. }
  168. }
  169. //----------------------------------------------------------------------------
  170. void ctkPlugin::uninstall()
  171. {
  172. Q_D(ctkPlugin);
  173. {
  174. ctkPluginPrivate::Locker sync(&d->operationLock);
  175. if (d->archive)
  176. {
  177. try
  178. {
  179. d->archive->setStartLevel(-2); // Mark as uninstalled
  180. }
  181. catch (...)
  182. { }
  183. }
  184. switch (d->state)
  185. {
  186. case UNINSTALLED:
  187. throw ctkIllegalStateException("Plugin is in UNINSTALLED state");
  188. case STARTING: // Lazy start
  189. case ACTIVE:
  190. case STOPPING:
  191. {
  192. const ctkRuntimeException* exception = 0;
  193. try
  194. {
  195. d->waitOnOperation(&d->operationLock, "ctkPlugin::uninstall", true);
  196. if (d->state & (ACTIVE | STARTING))
  197. {
  198. exception = d->stop0();
  199. }
  200. }
  201. catch (const ctkException& e)
  202. {
  203. // Force to install
  204. d->setStateInstalled(false);
  205. d->operationLock.wakeAll();
  206. exception = new ctkRuntimeException("Stopping plug-in failed", e);
  207. }
  208. d->operation.fetchAndStoreOrdered(ctkPluginPrivate::UNINSTALLING);
  209. if (exception != 0)
  210. {
  211. d->fwCtx->listeners.frameworkError(this->d_func()->q_func(), *exception);
  212. delete exception;
  213. }
  214. }
  215. // Fall through
  216. case RESOLVED:
  217. case INSTALLED:
  218. d->fwCtx->plugins->remove(d->location);
  219. if (d->operation.fetchAndAddOrdered(0) != ctkPluginPrivate::UNINSTALLING)
  220. {
  221. try
  222. {
  223. d->waitOnOperation(&d->operationLock, "Plugin::uninstall", true);
  224. d->operation.fetchAndStoreOrdered(ctkPluginPrivate::UNINSTALLING);
  225. }
  226. catch (const ctkPluginException& pe)
  227. {
  228. // Make sure that the context is invalid
  229. if (d->pluginContext != 0)
  230. {
  231. d->pluginContext->d_func()->invalidate();
  232. d->pluginContext.reset();
  233. }
  234. d->operation.fetchAndStoreOrdered(ctkPluginPrivate::UNINSTALLING);
  235. d->fwCtx->listeners.frameworkError(this->d_func()->q_func(), pe);
  236. }
  237. }
  238. d->state = INSTALLED;
  239. // TODO: use thread
  240. // bundleThread().bundleChanged(new BundleEvent(BundleEvent.UNRESOLVED, this));
  241. d->fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::UNRESOLVED, d->q_func()));
  242. d->cachedHeaders = getHeaders();
  243. d->pluginActivator = 0;
  244. d->state = UNINSTALLED;
  245. // Purge old archive
  246. if (d->archive)
  247. {
  248. d->archive->purge();
  249. }
  250. d->operation.fetchAndStoreOrdered(ctkPluginPrivate::IDLE);
  251. if (d->pluginDir.exists())
  252. {
  253. if (!ctk::removeDirRecursively(d->pluginDir.absolutePath()))
  254. {
  255. d->fwCtx->listeners.frameworkError(this->d_func()->q_func(), ctkRuntimeException("Failed to delete plugin data"));
  256. }
  257. d->pluginDir.setFile("");
  258. }
  259. // id, location and headers survive after uninstall.
  260. // There might be plug-in threads that are running start or stop
  261. // operations. This will wake them and give them a chance to terminate.
  262. d->operationLock.wakeAll();
  263. break;
  264. }
  265. }
  266. d->fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::UNINSTALLED, d->q_ptr));
  267. }
  268. //----------------------------------------------------------------------------
  269. ctkPluginContext* ctkPlugin::getPluginContext() const
  270. {
  271. //TODO security checks
  272. Q_D(const ctkPlugin);
  273. return d->pluginContext.data();
  274. }
  275. //----------------------------------------------------------------------------
  276. long ctkPlugin::getPluginId() const
  277. {
  278. Q_D(const ctkPlugin);
  279. return d->id;
  280. }
  281. //----------------------------------------------------------------------------
  282. QString ctkPlugin::getLocation() const
  283. {
  284. //TODO security
  285. Q_D(const ctkPlugin);
  286. return d->location;
  287. }
  288. //----------------------------------------------------------------------------
  289. QHash<QString, QString> ctkPlugin::getHeaders()
  290. {
  291. //TODO security
  292. Q_D(ctkPlugin);
  293. if (d->cachedRawHeaders.empty())
  294. {
  295. d->cachedRawHeaders = d->archive->getUnlocalizedAttributes();
  296. }
  297. if (d->state == UNINSTALLED)
  298. {
  299. return d->cachedHeaders;
  300. }
  301. //TODO use the embedded .qm files to localize header values
  302. return d->cachedRawHeaders;
  303. }
  304. //----------------------------------------------------------------------------
  305. QString ctkPlugin::getSymbolicName() const
  306. {
  307. Q_D(const ctkPlugin);
  308. return d->symbolicName;
  309. }
  310. //----------------------------------------------------------------------------
  311. QStringList ctkPlugin::getResourceList(const QString& path) const
  312. {
  313. Q_D(const ctkPlugin);
  314. return d->archive->findResourcesPath(path);
  315. }
  316. //----------------------------------------------------------------------------
  317. QStringList ctkPlugin::findResources(const QString& path,
  318. const QString& pattern, bool recurse) const
  319. {
  320. Q_D(const ctkPlugin);
  321. return d->findResourceEntries(path, pattern, recurse);
  322. }
  323. //----------------------------------------------------------------------------
  324. QByteArray ctkPlugin::getResource(const QString& path) const
  325. {
  326. Q_D(const ctkPlugin);
  327. return d->archive->getPluginResource(path);
  328. }
  329. //----------------------------------------------------------------------------
  330. ctkPluginLocalization ctkPlugin::getPluginLocalization(
  331. const QLocale& locale, const QString& base) const
  332. {
  333. Q_D(const ctkPlugin);
  334. // There are five searching candidates possible:
  335. // base +
  336. // "_" + language1 + "_" + country1 + ".qm"
  337. // or "_" + language1 + ".qm"
  338. // or "_" + language2 + "_" + country2 + ".qm"
  339. // or "_" + language2 + ".qm"
  340. // or "" + ".qm"
  341. //
  342. // Where language1[_country1[_variation1]] is the requested locale,
  343. // and language2[_country2[_variation2]] is the default locale.
  344. QChar localeSep('_');
  345. QChar baseSep('_');
  346. QStringList searchCandidates;
  347. QStringList localeParts = locale.name().split(localeSep);
  348. QStringList defaultParts = QLocale().name().split(localeSep);
  349. searchCandidates << baseSep + localeParts[0] + localeSep + localeParts[1];
  350. searchCandidates << baseSep + localeParts[0];
  351. searchCandidates << baseSep + defaultParts[0] + localeSep + defaultParts[1];
  352. searchCandidates << baseSep + defaultParts[0];
  353. searchCandidates << "";
  354. QString localizationPath = base.left(base.lastIndexOf('/'));
  355. QStringList resourceList = this->getResourceList(localizationPath);
  356. foreach(QString resource, resourceList)
  357. {
  358. foreach(QString candidate, searchCandidates)
  359. {
  360. if (resource.endsWith(candidate + ".qm"))
  361. {
  362. return ctkPluginLocalization(localizationPath + '/' + resource, locale, d->q_ptr);
  363. }
  364. }
  365. }
  366. return ctkPluginLocalization();
  367. }
  368. //----------------------------------------------------------------------------
  369. ctkVersion ctkPlugin::getVersion() const
  370. {
  371. Q_D(const ctkPlugin);
  372. return d->version;
  373. }
  374. //----------------------------------------------------------------------------
  375. QDebug operator<<(QDebug debug, ctkPlugin::State state)
  376. {
  377. switch (state)
  378. {
  379. case ctkPlugin::UNINSTALLED:
  380. return debug << "UNINSTALLED";
  381. case ctkPlugin::INSTALLED:
  382. return debug << "INSTALLED";
  383. case ctkPlugin::RESOLVED:
  384. return debug << "RESOLVED";
  385. case ctkPlugin::STARTING:
  386. return debug << "STARTING";
  387. case ctkPlugin::STOPPING:
  388. return debug << "STOPPING";
  389. case ctkPlugin::ACTIVE:
  390. return debug << "ACTIVE";
  391. default:
  392. return debug << "unknown";
  393. }
  394. }
  395. //----------------------------------------------------------------------------
  396. QDebug operator<<(QDebug debug, const ctkPlugin& plugin)
  397. {
  398. debug.nospace() << "ctkPlugin[" << "id=" << plugin.getPluginId() <<
  399. ", state=" << plugin.getState() << ", loc=" << plugin.getLocation() <<
  400. ", symName=" << plugin.getSymbolicName() << "]";
  401. return debug.maybeSpace();
  402. }
  403. //----------------------------------------------------------------------------
  404. QDebug operator<<(QDebug debug, ctkPlugin const * plugin)
  405. {
  406. return operator<<(debug, *plugin);
  407. }
  408. //----------------------------------------------------------------------------
  409. ctkLogStream& operator<<(ctkLogStream& stream, ctkPlugin const * plugin)
  410. {
  411. stream << plugin->getSymbolicName();
  412. return stream;
  413. }
  414. //----------------------------------------------------------------------------
  415. ctkLogStream& operator<<(ctkLogStream& stream, const QSharedPointer<ctkPlugin>& plugin)
  416. {
  417. return operator<<(stream, plugin.data());
  418. }