ctkPlugins.cpp 9.8 KB

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