ctkServiceReferencePrivate.cpp 5.1 KB

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