ctkPlugins.cpp 9.5 KB

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