ctkPluginPrivate.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) 2010 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 "ctkPluginPrivate_p.h"
  16. #include "ctkPluginConstants.h"
  17. #include "ctkPluginDatabaseException.h"
  18. #include "ctkPluginArchive_p.h"
  19. #include "ctkPluginFrameworkContext_p.h"
  20. #include "ctkPluginFrameworkUtil_p.h"
  21. #include "ctkPluginActivator.h"
  22. const ctkPlugin::States ctkPluginPrivate::RESOLVED_FLAGS = ctkPlugin::RESOLVED | ctkPlugin::STARTING | ctkPlugin::ACTIVE | ctkPlugin::STOPPING;
  23. ctkPluginPrivate::ctkPluginPrivate(
  24. ctkPlugin& qq,
  25. ctkPluginFrameworkContext* fw,
  26. ctkPluginArchive* pa)
  27. : q_ptr(&qq), fwCtx(fw), id(pa->getPluginId()),
  28. location(pa->getPluginLocation().toString()), state(ctkPlugin::INSTALLED),
  29. archive(pa), pluginContext(0), pluginActivator(0), pluginLoader(pa->getLibLocation()),
  30. lastModified(0), eagerActivation(false), activating(false), deactivating(false)
  31. {
  32. //TODO
  33. //checkCertificates(pa);
  34. checkManifestHeaders();
  35. // fill require list
  36. QString requireString = archive->getAttribute(PluginConstants::REQUIRE_PLUGIN);
  37. QList<QMap<QString, QStringList> > requireList = ctkPluginFrameworkUtil::parseEntries(PluginConstants::REQUIRE_PLUGIN,
  38. requireString, true, true, false);
  39. QListIterator<QMap<QString, QStringList> > i(requireList);
  40. while (i.hasNext())
  41. {
  42. const QMap<QString, QStringList>& e = i.next();
  43. const QStringList& res = e.value(PluginConstants::RESOLUTION_DIRECTIVE);
  44. const QStringList& version = e.value(PluginConstants::PLUGIN_VERSION_ATTRIBUTE);
  45. ctkRequirePlugin* rp = new ctkRequirePlugin(this, e.value("$key").front(),
  46. res.empty() ? QString() : res.front(),
  47. version.empty() ? QString() : version.front());
  48. require.push_back(rp);
  49. }
  50. }
  51. ctkPluginPrivate::ctkPluginPrivate(ctkPlugin& qq,
  52. ctkPluginFrameworkContext* fw,
  53. long id, const QString& loc, const QString& sym, const ctkVersion& ver)
  54. : q_ptr(&qq), fwCtx(fw), id(id), location(loc), symbolicName(sym), version(ver),
  55. state(ctkPlugin::INSTALLED), archive(0), pluginContext(0),
  56. pluginActivator(0), lastModified(0),
  57. eagerActivation(false), activating(false), deactivating(false)
  58. {
  59. }
  60. ctkPluginPrivate::~ctkPluginPrivate()
  61. {
  62. qDeleteAll(require);
  63. }
  64. ctkPlugin::State ctkPluginPrivate::getUpdatedState()
  65. {
  66. if (state & ctkPlugin::INSTALLED)
  67. {
  68. try
  69. {
  70. if (state == ctkPlugin::INSTALLED)
  71. {
  72. fwCtx->resolvePlugin(this);
  73. state = ctkPlugin::RESOLVED;
  74. fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::RESOLVED, this->q_func()));
  75. }
  76. }
  77. catch (const ctkPluginException& pe)
  78. {
  79. Q_Q(ctkPlugin);
  80. this->fwCtx->listeners.frameworkError(q, pe);
  81. throw;
  82. }
  83. }
  84. return state;
  85. }
  86. void ctkPluginPrivate::setAutostartSetting(const ctkPlugin::StartOptions& setting) {
  87. try
  88. {
  89. if (archive)
  90. {
  91. archive->setAutostartSetting(setting);
  92. }
  93. }
  94. catch (const ctkPluginDatabaseException& e)
  95. {
  96. Q_Q(ctkPlugin);
  97. this->fwCtx->listeners.frameworkError(q, e);
  98. }
  99. }
  100. void ctkPluginPrivate::checkManifestHeaders()
  101. {
  102. symbolicName = archive->getAttribute(PluginConstants::PLUGIN_SYMBOLICNAME);
  103. if (symbolicName.isEmpty())
  104. {
  105. throw std::invalid_argument(std::string("ctkPlugin has no symbolic name, location=") +
  106. qPrintable(location));
  107. }
  108. QString mpv = archive->getAttribute(PluginConstants::PLUGIN_VERSION);
  109. if (!mpv.isEmpty())
  110. {
  111. try
  112. {
  113. version = ctkVersion(mpv);
  114. }
  115. catch (const std::exception& e)
  116. {
  117. throw std::invalid_argument(std::string("ctkPlugin does not specify a valid ") +
  118. qPrintable(PluginConstants::PLUGIN_VERSION) + " header. Got exception: " + e.what());
  119. }
  120. }
  121. QString ap = archive->getAttribute(PluginConstants::PLUGIN_ACTIVATIONPOLICY);
  122. if (PluginConstants::ACTIVATION_EAGER == ap)
  123. {
  124. eagerActivation = true;
  125. }
  126. }
  127. void ctkPluginPrivate::finalizeActivation()
  128. {
  129. switch (getUpdatedState())
  130. {
  131. case ctkPlugin::INSTALLED:
  132. // we shouldn't be here, getUpdatedState should have thrown
  133. // an exception during resolving the plugin
  134. throw ctkPluginException("Internal error: expected exception on plugin resolve not thrown!");
  135. case ctkPlugin::STARTING:
  136. if (activating) return; // finalization already in progress.
  137. // Lazy activation; fall through to RESOLVED.
  138. case ctkPlugin::RESOLVED:
  139. //6:
  140. state = ctkPlugin::STARTING;
  141. activating = true;
  142. qDebug() << "activating #" << this->id;
  143. //7:
  144. if (!pluginContext)
  145. {
  146. pluginContext = new ctkPluginContext(this);
  147. }
  148. try
  149. {
  150. //TODO maybe call this in its own thread
  151. start0();
  152. }
  153. catch (...)
  154. {
  155. //8:
  156. state = ctkPlugin::STOPPING;
  157. // NYI, call outside lock
  158. fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::STOPPING, this->q_func()));
  159. removePluginResources();
  160. delete pluginContext;
  161. state = ctkPlugin::RESOLVED;
  162. // NYI, call outside lock
  163. fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::STOPPED, this->q_func()));
  164. activating = false;
  165. throw;
  166. }
  167. activating = false;
  168. break;
  169. case ctkPlugin::ACTIVE:
  170. break;
  171. case ctkPlugin::STOPPING:
  172. // This happens if start is called from inside the ctkPluginActivator::stop method.
  173. // Don't allow it.
  174. throw ctkPluginException("start called from ctkPluginActivator::stop",
  175. ctkPluginException::ACTIVATOR_ERROR);
  176. case ctkPlugin::UNINSTALLED:
  177. throw std::logic_error("ctkPlugin is in UNINSTALLED state");
  178. }
  179. }
  180. void ctkPluginPrivate::start0()
  181. {
  182. fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::STARTING, this->q_func()));
  183. try {
  184. pluginLoader.load();
  185. if (!pluginLoader.isLoaded())
  186. {
  187. throw ctkPluginException(QString("Loading plugin %1 failed: %2").arg(pluginLoader.fileName()).arg(pluginLoader.errorString()),
  188. ctkPluginException::ACTIVATOR_ERROR);
  189. }
  190. pluginActivator = qobject_cast<ctkPluginActivator*>(pluginLoader.instance());
  191. if (!pluginActivator)
  192. {
  193. throw ctkPluginException(QString("Creating ctkPluginActivator instance from %1 failed: %2").arg(pluginLoader.fileName()).arg(pluginLoader.errorString()),
  194. ctkPluginException::ACTIVATOR_ERROR);
  195. }
  196. pluginActivator->start(pluginContext);
  197. if (ctkPlugin::UNINSTALLED == state)
  198. {
  199. throw ctkPluginException("ctkPlugin uninstalled during start()", ctkPluginException::STATECHANGE_ERROR);
  200. }
  201. state = ctkPlugin::ACTIVE;
  202. }
  203. catch (const std::exception& e)
  204. {
  205. throw ctkPluginException("ctkPlugin start failed", ctkPluginException::ACTIVATOR_ERROR, e);
  206. }
  207. qDebug() << "activating #" << id << "completed.";
  208. //10:
  209. fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::STARTED, this->q_func()));
  210. }
  211. void ctkPluginPrivate::removePluginResources()
  212. {
  213. // automatic disconnect due to Qt signal slot
  214. //fwCtx->listeners.removeAllListeners(this);
  215. // TODO
  216. // Set srs = fwCtx.services.getRegisteredByBundle(this);
  217. // for (Iterator i = srs.iterator(); i.hasNext();) {
  218. // try {
  219. // ((ctkServiceRegistration)i.next()).unregister();
  220. // } catch (IllegalStateException ignore) {
  221. // // Someone has unregistered the service after stop completed.
  222. // // This should not occur, but we don't want get stuck in
  223. // // an illegal state so we catch it.
  224. // }
  225. // }
  226. // Set s = fwCtx.services.getUsedByBundle(this);
  227. // for (Iterator i = s.iterator(); i.hasNext(); ) {
  228. // ((ServiceRegistrationImpl) i.next()).reference.ungetService(this, false);
  229. // }
  230. }