ctkCmdLineModuleManager.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. 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::setValidationMode(const ValidationMode& mode)
  89. {
  90. d->ValidationMode = mode;
  91. }
  92. //----------------------------------------------------------------------------
  93. void ctkCmdLineModuleManager::registerBackend(ctkCmdLineModuleBackend *backend)
  94. {
  95. QMutexLocker lock(&d->Mutex);
  96. QList<QString> supportedSchemes = backend->schemes();
  97. // Check if there is already a backend registerd for any of the
  98. // supported schemes. We only supported one backend per scheme.
  99. foreach (QString scheme, supportedSchemes)
  100. {
  101. if (d->SchemeToBackend.contains(scheme))
  102. {
  103. throw ctkInvalidArgumentException(QString("A backend for scheme %1 is already registered.").arg(scheme));
  104. }
  105. }
  106. // All good
  107. foreach (QString scheme, supportedSchemes)
  108. {
  109. d->SchemeToBackend[scheme] = backend;
  110. }
  111. }
  112. //----------------------------------------------------------------------------
  113. ctkCmdLineModuleReference
  114. ctkCmdLineModuleManager::registerModule(const QUrl &location)
  115. {
  116. QByteArray xml;
  117. ctkCmdLineModuleBackend* backend = NULL;
  118. {
  119. QMutexLocker lock(&d->Mutex);
  120. d->checkBackends_unlocked(location);
  121. // If the module is already registered, just return the reference
  122. if (d->LocationToRef.contains(location))
  123. {
  124. return d->LocationToRef[location];
  125. }
  126. backend = d->SchemeToBackend[location.scheme()];
  127. }
  128. bool fromCache = false;
  129. qint64 newTimeStamp = 0;
  130. qint64 cacheTimeStamp = 0;
  131. if (d->ModuleCache)
  132. {
  133. newTimeStamp = backend->timeStamp(location);
  134. cacheTimeStamp = d->ModuleCache->timeStamp(location);
  135. if (cacheTimeStamp < 0 // i.e. timestamp is invalid
  136. || cacheTimeStamp < newTimeStamp) // i.e. timestamp is genuinely out of date
  137. {
  138. // newly fetch the XML description
  139. try
  140. {
  141. xml = backend->rawXmlDescription(location);
  142. }
  143. catch (...)
  144. {
  145. // cache the failed attempt
  146. d->ModuleCache->cacheXmlDescription(location, newTimeStamp, QByteArray());
  147. throw;
  148. }
  149. }
  150. else
  151. {
  152. // use the cached XML description
  153. xml = d->ModuleCache->rawXmlDescription(location);
  154. fromCache = true;
  155. }
  156. }
  157. else
  158. {
  159. xml = backend->rawXmlDescription(location);
  160. }
  161. if (xml.isEmpty())
  162. {
  163. if (!fromCache && d->ModuleCache)
  164. {
  165. d->ModuleCache->cacheXmlDescription(location, newTimeStamp, QByteArray());
  166. }
  167. throw ctkInvalidArgumentException(QString("No XML output available from ") + location.toString());
  168. }
  169. ctkCmdLineModuleReference ref;
  170. ref.d->Location = location;
  171. ref.d->RawXmlDescription = xml;
  172. ref.d->Backend = backend;
  173. if (d->ValidationMode != SKIP_VALIDATION)
  174. {
  175. // validate the outputted xml description
  176. QBuffer input(&xml);
  177. input.open(QIODevice::ReadOnly);
  178. ctkCmdLineModuleXmlValidator validator(&input);
  179. if (!validator.validateInput())
  180. {
  181. if (d->ModuleCache)
  182. {
  183. // validation failed, cache the description anyway
  184. d->ModuleCache->cacheXmlDescription(location, newTimeStamp, xml);
  185. }
  186. if (d->ValidationMode == STRICT_VALIDATION)
  187. {
  188. throw ctkInvalidArgumentException(QString("Validating module at %1 failed: %2")
  189. .arg(location.toString()).arg(validator.errorString()));
  190. }
  191. else
  192. {
  193. ref.d->XmlValidationErrorString = validator.errorString();
  194. }
  195. }
  196. else
  197. {
  198. if (d->ModuleCache && newTimeStamp > 0 && !fromCache)
  199. {
  200. // successfully validated the xml, cache it
  201. d->ModuleCache->cacheXmlDescription(location, newTimeStamp, xml);
  202. }
  203. }
  204. }
  205. else
  206. {
  207. if (!fromCache && d->ModuleCache)
  208. {
  209. // cache it
  210. d->ModuleCache->cacheXmlDescription(location, newTimeStamp, xml);
  211. }
  212. }
  213. {
  214. QMutexLocker lock(&d->Mutex);
  215. // Check that we don't have a race condition
  216. if (d->LocationToRef.contains(location))
  217. {
  218. // Another thread registered a module with the same location
  219. return d->LocationToRef[location];
  220. }
  221. d->LocationToRef[location] = ref;
  222. }
  223. emit moduleRegistered(ref);
  224. return ref;
  225. }
  226. //----------------------------------------------------------------------------
  227. void ctkCmdLineModuleManager::unregisterModule(const ctkCmdLineModuleReference& ref)
  228. {
  229. if (!ref) return;
  230. {
  231. QMutexLocker lock(&d->Mutex);
  232. if (!d->LocationToRef.contains(ref.location()))
  233. {
  234. return;
  235. }
  236. d->LocationToRef.remove(ref.location());
  237. if (d->ModuleCache)
  238. {
  239. d->ModuleCache->removeCacheEntry(ref.location());
  240. }
  241. }
  242. emit moduleUnregistered(ref);
  243. }
  244. //----------------------------------------------------------------------------
  245. void ctkCmdLineModuleManager::clearCache()
  246. {
  247. d->ModuleCache->clearCache();
  248. }
  249. //----------------------------------------------------------------------------
  250. void ctkCmdLineModuleManager::reloadModules()
  251. {
  252. foreach(const QUrl &location, d->LocationToRef.keys())
  253. {
  254. ctkCmdLineModuleReference ref = d->LocationToRef[location];
  255. this->unregisterModule(ref);
  256. this->registerModule(location);
  257. }
  258. }
  259. //----------------------------------------------------------------------------
  260. ctkCmdLineModuleReference ctkCmdLineModuleManager::moduleReference(const QUrl &location) const
  261. {
  262. QMutexLocker lock(&d->Mutex);
  263. QHash<QUrl, ctkCmdLineModuleReference>::const_iterator iter = d->LocationToRef.find(location);
  264. return (iter == d->LocationToRef.end()) ? ctkCmdLineModuleReference() : iter.value();
  265. }
  266. //----------------------------------------------------------------------------
  267. QList<ctkCmdLineModuleReference> ctkCmdLineModuleManager::moduleReferences() const
  268. {
  269. QMutexLocker lock(&d->Mutex);
  270. return d->LocationToRef.values();
  271. }
  272. //----------------------------------------------------------------------------
  273. ctkCmdLineModuleFuture ctkCmdLineModuleManager::run(ctkCmdLineModuleFrontend *frontend)
  274. {
  275. QMutexLocker lock(&d->Mutex);
  276. d->checkBackends_unlocked(frontend->location());
  277. ctkCmdLineModuleFuture future = d->SchemeToBackend[frontend->location().scheme()]->run(frontend);
  278. frontend->setFuture(future);
  279. emit frontend->started();
  280. return future;
  281. }