ctkPlugin.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 = 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. ctkPluginContext* ctkPlugin::getPluginContext() const
  144. {
  145. //TODO security checks
  146. Q_D(const ctkPlugin);
  147. return d->pluginContext;
  148. }
  149. long ctkPlugin::getPluginId() const
  150. {
  151. Q_D(const ctkPlugin);
  152. return d->id;
  153. }
  154. QString ctkPlugin::getLocation() const
  155. {
  156. //TODO security
  157. Q_D(const ctkPlugin);
  158. return d->location;
  159. }
  160. QHash<QString, QString> ctkPlugin::getHeaders()
  161. {
  162. //TODO security
  163. Q_D(ctkPlugin);
  164. if (d->cachedRawHeaders.empty())
  165. {
  166. d->cachedRawHeaders = d->archive->getUnlocalizedAttributes();
  167. }
  168. if (d->state == UNINSTALLED)
  169. {
  170. return d->cachedHeaders;
  171. }
  172. //TODO use the embedded .qm files to localize header values
  173. return d->cachedRawHeaders;
  174. }
  175. QString ctkPlugin::getSymbolicName() const
  176. {
  177. Q_D(const ctkPlugin);
  178. return d->symbolicName;
  179. }
  180. QStringList ctkPlugin::getResourceList(const QString& path) const
  181. {
  182. Q_D(const ctkPlugin);
  183. return d->archive->findResourcesPath(path);
  184. }
  185. QByteArray ctkPlugin::getResource(const QString& path) const
  186. {
  187. Q_D(const ctkPlugin);
  188. return d->archive->getPluginResource(path);
  189. }
  190. ctkVersion ctkPlugin::getVersion() const
  191. {
  192. Q_D(const ctkPlugin);
  193. return d->version;
  194. }
  195. QDebug operator<<(QDebug debug, ctkPlugin::State state)
  196. {
  197. switch (state)
  198. {
  199. case ctkPlugin::UNINSTALLED:
  200. return debug << "UNINSTALLED";
  201. case ctkPlugin::INSTALLED:
  202. return debug << "INSTALLED";
  203. case ctkPlugin::RESOLVED:
  204. return debug << "RESOLVED";
  205. case ctkPlugin::STARTING:
  206. return debug << "STARTING";
  207. case ctkPlugin::STOPPING:
  208. return debug << "STOPPING";
  209. case ctkPlugin::ACTIVE:
  210. return debug << "ACTIVE";
  211. default:
  212. return debug << "unknown";
  213. }
  214. }
  215. QDebug operator<<(QDebug debug, const ctkPlugin& plugin)
  216. {
  217. debug.nospace() << "ctkPlugin[" << "id=" << plugin.getPluginId() <<
  218. ", state=" << plugin.getState() << ", loc=" << plugin.getLocation() <<
  219. ", symName=" << plugin.getSymbolicName() << "]";
  220. return debug.maybeSpace();
  221. }
  222. QDebug operator<<(QDebug debug, ctkPlugin const * plugin)
  223. {
  224. return operator<<(debug, *plugin);
  225. }