ctkServiceReferencePrivate.cpp 4.8 KB

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