ctkPlugins.cpp 9.4 KB

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