ctkCmdLineModuleManager.cpp 8.8 KB

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