ctkPlugin.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 (savedException)
  126. {
  127. if (const ctkPluginException* pluginExc = dynamic_cast<const ctkPluginException*>(savedException))
  128. {
  129. throw pluginExc;
  130. }
  131. else
  132. {
  133. throw dynamic_cast<const std::logic_error*>(savedException);
  134. }
  135. }
  136. }
  137. void ctkPlugin::uninstall()
  138. {
  139. bool wasResolved = false;
  140. Q_D(ctkPlugin);
  141. if (d->archive)
  142. {
  143. try
  144. {
  145. d->archive->setStartLevel(-2); // Mark as uninstalled
  146. }
  147. catch (...)
  148. { }
  149. }
  150. d->cachedHeaders = getHeaders();
  151. switch (d->state)
  152. {
  153. case UNINSTALLED:
  154. throw std::logic_error("Plugin is in UNINSTALLED state");
  155. case STARTING: // Lazy start
  156. case ACTIVE:
  157. case STOPPING:
  158. try
  159. {
  160. //TODO: If activating or deactivating, wait a litle
  161. // we don't use mutliple threads to start plugins for now
  162. //d->waitOnActivation(fwCtx.packages, "Bundle.uninstall", false);
  163. if (d->state & (ACTIVE | STARTING))
  164. {
  165. try
  166. {
  167. d->stop0(d->state == ACTIVE);
  168. }
  169. catch (const std::exception& exception)
  170. {
  171. // NYI! not call inside lock
  172. d->fwCtx->listeners.frameworkError(this, exception);
  173. }
  174. }
  175. }
  176. catch (const std::exception& e)
  177. {
  178. d->deactivating = false;
  179. //fwCtx.packages.notifyAll();
  180. d->fwCtx->listeners.frameworkError(this, e);
  181. }
  182. // Fall through
  183. case RESOLVED:
  184. wasResolved = true;
  185. // Fall through
  186. case INSTALLED:
  187. d->fwCtx->plugins->remove(d->location);
  188. d->pluginActivator = 0;
  189. if (d->pluginDir.exists())
  190. {
  191. if (!ctkPluginFrameworkUtil::removeDir(d->pluginDir.absolutePath()))
  192. {
  193. // Plugin dir is not deleted completely, make sure we mark
  194. // it as uninstalled for next framework restart
  195. if (d->archive)
  196. {
  197. try
  198. {
  199. d->archive->setStartLevel(-2); // Mark as uninstalled
  200. }
  201. catch (const std::exception& e)
  202. {
  203. // NYI! Generate FrameworkError if dir still exists!?
  204. qDebug() << "Failed to mark plugin" << d->id
  205. << "as uninstalled," << d->pluginDir.absoluteFilePath()
  206. << "must be deleted manually:" << e.what();
  207. }
  208. }
  209. }
  210. d->pluginDir.setFile("");
  211. }
  212. if (d->archive)
  213. {
  214. d->archive->purge();
  215. }
  216. // id, location and headers survive after uninstall.
  217. // TODO: UNRESOLVED must be sent out during installed state
  218. // This needs to be reviewed. See OSGi bug #1374
  219. d->state = INSTALLED;
  220. d->modified();
  221. // Broadcast events
  222. if (wasResolved)
  223. {
  224. d->fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::UNRESOLVED, d->q_ptr));
  225. }
  226. d->state = UNINSTALLED;
  227. d->fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::UNINSTALLED, d->q_ptr));
  228. break;
  229. }
  230. }
  231. ctkPluginContext* ctkPlugin::getPluginContext() const
  232. {
  233. //TODO security checks
  234. Q_D(const ctkPlugin);
  235. return d->pluginContext.data();
  236. }
  237. long ctkPlugin::getPluginId() const
  238. {
  239. Q_D(const ctkPlugin);
  240. return d->id;
  241. }
  242. QString ctkPlugin::getLocation() const
  243. {
  244. //TODO security
  245. Q_D(const ctkPlugin);
  246. return d->location;
  247. }
  248. QHash<QString, QString> ctkPlugin::getHeaders()
  249. {
  250. //TODO security
  251. Q_D(ctkPlugin);
  252. if (d->cachedRawHeaders.empty())
  253. {
  254. d->cachedRawHeaders = d->archive->getUnlocalizedAttributes();
  255. }
  256. if (d->state == UNINSTALLED)
  257. {
  258. return d->cachedHeaders;
  259. }
  260. //TODO use the embedded .qm files to localize header values
  261. return d->cachedRawHeaders;
  262. }
  263. QString ctkPlugin::getSymbolicName() const
  264. {
  265. Q_D(const ctkPlugin);
  266. return d->symbolicName;
  267. }
  268. QStringList ctkPlugin::getResourceList(const QString& path) const
  269. {
  270. Q_D(const ctkPlugin);
  271. return d->archive->findResourcesPath(path);
  272. }
  273. QByteArray ctkPlugin::getResource(const QString& path) const
  274. {
  275. Q_D(const ctkPlugin);
  276. return d->archive->getPluginResource(path);
  277. }
  278. ctkVersion ctkPlugin::getVersion() const
  279. {
  280. Q_D(const ctkPlugin);
  281. return d->version;
  282. }
  283. QDebug operator<<(QDebug debug, ctkPlugin::State state)
  284. {
  285. switch (state)
  286. {
  287. case ctkPlugin::UNINSTALLED:
  288. return debug << "UNINSTALLED";
  289. case ctkPlugin::INSTALLED:
  290. return debug << "INSTALLED";
  291. case ctkPlugin::RESOLVED:
  292. return debug << "RESOLVED";
  293. case ctkPlugin::STARTING:
  294. return debug << "STARTING";
  295. case ctkPlugin::STOPPING:
  296. return debug << "STOPPING";
  297. case ctkPlugin::ACTIVE:
  298. return debug << "ACTIVE";
  299. default:
  300. return debug << "unknown";
  301. }
  302. }
  303. QDebug operator<<(QDebug debug, const ctkPlugin& plugin)
  304. {
  305. debug.nospace() << "ctkPlugin[" << "id=" << plugin.getPluginId() <<
  306. ", state=" << plugin.getState() << ", loc=" << plugin.getLocation() <<
  307. ", symName=" << plugin.getSymbolicName() << "]";
  308. return debug.maybeSpace();
  309. }
  310. QDebug operator<<(QDebug debug, ctkPlugin const * plugin)
  311. {
  312. return operator<<(debug, *plugin);
  313. }