ctkServiceReferencePrivate.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 "ctkServiceReferencePrivate.h"
  16. #include <QObject>
  17. #include <QMutexLocker>
  18. #include "ctkPluginConstants.h"
  19. #include "ctkServiceFactory.h"
  20. #include "ctkServiceException.h"
  21. #include "ctkPluginPrivate_p.h"
  22. #include "ctkServiceRegistrationPrivate.h"
  23. #include "ctkPluginFrameworkContext_p.h"
  24. ctkServiceReferencePrivate::ctkServiceReferencePrivate(ctkServiceRegistrationPrivate* reg)
  25. : ref(1), registration(reg)
  26. {
  27. }
  28. QObject* ctkServiceReferencePrivate::getService(ctkPlugin* plugin)
  29. {
  30. QObject* s = 0;
  31. {
  32. QMutexLocker lock(&registration->propsLock);
  33. if (registration->available)
  34. {
  35. int count = registration->dependents.value(plugin);
  36. if (count == 0)
  37. {
  38. QStringList classes =
  39. registration->properties.value(ctkPluginConstants::OBJECTCLASS).toStringList();
  40. registration->dependents[plugin] = 1;
  41. if (ctkServiceFactory* serviceFactory = qobject_cast<ctkServiceFactory*>(registration->getService()))
  42. {
  43. try
  44. {
  45. s = serviceFactory->getService(plugin, registration->q_func());
  46. }
  47. catch (const std::exception& pe)
  48. {
  49. ctkServiceException se("ctkServiceFactory throw an exception",
  50. ctkServiceException::FACTORY_EXCEPTION, &pe);
  51. plugin->d_func()->fwCtx->listeners.frameworkError
  52. (registration->plugin->q_func(), se);
  53. return 0;
  54. }
  55. if (s == 0) {
  56. ctkServiceException se("ctkServiceFactory produced null",
  57. ctkServiceException::FACTORY_ERROR);
  58. plugin->d_func()->fwCtx->listeners.frameworkError
  59. (registration->plugin->q_func(), se);
  60. return 0;
  61. }
  62. for (QStringListIterator i(classes); i.hasNext(); )
  63. {
  64. QString cls = i.next();
  65. if (!registration->plugin->fwCtx->services.checkServiceClass(s, cls))
  66. {
  67. ctkServiceException se(QString("ctkServiceFactory produced an object ") +
  68. "that did not implement: " + cls,
  69. ctkServiceException::FACTORY_ERROR);
  70. plugin->d_func()->fwCtx->listeners.frameworkError
  71. (registration->plugin->q_func(), se);
  72. return 0;
  73. }
  74. }
  75. registration->serviceInstances.insert(plugin, s);
  76. }
  77. else
  78. {
  79. s = registration->getService();
  80. }
  81. }
  82. else
  83. {
  84. registration->dependents.insert(plugin, count + 1);
  85. if (qobject_cast<ctkServiceFactory*>(registration->getService()))
  86. {
  87. s = registration->serviceInstances.value(plugin);
  88. }
  89. else
  90. {
  91. s = registration->getService();
  92. }
  93. }
  94. }
  95. }
  96. return s;
  97. }
  98. bool ctkServiceReferencePrivate::ungetService(ctkPlugin* plugin, bool checkRefCounter)
  99. {
  100. QMutexLocker lock(&registration->propsLock);
  101. bool hadReferences = false;
  102. bool removeService = false;
  103. int count= registration->dependents.value(plugin);
  104. if (count > 0)
  105. {
  106. hadReferences = true;
  107. }
  108. if(checkRefCounter)
  109. {
  110. if (count > 1)
  111. {
  112. registration->dependents[plugin] = count - 1;
  113. }
  114. else if(count == 1)
  115. {
  116. removeService = true;
  117. }
  118. }
  119. else
  120. {
  121. removeService = true;
  122. }
  123. if (removeService)
  124. {
  125. QObject* sfi = registration->serviceInstances[plugin];
  126. registration->serviceInstances.remove(plugin);
  127. if (sfi != 0)
  128. {
  129. try
  130. {
  131. qobject_cast<ctkServiceFactory*>(registration->getService())->ungetService(plugin,
  132. registration->q_func(), sfi);
  133. }
  134. catch (const std::exception& e)
  135. {
  136. plugin->d_func()->fwCtx->listeners.frameworkError(registration->plugin->q_func(), e);
  137. }
  138. }
  139. registration->dependents.remove(plugin);
  140. }
  141. return hadReferences;
  142. }
  143. ServiceProperties ctkServiceReferencePrivate::getProperties() const
  144. {
  145. return registration->properties;
  146. }