ctkPlugin.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 "ctkPlugin.h"
  16. #include "ctkPluginContext.h"
  17. #include "ctkPluginContext_p.h"
  18. #include "ctkPluginFrameworkUtil_p.h"
  19. #include "ctkPluginPrivate_p.h"
  20. #include "ctkPluginArchive_p.h"
  21. #include "ctkPluginFrameworkContext_p.h"
  22. #include "ctkServices_p.h"
  23. #include "ctkUtils.h"
  24. #include <QStringList>
  25. //----------------------------------------------------------------------------
  26. ctkPlugin::ctkPlugin()
  27. : d_ptr(0)
  28. {
  29. }
  30. //----------------------------------------------------------------------------
  31. void ctkPlugin::init(ctkPluginPrivate* dd)
  32. {
  33. if (d_ptr) throw std::logic_error("ctkPlugin already initialized");
  34. d_ptr = dd;
  35. }
  36. //----------------------------------------------------------------------------
  37. void ctkPlugin::init(const QWeakPointer<ctkPlugin>& self,
  38. ctkPluginFrameworkContext* fw,
  39. ctkPluginArchive* pa)
  40. {
  41. if (d_ptr) throw std::logic_error("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::uninstall()
  142. {
  143. Q_D(ctkPlugin);
  144. {
  145. ctkPluginPrivate::Locker sync(&d->operationLock);
  146. if (d->archive)
  147. {
  148. try
  149. {
  150. d->archive->setStartLevel(-2); // Mark as uninstalled
  151. }
  152. catch (...)
  153. { }
  154. }
  155. switch (d->state)
  156. {
  157. case UNINSTALLED:
  158. throw ctkIllegalStateException("Plugin is in UNINSTALLED state");
  159. case STARTING: // Lazy start
  160. case ACTIVE:
  161. case STOPPING:
  162. {
  163. const ctkRuntimeException* exception = 0;
  164. try
  165. {
  166. d->waitOnOperation(&d->operationLock, "ctkPlugin::uninstall", true);
  167. if (d->state & (ACTIVE | STARTING))
  168. {
  169. exception = d->stop0();
  170. }
  171. }
  172. catch (const std::exception& e)
  173. {
  174. // Force to install
  175. d->setStateInstalled(false);
  176. d->operationLock.wakeAll();
  177. exception = new ctkRuntimeException("Stopping plug-in failed", &e);
  178. }
  179. d->operation.fetchAndStoreOrdered(ctkPluginPrivate::UNINSTALLING);
  180. if (exception != 0)
  181. {
  182. d->fwCtx->listeners.frameworkError(this->d_func()->q_func(), *exception);
  183. delete exception;
  184. }
  185. }
  186. // Fall through
  187. case RESOLVED:
  188. case INSTALLED:
  189. d->fwCtx->plugins->remove(d->location);
  190. if (d->operation.fetchAndAddOrdered(0) != ctkPluginPrivate::UNINSTALLING)
  191. {
  192. try
  193. {
  194. d->waitOnOperation(&d->operationLock, "Plugin::uninstall", true);
  195. d->operation.fetchAndStoreOrdered(ctkPluginPrivate::UNINSTALLING);
  196. }
  197. catch (const ctkPluginException& pe)
  198. {
  199. // Make sure that the context is invalid
  200. if (d->pluginContext != 0)
  201. {
  202. d->pluginContext->d_func()->invalidate();
  203. d->pluginContext.reset();
  204. }
  205. d->operation.fetchAndStoreOrdered(ctkPluginPrivate::UNINSTALLING);
  206. d->fwCtx->listeners.frameworkError(this->d_func()->q_func(), pe);
  207. }
  208. }
  209. d->state = INSTALLED;
  210. // TODO: use thread
  211. // bundleThread().bundleChanged(new BundleEvent(BundleEvent.UNRESOLVED, this));
  212. d->fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::UNRESOLVED, d->q_func()));
  213. d->cachedHeaders = getHeaders();
  214. d->pluginActivator = 0;
  215. d->state = UNINSTALLED;
  216. // Purge old archive
  217. if (d->archive)
  218. {
  219. d->archive->purge();
  220. }
  221. d->operation.fetchAndStoreOrdered(ctkPluginPrivate::IDLE);
  222. if (d->pluginDir.exists())
  223. {
  224. if (!ctk::removeDirRecursively(d->pluginDir.absolutePath()))
  225. {
  226. d->fwCtx->listeners.frameworkError(this->d_func()->q_func(), std::runtime_error("Failed to delete plugin data"));
  227. }
  228. d->pluginDir.setFile("");
  229. }
  230. // id, location and headers survive after uninstall.
  231. // There might be plug-in threads that are running start or stop
  232. // operations. This will wake them and give them a chance to terminate.
  233. d->operationLock.wakeAll();
  234. break;
  235. }
  236. }
  237. d->fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::UNINSTALLED, d->q_ptr));
  238. }
  239. //----------------------------------------------------------------------------
  240. ctkPluginContext* ctkPlugin::getPluginContext() const
  241. {
  242. //TODO security checks
  243. Q_D(const ctkPlugin);
  244. return d->pluginContext.data();
  245. }
  246. //----------------------------------------------------------------------------
  247. long ctkPlugin::getPluginId() const
  248. {
  249. Q_D(const ctkPlugin);
  250. return d->id;
  251. }
  252. //----------------------------------------------------------------------------
  253. QString ctkPlugin::getLocation() const
  254. {
  255. //TODO security
  256. Q_D(const ctkPlugin);
  257. return d->location;
  258. }
  259. //----------------------------------------------------------------------------
  260. QHash<QString, QString> ctkPlugin::getHeaders()
  261. {
  262. //TODO security
  263. Q_D(ctkPlugin);
  264. if (d->cachedRawHeaders.empty())
  265. {
  266. d->cachedRawHeaders = d->archive->getUnlocalizedAttributes();
  267. }
  268. if (d->state == UNINSTALLED)
  269. {
  270. return d->cachedHeaders;
  271. }
  272. //TODO use the embedded .qm files to localize header values
  273. return d->cachedRawHeaders;
  274. }
  275. //----------------------------------------------------------------------------
  276. QString ctkPlugin::getSymbolicName() const
  277. {
  278. Q_D(const ctkPlugin);
  279. return d->symbolicName;
  280. }
  281. //----------------------------------------------------------------------------
  282. QStringList ctkPlugin::getResourceList(const QString& path) const
  283. {
  284. Q_D(const ctkPlugin);
  285. return d->archive->findResourcesPath(path);
  286. }
  287. //----------------------------------------------------------------------------
  288. QStringList ctkPlugin::findResources(const QString& path,
  289. const QString& pattern, bool recurse) const
  290. {
  291. Q_D(const ctkPlugin);
  292. return d->findResourceEntries(path, pattern, recurse);
  293. }
  294. //----------------------------------------------------------------------------
  295. QByteArray ctkPlugin::getResource(const QString& path) const
  296. {
  297. Q_D(const ctkPlugin);
  298. return d->archive->getPluginResource(path);
  299. }
  300. //----------------------------------------------------------------------------
  301. ctkPluginLocalization ctkPlugin::getPluginLocalization(
  302. const QLocale& locale, const QString& base) const
  303. {
  304. Q_D(const ctkPlugin);
  305. // There are five searching candidates possible:
  306. // base +
  307. // "_" + language1 + "_" + country1 + ".qm"
  308. // or "_" + language1 + ".qm"
  309. // or "_" + language2 + "_" + country2 + ".qm"
  310. // or "_" + language2 + ".qm"
  311. // or "" + ".qm"
  312. //
  313. // Where language1[_country1[_variation1]] is the requested locale,
  314. // and language2[_country2[_variation2]] is the default locale.
  315. QChar localeSep('_');
  316. QChar baseSep('_');
  317. QStringList searchCandidates;
  318. QStringList localeParts = locale.name().split(localeSep);
  319. QStringList defaultParts = QLocale().name().split(localeSep);
  320. searchCandidates << baseSep + localeParts[0] + localeSep + localeParts[1];
  321. searchCandidates << baseSep + localeParts[0];
  322. searchCandidates << baseSep + defaultParts[0] + localeSep + defaultParts[1];
  323. searchCandidates << baseSep + defaultParts[0];
  324. searchCandidates << "";
  325. QString localizationPath = base.left(base.lastIndexOf('/'));
  326. QStringList resourceList = this->getResourceList(localizationPath);
  327. foreach(QString resource, resourceList)
  328. {
  329. foreach(QString candidate, searchCandidates)
  330. {
  331. if (resource.endsWith(candidate + ".qm"))
  332. {
  333. return ctkPluginLocalization(localizationPath + '/' + resource, locale, d->q_ptr);
  334. }
  335. }
  336. }
  337. return ctkPluginLocalization();
  338. }
  339. //----------------------------------------------------------------------------
  340. ctkVersion ctkPlugin::getVersion() const
  341. {
  342. Q_D(const ctkPlugin);
  343. return d->version;
  344. }
  345. //----------------------------------------------------------------------------
  346. QDebug operator<<(QDebug debug, ctkPlugin::State state)
  347. {
  348. switch (state)
  349. {
  350. case ctkPlugin::UNINSTALLED:
  351. return debug << "UNINSTALLED";
  352. case ctkPlugin::INSTALLED:
  353. return debug << "INSTALLED";
  354. case ctkPlugin::RESOLVED:
  355. return debug << "RESOLVED";
  356. case ctkPlugin::STARTING:
  357. return debug << "STARTING";
  358. case ctkPlugin::STOPPING:
  359. return debug << "STOPPING";
  360. case ctkPlugin::ACTIVE:
  361. return debug << "ACTIVE";
  362. default:
  363. return debug << "unknown";
  364. }
  365. }
  366. //----------------------------------------------------------------------------
  367. QDebug operator<<(QDebug debug, const ctkPlugin& plugin)
  368. {
  369. debug.nospace() << "ctkPlugin[" << "id=" << plugin.getPluginId() <<
  370. ", state=" << plugin.getState() << ", loc=" << plugin.getLocation() <<
  371. ", symName=" << plugin.getSymbolicName() << "]";
  372. return debug.maybeSpace();
  373. }
  374. //----------------------------------------------------------------------------
  375. QDebug operator<<(QDebug debug, ctkPlugin const * plugin)
  376. {
  377. return operator<<(debug, *plugin);
  378. }
  379. //----------------------------------------------------------------------------
  380. ctkLogStream& operator<<(ctkLogStream& stream, ctkPlugin const * plugin)
  381. {
  382. stream << plugin->getSymbolicName();
  383. return stream;
  384. }
  385. //----------------------------------------------------------------------------
  386. ctkLogStream& operator<<(ctkLogStream& stream, const QSharedPointer<ctkPlugin>& plugin)
  387. {
  388. return operator<<(stream, plugin.data());
  389. }