ctkPlugin.cpp 8.5 KB

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