ctkCmdLineModuleManager.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. if (d->ModuleCache)
  126. {
  127. newTimeStamp = backend->timeStamp(location);
  128. if (d->ModuleCache->timeStamp(location) < newTimeStamp)
  129. {
  130. // newly fetch the XML description
  131. try
  132. {
  133. xml = backend->rawXmlDescription(location);
  134. }
  135. catch (...)
  136. {
  137. // cache the failed attempt
  138. d->ModuleCache->cacheXmlDescription(location, newTimeStamp, QByteArray());
  139. throw;
  140. }
  141. }
  142. else
  143. {
  144. // use the cached XML description
  145. xml = d->ModuleCache->rawXmlDescription(location);
  146. fromCache = true;
  147. }
  148. }
  149. else
  150. {
  151. xml = backend->rawXmlDescription(location);
  152. }
  153. if (xml.isEmpty())
  154. {
  155. if (!fromCache && d->ModuleCache)
  156. {
  157. d->ModuleCache->cacheXmlDescription(location, newTimeStamp, QByteArray());
  158. }
  159. throw ctkInvalidArgumentException(QString("No XML output available from ") + location.toString());
  160. }
  161. ctkCmdLineModuleReference ref;
  162. ref.d->Location = location;
  163. ref.d->RawXmlDescription = xml;
  164. ref.d->Backend = backend;
  165. if (d->ValidationMode != SKIP_VALIDATION)
  166. {
  167. // validate the outputted xml description
  168. QBuffer input(&xml);
  169. input.open(QIODevice::ReadOnly);
  170. ctkCmdLineModuleXmlValidator validator(&input);
  171. if (!validator.validateInput())
  172. {
  173. if (d->ModuleCache)
  174. {
  175. // validation failed, cache the description anyway
  176. d->ModuleCache->cacheXmlDescription(location, newTimeStamp, xml);
  177. }
  178. if (d->ValidationMode == STRICT_VALIDATION)
  179. {
  180. throw ctkInvalidArgumentException(QString("Validating module at %1 failed: %2")
  181. .arg(location.toString()).arg(validator.errorString()));
  182. }
  183. else
  184. {
  185. ref.d->XmlValidationErrorString = validator.errorString();
  186. }
  187. }
  188. else
  189. {
  190. if (d->ModuleCache && newTimeStamp > 0)
  191. {
  192. // successfully validated the xml, cache it
  193. d->ModuleCache->cacheXmlDescription(location, newTimeStamp, xml);
  194. }
  195. }
  196. }
  197. else
  198. {
  199. if (!fromCache && d->ModuleCache)
  200. {
  201. // cache it
  202. d->ModuleCache->cacheXmlDescription(location, newTimeStamp, xml);
  203. }
  204. }
  205. {
  206. QMutexLocker lock(&d->Mutex);
  207. // Check that we don't have a race condition
  208. if (d->LocationToRef.contains(location))
  209. {
  210. // Another thread registered a module with the same location
  211. return d->LocationToRef[location];
  212. }
  213. d->LocationToRef[location] = ref;
  214. }
  215. emit moduleRegistered(ref);
  216. return ref;
  217. }
  218. //----------------------------------------------------------------------------
  219. void ctkCmdLineModuleManager::unregisterModule(const ctkCmdLineModuleReference& ref)
  220. {
  221. if (!ref) return;
  222. {
  223. QMutexLocker lock(&d->Mutex);
  224. if (!d->LocationToRef.contains(ref.location()))
  225. {
  226. return;
  227. }
  228. d->LocationToRef.remove(ref.location());
  229. if (d->ModuleCache)
  230. {
  231. d->ModuleCache->removeCacheEntry(ref.location());
  232. }
  233. }
  234. emit moduleUnregistered(ref);
  235. }
  236. //----------------------------------------------------------------------------
  237. ctkCmdLineModuleReference ctkCmdLineModuleManager::moduleReference(const QUrl &location) const
  238. {
  239. QMutexLocker lock(&d->Mutex);
  240. QHash<QUrl, ctkCmdLineModuleReference>::const_iterator iter = d->LocationToRef.find(location);
  241. return (iter == d->LocationToRef.end()) ? ctkCmdLineModuleReference() : iter.value();
  242. }
  243. //----------------------------------------------------------------------------
  244. QList<ctkCmdLineModuleReference> ctkCmdLineModuleManager::moduleReferences() const
  245. {
  246. QMutexLocker lock(&d->Mutex);
  247. return d->LocationToRef.values();
  248. }
  249. //----------------------------------------------------------------------------
  250. ctkCmdLineModuleFuture ctkCmdLineModuleManager::run(ctkCmdLineModuleFrontend *frontend)
  251. {
  252. QMutexLocker lock(&d->Mutex);
  253. d->checkBackends_unlocked(frontend->location());
  254. ctkCmdLineModuleFuture future = d->SchemeToBackend[frontend->location().scheme()]->run(frontend);
  255. frontend->setFuture(future);
  256. emit frontend->started();
  257. return future;
  258. }