ctkPlugin.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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. ctkPlugin::ctkPlugin()
  24. : d_ptr(0)
  25. {
  26. }
  27. void ctkPlugin::init(ctkPluginPrivate* dd)
  28. {
  29. if (d_ptr) throw std::logic_error("ctkPlugin already initialized");
  30. d_ptr = dd;
  31. }
  32. void ctkPlugin::init(const QWeakPointer<ctkPlugin>& self,
  33. ctkPluginFrameworkContext* fw,
  34. ctkPluginArchive* pa)
  35. {
  36. if (d_ptr) throw std::logic_error("ctkPlugin already initialized");
  37. d_ptr = new ctkPluginPrivate(self, fw, pa);
  38. }
  39. ctkPlugin::~ctkPlugin()
  40. {
  41. delete d_ptr;
  42. }
  43. ctkPlugin::State ctkPlugin::getState() const
  44. {
  45. Q_D(const ctkPlugin);
  46. return d->state;
  47. }
  48. void ctkPlugin::start(const StartOptions& options)
  49. {
  50. Q_D(ctkPlugin);
  51. if (d->state == UNINSTALLED)
  52. {
  53. throw std::logic_error("ctkPlugin is uninstalled");
  54. }
  55. // Initialize the activation; checks initialization of lazy
  56. // activation.
  57. //TODO 1: If activating or deactivating, wait a litle
  58. // we don't use mutliple threads to start plugins for now
  59. //waitOnActivation(lock, "ctkPlugin::start", false);
  60. //2: start() is idempotent, i.e., nothing to do when already started
  61. if (d->state == ACTIVE)
  62. {
  63. return;
  64. }
  65. //3: Record non-transient start requests.
  66. if ((options & START_TRANSIENT) == 0)
  67. {
  68. d->setAutostartSetting(options);
  69. }
  70. //4: Resolve plugin (if needed)
  71. d->getUpdatedState();
  72. //5: Eager?
  73. if ((options & START_ACTIVATION_POLICY) && !d->eagerActivation )
  74. {
  75. if (STARTING == d->state) return;
  76. d->state = STARTING;
  77. d->pluginContext.reset(new ctkPluginContext(this->d_func()));
  78. ctkPluginEvent pluginEvent(ctkPluginEvent::LAZY_ACTIVATION, d->q_ptr);
  79. d->fwCtx->listeners.emitPluginChanged(pluginEvent);
  80. }
  81. else
  82. {
  83. d->finalizeActivation();
  84. }
  85. }
  86. void ctkPlugin::stop(const StopOptions& options)
  87. {
  88. Q_D(ctkPlugin);
  89. const std::exception* savedException = 0;
  90. //1:
  91. if (d->state == UNINSTALLED)
  92. {
  93. throw std::logic_error("Plugin is uninstalled");
  94. }
  95. //2: If activating or deactivating, wait a litle
  96. // We don't support threaded start/stop methods, so we don't need to wait
  97. //waitOnActivation(fwCtx.packages, "Plugin::stop", false);
  98. //3:
  99. if ((options & STOP_TRANSIENT) == 0)
  100. {
  101. d->ignoreAutostartSetting();
  102. }
  103. bool wasStarted = false;
  104. switch (d->state)
  105. {
  106. case INSTALLED:
  107. case RESOLVED:
  108. case STOPPING:
  109. case UNINSTALLED:
  110. //4:
  111. return;
  112. case ACTIVE:
  113. wasStarted = true;
  114. case STARTING: // Lazy start...
  115. try
  116. {
  117. d->stop0(wasStarted);
  118. }
  119. catch (const std::exception* exc)
  120. {
  121. savedException = exc;
  122. }
  123. break;
  124. };
  125. if (d->state != UNINSTALLED)
  126. {
  127. d->fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::STOPPED, d->q_ptr));
  128. }
  129. if (savedException)
  130. {
  131. if (const ctkPluginException* pluginExc = dynamic_cast<const ctkPluginException*>(savedException))
  132. {
  133. throw pluginExc;
  134. }
  135. else
  136. {
  137. throw dynamic_cast<const std::logic_error*>(savedException);
  138. }
  139. }
  140. }
  141. void ctkPlugin::uninstall()
  142. {
  143. bool wasResolved = false;
  144. Q_D(ctkPlugin);
  145. if (d->archive)
  146. {
  147. try
  148. {
  149. d->archive->setStartLevel(-2); // Mark as uninstalled
  150. }
  151. catch (...)
  152. { }
  153. }
  154. d->cachedHeaders = getHeaders();
  155. switch (d->state)
  156. {
  157. case UNINSTALLED:
  158. throw std::logic_error("Plugin is in UNINSTALLED state");
  159. case STARTING: // Lazy start
  160. case ACTIVE:
  161. case STOPPING:
  162. try
  163. {
  164. //TODO: If activating or deactivating, wait a litle
  165. // we don't use mutliple threads to start plugins for now
  166. //d->waitOnActivation(fwCtx.packages, "Bundle.uninstall", false);
  167. if (d->state & (ACTIVE | STARTING))
  168. {
  169. try
  170. {
  171. d->stop0(d->state == ACTIVE);
  172. }
  173. catch (const std::exception& exception)
  174. {
  175. // NYI! not call inside lock
  176. d->fwCtx->listeners.frameworkError(this, exception);
  177. }
  178. }
  179. }
  180. catch (const std::exception& e)
  181. {
  182. d->deactivating = false;
  183. //fwCtx.packages.notifyAll();
  184. d->fwCtx->listeners.frameworkError(this, e);
  185. }
  186. // Fall through
  187. case RESOLVED:
  188. wasResolved = true;
  189. // Fall through
  190. case INSTALLED:
  191. d->fwCtx->plugins->remove(d->location);
  192. d->pluginActivator = 0;
  193. if (d->pluginDir.exists())
  194. {
  195. if (!ctkPluginFrameworkUtil::removeDir(d->pluginDir.absolutePath()))
  196. {
  197. // Plugin dir is not deleted completely, make sure we mark
  198. // it as uninstalled for next framework restart
  199. if (d->archive)
  200. {
  201. try
  202. {
  203. d->archive->setStartLevel(-2); // Mark as uninstalled
  204. }
  205. catch (const std::exception& e)
  206. {
  207. // NYI! Generate FrameworkError if dir still exists!?
  208. qDebug() << "Failed to mark plugin" << d->id
  209. << "as uninstalled," << d->pluginDir.absoluteFilePath()
  210. << "must be deleted manually:" << e.what();
  211. }
  212. }
  213. }
  214. d->pluginDir.setFile("");
  215. }
  216. if (d->archive)
  217. {
  218. d->archive->purge();
  219. }
  220. // id, location and headers survive after uninstall.
  221. // TODO: UNRESOLVED must be sent out during installed state
  222. // This needs to be reviewed. See OSGi bug #1374
  223. d->state = INSTALLED;
  224. d->modified();
  225. // Broadcast events
  226. if (wasResolved)
  227. {
  228. d->fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::UNRESOLVED, d->q_ptr));
  229. }
  230. d->state = UNINSTALLED;
  231. d->fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::UNINSTALLED, d->q_ptr));
  232. break;
  233. }
  234. }
  235. ctkPluginContext* ctkPlugin::getPluginContext() const
  236. {
  237. //TODO security checks
  238. Q_D(const ctkPlugin);
  239. return d->pluginContext.data();
  240. }
  241. long ctkPlugin::getPluginId() const
  242. {
  243. Q_D(const ctkPlugin);
  244. return d->id;
  245. }
  246. QString ctkPlugin::getLocation() const
  247. {
  248. //TODO security
  249. Q_D(const ctkPlugin);
  250. return d->location;
  251. }
  252. QHash<QString, QString> ctkPlugin::getHeaders()
  253. {
  254. //TODO security
  255. Q_D(ctkPlugin);
  256. if (d->cachedRawHeaders.empty())
  257. {
  258. d->cachedRawHeaders = d->archive->getUnlocalizedAttributes();
  259. }
  260. if (d->state == UNINSTALLED)
  261. {
  262. return d->cachedHeaders;
  263. }
  264. //TODO use the embedded .qm files to localize header values
  265. return d->cachedRawHeaders;
  266. }
  267. QString ctkPlugin::getSymbolicName() const
  268. {
  269. Q_D(const ctkPlugin);
  270. return d->symbolicName;
  271. }
  272. QStringList ctkPlugin::getResourceList(const QString& path) const
  273. {
  274. Q_D(const ctkPlugin);
  275. return d->archive->findResourcesPath(path);
  276. }
  277. QByteArray ctkPlugin::getResource(const QString& path) const
  278. {
  279. Q_D(const ctkPlugin);
  280. return d->archive->getPluginResource(path);
  281. }
  282. ctkVersion ctkPlugin::getVersion() const
  283. {
  284. Q_D(const ctkPlugin);
  285. return d->version;
  286. }
  287. QDebug operator<<(QDebug debug, ctkPlugin::State state)
  288. {
  289. switch (state)
  290. {
  291. case ctkPlugin::UNINSTALLED:
  292. return debug << "UNINSTALLED";
  293. case ctkPlugin::INSTALLED:
  294. return debug << "INSTALLED";
  295. case ctkPlugin::RESOLVED:
  296. return debug << "RESOLVED";
  297. case ctkPlugin::STARTING:
  298. return debug << "STARTING";
  299. case ctkPlugin::STOPPING:
  300. return debug << "STOPPING";
  301. case ctkPlugin::ACTIVE:
  302. return debug << "ACTIVE";
  303. default:
  304. return debug << "unknown";
  305. }
  306. }
  307. QDebug operator<<(QDebug debug, const ctkPlugin& plugin)
  308. {
  309. debug.nospace() << "ctkPlugin[" << "id=" << plugin.getPluginId() <<
  310. ", state=" << plugin.getState() << ", loc=" << plugin.getLocation() <<
  311. ", symName=" << plugin.getSymbolicName() << "]";
  312. return debug.maybeSpace();
  313. }
  314. QDebug operator<<(QDebug debug, ctkPlugin const * plugin)
  315. {
  316. return operator<<(debug, *plugin);
  317. }