ctkPlugins.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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());
  93. res->init(res, fwCtx, pa);
  94. plugins.insert(location.toString(), res);
  95. fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::INSTALLED, res));
  96. return res;
  97. }
  98. catch (const std::exception& e)
  99. {
  100. if (pa)
  101. {
  102. pa->purge();
  103. }
  104. // if (dynamic_cast<const SecurityException&>(e)) {
  105. // throw;
  106. // }
  107. // else
  108. // {
  109. throw ctkPluginException(QString("Failed to install plugin: ") + QString(e.what()),
  110. ctkPluginException::UNSPECIFIED, &e);
  111. // }
  112. }
  113. }
  114. }
  115. void ctkPlugins::remove(const QUrl& location)
  116. {
  117. QWriteLocker lock(&pluginsLock);
  118. plugins.remove(location.toString());
  119. }
  120. QSharedPointer<ctkPlugin> ctkPlugins::getPlugin(int id) const
  121. {
  122. if (!fwCtx)
  123. { // This plugins instance has been closed!
  124. throw std::logic_error("ctkPlugins::getPlugin(id) called on closed plugins object.");
  125. }
  126. {
  127. QReadLocker lock(&pluginsLock);
  128. QHashIterator<QString, QSharedPointer<ctkPlugin> > it(plugins);
  129. while (it.hasNext())
  130. {
  131. QSharedPointer<ctkPlugin> plugin = it.next().value();
  132. if (plugin->getPluginId() == id)
  133. {
  134. return plugin;
  135. }
  136. }
  137. }
  138. return QSharedPointer<ctkPlugin>();
  139. }
  140. ctkPlugin* ctkPlugins::getPlugin(const QString& location) const
  141. {
  142. if (!fwCtx)
  143. { // This plugins instance has been closed!
  144. throw std::logic_error("ctkPlugins::getPlugin(location) called on closed plugins object.");
  145. }
  146. QReadLocker lock(&pluginsLock);
  147. QHash<QString, QSharedPointer<ctkPlugin> >::const_iterator it = plugins.find(location);
  148. if (it != plugins.end()) return it.value().data();
  149. return 0;
  150. }
  151. ctkPlugin* ctkPlugins::getPlugin(const QString& name, const ctkVersion& version) const
  152. {
  153. if (!fwCtx)
  154. { // This ctkPlugins instance has been closed!
  155. throw std::logic_error("ctkPlugins::getPlugin(name, version) called on closed plugins object.");
  156. }
  157. {
  158. QReadLocker lock(&pluginsLock);
  159. QHashIterator<QString, QSharedPointer<ctkPlugin> > it(plugins);
  160. while (it.hasNext())
  161. {
  162. QSharedPointer<ctkPlugin> plugin = it.next().value();
  163. if ((name == plugin->getSymbolicName()) && (version == plugin->getVersion()))
  164. {
  165. return plugin.data();
  166. }
  167. }
  168. }
  169. return 0;
  170. }
  171. QList<QSharedPointer<ctkPlugin> > ctkPlugins::getPlugins() const
  172. {
  173. if (!fwCtx)
  174. { // This plugins instance has been closed!
  175. throw std::logic_error("ctkPlugins::getPlugins() called on closed plugins object.");
  176. }
  177. {
  178. QReadLocker lock(&pluginsLock);
  179. return plugins.values();
  180. }
  181. }
  182. QList<ctkPlugin*> ctkPlugins::getPlugins(const QString& name) const
  183. {
  184. QList<ctkPlugin*> res;
  185. {
  186. QReadLocker lock(&pluginsLock);
  187. QHashIterator<QString, QSharedPointer<ctkPlugin> > it(plugins);
  188. while (it.hasNext())
  189. {
  190. ctkPlugin* plugin = it.next().value().data();
  191. if (name == plugin->getSymbolicName())
  192. {
  193. res.push_back(plugin);
  194. }
  195. }
  196. }
  197. return res;
  198. }
  199. QList<ctkPlugin*> ctkPlugins::getPlugins(const QString& name, const ctkVersionRange& range) const
  200. {
  201. if (!fwCtx)
  202. { // This plugins instance has been closed!
  203. throw std::logic_error("ctkPlugins::getPlugins(name, versionRange) called on closed plugins object.");
  204. }
  205. QList<ctkPlugin*> pluginsWithName = getPlugins(name);
  206. QList<ctkPlugin*> res;
  207. QListIterator<ctkPlugin*> it(pluginsWithName);
  208. while (it.hasNext()) {
  209. ctkPlugin* plugin = it.next();
  210. if (range.withinRange(plugin->getVersion()))
  211. {
  212. int j = res.size();
  213. while (--j >= 0)
  214. {
  215. if (plugin->getVersion().compare(res.at(j)->getVersion()) <= 0)
  216. {
  217. break;
  218. }
  219. }
  220. res.insert(j + 1, plugin);
  221. }
  222. }
  223. return res;
  224. }
  225. QList<ctkPlugin*> ctkPlugins::getActivePlugins() const
  226. {
  227. if (!fwCtx)
  228. { // This plugins instance has been closed!
  229. throw std::logic_error("ctkPlugins::getActivePlugins() called on closed plugins object.");
  230. }
  231. QList<ctkPlugin*> slist;
  232. {
  233. QReadLocker lock(&pluginsLock);
  234. QHashIterator<QString, QSharedPointer<ctkPlugin> > it(plugins);
  235. while (it.hasNext())
  236. {
  237. QSharedPointer<ctkPlugin> plugin = it.next().value();
  238. ctkPlugin::State s = plugin->getState();
  239. if (s == ctkPlugin::ACTIVE || s == ctkPlugin::STARTING) {
  240. slist.push_back(plugin.data());
  241. }
  242. }
  243. }
  244. return slist;
  245. }
  246. void ctkPlugins::load()
  247. {
  248. QList<ctkPluginArchive*> pas = fwCtx->storage->getAllPluginArchives();
  249. QListIterator<ctkPluginArchive*> it(pas);
  250. {
  251. QWriteLocker lock(&pluginsLock);
  252. while (it.hasNext())
  253. {
  254. ctkPluginArchive* pa = it.next();
  255. try
  256. {
  257. QSharedPointer<ctkPlugin> plugin(new ctkPlugin());
  258. plugin->init(plugin, fwCtx, pa);
  259. plugins.insert(pa->getPluginLocation().toString(), plugin);
  260. }
  261. catch (const std::exception& e)
  262. {
  263. pa->setAutostartSetting(-1); // Do not start on launch
  264. pa->setStartLevel(-2); // Mark as uninstalled
  265. std::cerr << "Error: Failed to load bundle "
  266. << pa->getPluginId()
  267. << " (" << qPrintable(pa->getPluginLocation().toString()) << ")"
  268. << " uninstalled it!\n";
  269. std::cerr << e.what();
  270. }
  271. }
  272. }
  273. }
  274. void ctkPlugins::startPlugins(const QList<ctkPlugin*>& slist) const
  275. {
  276. // Sort in start order
  277. // Resolve first to avoid dead lock
  278. QListIterator<ctkPlugin*> it(slist);
  279. while (it.hasNext())
  280. {
  281. ctkPlugin* plugin = it.next();
  282. ctkPluginPrivate* pp = plugin->d_func();
  283. pp->getUpdatedState();
  284. }
  285. it.toFront();
  286. while (it.hasNext())
  287. {
  288. ctkPlugin* plugin = it.next();
  289. ctkPluginPrivate* pp = plugin->d_func();
  290. if (pp->getUpdatedState() == ctkPlugin::RESOLVED)
  291. {
  292. try
  293. {
  294. plugin->start(0);
  295. }
  296. catch (const ctkPluginException& pe)
  297. {
  298. pp->fwCtx->listeners.frameworkError(plugin, pe);
  299. }
  300. }
  301. }
  302. }