ctkCmdLineModuleManager.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 "ctkCmdLineModuleManager.h"
  16. #include "ctkCmdLineModuleBackend.h"
  17. #include "ctkCmdLineModuleFrontend.h"
  18. #include "ctkCmdLineModuleTimeoutException.h"
  19. #include "ctkCmdLineModuleCache_p.h"
  20. #include "ctkCmdLineModuleFuture.h"
  21. #include "ctkCmdLineModuleXmlValidator.h"
  22. #include "ctkCmdLineModuleReference.h"
  23. #include "ctkCmdLineModuleReference_p.h"
  24. #include <ctkException.h>
  25. #include <QFileInfo>
  26. #include <QDir>
  27. #include <QStringList>
  28. #include <QBuffer>
  29. #include <QUrl>
  30. #include <QHash>
  31. #include <QList>
  32. #include <QMutex>
  33. #include <QFuture>
  34. #if (QT_VERSION < QT_VERSION_CHECK(4,7,0))
  35. extern int qHash(const QUrl& url);
  36. #endif
  37. //----------------------------------------------------------------------------
  38. struct ctkCmdLineModuleManagerPrivate
  39. {
  40. ctkCmdLineModuleManagerPrivate(ctkCmdLineModuleManager::ValidationMode mode, const QString& cacheDir)
  41. : XmlTimeOut(30000)
  42. , ValidationMode(mode)
  43. {
  44. QFileInfo fileInfo(cacheDir);
  45. if (!fileInfo.exists())
  46. {
  47. if (!QDir().mkpath(cacheDir))
  48. {
  49. qWarning() << "Command line module cache disabled. Directory" << cacheDir << "could not be created.";
  50. return;
  51. }
  52. }
  53. if (fileInfo.isWritable())
  54. {
  55. ModuleCache.reset(new ctkCmdLineModuleCache(cacheDir));
  56. }
  57. else
  58. {
  59. qWarning() << "Command line module cache disabled. Directory" << cacheDir << "is not writable.";
  60. }
  61. }
  62. void checkBackends_unlocked(const QUrl& location)
  63. {
  64. if (!this->SchemeToBackend.contains(location.scheme()))
  65. {
  66. throw ctkInvalidArgumentException(QString("No suitable backend registered for module at ") + location.toString());
  67. }
  68. }
  69. QMutex Mutex;
  70. QHash<QString, ctkCmdLineModuleBackend*> SchemeToBackend;
  71. QHash<QUrl, ctkCmdLineModuleReference> LocationToRef;
  72. QScopedPointer<ctkCmdLineModuleCache> ModuleCache;
  73. int XmlTimeOut;
  74. const ctkCmdLineModuleManager::ValidationMode ValidationMode;
  75. };
  76. //----------------------------------------------------------------------------
  77. ctkCmdLineModuleManager::ctkCmdLineModuleManager(ValidationMode validationMode, const QString& cacheDir)
  78. : d(new ctkCmdLineModuleManagerPrivate(validationMode, cacheDir))
  79. {
  80. }
  81. //----------------------------------------------------------------------------
  82. ctkCmdLineModuleManager::~ctkCmdLineModuleManager()
  83. {
  84. }
  85. //----------------------------------------------------------------------------
  86. ctkCmdLineModuleManager::ValidationMode ctkCmdLineModuleManager::validationMode() const
  87. {
  88. return d->ValidationMode;
  89. }
  90. //----------------------------------------------------------------------------
  91. void ctkCmdLineModuleManager::setXmlTimeout(int xmlTimeout)
  92. {
  93. d->XmlTimeOut = xmlTimeout;
  94. }
  95. //----------------------------------------------------------------------------
  96. int ctkCmdLineModuleManager::xmlTimeout() const
  97. {
  98. return d->XmlTimeOut;
  99. }
  100. //----------------------------------------------------------------------------
  101. void ctkCmdLineModuleManager::registerBackend(ctkCmdLineModuleBackend *backend)
  102. {
  103. QMutexLocker lock(&d->Mutex);
  104. QList<QString> supportedSchemes = backend->schemes();
  105. // Check if there is already a backend registerd for any of the
  106. // supported schemes. We only supported one backend per scheme.
  107. foreach (QString scheme, supportedSchemes)
  108. {
  109. if (d->SchemeToBackend.contains(scheme))
  110. {
  111. throw ctkInvalidArgumentException(QString("A backend for scheme %1 is already registered.").arg(scheme));
  112. }
  113. }
  114. // All good
  115. foreach (QString scheme, supportedSchemes)
  116. {
  117. d->SchemeToBackend[scheme] = backend;
  118. }
  119. }
  120. //----------------------------------------------------------------------------
  121. ctkCmdLineModuleReference
  122. ctkCmdLineModuleManager::registerModule(const QUrl &location)
  123. {
  124. QByteArray xml;
  125. ctkCmdLineModuleBackend* backend = NULL;
  126. {
  127. QMutexLocker lock(&d->Mutex);
  128. d->checkBackends_unlocked(location);
  129. // If the module is already registered, just return the reference
  130. if (d->LocationToRef.contains(location))
  131. {
  132. return d->LocationToRef[location];
  133. }
  134. backend = d->SchemeToBackend[location.scheme()];
  135. }
  136. bool fromCache = false;
  137. qint64 newTimeStamp = 0;
  138. if (d->ModuleCache)
  139. {
  140. newTimeStamp = backend->timeStamp(location);
  141. if (d->ModuleCache->timeStamp(location) < newTimeStamp)
  142. {
  143. // newly fetch the XML description
  144. try
  145. {
  146. xml = backend->rawXmlDescription(location, d->XmlTimeOut);
  147. }
  148. catch (const ctkCmdLineModuleTimeoutException&)
  149. {
  150. // in case of a time-out, do not cache it as a failed attempt
  151. // by recording an empty QByteArray in the cache
  152. throw;
  153. }
  154. catch (...)
  155. {
  156. // cache the failed attempt
  157. d->ModuleCache->cacheXmlDescription(location, newTimeStamp, QByteArray());
  158. throw;
  159. }
  160. }
  161. else
  162. {
  163. // use the cached XML description
  164. xml = d->ModuleCache->rawXmlDescription(location);
  165. fromCache = true;
  166. }
  167. }
  168. else
  169. {
  170. xml = backend->rawXmlDescription(location, d->XmlTimeOut);
  171. }
  172. if (xml.isEmpty())
  173. {
  174. if (!fromCache && d->ModuleCache)
  175. {
  176. d->ModuleCache->cacheXmlDescription(location, newTimeStamp, QByteArray());
  177. }
  178. throw ctkInvalidArgumentException(QString("No XML output available from ") + location.toString());
  179. }
  180. ctkCmdLineModuleReference ref;
  181. ref.d->Location = location;
  182. ref.d->RawXmlDescription = xml;
  183. ref.d->Backend = backend;
  184. if (d->ValidationMode != SKIP_VALIDATION)
  185. {
  186. // validate the outputted xml description
  187. QBuffer input(&xml);
  188. input.open(QIODevice::ReadOnly);
  189. ctkCmdLineModuleXmlValidator validator(&input);
  190. if (!validator.validateInput())
  191. {
  192. if (d->ModuleCache)
  193. {
  194. // validation failed, cache the description anyway
  195. d->ModuleCache->cacheXmlDescription(location, newTimeStamp, xml);
  196. }
  197. if (d->ValidationMode == STRICT_VALIDATION)
  198. {
  199. throw ctkInvalidArgumentException(QString("Validating module at %1 failed: %2")
  200. .arg(location.toString()).arg(validator.errorString()));
  201. }
  202. else
  203. {
  204. ref.d->XmlValidationErrorString = validator.errorString();
  205. }
  206. }
  207. else
  208. {
  209. if (d->ModuleCache && newTimeStamp > 0)
  210. {
  211. // successfully validated the xml, cache it
  212. d->ModuleCache->cacheXmlDescription(location, newTimeStamp, xml);
  213. }
  214. }
  215. }
  216. else
  217. {
  218. if (!fromCache && d->ModuleCache)
  219. {
  220. // cache it
  221. d->ModuleCache->cacheXmlDescription(location, newTimeStamp, xml);
  222. }
  223. }
  224. {
  225. QMutexLocker lock(&d->Mutex);
  226. // Check that we don't have a race condition
  227. if (d->LocationToRef.contains(location))
  228. {
  229. // Another thread registered a module with the same location
  230. return d->LocationToRef[location];
  231. }
  232. d->LocationToRef[location] = ref;
  233. }
  234. emit moduleRegistered(ref);
  235. return ref;
  236. }
  237. //----------------------------------------------------------------------------
  238. void ctkCmdLineModuleManager::unregisterModule(const ctkCmdLineModuleReference& ref)
  239. {
  240. if (!ref) return;
  241. {
  242. QMutexLocker lock(&d->Mutex);
  243. if (!d->LocationToRef.contains(ref.location()))
  244. {
  245. return;
  246. }
  247. d->LocationToRef.remove(ref.location());
  248. if (d->ModuleCache)
  249. {
  250. d->ModuleCache->removeCacheEntry(ref.location());
  251. }
  252. }
  253. emit moduleUnregistered(ref);
  254. }
  255. //----------------------------------------------------------------------------
  256. ctkCmdLineModuleReference ctkCmdLineModuleManager::moduleReference(const QUrl &location) const
  257. {
  258. QMutexLocker lock(&d->Mutex);
  259. QHash<QUrl, ctkCmdLineModuleReference>::const_iterator iter = d->LocationToRef.find(location);
  260. return (iter == d->LocationToRef.end()) ? ctkCmdLineModuleReference() : iter.value();
  261. }
  262. //----------------------------------------------------------------------------
  263. QList<ctkCmdLineModuleReference> ctkCmdLineModuleManager::moduleReferences() const
  264. {
  265. QMutexLocker lock(&d->Mutex);
  266. return d->LocationToRef.values();
  267. }
  268. //----------------------------------------------------------------------------
  269. ctkCmdLineModuleFuture ctkCmdLineModuleManager::run(ctkCmdLineModuleFrontend *frontend)
  270. {
  271. QMutexLocker lock(&d->Mutex);
  272. d->checkBackends_unlocked(frontend->location());
  273. ctkCmdLineModuleFuture future = d->SchemeToBackend[frontend->location().scheme()]->run(frontend);
  274. frontend->setFuture(future);
  275. emit frontend->started();
  276. return future;
  277. }