ctkCmdLineModuleManager.cpp 7.3 KB

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