ctkCmdLineModuleManager.cpp 8.4 KB

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