ctkPlugin.cpp 8.6 KB

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