ctkCmdLineModuleManager.cpp 8.0 KB

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