ctkPluginPrivate.cpp 8.6 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 Plugin::States PluginPrivate::RESOLVED_FLAGS = Plugin::RESOLVED | Plugin::STARTING | Plugin::ACTIVE | Plugin::STOPPING;
  23. PluginPrivate::PluginPrivate(
  24. Plugin& qq,
  25. PluginFrameworkContext* fw,
  26. PluginArchive* pa)
  27. : q_ptr(&qq), fwCtx(fw), id(pa->getPluginId()),
  28. location(pa->getPluginLocation().toString()), state(Plugin::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 = PluginFrameworkUtil::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. RequirePlugin* rp = new RequirePlugin(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. PluginPrivate::PluginPrivate(Plugin& qq,
  52. PluginFrameworkContext* fw,
  53. long id, const QString& loc, const QString& sym, const Version& ver)
  54. : q_ptr(&qq), fwCtx(fw), id(id), location(loc), symbolicName(sym), version(ver),
  55. state(Plugin::INSTALLED), archive(0), pluginContext(0),
  56. pluginActivator(0), lastModified(0),
  57. eagerActivation(false), activating(false), deactivating(false)
  58. {
  59. }
  60. PluginPrivate::~PluginPrivate()
  61. {
  62. qDeleteAll(require);
  63. }
  64. Plugin::State PluginPrivate::getUpdatedState()
  65. {
  66. if (state & Plugin::INSTALLED)
  67. {
  68. try
  69. {
  70. if (state == Plugin::INSTALLED)
  71. {
  72. fwCtx->resolvePlugin(this);
  73. state = Plugin::RESOLVED;
  74. fwCtx->listeners.emitPluginChanged(PluginEvent(PluginEvent::RESOLVED, this->q_func()));
  75. }
  76. }
  77. catch (const PluginException& pe)
  78. {
  79. Q_Q(Plugin);
  80. this->fwCtx->listeners.frameworkError(q, pe);
  81. throw;
  82. }
  83. }
  84. return state;
  85. }
  86. void PluginPrivate::setAutostartSetting(const Plugin::StartOptions& setting) {
  87. try
  88. {
  89. if (archive)
  90. {
  91. archive->setAutostartSetting(setting);
  92. }
  93. }
  94. catch (const PluginDatabaseException& e)
  95. {
  96. Q_Q(Plugin);
  97. this->fwCtx->listeners.frameworkError(q, e);
  98. }
  99. }
  100. void PluginPrivate::checkManifestHeaders()
  101. {
  102. symbolicName = archive->getAttribute(PluginConstants::PLUGIN_SYMBOLICNAME);
  103. if (symbolicName.isEmpty())
  104. {
  105. throw std::invalid_argument(std::string("Plugin 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 = Version(mpv);
  114. }
  115. catch (const std::exception& e)
  116. {
  117. throw std::invalid_argument(std::string("Plugin 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 PluginPrivate::finalizeActivation()
  128. {
  129. switch (getUpdatedState())
  130. {
  131. case Plugin::INSTALLED:
  132. // we shouldn't be here, getUpdatedState should have thrown
  133. // an exception during resolving the plugin
  134. throw PluginException("Internal error: expected exception on plugin resolve not thrown!");
  135. case Plugin::STARTING:
  136. if (activating) return; // finalization already in progress.
  137. // Lazy activation; fall through to RESOLVED.
  138. case Plugin::RESOLVED:
  139. //6:
  140. state = Plugin::STARTING;
  141. activating = true;
  142. qDebug() << "activating #" << this->id;
  143. //7:
  144. if (!pluginContext)
  145. {
  146. pluginContext = new PluginContext(this);
  147. }
  148. try
  149. {
  150. //TODO maybe call this in its own thread
  151. start0();
  152. }
  153. catch (...)
  154. {
  155. //8:
  156. state = Plugin::STOPPING;
  157. // NYI, call outside lock
  158. fwCtx->listeners.emitPluginChanged(PluginEvent(PluginEvent::STOPPING, this->q_func()));
  159. removePluginResources();
  160. delete pluginContext;
  161. state = Plugin::RESOLVED;
  162. // NYI, call outside lock
  163. fwCtx->listeners.emitPluginChanged(PluginEvent(PluginEvent::STOPPED, this->q_func()));
  164. activating = false;
  165. throw;
  166. }
  167. activating = false;
  168. break;
  169. case Plugin::ACTIVE:
  170. break;
  171. case Plugin::STOPPING:
  172. // This happens if start is called from inside the PluginActivator::stop method.
  173. // Don't allow it.
  174. throw PluginException("start called from PluginActivator::stop",
  175. PluginException::ACTIVATOR_ERROR);
  176. case Plugin::UNINSTALLED:
  177. throw std::logic_error("Plugin is in UNINSTALLED state");
  178. }
  179. }
  180. void PluginPrivate::start0()
  181. {
  182. fwCtx->listeners.emitPluginChanged(PluginEvent(PluginEvent::STARTING, this->q_func()));
  183. try {
  184. pluginLoader.load();
  185. if (!pluginLoader.isLoaded())
  186. {
  187. throw PluginException(QString("Loading plugin %1 failed: %2").arg(pluginLoader.fileName()).arg(pluginLoader.errorString()),
  188. PluginException::ACTIVATOR_ERROR);
  189. }
  190. pluginActivator = qobject_cast<PluginActivator*>(pluginLoader.instance());
  191. if (!pluginActivator)
  192. {
  193. throw PluginException(QString("Creating PluginActivator instance from %1 failed: %2").arg(pluginLoader.fileName()).arg(pluginLoader.errorString()),
  194. PluginException::ACTIVATOR_ERROR);
  195. }
  196. pluginActivator->start(pluginContext);
  197. if (Plugin::UNINSTALLED == state)
  198. {
  199. throw PluginException("Plugin uninstalled during start()", PluginException::STATECHANGE_ERROR);
  200. }
  201. state = Plugin::ACTIVE;
  202. }
  203. catch (const std::exception& e)
  204. {
  205. throw PluginException("Plugin start failed", PluginException::ACTIVATOR_ERROR, e);
  206. }
  207. qDebug() << "activating #" << id << "completed.";
  208. //10:
  209. fwCtx->listeners.emitPluginChanged(PluginEvent(PluginEvent::STARTED, this->q_func()));
  210. }
  211. void PluginPrivate::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. // ((ServiceRegistration)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. }