ctkPlugin.cpp 8.8 KB

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