ctkPlugin.cpp 8.4 KB

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