ctkPlugin.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 "ctkPluginFrameworkUtil_p.h"
  17. #include "ctkPluginPrivate_p.h"
  18. #include "ctkPluginArchive_p.h"
  19. #include "ctkPluginFrameworkContext_p.h"
  20. #include "ctkServices_p.h"
  21. #include <QStringList>
  22. ctkPlugin::ctkPlugin()
  23. : d_ptr(0)
  24. {
  25. }
  26. void ctkPlugin::init(ctkPluginPrivate* dd)
  27. {
  28. if (d_ptr) throw std::logic_error("ctkPlugin already initialized");
  29. d_ptr = dd;
  30. }
  31. void ctkPlugin::init(const QWeakPointer<ctkPlugin>& self,
  32. ctkPluginFrameworkContext* fw,
  33. ctkPluginArchive* pa)
  34. {
  35. if (d_ptr) throw std::logic_error("ctkPlugin already initialized");
  36. d_ptr = new ctkPluginPrivate(self, fw, pa);
  37. }
  38. ctkPlugin::~ctkPlugin()
  39. {
  40. delete d_ptr;
  41. }
  42. ctkPlugin::State ctkPlugin::getState() const
  43. {
  44. Q_D(const ctkPlugin);
  45. return d->state;
  46. }
  47. void ctkPlugin::start(const StartOptions& options)
  48. {
  49. Q_D(ctkPlugin);
  50. if (d->state == UNINSTALLED)
  51. {
  52. throw std::logic_error("ctkPlugin is uninstalled");
  53. }
  54. // Initialize the activation; checks initialization of lazy
  55. // activation.
  56. //TODO 1: If activating or deactivating, wait a litle
  57. // we don't use mutliple threads to start plugins for now
  58. //waitOnActivation(lock, "ctkPlugin::start", false);
  59. //2: start() is idempotent, i.e., nothing to do when already started
  60. if (d->state == ACTIVE)
  61. {
  62. return;
  63. }
  64. //3: Record non-transient start requests.
  65. if ((options & START_TRANSIENT) == 0)
  66. {
  67. d->setAutostartSetting(options);
  68. }
  69. //4: Resolve plugin (if needed)
  70. d->getUpdatedState();
  71. //5: Eager?
  72. if ((options & START_ACTIVATION_POLICY) && !d->eagerActivation )
  73. {
  74. if (STARTING == d->state) return;
  75. d->state = STARTING;
  76. d->pluginContext.reset(new ctkPluginContext(this->d_func()));
  77. ctkPluginEvent pluginEvent(ctkPluginEvent::LAZY_ACTIVATION, d->q_ptr);
  78. d->fwCtx->listeners.emitPluginChanged(pluginEvent);
  79. }
  80. else
  81. {
  82. d->finalizeActivation();
  83. }
  84. }
  85. void ctkPlugin::stop(const StopOptions& options)
  86. {
  87. Q_D(ctkPlugin);
  88. const std::exception* savedException = 0;
  89. //1:
  90. if (d->state == UNINSTALLED)
  91. {
  92. throw std::logic_error("Plugin is uninstalled");
  93. }
  94. //2: If activating or deactivating, wait a litle
  95. // We don't support threaded start/stop methods, so we don't need to wait
  96. //waitOnActivation(fwCtx.packages, "Plugin::stop", false);
  97. //3:
  98. if ((options & STOP_TRANSIENT) == 0)
  99. {
  100. d->ignoreAutostartSetting();
  101. }
  102. bool wasStarted = false;
  103. switch (d->state)
  104. {
  105. case INSTALLED:
  106. case RESOLVED:
  107. case STOPPING:
  108. case UNINSTALLED:
  109. //4:
  110. return;
  111. case ACTIVE:
  112. wasStarted = true;
  113. case STARTING: // Lazy start...
  114. try
  115. {
  116. d->stop0(wasStarted);
  117. }
  118. catch (const std::exception* exc)
  119. {
  120. savedException = exc;
  121. }
  122. break;
  123. };
  124. if (d->state != UNINSTALLED)
  125. {
  126. d->fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::STOPPED, d->q_ptr));
  127. }
  128. if (savedException)
  129. {
  130. if (const ctkPluginException* pluginExc = dynamic_cast<const ctkPluginException*>(savedException))
  131. {
  132. throw pluginExc;
  133. }
  134. else
  135. {
  136. throw dynamic_cast<const std::logic_error*>(savedException);
  137. }
  138. }
  139. }
  140. void ctkPlugin::uninstall()
  141. {
  142. bool wasResolved = false;
  143. Q_D(ctkPlugin);
  144. if (d->archive)
  145. {
  146. try
  147. {
  148. d->archive->setStartLevel(-2); // Mark as uninstalled
  149. }
  150. catch (...)
  151. { }
  152. }
  153. d->cachedHeaders = getHeaders();
  154. switch (d->state)
  155. {
  156. case UNINSTALLED:
  157. throw std::logic_error("Plugin is in UNINSTALLED state");
  158. case STARTING: // Lazy start
  159. case ACTIVE:
  160. case STOPPING:
  161. try
  162. {
  163. //TODO: If activating or deactivating, wait a litle
  164. // we don't use mutliple threads to start plugins for now
  165. //d->waitOnActivation(fwCtx.packages, "Bundle.uninstall", false);
  166. if (d->state & (ACTIVE | STARTING))
  167. {
  168. try
  169. {
  170. d->stop0(d->state == ACTIVE);
  171. }
  172. catch (const std::exception& exception)
  173. {
  174. // NYI! not call inside lock
  175. d->fwCtx->listeners.frameworkError(this, exception);
  176. }
  177. }
  178. }
  179. catch (const std::exception& e)
  180. {
  181. d->deactivating = false;
  182. //fwCtx.packages.notifyAll();
  183. d->fwCtx->listeners.frameworkError(this, e);
  184. }
  185. // Fall through
  186. case RESOLVED:
  187. wasResolved = true;
  188. // Fall through
  189. case INSTALLED:
  190. d->fwCtx->plugins->remove(d->location);
  191. d->pluginActivator = 0;
  192. if (d->pluginDir.exists())
  193. {
  194. if (!ctkPluginFrameworkUtil::removeDir(d->pluginDir.absolutePath()))
  195. {
  196. // Plugin dir is not deleted completely, make sure we mark
  197. // it as uninstalled for next framework restart
  198. if (d->archive)
  199. {
  200. try
  201. {
  202. d->archive->setStartLevel(-2); // Mark as uninstalled
  203. }
  204. catch (const std::exception& e)
  205. {
  206. // NYI! Generate FrameworkError if dir still exists!?
  207. qDebug() << "Failed to mark plugin" << d->id
  208. << "as uninstalled," << d->pluginDir.absoluteFilePath()
  209. << "must be deleted manually:" << e.what();
  210. }
  211. }
  212. }
  213. d->pluginDir.setFile("");
  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, d->q_ptr));
  228. }
  229. d->state = UNINSTALLED;
  230. d->fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::UNINSTALLED, d->q_ptr));
  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. }