ctkPlugins.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 "ctkPlugins_p.h"
  16. #include "ctkPluginPrivate_p.h"
  17. #include "ctkPluginArchive_p.h"
  18. #include "ctkPluginException.h"
  19. #include "ctkPluginFrameworkContext_p.h"
  20. #include "ctkVersionRange_p.h"
  21. #include <stdexcept>
  22. #include <iostream>
  23. #include <QUrl>
  24. //----------------------------------------------------------------------------
  25. ctkPlugins::ctkPlugins(ctkPluginFrameworkContext* fw)
  26. {
  27. fwCtx = fw;
  28. plugins.insert(fw->systemPlugin->getLocation(), fw->systemPlugin);
  29. }
  30. //----------------------------------------------------------------------------
  31. void ctkPlugins::clear()
  32. {
  33. QWriteLocker lock(&pluginsLock);
  34. plugins.clear();
  35. fwCtx = 0;
  36. }
  37. //----------------------------------------------------------------------------
  38. QSharedPointer<ctkPlugin> ctkPlugins::install(const QUrl& location, QIODevice* in)
  39. {
  40. if (!fwCtx)
  41. { // This ctkPlugins instance has been closed!
  42. throw std::logic_error("ctkPlugins::install(location, inputStream) called on closed plugins object.");
  43. }
  44. {
  45. QWriteLocker lock(&pluginsLock);
  46. QHash<QString, QSharedPointer<ctkPlugin> >::const_iterator it = plugins.find(location.toString());
  47. if (it != plugins.end())
  48. {
  49. return it.value();
  50. }
  51. // install new plugin
  52. ctkPluginArchive* pa = 0;
  53. QString localPluginPath;
  54. try
  55. {
  56. if (!in)
  57. {
  58. // extract the input stream from the given location
  59. // //TODO Support for http proxy authentication
  60. // //TODO put in update as well
  61. // String auth = fwCtx.props.getProperty("http.proxyAuth");
  62. // if (auth != null && !"".equals(auth)) {
  63. // if ("http".equals(url.getProtocol()) ||
  64. // "https".equals(url.getProtocol())) {
  65. // String base64 = Util.base64Encode(auth);
  66. // conn.setRequestProperty("Proxy-Authorization",
  67. // "Basic " + base64);
  68. // }
  69. // }
  70. // // Support for http basic authentication
  71. // String basicAuth = fwCtx.props.getProperty("http.basicAuth");
  72. // if (basicAuth != null && !"".equals(basicAuth)) {
  73. // if ("http".equals(url.getProtocol()) ||
  74. // "https".equals(url.getProtocol())) {
  75. // String base64 = Util.base64Encode(basicAuth);
  76. // conn.setRequestProperty("Authorization",
  77. // "Basic " +base64);
  78. // }
  79. // }
  80. if (location.scheme() != "file")
  81. {
  82. throw std::runtime_error(std::string("Unsupported url scheme: ") + qPrintable(location.scheme()));
  83. }
  84. else
  85. {
  86. qDebug() << QString("Trying to install file:") << location.path();
  87. localPluginPath = location.toLocalFile();
  88. }
  89. }
  90. else
  91. {
  92. //TODO copy the QIODevice to a local cache
  93. }
  94. pa = fwCtx->storage->insertPlugin(location, localPluginPath);
  95. QSharedPointer<ctkPlugin> res(new ctkPlugin());
  96. res->init(res, fwCtx, pa);
  97. plugins.insert(location.toString(), res);
  98. fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::INSTALLED, res));
  99. return res;
  100. }
  101. catch (const std::exception& e)
  102. {
  103. if (pa)
  104. {
  105. pa->purge();
  106. }
  107. // if (dynamic_cast<const SecurityException&>(e)) {
  108. // throw;
  109. // }
  110. // else
  111. // {
  112. throw ctkPluginException(QString("Failed to install plugin: ") + QString(e.what()),
  113. ctkPluginException::UNSPECIFIED, &e);
  114. // }
  115. }
  116. }
  117. }
  118. //----------------------------------------------------------------------------
  119. void ctkPlugins::remove(const QUrl& location)
  120. {
  121. QWriteLocker lock(&pluginsLock);
  122. plugins.remove(location.toString());
  123. }
  124. //----------------------------------------------------------------------------
  125. QSharedPointer<ctkPlugin> ctkPlugins::getPlugin(int id) const
  126. {
  127. if (!fwCtx)
  128. { // This plugins instance has been closed!
  129. throw std::logic_error("ctkPlugins::getPlugin(id) called on closed plugins object.");
  130. }
  131. {
  132. QReadLocker lock(&pluginsLock);
  133. QHashIterator<QString, QSharedPointer<ctkPlugin> > it(plugins);
  134. while (it.hasNext())
  135. {
  136. QSharedPointer<ctkPlugin> plugin = it.next().value();
  137. if (plugin->getPluginId() == id)
  138. {
  139. return plugin;
  140. }
  141. }
  142. }
  143. return QSharedPointer<ctkPlugin>();
  144. }
  145. //----------------------------------------------------------------------------
  146. QSharedPointer<ctkPlugin> ctkPlugins::getPlugin(const QString& location) const
  147. {
  148. if (!fwCtx)
  149. { // This plugins instance has been closed!
  150. throw std::logic_error("ctkPlugins::getPlugin(location) called on closed plugins object.");
  151. }
  152. QReadLocker lock(&pluginsLock);
  153. QHash<QString, QSharedPointer<ctkPlugin> >::const_iterator it = plugins.find(location);
  154. if (it != plugins.end()) return it.value();
  155. return QSharedPointer<ctkPlugin>(0);
  156. }
  157. //----------------------------------------------------------------------------
  158. QSharedPointer<ctkPlugin> ctkPlugins::getPlugin(const QString& name, const ctkVersion& version) const
  159. {
  160. if (!fwCtx)
  161. { // This ctkPlugins instance has been closed!
  162. throw std::logic_error("ctkPlugins::getPlugin(name, version) called on closed plugins object.");
  163. }
  164. {
  165. QReadLocker lock(&pluginsLock);
  166. QHashIterator<QString, QSharedPointer<ctkPlugin> > it(plugins);
  167. while (it.hasNext())
  168. {
  169. QSharedPointer<ctkPlugin> plugin = it.next().value();
  170. if ((name == plugin->getSymbolicName()) && (version == plugin->getVersion()))
  171. {
  172. return plugin;
  173. }
  174. }
  175. }
  176. return QSharedPointer<ctkPlugin>(0);
  177. }
  178. //----------------------------------------------------------------------------
  179. QList<QSharedPointer<ctkPlugin> > ctkPlugins::getPlugins() const
  180. {
  181. if (!fwCtx)
  182. { // This plugins instance has been closed!
  183. throw std::logic_error("ctkPlugins::getPlugins() called on closed plugins object.");
  184. }
  185. {
  186. QReadLocker lock(&pluginsLock);
  187. return plugins.values();
  188. }
  189. }
  190. //----------------------------------------------------------------------------
  191. QList<ctkPlugin*> ctkPlugins::getPlugins(const QString& name) const
  192. {
  193. QList<ctkPlugin*> res;
  194. {
  195. QReadLocker lock(&pluginsLock);
  196. QHashIterator<QString, QSharedPointer<ctkPlugin> > it(plugins);
  197. while (it.hasNext())
  198. {
  199. ctkPlugin* plugin = it.next().value().data();
  200. if (name == plugin->getSymbolicName())
  201. {
  202. res.push_back(plugin);
  203. }
  204. }
  205. }
  206. return res;
  207. }
  208. //----------------------------------------------------------------------------
  209. QList<ctkPlugin*> ctkPlugins::getPlugins(const QString& name, const ctkVersionRange& range) const
  210. {
  211. if (!fwCtx)
  212. { // This plugins instance has been closed!
  213. throw std::logic_error("ctkPlugins::getPlugins(name, versionRange) called on closed plugins object.");
  214. }
  215. QList<ctkPlugin*> pluginsWithName = getPlugins(name);
  216. QList<ctkPlugin*> res;
  217. QListIterator<ctkPlugin*> it(pluginsWithName);
  218. while (it.hasNext()) {
  219. ctkPlugin* plugin = it.next();
  220. if (range.withinRange(plugin->getVersion()))
  221. {
  222. int j = res.size();
  223. while (--j >= 0)
  224. {
  225. if (plugin->getVersion().compare(res.at(j)->getVersion()) <= 0)
  226. {
  227. break;
  228. }
  229. }
  230. res.insert(j + 1, plugin);
  231. }
  232. }
  233. return res;
  234. }
  235. //----------------------------------------------------------------------------
  236. QList<ctkPlugin*> ctkPlugins::getActivePlugins() const
  237. {
  238. if (!fwCtx)
  239. { // This plugins instance has been closed!
  240. throw std::logic_error("ctkPlugins::getActivePlugins() called on closed plugins object.");
  241. }
  242. QList<ctkPlugin*> slist;
  243. {
  244. QReadLocker lock(&pluginsLock);
  245. QHashIterator<QString, QSharedPointer<ctkPlugin> > it(plugins);
  246. while (it.hasNext())
  247. {
  248. QSharedPointer<ctkPlugin> plugin = it.next().value();
  249. ctkPlugin::State s = plugin->getState();
  250. if (s == ctkPlugin::ACTIVE || s == ctkPlugin::STARTING) {
  251. slist.push_back(plugin.data());
  252. }
  253. }
  254. }
  255. return slist;
  256. }
  257. //----------------------------------------------------------------------------
  258. void ctkPlugins::load()
  259. {
  260. QList<ctkPluginArchive*> pas = fwCtx->storage->getAllPluginArchives();
  261. QListIterator<ctkPluginArchive*> it(pas);
  262. {
  263. QWriteLocker lock(&pluginsLock);
  264. while (it.hasNext())
  265. {
  266. ctkPluginArchive* pa = it.next();
  267. try
  268. {
  269. QSharedPointer<ctkPlugin> plugin(new ctkPlugin());
  270. plugin->init(plugin, fwCtx, pa);
  271. plugins.insert(pa->getPluginLocation().toString(), plugin);
  272. }
  273. catch (const std::exception& e)
  274. {
  275. pa->setAutostartSetting(-1); // Do not start on launch
  276. pa->setStartLevel(-2); // Mark as uninstalled
  277. std::cerr << "Error: Failed to load bundle "
  278. << pa->getPluginId()
  279. << " (" << qPrintable(pa->getPluginLocation().toString()) << ")"
  280. << " uninstalled it!\n";
  281. std::cerr << e.what();
  282. }
  283. }
  284. }
  285. }
  286. //----------------------------------------------------------------------------
  287. void ctkPlugins::startPlugins(const QList<ctkPlugin*>& slist) const
  288. {
  289. // Sort in start order
  290. // Resolve first to avoid dead lock
  291. QListIterator<ctkPlugin*> it(slist);
  292. while (it.hasNext())
  293. {
  294. ctkPlugin* plugin = it.next();
  295. ctkPluginPrivate* pp = plugin->d_func();
  296. pp->getUpdatedState();
  297. }
  298. it.toFront();
  299. while (it.hasNext())
  300. {
  301. ctkPlugin* plugin = it.next();
  302. ctkPluginPrivate* pp = plugin->d_func();
  303. if (pp->getUpdatedState() == ctkPlugin::RESOLVED)
  304. {
  305. try
  306. {
  307. plugin->start(0);
  308. }
  309. catch (const ctkPluginException& pe)
  310. {
  311. pp->fwCtx->listeners.frameworkError(pp->q_func(), pe);
  312. }
  313. }
  314. }
  315. }