ctkPluginPrivate.cpp 8.7 KB

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