ctkPlugin.cpp 12 KB

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