ctkCmdLineModuleManager.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 "ctkCmdLineModuleFuture.h"
  19. #include "ctkCmdLineModuleXmlValidator.h"
  20. #include "ctkCmdLineModuleReference.h"
  21. #include "ctkCmdLineModuleReference_p.h"
  22. #include <ctkException.h>
  23. #include <QStringList>
  24. #include <QBuffer>
  25. #include <QUrl>
  26. #include <QHash>
  27. #include <QList>
  28. #include <QMutex>
  29. #include <QFuture>
  30. struct ctkCmdLineModuleManagerPrivate
  31. {
  32. ctkCmdLineModuleManagerPrivate(ctkCmdLineModuleManager::ValidationMode mode)
  33. : ValidationMode(mode)
  34. {}
  35. void checkBackends_unlocked(const QUrl& location)
  36. {
  37. if (!this->SchemeToBackend.contains(location.scheme()))
  38. {
  39. throw ctkInvalidArgumentException(QString("No suitable backend registered for module at ") + location.toString());
  40. }
  41. }
  42. QMutex Mutex;
  43. QHash<QString, ctkCmdLineModuleBackend*> SchemeToBackend;
  44. QHash<QUrl, ctkCmdLineModuleReference> LocationToRef;
  45. const ctkCmdLineModuleManager::ValidationMode ValidationMode;
  46. };
  47. ctkCmdLineModuleManager::ctkCmdLineModuleManager(ValidationMode validationMode)
  48. : d(new ctkCmdLineModuleManagerPrivate(validationMode))
  49. {
  50. }
  51. ctkCmdLineModuleManager::~ctkCmdLineModuleManager()
  52. {
  53. }
  54. void ctkCmdLineModuleManager::registerBackend(ctkCmdLineModuleBackend *backend)
  55. {
  56. QMutexLocker lock(&d->Mutex);
  57. QList<QString> supportedSchemes = backend->schemes();
  58. // Check if there is already a backend registerd for any of the
  59. // supported schemes. We only supported one backend per scheme.
  60. foreach (QString scheme, supportedSchemes)
  61. {
  62. if (d->SchemeToBackend.contains(scheme))
  63. {
  64. throw ctkInvalidArgumentException(QString("A backend for scheme %1 is already registered.").arg(scheme));
  65. }
  66. }
  67. // All good
  68. foreach (QString scheme, supportedSchemes)
  69. {
  70. d->SchemeToBackend[scheme] = backend;
  71. }
  72. }
  73. ctkCmdLineModuleReference
  74. ctkCmdLineModuleManager::registerModule(const QUrl &location)
  75. {
  76. QByteArray xml;
  77. ctkCmdLineModuleBackend* backend = NULL;
  78. {
  79. QMutexLocker lock(&d->Mutex);
  80. d->checkBackends_unlocked(location);
  81. // If the module is already registered, just return the reference
  82. if (d->LocationToRef.contains(location))
  83. {
  84. return d->LocationToRef[location];
  85. }
  86. backend = d->SchemeToBackend[location.scheme()];
  87. }
  88. xml = backend->rawXmlDescription(location);
  89. if (xml.isEmpty())
  90. {
  91. throw ctkInvalidArgumentException(QString("No XML output available from ") + location.toString());
  92. }
  93. ctkCmdLineModuleReference ref;
  94. ref.d->Location = location;
  95. ref.d->RawXmlDescription = xml;
  96. ref.d->Backend = backend;
  97. if (d->ValidationMode != SKIP_VALIDATION)
  98. {
  99. // validate the outputted xml description
  100. QBuffer input(&xml);
  101. input.open(QIODevice::ReadOnly);
  102. ctkCmdLineModuleXmlValidator validator(&input);
  103. if (!validator.validateInput())
  104. {
  105. if (d->ValidationMode == STRICT_VALIDATION)
  106. {
  107. throw ctkInvalidArgumentException(QString("Validating module at %1 failed: %2")
  108. .arg(location.toString()).arg(validator.errorString()));
  109. }
  110. else
  111. {
  112. ref.d->XmlValidationErrorString = validator.errorString();
  113. }
  114. }
  115. }
  116. {
  117. QMutexLocker lock(&d->Mutex);
  118. // Check that we don't have a race condition
  119. if (d->LocationToRef.contains(location))
  120. {
  121. // Another thread registered a module with the same location
  122. return d->LocationToRef[location];
  123. }
  124. d->LocationToRef[location] = ref;
  125. }
  126. emit moduleRegistered(ref);
  127. return ref;
  128. }
  129. void ctkCmdLineModuleManager::unregisterModule(const ctkCmdLineModuleReference& ref)
  130. {
  131. {
  132. QMutexLocker lock(&d->Mutex);
  133. if (!d->LocationToRef.contains(ref.location()))
  134. {
  135. return;
  136. }
  137. d->LocationToRef.remove(ref.location());
  138. }
  139. emit moduleUnregistered(ref);
  140. }
  141. ctkCmdLineModuleReference ctkCmdLineModuleManager::moduleReference(const QUrl &location) const
  142. {
  143. QMutexLocker lock(&d->Mutex);
  144. return d->LocationToRef[location];
  145. }
  146. QList<ctkCmdLineModuleReference> ctkCmdLineModuleManager::moduleReferences() const
  147. {
  148. QMutexLocker lock(&d->Mutex);
  149. return d->LocationToRef.values();
  150. }
  151. ctkCmdLineModuleFuture ctkCmdLineModuleManager::run(ctkCmdLineModuleFrontend *frontend)
  152. {
  153. QMutexLocker lock(&d->Mutex);
  154. d->checkBackends_unlocked(frontend->location());
  155. ctkCmdLineModuleFuture future = d->SchemeToBackend[frontend->location().scheme()]->run(frontend);
  156. frontend->setFuture(future);
  157. return future;
  158. }