ctkPlugin.cpp 12 KB

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